ultimix
context_set_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 $BlockSettings = false;
39  var $CachedMultyFS = false;
40  var $PageComposer = false;
41  var $Security = false;
42  var $SecurityParser = false;
43  var $SecurityUtilities = false;
44  var $String = false;
45  var $Trace = false;
46 
61  function __construct()
62  {
63  try
64  {
65  $this->BlockSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
66  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
67  $this->PageComposer = get_package( 'page::page_composer' , 'last' , __FILE__ );
68  $this->Security = get_package( 'security' , 'last' , __FILE__ );
69  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
70  $this->SecurityUtilities = get_package( 'security::security_utilities' , 'last' , __FILE__ );
71  $this->String = get_package( 'string' , 'last' , __FILE__ );
72  $this->Trace = get_package( 'trace' , 'last' , __FILE__ );
73  }
74  catch( Exception $e )
75  {
76  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
77  }
78  }
79 
102  function set_header_template( $Options , &$Paging , $Prefix )
103  {
104  try
105  {
106  $FileName = $Options->get_setting( 'header' , $Prefix.'_header.tpl' );
107  $Path = dirname( $Options->get_setting( 'file_path' ) )."/res/templates/$FileName";
108  $Header = $this->CachedMultyFS->file_get_contents( $Path );
109 
110  $Paging->set( 'Header' , $Header );
111  }
112  catch( Exception $e )
113  {
114  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
115  }
116  }
117 
140  function set_item_template( $Options , &$Paging , $Prefix )
141  {
142  try
143  {
144  $FileName = $Options->get_setting( 'item' , $Prefix.'_item.tpl' );
145  $Path = dirname( $Options->get_setting( 'file_path' ) )."/res/templates/$FileName";
146  $Item = $this->CachedMultyFS->file_get_contents( $Path );
147 
148  $Paging->set( 'ItemTemplate' , $Item );
149  }
150  catch( Exception $e )
151  {
152  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
153  }
154  }
155 
178  function set_no_data_found_message( $Options , &$Paging )
179  {
180  try
181  {
182  if( $FileName = $Options->get_setting( 'no_data_found_message' , false ) )
183  {
184  $Path = dirname( $Options->get_setting( 'file_path' ) )."/res/templates/$FileName";
185  }
186  else
187  {
188  $Path = dirname( __FILE__ )."/res/templates/default_no_data_found_message.tpl";
189  }
190 
191  $NoDataFoundTemplate = $this->CachedMultyFS->file_get_contents( $Path );
192 
193  $Paging->set( 'NoDataFoundMessage' , $NoDataFoundTemplate );
194  }
195  catch( Exception $e )
196  {
197  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
198  }
199  }
200 
223  function set_footer_template( $Options , &$Paging , $Prefix )
224  {
225  try
226  {
227  $FileName = $Options->get_setting( 'footer' , $Prefix.'_footer.tpl' );
228  $Path = dirname( $Options->get_setting( 'file_path' ) )."/res/templates/$FileName";
229  $Footer = $this->CachedMultyFS->file_get_contents( $Path );
230 
231  $Paging->set( 'Footer' , $Footer );
232  }
233  catch( Exception $e )
234  {
235  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
236  }
237  }
238 
261  function set_main_settings( $Options , &$Paging )
262  {
263  try
264  {
265  $Paging->set( 'RecordsPerPage' , $Options->get_setting( 'records_per_page' , 20 ) );
266 
267  $Paging->set( 'PageField' , $Options->get_setting( 'page_field' , 'page' ) );
268  }
269  catch( Exception $e )
270  {
271  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
272  }
273  }
274 
301  function get_original_records( &$Options , $IdList )
302  {
303  try
304  {
305  $Provider = $this->get_data_provider( $Options , $this->Provider );
306 
307  if( method_exists( $Provider , 'select_list' ) === false )
308  {
309  $ClassName = $Provider ? get_class( $Provider ) : 'undefined_class';
310  throw( new Exception( 'Method "select_list" was not found in the class "'.$ClassName."'" ) );
311  }
312 
313  $Records = call_user_func( array( $Provider , 'select_list' ) , implode( ',' , $IdList ) );
314 
315  return( $Records );
316  }
317  catch( Exception $e )
318  {
319  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
320  }
321  }
322 
349  function get_data_record( &$Options , $IdList )
350  {
351  try
352  {
353  $Records = $this->get_original_records( $Options , $IdList );
354 
355  $Record = $Records[ 0 ];
356 
357  if( intval( $Options->get_setting( 'massive_processing' , 1 ) ) )
358  {
359  $c = count( $Records );
360  for( $i = 1 ; $i < $c ; $i++ )
361  {
362  foreach( $Records[ $i ] as $k => $v )
363  {
364  if( get_field( $Record , $k ) !== $v )
365  {
366  set_field( $Record , $k , false );
367  }
368  }
369  }
370  }
371 
372  return( $Record );
373  }
374  catch( Exception $e )
375  {
376  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
377  }
378  }
379 
414  function extract_data_from_request( &$Options , $Record , $SettingName , $Prefix )
415  {
416  try
417  {
418  if( $Options->get_setting( $SettingName , false ) )
419  {
420  $Parameter = $Options->get_setting( $SettingName );
421  $PostedRecord = $this->SecurityParser->parse_http_parameters( $Parameter );
422 
423  foreach( $PostedRecord as $k => $v )
424  {
425  $k = str_replace( $Prefix.'_' , '' , $k );
426 
427  if( $v !== false )
428  {
429  set_field( $Record , $k , $v );
430  }
431  }
432  }
433 
434  return( $Record );
435  }
436  catch( Exception $e )
437  {
438  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
439  }
440  }
441 
464  function get_data_provider( &$Options , &$DefaultProvider )
465  {
466  try
467  {
468  if( $Options->get_setting( 'access_package_name' , false ) )
469  {
470  $PackageName = $Options->get_setting( 'access_package_name' );
471  $PackageVersion = $Options->get_setting( 'access_package_version' , 'last' );
472 
473  return( get_package( $PackageName , $PackageVersion , __FILE__ ) );
474  }
475  else
476  {
477  return( $DefaultProvider );
478  }
479  }
480  catch( Exception $e )
481  {
482  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
483  }
484  }
485 
508  function get_posted_ids( $Prefix )
509  {
510  try
511  {
512  if( $this->Security->get_gp( $Prefix.'_record_id' , 'command' , false ) !== false &&
513  $this->Security->get_gp( $Prefix.'_record_id' , 'command' ) != -1 )
514  {
515  $Ids = array( $this->Security->get_gp( $Prefix.'_record_id' , 'command' ) );
516  }
517  else
518  {
519  $Mode = POST | PREFIX_NAME | KEYS;
520  $Ids = $this->SecurityUtilities->get_global( '_id_' , 'string' , $Mode , array() );
521  }
522 
523  return( $Ids );
524  }
525  catch( Exception $e )
526  {
527  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
528  }
529  }
530 
557  private function get_form_value( &$Settings , $Data )
558  {
559  try
560  {
561  $Name = $Settings->get_setting( 'name' );
562  $Type = $Settings->get_setting( 'type' , 'string' );
563 
564  if( $this->Security->get_gp( $Name , 'set' ) )
565  {
566  $Value = $this->Security->get_gp( $Name , $Type );
567  }
568  else
569  {
570  $Value = get_field( $Data , $Name , '' );
571  }
572 
573  if( is_array( $Value ) )
574  {
575  $Value = serialize( $Value );
576  $Value = $this->Security->get( $Value , 'string' );
577  }
578 
579  return( $Value );
580  }
581  catch( Exception $e )
582  {
583  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
584  }
585  }
586 
613  function set_form_data( $Form , $Data )
614  {
615  try
616  {
617  for( ; $Parameters = $this->String->get_macro_parameters( $Form , 'form_value' ) ; )
618  {
619  $this->BlockSettings->load_settings( $Parameters );
620  $FormValue = $this->get_form_value( $this->BlockSettings , $Data );
621  $Form = str_replace( "{form_value:$Parameters}" , $FormValue , $Form );
622  }
623 
624  $Form = $this->String->print_record( $Form , $Data );
625  foreach( $Data as $k => $v )
626  {
627  if( is_array( $v ) === false && is_object( $v ) === false )
628  {
629  $Form = str_replace( '{'.$k.'_original}' , $v , $Form );
630  }
631  }
632 
633  return( $Form );
634  }
635  catch( Exception $e )
636  {
637  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
638  }
639  }
640 
667  private function set_data_accessor( $Options , &$Paging , $QueryString = '1 = 1' )
668  {
669  try
670  {
671  $PackageName = $Options->get_setting( 'access_package_name' );
672  $PackageVersion = $Options->get_setting( 'access_package_version' , 'last' );
673  $Method = $Options->get_setting( 'select_func' , 'select' );
674 
675  $this->Trace->add_trace_string(
676  "{lang:data_accessor} : $PackageName.$PackageVersion->$Method" , COMMON
677  );
678 
679  $Paging->set( 'DataAccessor' , create_function( '$Start , $Limit , $Field , $Order , $Options' ,
680  "\$Object = get_package( '$PackageName' , '$PackageVersion' , __FILE__ );
681  return( \$Object->$Method( \$Start , \$Limit , \$Field , \$Order ,
682  \"$QueryString\" , \$Options ) );" ) );
683  }
684  catch( Exception $e )
685  {
686  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
687  }
688  }
689 
716  function set_grid_data( $Options , &$Paging , $QueryString = '1 = 1' )
717  {
718  try
719  {
720  $this->set_data_accessor( $Options , $Paging , $QueryString );
721 
722  if( $Options->get_setting( 'draw_package_name' , false ) )
723  {
724  $DrawPackageName = $Options->get_setting( 'draw_package_name' );
725  $DrawPackageVersion = $Options->get_setting( 'draw_package_version' , 'last' );
726  $DrawFunction = $Options->get_setting( 'draw_func' , 'select' );
727 
728  $Paging->set( 'CallbackFunc' , create_function( '$Template , $Record' ,
729  "\$Object = get_package( '$DrawPackageName' , '$DrawPackageVersion' , __FILE__ );
730  return( \$Object->$DrawFunction( \$Template , \$Record ) );" ) );
731  }
732  }
733  catch( Exception $e )
734  {
735  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
736  }
737  }
738 
777  function get_form( &$Options , $IdList , $FormFileName , $Prefix , $State )
778  {
779  try
780  {
781  $FormFileName = $Options->get_setting( 'form_template' , $FormFileName );
782  $FormFilePath = dirname( $Options->get_setting( 'file_path' ) )."/res/templates/$FormFileName.tpl";
783  $Form = $this->CachedMultyFS->file_get_contents( $FormFilePath );
784  $Form = str_replace( '{prefix}' , $Prefix , $Form );
785  $Form = str_replace( '{state}' , $State , $Form );
786  $Form = str_replace( '{ids}' , implode( ',' , $IdList ) , $Form );
787 
788  return( $Form );
789  }
790  catch( Exception $e )
791  {
792  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
793  }
794  }
795  }
796 
797 ?>