ultimix
link.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
26  class link_1_0_0{
27 
38  var $Database = false;
39  var $LinkDictionary = false;
40  var $Security = false;
41 
56  function __construct()
57  {
58  try
59  {
60  $this->Database = get_package( 'database' , 'last' , __FILE__ );
61  $this->LinkDictionary = get_package( 'link::link_dictionary' , 'last' , __FILE__ );
62  $this->Security = get_package( 'security' , 'last' , __FILE__ );
63  }
64  catch( Exception $e )
65  {
66  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
67  }
68  }
69 
104  function create_link_object( $Object1Id , $Object2Id , $Object1Type ,
105  $Object2Type , $SingleLinkOnly = false )
106  {
107  try
108  {
109  if( $SingleLinkOnly && $this->link_exists( $Object1Id , $Object2Id , $Object1Type , $Object2Type ) )
110  {
111  return;
112  }
113 
114  $Object1Id = $this->Security->get( $Object1Id , 'integer' );
115  $Object2Id = $this->Security->get( $Object2Id , 'integer' );
116 
117  $LinkType = $this->LinkDictionary->get_link_type( $Object1Type , $Object2Type );
118 
119  $this->Database->insert(
120  'umx_link' , 'object1_id , object2_id , type' , "$Object1Id , $Object2Id , $LinkType"
121  );
122  $this->Database->commit();
123  }
124  catch( Exception $e )
125  {
126  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
127  }
128  }
129 
164  function create_link( $Object1Id , $Object2Id , $Object1Type ,
165  $Object2Type , $SingleLinkOnly = false )
166  {
167  try
168  {
169  if( is_array( $Object1Id ) )
170  {
171  foreach( $Object1Id as $k => $o1id )
172  {
173  $this->create_link( $o1id , $Object2Id , $Object1Type , $Object2Type , $SingleLinkOnly );
174  }
175  }
176  elseif( is_array( $Object2Id ) )
177  {
178  foreach( $Object2Id as $k => $o2id )
179  {
180  $this->create_link( $Object1Id , $o2id , $Object1Type , $Object2Type , $SingleLinkOnly );
181  }
182  }
183  else
184  {
185  $this->create_link_object(
186  $Object1Id , $Object2Id , $Object1Type , $Object2Type , $SingleLinkOnly
187  );
188  }
189  }
190  catch( Exception $e )
191  {
192  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
193  }
194  }
195 
226  function compile_select_condition( $ObjectId , $FieldName , $Conditions )
227  {
228  try
229  {
230  if( $ObjectId !== false )
231  {
232  if( is_array( $ObjectId ) )
233  {
234  $ObjectId = implode( ',' , $ObjectId );
235  }
236  $ObjectId = $this->Security->get( $ObjectId , 'integer_list' );
237  $Conditions [] = "$FieldName IN ( $ObjectId )";
238  }
239 
240  return( $Conditions );
241  }
242  catch( Exception $e )
243  {
244  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
245  }
246  }
247 
282  function prepare_conditions( $Object1Id , $Object2Id , $Object1Type , $Object2Type )
283  {
284  try
285  {
286  $Conditions = array();
287  $Conditions = $this->compile_select_condition( $Object1Id , 'object1_id' , $Conditions );
288  $Conditions = $this->compile_select_condition( $Object2Id , 'object2_id' , $Conditions );
289 
290  if( $Object1Type !== false || $Object2Type != false )
291  {
292  $Types = $this->LinkDictionary->get_link_type( $Object1Type , $Object2Type );
293  $Conditions [] = "type IN ( $Types )";
294  }
295 
296  return( implode( ' AND ' , $Conditions ) );
297  }
298  catch( Exception $e )
299  {
300  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
301  }
302  }
303 
338  function get_links( $Object1Id , $Object2Id , $Object1Type , $Object2Type )
339  {
340  try
341  {
342  $Conditions = $this->prepare_conditions( $Object1Id , $Object2Id , $Object1Type , $Object2Type );
343  $Fields = 'umx_link.* , umx_link_dictionary.object1_type , umx_link_dictionary.object2_type';
344  $Conditions = "umx_link.type = umx_link_dictionary.id AND $Conditions";
345 
346  return( $this->Database->select( $Fields , 'umx_link , umx_link_dictionary' , $Conditions ) );
347  }
348  catch( Exception $e )
349  {
350  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
351  }
352  }
353 
384  function delete_link( $Object1Id , $Object2Id , $Object1Type , $Object2Type )
385  {
386  try
387  {
388  if( $Object1Type === false && $Object2Type === false )
389  {
390  throw( new Exception( "At least one type must be set" ) );
391  }
392 
393  $Conditions = $this->prepare_conditions( $Object1Id , $Object2Id , $Object1Type , $Object2Type );
394 
395  $this->Database->delete( 'umx_link' , $Conditions );
396  $this->Database->commit();
397  }
398  catch( Exception $e )
399  {
400  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
401  }
402  }
403 
438  function get_links_count( $Object1Id , $Object2Id , $Object1Type , $Object2Type )
439  {
440  try
441  {
442  $Conditions = $this->prepare_conditions( $Object1Id , $Object2Id , $Object1Type , $Object2Type );
443  $Records = $this->Database->select( 'COUNT( * ) AS links_count' , 'umx_link' , $Conditions );
444 
445  return( isset( $Records[ 0 ] ) === false ? 0 : get_field( $Records[ 0 ] , 'links_count' ) );
446  }
447  catch( Exception $e )
448  {
449  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
450  }
451  }
452 
487  function link_exists( $Object1Id , $Object2Id , $Object1Type , $Object2Type )
488  {
489  try
490  {
491  return( $this->get_links_count( $Object1Id , $Object2Id , $Object1Type , $Object2Type ) > 0 );
492  }
493  catch( Exception $e )
494  {
495  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
496  }
497  }
498 
533  function update_link( $Object1Id , $OldObject2Id , $NewObject2Id , $Object1Type , $Object2Type )
534  {
535  try
536  {
537  if( strpos( $Object1Id , ',' ) === true )
538  {
539  $Object1Id = explode( ',' , $Object1Id );
540  }
541  else
542  {
543  $Object1Id = array( $Object1Id );
544  }
545 
546  foreach( $Object1Id as $k => $id )
547  {
548  $this->delete_link( $id , $OldObject2Id , $Object1Type , $Object2Type );
549 
550  $this->create_link( $id , $NewObject2Id , $Object1Type , $Object2Type );
551  }
552  }
553  catch( Exception $e )
554  {
555  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
556  }
557  }
558  }
559 
560 ?>