ultimix
paging.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 
16  class paging_1_0_0{
17 
28  var $Header = '';
29 
40  var $Footer = '';
41 
52  var $CallbackFunc = false;
53 
64  var $FormId = false;
65 
76  var $ItemTemplate = '';
77 
88  var $DataAccessor = false;
89 
100  var $Page = 0;
101 
112  var $PageField = 'page';
113 
124  var $RecordsPerPage = 20;
125 
136  var $Prefix = 'default';
137 
148  var $CustomButtons = '';
149 
160  var $Ajaxed = false;
161 
172  var $FormRequired = true;
173 
185 
196  var $GridId = false;
197 
208  var $CachedMultyFS = false;
209  var $PagingMarkup = false;
210  var $Security = false;
211  var $Settings = false;
212  var $String = false;
213  var $Utilities = false;
214 
229  private function load_scripts()
230  {
231  try
232  {
233  $Path = _get_package_relative_path_ex( 'gui::paging' , '1.0.0' );
234  $PageJS = get_package( 'page::page_js' , 'last' , __FILE__ );
235  $PageJS->add_javascript( "{http_host}/$Path/include/js/paging.js" );
236 
237  $this->NoDataFoundMessage = $this->CachedMultyFS->get_template(
238  __FILE__ , 'data_for_grid_was_not_found.tpl'
239  );
240 
241  $this->CallbackFunc = create_function(
242  '$Template , $Record',
243  '$String = get_package( "string" , "last" , __FILE__ );'.
244  'return( $String->print_record( $Template , $Record ) );'
245  );
246  }
247  catch( Exception $e )
248  {
249  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
250  }
251  }
252 
267  function __construct()
268  {
269  try
270  {
271  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
272  $this->PagingMarkup = get_package( 'gui::paging::paging_markup' , 'last' , __FILE__ );
273  $this->Security = get_package( 'security' , 'last' , __FILE__ );
274  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
275  $this->String = get_package( 'string' , 'last' , __FILE__ );
276  $this->Utilities = get_package( 'utilities' , 'last' , __FILE__ );
277 
278  $this->load_scripts();
279  }
280  catch( Exception $e )
281  {
282  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
283  }
284  }
285 
304  function set( $FieldName , $FieldValue )
305  {
306  $this->$FieldName = $FieldValue;
307  }
308 
327  private function compile_hidden_fields()
328  {
329  try
330  {
331  if( $this->Security->get_gp( 'add_hidden_fields' , 'integer' , 1 ) )
332  {
333  $Code = '<input type="hidden" name="ajaxed" value="'.( $this->Ajaxed ? 1 : 0 ).'">
334  <!--input type="hidden" name="page" value="'.$this->Page.'"-->
335  <input type="hidden" id="reorder_field" class="reorder_field" name="reorder_field" value="{field}">
336  <input type="hidden" id="order" class="order" name="order" value="{order}">'.$this->CustomButtons;
337 
338  return( $Code );
339  }
340  return( '' );
341  }
342  catch( Exception $e )
343  {
344  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
345  }
346  }
347 
366  function compile_header()
367  {
368  try
369  {
370  $this->FormRequired = $this->Security->get_gp( 'paging_require_form' , 'integer' , true );
371 
372  $HiddenFields = $this->compile_hidden_fields();
373 
374  $HiddenFields = $this->PagingMarkup->compile_sort_link( $this , $HiddenFields );
375 
376  if( $this->FormRequired )
377  {
378  return( '<form id="'.$this->FormId.'" action="" method="post">'.$HiddenFields.$this->Header );
379  }
380  else
381  {
382  return( $HiddenFields.$this->Header );
383  }
384  }
385  catch( Exception $e )
386  {
387  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
388  }
389  }
390 
409  function compile_footer()
410  {
411  try
412  {
413  $this->Footer = str_replace( '{page_name}' , $this->PageField , $this->Footer );
414  $this->Footer = str_replace( '{records_per_page}' , $this->RecordsPerPage , $this->Footer );
415 
416  if( $this->FormId !== false && $this->FormRequired )
417  {
418  return( $this->Footer.'</form>' );
419  }
420  else
421  {
422  return( $this->Footer );
423  }
424  }
425  catch( Exception $e )
426  {
427  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
428  }
429  }
430 
453  private function compile_items( &$DataToDisplay )
454  {
455  try
456  {
457  $RetView = '';
458  $c = count( $DataToDisplay );
459 
460  for( $i = 0 ; $i < $c && $i < $this->RecordsPerPage ; $i++ )
461  {
462  $Tmp = $this->ItemTemplate;
463 
464  $Tmp = call_user_func( $this->CallbackFunc , $Tmp , $DataToDisplay[ $i ] );
465  $Tmp = str_replace( '{odd_factor}' , ( $i % 2 === 0 ? 'even' : 'odd' ) , $Tmp );
466 
467  $RetView .= $Tmp;
468  }
469 
470  return( $RetView );
471  }
472  catch( Exception $e )
473  {
474  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
475  }
476  }
477 
500  private function compile_macros( &$DataToDisplay )
501  {
502  try
503  {
504  $RetView = $this->compile_items( $DataToDisplay );
505 
506  $RetView = $this->compile_header().'{set_var:name='.$this->FormId.'_records_count;value='.
507  count( $DataToDisplay ).'}'.$RetView.$this->compile_footer();
508 
509  $RetView = $this->PagingMarkup->compile_left_slider( $this , $RetView );
510  $RetView = $this->PagingMarkup->compile_records_per_page_control( $this , $RetView );
511  $RetView = $this->PagingMarkup->compile_right_slider( $this , count( $DataToDisplay ) , $RetView );
512 
513  return( str_replace( '{prefix}' , $this->Prefix , $RetView ) );
514  }
515  catch( Exception $e )
516  {
517  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
518  }
519  }
520 
539  private function compile_no_data_to_display()
540  {
541  try
542  {
543  $this->FormRequired = $this->FormId === false ? false : true;
544  $this->NoDataFoundMessage = str_replace( '{prefix}' , $this->Prefix , $this->NoDataFoundMessage );
545 
546  if( $this->FormRequired )
547  {
548  return(
549  '{set_var:name='.$this->FormId.'_records_count;value=0}<form id="'.$this->FormId.
550  '" action="" method="post">'.$this->CustomButtons.$this->NoDataFoundMessage.'</form>'
551  );
552  }
553  else
554  {
555  return( '{set_var:name='.$this->FormId.'_records_count;value=0}'.$this->NoDataFoundMessage );
556  }
557  }
558  catch( Exception $e )
559  {
560  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
561  }
562  }
563 
586  private function get_data_to_display( &$Options )
587  {
588  try
589  {
590  $ReorderField = $this->Security->get_gp( 'reorder_field' , 'command' , false );
591  $Order = $this->Security->get_gp( 'order' , 'command' , false );
592 
593  if( strlen( $ReorderField ) == 0 )
594  {
595  $ReorderField = false;
596  }
597  if( strlen( $Order ) == 0 )
598  {
599  $Order = false;
600  }
601 
602  $DataToDisplay = call_user_func(
603  $this->DataAccessor , ( $this->Page - 1 ) * $this->RecordsPerPage ,
604  $this->RecordsPerPage + 1 , $ReorderField , $Order , $Options
605  );
606 
607  return( $DataToDisplay );
608  }
609  catch( Exception $e )
610  {
611  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
612  }
613  }
614 
633  private function primary_init( &$Options )
634  {
635  try
636  {
637  $this->GridId = md5( microtime( true ) );
638 
639  $this->Page = $this->Security->get_gp( $this->PageField , 'integer' , 1 );
640 
641  $this->RecordsPerPage = $Options->get_setting( 'records_per_page' , $this->RecordsPerPage );
642 
643  $this->RecordsPerPage = $this->Security->get_c(
644  $this->Prefix.'_records_per_page' , 'integer' , $this->RecordsPerPage
645  );
646  }
647  catch( Exception $e )
648  {
649  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
650  }
651  }
652 
679  function draw( $DataToDisplay = false , $Options = false )
680  {
681  try
682  {
683  $this->primary_init( $Options );
684 
685  if( $DataToDisplay === false && $this->DataAccessor === false )
686  {
687  throw( new Exception( 'No data was specified' ) );
688  }
689  if( $this->DataAccessor )
690  {
691  $DataToDisplay = $this->get_data_to_display( $Options );
692  }
693 
694  if( count( $DataToDisplay ) === 0 )
695  {
696  return( $this->compile_no_data_to_display() );
697  }
698 
699  return( $this->compile_macros( $DataToDisplay ) );
700  }
701  catch( Exception $e )
702  {
703  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
704  }
705  }
706  }
707 
708 ?>