XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
template.php
Go to the documentation of this file.
1 <?php
2 // $Id: template.php 1041 2012-09-09 06:23:26Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 include_once RMCPATH.'/include/tpl_functions.php';
12 
17 {
18  private $type = 'front';
22  public $tpl_head = array();
26  public $tpl_scripts = array();
27  public $tpl_hscripts = array();
31  public $tpl_styles = array();
35  private $tpl_menus = array();
39  private $tpl_vars = array();
43  private $messages = array();
47  private $menus = array();
51  private $toolbar = array();
55  private $help_link = array();
59  private $metas = array();
63  private $version = '';
68  function __construct(){
69  global $rmc_config;
70 
71  if (!function_exists("xoops_cp_header") && !$rmc_config['jquery']) return;
72  $this->version = str_replace(" ", '-', RMCVERSION);
73  $this->add_jquery(true);
74 
75  }
76 
82  public function get(){
83  static $instance;
84 
85  if (!isset($instance)) {
86  $instance = new RMTemplate();
87  }
88 
89  return $instance;
90 
91  }
92 
96  public function header(){
97  ob_start();
98  }
99 
100  public function footer(){
102  $content = ob_get_clean();
103  ob_start();
104 
105  $rmc_config = RMFunctions::configs();
106  $theme = isset($rmc_config['theme']) ? $rmc_config['theme'] : 'default';
107 
108  if (!file_exists(RMCPATH.'/themes/'.$theme.'/admin_gui.php')){
109  $theme = 'default';
110  }
111 
112  $rm_theme_url = RMCURL.'/themes/'.$theme;
113 
114  // Check if there are redirect messages
115  $rmc_messages = array();
116  if (isset($_SESSION['rmMsg'])){
117  foreach ($_SESSION['rmMsg'] as $msg){
118  $rmc_messages[] = $msg;
119  }
120  unset($_SESSION['rmMsg']);
121  }
122 
123  include_once RMCPATH.'/themes/'.$theme.'/admin_gui.php';
124  $output = ob_get_clean();
125 
126  $output = RMEvents::get()->run_event('rmcommon.admin.output', $output);
127 
128  echo $output;
129  }
130 
139  public function get_template($file, $type='module',$module='rmcommon',$plugin=''){
140  global $rmc_config, $xoopsConfig;
141 
142 
143  $type = $type=='' ? 'module' : $type;
144 
145  if (!function_exists("xoops_cp_header")){
146 
147  $theme = $xoopsConfig['theme_set'];
148  $where = XOOPS_THEME_PATH.'/'.$theme;
149  $where .= $type=='module' ? '/modules/' : '/'.$plugin.'s/';
150  $where .= $module.($plugin!='' ? '/'.$plugin : '');
151 
152  if(is_file($where.'/'.$file)) return $where.'/'.$file;
153 
154  $where = XOOPS_ROOT_PATH.'/modules/'.$module.'/templates';
155  $where .= $type!='module' ? "/$type" : '';
156  $where .= "/$file";
157 
158 
159  if(is_file($where)) return $where;
160 
161  }
162 
163  $theme = isset($rmc_config['theme']) ? $rmc_config['theme'] : 'default';
164 
165  $where = $type=='module' ? 'modules/'.$module : ($type=='plugin' ? $module.'/'.$plugin : 'modules/rmcommon');
166 
167  $lpath = RMCPATH.'/themes/'.$theme.'/'.$where.'/'.$file;
168 
169  if (!is_dir(RMCPATH.'/themes/'.$theme)){
170  $theme = 'default';
171  }
172 
173  if (file_exists($lpath))
174  return $lpath;
175 
176  if ($type=='plugin'){
177  return RMCPATH.'/plugins/'.$plugin.'/templates/'.$file;
178  } else {
179  return XOOPS_ROOT_PATH.'/'.$where.'/templates/'.$file;
180  }
181 
182  }
183 
190  public function tpl_path($file, $module, $subdir = 'templates'){
191  global $xoopsConfig;
192 
193  $theme = $xoopsConfig['theme_set'];
194  $subdir = $subdir == '' ? 'templates' : $subdir;
195 
196  if(!is_dir(XOOPS_ROOT_PATH.'/modules/'.$module)) return;
197 
198  $tpath = XOOPS_THEME_PATH.'/'.$theme.'/modules/'.$module.'/'.($subdir!='templates'?$subdir.'/':'').$file;
199 
200  if(is_file($tpath))
201  return $tpath;
202 
203  return XOOPS_ROOT_PATH.'/modules/'.$module.'/'.$subdir.'/'.$file;
204 
205  }
206 
211  public function location_id($id){
212 
213  }
214 
219  public function set_help($link){
220  //trigger_error(__('RMTemplate::set_help is deprecated. Use add_help instead.','rmcommon'), E_USER_WARNING);
221  //$this->add_help($caption, $link);
222  $this->add_help(__('Help','rmcommon'),$link);
223  }
224 
225  public function help($single = 0){
226  if($single)
227  return $this->help_link[0]['link'];
228  else
229  return $this->help_link;
230  }
231 
232  public function add_help($caption, $link){
233  $this->help_link[] = array(
234  'caption' => $caption,
235  'link' => $link
236  );
237  }
238 
244  public function add_message($message, $level=0){
245  $this->messages[] = array('text'=>$message, 'level'=>$level);
246  }
251  public function get_messages(){
252  return $this->messages;
253  }
254 
260  public function add_head($head){
261  // Dynamic header (It must be be an array)
262  if (is_array($head)):
263  array_merge($this->tpl_head, $head);
264  else:
265  $this->tpl_head[] = $head;
266  endif;
267  }
272  public function get_head(){
273  return $this->tpl_head;
274  }
275 
284  public function add_script($url,$type='text/javascript',$footer = false){
285  if($url=='') return;
286  if (strpos($url, "?")>1){
287  if (strpos($url, 'ver=')===FALSE){
288  $url .= "&amp;ver=".$this->version;
289  }
290  } else {
291  $url .= "?ver=".$this->version;
292  }
293 
294  $id = crc32($url.$type);
295  if (isset($this->tpl_scripts[$id])) return;
296 
297  $this->tpl_scripts[$id] = array('url'=>$url,'type'=>$type, 'footer'=>$footer);
298  }
299 
304  public function add_head_script($script){
305  $this->tpl_hscripts[] = $script;
306  }
307 
311  public function head_scripts(){
312  $ret = '<script type="text/javascript">'."\n";
313 
314  foreach($this->tpl_hscripts as $script){
315 
316  $ret .= $script."\n";
317  $ret .= "//".str_repeat("-",20)."\n";
318 
319  }
320 
321  $ret .= '</script>';
322 
323  return $ret;
324 
325  }
326 
330  public function add_local_script($file, $element='rmcommon', $subfolder='', $type='text/javascript', $more='', $footer = false){
331  global $rmc_config, $xoopsConfig;
332 
333  if($file=='jquery.min.js' && $element=='rmcommon' && !$rmc_config['jquery']) return;
334 
335  // Id for element
336  $url = XOOPS_URL.'/modules/'.$element.'/'.$subfolder.'/js/'.$file;
337 
338  if (strpos($url, "?")>1){
339  if (strpos($url, 'ver=')===FALSE){
340  $url .= "&amp;ver=".$this->version;
341  }
342  } else {
343  $url .= "?ver=".$this->version;
344  }
345 
346  $id = crc32($url.$type.$more);
347 
348  if (isset($this->tpl_scripts[$id])) return;
349 
350  if (!defined('XOOPS_CPFUNC_LOADED')){
351  $theme = isset($xoopsConfig['theme_set']) ? $xoopsConfig['theme_set'] : 'default';
352  $themepath = XOOPS_THEME_PATH.'/'.$theme;
353  $themeurl = XOOPS_THEME_URL.'/'.$theme;
354  } else {
355  $theme = isset($rmc_config['theme']) ? $rmc_config['theme'] : 'default';
356  $themepath = RMCPATH.'/themes/'.$theme;
357  $themeurl = RMCURL.'/themes/'.$theme;
358  }
359 
360  $theme_file = $themepath.'/js/'.$element.($element!='' ? '/' : '').($subfolder!='' ? $subfolder.'/' : '').$file;
361 
362  if (is_file($theme_file)){
363  $url = $themeurl.'/js/'.($element!='' ? $element.'/' : '').($subfolder!='' ? $subfolder.'/' : '').$file;
364  } else {
365  $url = XOOPS_URL.'/'.($element!='' ? 'modules/'.$element.'/' : '').($subfolder!='' ? $subfolder.'/' : '').'js/'.$file;
366  }
367 
368  if (strpos($url, "?")>1){
369  if (strpos($url, 'ver=')===FALSE){
370  $url .= "&amp;ver=".$this->version;
371  }
372  } else {
373  $url .= "?ver=".$this->version;
374  }
375 
376  $this->tpl_scripts[$id] = array('url'=>$url,'type'=>$type, 'more'=>$more, 'footer' => $footer);
377 
378  }
379 
380  public function add_theme_script($script, $theme='', $subfolder='', $type='text/javascript', $more='', $footer = false){
381  global $xoopsConfig, $rmc_config;
382 
383  if(defined('XOOPS_CPFUNC_LOADED')){
384 
385  $theme = $theme=='' ? $rmc_config['theme'] : $theme;
386  $file = RMCPATH.'/themes/'.$theme.'/js/'.($subfolder!=''?$subfolder.'/':'').$script;
387  $url = str_replace(RMCPATH, RMCURL, $file);
388 
389  } else {
390 
391  $theme = $theme=='' ? $xoopsConfig['theme_set'] :$theme;
392  $file = XOOPS_THEME_PATH.'/'.$theme.'/js/'.($subfolder!=''?$subfolder.'/':'').$script;
393  $url = str_replace(XOOPS_THEME_PATH, XOOPS_THEME_URL, $file);
394 
395  }
396 
397  $id = crc32($script.$theme.$subfolder.$type.$more);
398 
399  if(!file_exists($file)) return;
400 
401  if (strpos($url, "?")>1){
402  if (strpos($url, 'ver=')===FALSE){
403  $url .= "&amp;ver=".$this->version;
404  }
405  } else {
406  $url .= "?ver=".$this->version;
407  }
408 
409  $this->tpl_scripts[$id] = array(
410  'url'=>$url,
411  'type'=>$type,
412  'more'=>$more,
413  'footer' => $footer
414  );
415 
416  }
420  public function add_jquery($ui=true, $local=true){
421  global $rmc_config;
422 
423  if(!$rmc_config['jquery']) return;
424 
425  if($local)
426  $this->add_local_script('jquery.min.js', 'rmcommon', 'include');
427  else
428  $this->add_script("http://code.jquery.com/jquery-latest.js");
429 
430  if ($ui)
431  $this->add_local_script('jquery-ui.min.js', 'rmcommon', 'include');
432  }
436  public function get_scripts(){
437  $ev = RMEvents::get();
438  $this->tpl_scripts = $ev->run_event('rmcommon.get.scripts',$this->tpl_scripts);
439  return $this->tpl_scripts;
440  }
441 
445  public function clear_scripts(){
446  $this->tpl_scripts = array();
447  }
451  public function add_scripts_array($scripts){
452  if (!is_array($scripts)) return;
453 
454  foreach ($scripts as $script){
455  // Why? becouse we need to add an id to script
456  $this->add_script($script['url'], $script['type']);
457  }
458  }
459 
465  public function add_style($sheet, $element='rmcommon', $subfolder='', $media='all', $more='', $front = false, $footer = false){
466  global $rmc_config, $xoopsConfig;
467 
468  $id = crc32($sheet.$element.$subfolder.$media.$more);
469 
470  if (isset($this->tpl_styles[$id])) return;
471 
472  if (!function_exists("xoops_cp_header")){
473  $theme = isset($xoopsConfig['theme_set']) ? $xoopsConfig['theme_set'] : 'default';
474  $themepath = XOOPS_THEME_PATH.'/'.$theme;
475  $themeurl = XOOPS_THEME_URL.'/'.$theme;
476  } else {
477  $theme = isset($rmc_config['theme']) ? $rmc_config['theme'] : 'default';
478  $themepath = RMCPATH.'/themes/'.$theme;
479  $themeurl = RMCURL.'/themes/'.$theme;
480  }
481 
482  $theme_file = $themepath.'/css/'.$element.($element!='' ? '/' : '').($subfolder!='' ? $subfolder.'/' : '').$sheet;
483 
484  if (is_file($theme_file)){
485  $url = $themeurl.'/css/'.($element!='' ? $element.'/' : '').($subfolder!='' ? $subfolder.'/' : '').$sheet;
486  } else {
487  $url = XOOPS_URL.'/'.($element!='' ? 'modules/'.$element.'/' : '').($subfolder!='' ? $subfolder.'/' : '').'css/'.$sheet;
488  }
489 
490  if (strpos($url, "?")>1){
491  if (strpos($url, 'ver=')===FALSE){
492  $url .= "&amp;ver=".$this->version;
493  }
494  } else {
495  $url .= "?ver=".$this->version;
496  }
497 
498  $this->tpl_styles[$id] = array(
499  'url'=>$url,
500  'rel'=>'stylesheet',
501  'media'=>$media,
502  'more'=>$more,
503  'footer' => $footer
504  );
505 
506  }
507 
511  public function add_xoops_style($sheet, $element='rmcommon', $subfolder='', $media='all', $more='', $footer = false){
512 
513  $this->add_style($sheet, $element, $subfolder, $media, $more, true, $footer);
514 
515  }
516 
525  public function add_theme_style($sheet, $theme='', $subfolder='', $media='all', $more='', $footer=false){
526  global $xoopsConfig, $rmc_config;
527 
528  if(defined('XOOPS_CPFUNC_LOADED') && is_dir(RMCPATH.'/themes/'.$theme)){
529 
530  $theme = $theme=='' ? $rmc_config['theme'] : $theme;
531  $file = RMCPATH.'/themes/'.$theme.'/css/'.($subfolder!=''?$subfolder.'/':'').$sheet;
532  $url = str_replace(RMCPATH, RMCURL, $file);
533 
534  } else {
535 
536  $theme = $theme=='' ? $xoopsConfig['theme_set'] :$theme;
537  $file = XOOPS_THEME_PATH.'/'.$theme.'/css/'.($subfolder!=''?$subfolder.'/':'').$sheet;
538  $url = str_replace(XOOPS_THEME_PATH, XOOPS_THEME_URL, $file);
539 
540  }
541 
542  $id = crc32($sheet.$theme.$subfolder.$media.$more);
543 
544  if(!file_exists($file)) return;
545 
546  if (strpos($url, "?")>1){
547  if (strpos($url, 'ver=')===FALSE){
548  $url .= "&amp;ver=".$this->version;
549  }
550  } else {
551  $url .= "?ver=".$this->version;
552  }
553 
554  $this->tpl_styles[$id] = array(
555  'url'=>$url,
556  'rel'=>'stylesheet',
557  'media'=>$media,
558  'more'=>$more,
559  'footer' => $footer
560  );
561 
562  }
563 
567  public function style_url($sheet, $element='rmcommon', $subfolder=''){
568  global $xoopsConfig, $rmc_config;
569 
570  if(defined('XOOPS_CPFUNC_LOADED')){
571 
572  $theme = isset($rmc_config['theme']) ? $rmc_config['theme'] : 'twop6';
573  $file = RMCPATH.'/themes/'.$theme.'/css/'.($element!='' ? $element : 'rmcommon').'/'.($subfolder!=''?$subfolder.'/':'').$sheet;
574  $url = str_replace(RMCPATH, RMCURL, $file);
575 
576  } else {
577 
578  $theme = $xoopsConfig['theme_set'];
579  $file = XOOPS_THEME_PATH.'/'.$theme.'/css/'.($subfolder!=''?$subfolder.'/':'').$sheet;
580  $url = str_replace(XOOPS_THEME_PATH, XOOPS_THEME_URL, $file);
581 
582  }
583 
584  if (!is_file($file)){
585  $url = XOOPS_URL.'/'.($element!='' ? 'modules/'.$element.'/' : '').($subfolder!='' ? $subfolder.'/' : '').'css/'.$sheet;
586  }
587 
588  if (strpos($url, "?")>1){
589  if (strpos($url, 'ver=')===FALSE){
590  $url .= "&amp;ver=".$this->version;
591  }
592  } else {
593  $url .= "?ver=".$this->version;
594  }
595 
596  return $url;
597  }
598 
602  public function get_styles(){
603  $ev = RMEvents::get();
604  $this->tpl_styles = $ev->run_event('rmcommon.get.styles',$this->tpl_styles);
605  return $this->tpl_styles;
606  }
610  public function clear_styles(){
611  $this->tpl_styles = array();
612  }
616  public function add_styles_array($styles){
617  if (!is_array($styles)) return;
618 
619  foreach ($styles as $style){
620  // Why? becouse we need to add an id to style
621  $this->add_style($style['sheet'], $style['system'], $style['subfolder'], $style['media'], $style['more']);
622  }
623  }
624 
630  public function assign($varname, $value){
631  $this->tpl_vars[$varname] = $value;
632  }
638  public function append($varname, $value){
639  $this->tpl_vars[$varname][] = $value;
640  }
644  public function vars(){
645  return $this->tpl_vars;
646  }
653  public function get_var($varname){
654  if (isset($this->tpl_vars[$varname])){
655  return $this->tpl_vars[$varname];
656  }
657  return false;
658  }
659 
670  public function add_menu_option($caption, $link, $icon='', $class='', $target=''){
671  if ($caption=='' || $link=='') return;
672 
673  $id = crc32($link);
674 
675  if (isset($this->tpl_menus[$id])) return;
676 
677  $this->tpl_menus[$id] = array('caption'=>$caption,'link'=>$link,'icon'=>$icon,'class'=>$class,'target'=>$target, 'type'=>'normal');
678  }
679 
680  public function add_separator(){
681  $this->tpl_menus = array('type'=>'separator');
682  }
686  public function menu_options(){
687 
688  $this->tpl_menus = RMEvents::get()->run_event('rmcommon.menus_options',$this->tpl_menus, $this);
689 
690  return $this->tpl_menus;
691  }
692 
696  public function add_menu($title, $link, $icon='', $class='', $location='', $options=array()){
697  $this->menus[] = array(
698  'title' => $title,
699  'link' => $link,
700  'icon' => $icon,
701  'class' => $class,
702  'location' => $location,
703  'options' => $options
704  );
705  }
706 
707  public function get_menus(){
708  return $this->menus;
709  }
710 
711  public function add_tool($title, $link, $icon='', $location=''){
712  $this->toolbar[] = array(
713  'title' => $title,
714  'link' => $link,
715  'icon' => $icon,
716  'location' => $location
717  );
718  }
719 
720  public function get_toolbar(){
721  return $this->toolbar;
722  }
723 
727  public function add_meta($name, $content){
728 
729  $this->metas[$name] = $content;
730 
731  }
732  public function get_metas(){
733  return $this->metas;
734  }
735 
736 }