ultimix
link_utilities.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 
27 
38  var $Database = false;
39  var $Link = false;
40  var $Security = false;
41  var $String = false;
42  var $UserAlgorithms = false;
43 
58  function __construct()
59  {
60  try
61  {
62  $this->Database = get_package( 'database' , 'last' , __FILE__ );
63  $this->Link = get_package( 'link' , 'last' , __FILE__ );
64  $this->Security = get_package( 'security' , 'last' , __FILE__ );
65  $this->String = get_package( 'string' , 'last' , __FILE__ );
66  $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
67  }
68  catch( Exception $e )
69  {
70  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
71  }
72  }
73 
112  function get_dependent_objects( $MasterId , $MasterType , $ObjectType , &$Access = false )
113  {
114  try
115  {
116  $MasterId = $this->Security->get( $MasterId , 'integer' );
117  $MasterType = $this->Security->get( $MasterType , 'command' );
118  $ids = array( 0 );
119  $Links = $this->Link->get_links( $MasterId , false , $MasterType , $ObjectType );
120  if( isset( $Links[ 0 ] ) )
121  {
122  $ids = get_field_ex( $Links , 'object2_id' );
123  }
124  if( $Access === false )
125  {
126  return( $ids );
127  }
128  else
129  {
130  return( $Access->select_list( implode( ',' , $ids ) ) );
131  }
132  }
133  catch( Exception $e )
134  {
135  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
136  }
137  }
138 
169  function dependent_objects_exist( $MasterId , $MasterType , $ObjectType )
170  {
171  try
172  {
173  $MasterId = $this->Security->get( $MasterId , 'integer' );
174  $MasterType = $this->Security->get( $MasterType , 'command' );
175 
176  $ids = $this->get_dependent_objects( $MasterId , $MasterType , $ObjectType );
177 
178  return( isset( $ids[ 0 ] ) && $ids[ 0 ] );
179  }
180  catch( Exception $e )
181  {
182  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
183  }
184  }
185 
224  function get_master_objects( $ObjectId , $MasterType , $ObjectType , $Access = false )
225  {
226  try
227  {
228  $ObjectId = $this->Security->get( $ObjectId , 'integer' );
229  $MasterType = $this->Security->get( $MasterType , 'command' );
230 
231  $ids = array( 0 );
232  $Links = $this->Link->get_links( false , $ObjectId , $MasterType , $ObjectType );
233 
234  if( isset( $Links[ 0 ] ) )
235  {
236  $ids = get_field_ex( $Links , 'object1_id' );
237  }
238 
239  if( $Access === false )
240  {
241  return( $ids );
242  }
243  else
244  {
245  return( $Access->select_list( implode( ',' , $ids ) ) );
246  }
247  }
248  catch( Exception $e )
249  {
250  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
251  }
252  }
253 
284  function master_objects_exists( $ObjectId , $MasterType , $ObjectType )
285  {
286  try
287  {
288  $ObjectId = $this->Security->get( $ObjectId , 'integer' );
289  $MasterType = $this->Security->get( $MasterType , 'command' );
290 
291  $ids = $this->get_master_objects( $ObjectId , $MasterType , $ObjectType );
292 
293  return( isset( $ids[ 0 ] ) && $ids[ 0 ] );
294  }
295  catch( Exception $e )
296  {
297  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
298  }
299  }
300 
327  function trasform_master_parameters( $MasterId , $MasterType )
328  {
329  try
330  {
331  $MasterType = $this->Security->get( $MasterType , 'command' );
332 
333  if( $MasterId === false && $MasterType == 'user' )
334  {
335  $MasterId = $this->UserAlgorithms->get_id();
336  }
337 
338  $MasterId = $this->Security->get( $MasterId , 'integer' );
339 
340  return( array( $MasterId , $MasterType ) );
341  }
342  catch( Exception $e )
343  {
344  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
345  }
346  }
347 
394  function get_sub_records( $Object1Type , $Object2Type , &$Records , $FieldName1 ,
395  $JoiningTableName , $FieldName2 , $Fields = '*' )
396  {
397  try
398  {
399  if( isset( $Records[ 0 ] ) )
400  {
401  $LinkType = $this->LinkDictionary->get_link_type( $Object1Type , $Object2Type );
402  $Ids = implode_ex( ',' , $Records , $FieldName1 );
403  $Fields = "$Fields , umx_link.object1_id AS _record_original_id";
404  $Tables = "$JoiningTableName , umx_link";
405  $JoinConditions = "umx_link.type $LinkType AND umx_link.object1_id IN ( $Ids ) AND
406  umx_link.object2_id = $JoiningTableName".".$FieldName2";
407  return( $this->Database->select( $Fields , $Tables , $JoinConditions ) );
408  }
409  else
410  {
411  return( array() );
412  }
413  }
414  catch( Exception $e )
415  {
416  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
417  }
418  }
419 
462  function get_joining_records( &$Records , $FieldName1 , $FieldName2 , $JoiningTableName ,
463  $ExtendingFields , $Condition = '1 = 1' )
464  {
465  try
466  {
467  if( isset( $Records[ 0 ] ) )
468  {
469  $Values = implode( ' , ' , array_unique( get_field_ex( $Records , $FieldName1 ) ) );
470  $SubRecords = $this->Database->select(
471  "$FieldName2 AS _joining_field , $ExtendingFields" , "$JoiningTableName" ,
472  "$FieldName2 IN ( $Values ) AND ( $Condition )"
473  );
474  return( $SubRecords );
475  }
476 
477  return( array() );
478  }
479  catch( Exception $e )
480  {
481  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
482  }
483  }
484 
535  function join_data( &$Records , $Object1Type , $Object2Type , $FieldName1 ,
536  $FieldName2 , $JoiningTableName , $JoinName , $Fields = '*' )
537  {
538  try
539  {
540  $SubRecords = $this->get_sub_records( $Object1Type , $Object2Type , $Records , $FieldName1 ,
541  $JoiningTableName , $FieldName2 );
542 
543  foreach( $Records as $k1 => $v1 )
544  {
545  set_field( $Records[ $k1 ] , $JoinName , array() );
546  foreach( $SubRecords as $k2 => $v2 )
547  {
548  if( get_field( $v1 , $FieldName1 ) == get_field( $v2 , $FieldName2 ) )
549  {
550  append_to_field( $Records[ $k1 ] , $JoinName , $v2 );
551  }
552  }
553  }
554 
555  return( $Records );
556  }
557  catch( Exception $e )
558  {
559  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
560  }
561  }
562 
609  function join_single_to_single_data( &$Records , $FieldName1 ,
610  $FieldName2 , $JoiningTableName , $JoinName , $Fields = '*' , $Condition = '1 = 1' )
611  {
612  try
613  {
614  $SubRecords = $this->get_joining_records(
615  $Records , $FieldName1 , $FieldName2 , $JoiningTableName , $Fields , $Condition
616  );
617  foreach( $Records as $k1 => $v1 )
618  {
619  foreach( $SubRecords as $k2 => $v2 )
620  {
621  if( get_field( $v1 , $FieldName1 ) == get_field( $v2 , '_joining_field' ) )
622  {
623  if( get_field( $Records[ $k1 ] , $JoinName , false ) !== false )
624  {
625  throw( new Exception( "The field \"$JoinName\" was already set" ) );
626  }
627  set_field( $Records[ $k1 ] , $JoinName , remove_fields( $v2 , '_joining_field' ) );
628  }
629  }
630  }
631  return( $Records );
632  }
633  catch( Exception $e )
634  {
635  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
636  }
637  }
638 
685  function join_single_to_many_data( &$Records , $FieldName1 ,
686  $FieldName2 , $JoiningTableName , $JoinName , $Fields = '*' , $Condition = '1 = 1' )
687  {
688  try
689  {
690  $SubRecords = $this->get_joining_records(
691  $Records , $FieldName1 , $FieldName2 , $JoiningTableName , $Fields , $Condition
692  );
693 
694  foreach( $Records as $k1 => $v1 )
695  {
696  set_field( $Records[ $k1 ] , $JoinName , array() );
697  foreach( $SubRecords as $k2 => $v2 )
698  {
699  if( get_field( $v1 , $FieldName1 ) == get_field( $v2 , '_joining_field' ) )
700  {
701  append_to_field( $Records[ $k1 ] , $JoinName , remove_fields( $v2 , '_joining_field' ) );
702  }
703  }
704  }
705 
706  return( $Records );
707  }
708  catch( Exception $e )
709  {
710  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
711  }
712  }
713 
756  function extend_records( &$Records , $FieldName1 , $FieldName2 , $JoiningTableName ,
757  $ExtendingFields , $Condition = '1 = 1' )
758  {
759  try
760  {
761  $SubRecords = $this->get_joining_records(
762  $Records , $FieldName1 , $FieldName2 , $JoiningTableName ,
763  implode( ' , ' , $ExtendingFields ) , $Condition
764  );
765 
766  foreach( $Records as $k1 => $v1 )
767  {
768  foreach( $SubRecords as $k2 => $v2 )
769  {
770  if( get_field( $v1 , $FieldName1 ) == get_field( $v2 , '_joining_field' ) )
771  {
772  extend( $Records[ $k1 ] , remove_fields( $v2 , '_joining_field' ) );
773  }
774  }
775  }
776 
777  return( $Records );
778  }
779  catch( Exception $e )
780  {
781  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
782  }
783  }
784  }
785 
786 ?>