ultimix
base_template.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 $Template = false;
39 
50  var $MacroSettings = false;
51  var $CachedMultyFS = false;
52  var $Lang = false;
53  var $Security = false;
54  var $Settings = false;
55  var $String = false;
56  var $Tags = false;
57 
68  function __construct()
69  {
70  try
71  {
72  $this->MacroSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
73  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
74  $this->Lang = get_package( 'lang' , 'last' , __FILE__ );
75  $this->Security = get_package( 'security' , 'last' , __FILE__ );
76  $this->String = get_package( 'string' , 'last' , __FILE__ );
77  $this->Tags = get_package( 'string::tags' , 'last' , __FILE__ );
78 
79  $this->StylesheetParsed = false;
80  }
81  catch( Exception $e )
82  {
83  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
84  }
85  }
86 
105  function add_stylesheets( $File )
106  {
107  try
108  {
109  if( $this->CachedMultyFS->file_exists( dirname( $File ).'/res/css/css_list' ) )
110  {
111  $List = $this->CachedMultyFS->file_get_contents(
112  dirname( $File ).'/res/css/css_list' , 'exploded'
113  );
114 
115  $PageCSS = get_package( 'page::page_css' , 'last' , __FILE__ );
116 
117  foreach( $List as $k => $v )
118  {
119  $PageCSS->add_stylesheet(
120  str_replace( './' , '{http_host}/' , _get_package_relative_path( $File )."/res/css/$v" )
121  );
122  }
123  }
124  }
125  catch( Exception $e )
126  {
127  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
128  }
129  }
130 
157  function parse( $File , $Variable , $Value )
158  {
159  try
160  {
161  if( $this->Template === false )
162  {
163  $this->Template = $this->get_template( $File );
164  }
165 
166  if( strpos( $this->Template , '{'.$Variable.'}' ) !== false )
167  {
168  $Variable = $this->Security->get( $Variable , 'string' );
169  $this->Template = str_replace( '{'.$Variable.'}' , $Value.'{'.$Variable.'}' , $this->Template );
170  }
171  }
172  catch( Exception $e )
173  {
174  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
175  }
176  }
177 
193  {
194  try
195  {
196  if( $this->Security->get_gp( 'template' , 'set' ) )
197  {
198  $TemplateName = $this->Security->get_gp( 'template' , 'command' );
199 
200  if( $TemplateName == 'ajax_result_template' ||
201  $TemplateName == 'primitive' ||
202  $TemplateName == 'print' ||
203  $TemplateName == 'standalone_view' )
204  {
205  return( $TemplateName );
206  }
207  else
208  {
209  throw( new Exception( "Template \"$TemplateName\" was not found" ) );
210  }
211  }
212 
213  return( 'template' );
214  }
215  catch( Exception $e )
216  {
217  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
218  }
219  }
220 
243  private function handle_template_block( $PageName , &$Changed )
244  {
245  try
246  {
247  if( $this->String->block_exists( $this->Template , 'template:'.$PageName , 'template:~'.$PageName ) )
248  {
249  $Changed = true;
250  $this->Template = $this->String->show_block(
251  $this->Template , 'template:'.$PageName , 'template:~'.$PageName , $Changed
252  );
253  }
254  else
255  {
256  if( $this->String->block_exists( $this->Template , 'template:_default_page' ,
257  'template:~_default_page' ) )
258  {
259  $Changed = true;
260  $this->Template = $this->String->show_block(
261  $this->Template , 'template:_default_page' , 'template:~_default_page' , $Changed
262  );
263  }
264  }
265  }
266  catch( Exception $e )
267  {
268  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
269  }
270  }
271 
294  private function handle_includes( $File , &$Changed )
295  {
296  try
297  {
298  for( ; $BlockName = $this->String->get_macro_parameters( $this->Template , 'include' ) ; )
299  {
300  $IncludedTemplate = $this->CachedMultyFS->get_template( $File , "$BlockName.tpl" );
301 
302  $this->Template = str_replace( "{include:$BlockName}" , $IncludedTemplate , $this->Template );
303  $Changed = true;
304  }
305  }
306  catch( Exception $e )
307  {
308  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
309  }
310  }
311 
338  function process( $File , $PageName , &$Changed )
339  {
340  try
341  {
342  if( $this->Template === false )
343  {
344  $this->Template = $this->get_template( $File );
345  }
346  $this->Template = $this->get_template();
347 
348  $this->handle_template_block( $PageName , $Changed );
349 
350  $this->Template = $this->String->hide_unprocessed_blocks( $this->Template , 'template' , $Changed );
351 
352  $this->handle_includes( $File , $Changed );
353  }
354  catch( Exception $e )
355  {
356  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
357  }
358  }
359 
386  function get_placeholder_parameters( $File , $PurePlaceHolderName )
387  {
388  try
389  {
390  if( $this->Template === false )
391  {
392  $this->Template = $this->get_template( $File );
393  }
394 
395  if( $this->String->block_exists( $this->Template , $PurePlaceHolderName ) )
396  {
397  $Params = $this->String->get_macro_parameters( $this->Template , $PurePlaceHolderName );
398  return( $Params );
399  }
400  else
401  {
402  return( false );
403  }
404  }
405  catch( Exception $e )
406  {
407  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
408  }
409  }
410 
437  private function handle_color_scheme( $File , $Str )
438  {
439  try
440  {
441  //TODO: move this function to page::page_markup
442  for( ; $Params = $this->String->get_macro_parameters( $Str , 'color_scheme' ) ; )
443  {
444  $this->MacroSettings->load_settings( $Params );
445  $Available = explode( ',' , $this->MacroSettings->get_setting( 'available' , 'default' ) );
446 
447  $Sheme = $this->Settings->get_setting( 'color_scheme' , 'default' );
448 
449  if( array_search( $Sheme , $Available ) !== false )
450  {
451  $Str = str_replace( "{color_scheme:$Params}" , $Sheme , $Str );
452  }
453  else
454  {
455  $Str = str_replace( "{color_scheme:$Params}" , 'default' , $Str );
456  }
457  }
458 
459  return( $Str );
460  }
461  catch( Exception $e )
462  {
463  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
464  }
465  }
466 
489  function compile_string( $File , $Str )
490  {
491  try
492  {
493  if( $this->Settings === false )
494  {
495  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
496  if( $this->CachedMultyFS->file_exists( dirname( $File ).'/conf/cf_template_settings' ) )
497  {
498  $this->Settings->load_file( dirname( $File ).'/conf/cf_template_settings' );
499  }
500  }
501 
502  $Str = str_replace(
503  array( '{color_scheme}' , '{locale}' ) ,
504  array( '{color_scheme:available=default}' , $this->Lang->get_locale() ) ,
505  $Str
506  );
507 
508  $Str = $this->handle_color_scheme( $File , $Str );
509 
510  return( $Str );
511  }
512  catch( Exception $e )
513  {
514  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
515  }
516  }
517 
540  private function captcha_substitutions( $File , $PageName )
541  {
542  try
543  {
544  $PlaceHolders = array( '{captcha_image}' , '{captcha_field}' );
545 
546  $Subs = array(
547  '<img src="./captcha.html" align="top">' ,
548  '<input type="text" class="flat width_100" size="25" name="captcha" value="" '.
549  'style="text-align: center;">'
550  );
551 
552  $this->Template = str_replace( $PlaceHolders , $Subs , $this->Template );
553  }
554  catch( Exception $e )
555  {
556  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
557  }
558  }
559 
582  private function standart_substitutions( $File , $PageName )
583  {
584  try
585  {
586  global $StartGenerationTime;
587 
588  $PlaceHolders = array(
589  '{template_path}' , '{gen_time}' , '{captcha}' ,
590  '[page_name]' , '/./' , '[random]' , '{request_uri}'
591  );
592 
593  $Subs = array(
594  _get_package_relative_path( $File ) , microtime( true ) - $StartGenerationTime ,
595  '{captcha_image}&nbsp;{captcha_field}' , $PageName , '/' , rand() ,
596  $this->Security->get_srv( 'REQUEST_URI' , 'string' )
597  );
598  $this->Template = str_replace( $PlaceHolders , $Subs , $this->Template );
599 
600  $this->captcha_substitutions( $File , $PageName );
601  }
602  catch( Exception $e )
603  {
604  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
605  }
606  }
607 
623  {
624  try
625  {
626  $PlaceHolders = array(
627  '{header}' , '{footer}' , '{title}' , '{some_menu}' , '{pathway}' , '{menu}' , '{error_message}' ,
628  '{success_message}' , '{main}' , '{bottom}' , '{banner}' , '{banners}'
629  );
630  $this->Template = str_replace( $PlaceHolders , '' , $this->Template );
631 
632  $c = 0;
633  $PlaceHolders = array();
634 
635  do
636  {
637  preg_match( '/\{([a-zA-Z0-9_]+)\}/' , $this->Template , $PlaceHolders );
638 
639  $c = count( $PlaceHolders );
640 
641  if( $c > 1 )
642  {
643  $this->Template = str_replace( '{'.$PlaceHolders[ 1 ].'}' , '' , $this->Template );
644  }
645  }
646  while( $c > 1 );
647  }
648  catch( Exception $e )
649  {
650  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
651  }
652  }
653 
676  function cleanup( $File , $PageName )
677  {
678  try
679  {
680  if( $this->Template === false )
681  {
682  $this->Template = $this->get_template( $File );
683  }
684 
685  $this->Template = $this->Tags->compile_ultimix_tags( $this->Template );
686 
687  $this->standart_substitutions( $File , $PageName );
688 
689  $this->cleanup_placeholders();
690 
691  $this->Template = $this->compile_string( $File , $this->Template );
692 
693  $this->Template = str_replace( array( '[lfb]' , '[rfb]' ) , array( '{' , '}' ) , $this->Template );
694  }
695  catch( Exception $e )
696  {
697  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
698  }
699  }
700 
723  function get_template( $File = '' )
724  {
725  try
726  {
727  if( $this->Template === false )
728  {
729  /* чтение шаблона в переменную */
730  $TemplateName = $this->extract_template_name();
731  $this->Template = $this->CachedMultyFS->get_template( $File , "$TemplateName.tpl" );
732  }
733 
734  /* pre-обработка шаблона завершена, поэтому можно отдавать его клиентским пакетам */
735  return( $this->Template );
736  }
737  catch( Exception $e )
738  {
739  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
740  }
741  }
742 
757  function set_template( $theTemplate )
758  {
759  $this->Template = $theTemplate;
760  }
761  };
762 
763 ?>