PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/html/layout/chamilo_template.class.php

https://bitbucket.org/chamilo/chamilo/
PHP | 245 lines | 178 code | 35 blank | 32 comment | 19 complexity | dbde8919849abeec35302c8badfd4ec9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. namespace common\libraries;
  3. require_once Path :: get_plugin_path() . 'phpbb3/phpbb3_template.php';
  4. use \Phpbb3Template;
  5. require_once Path :: get_library_path() . 'html/layout/chamilo_template_compiler.class.php';
  6. define('WEB_TPL_PATH', 'WEB_TPL_PATH');
  7. define('SYS_TPL_PATH', 'SYS_TPL_PATH');
  8. class ChamiloTemplate extends Phpbb3Template
  9. {
  10. private static $instance;
  11. private $theme;
  12. function __construct($theme)
  13. {
  14. $this->set_theme($theme);
  15. $this->set_template();
  16. }
  17. static function get_instance($theme)
  18. {
  19. if (! isset(self :: $instance))
  20. {
  21. self :: $instance = new self($theme);
  22. }
  23. return self :: $instance;
  24. }
  25. function get_configuration()
  26. {
  27. return array();
  28. }
  29. /**
  30. * Set template location
  31. * @access public
  32. */
  33. function set_template()
  34. {
  35. // TODO: What if these paths don't exist?
  36. $this->root = $this->get_template_path();
  37. $this->cachepath = $this->get_cache_path();
  38. if (! file_exists($this->root))
  39. {
  40. trigger_error('Template path could not be found: ' . $this->root, E_USER_ERROR);
  41. }
  42. $this->_rootref = &$this->_tpldata['.'][0];
  43. return true;
  44. }
  45. /**
  46. * Sets the template filenames for handles. $filename_array
  47. * should be a hash of handle => filename pairs.
  48. * @access public
  49. */
  50. function set_filenames($filename_array)
  51. {
  52. if (! is_array($filename_array))
  53. {
  54. return false;
  55. }
  56. foreach ($filename_array as $handle => $filename)
  57. {
  58. if (empty($filename))
  59. {
  60. trigger_error('ChamiloTemplate->set_filenames: Empty filename specified for' . $handle, E_USER_ERROR);
  61. }
  62. $this->filename[$handle] = $filename;
  63. $this->files[$handle] = $this->root . $filename;
  64. }
  65. return true;
  66. }
  67. /**
  68. * Display handle
  69. * @access public
  70. */
  71. function display($handle, $include_once = true)
  72. {
  73. if (defined('IN_ERROR_HANDLER'))
  74. {
  75. if ((E_NOTICE & error_reporting()) == E_NOTICE)
  76. {
  77. error_reporting(error_reporting() ^ E_NOTICE);
  78. }
  79. }
  80. if ($filename = $this->_tpl_load($handle))
  81. {
  82. ($include_once) ? include_once ($filename) : include ($filename);
  83. }
  84. else
  85. {
  86. eval(' ?>' . $this->compiled_code[$handle] . '<?php
  87. namespace common\libraries; ');
  88. }
  89. return true;
  90. }
  91. function render($handle, $return = true)
  92. {
  93. if ($return)
  94. {
  95. return $this->assign_display($handle);
  96. }
  97. else
  98. {
  99. $this->display($handle);
  100. }
  101. }
  102. /**
  103. * Load a compiled template if possible, if not, recompile it
  104. * @access private
  105. */
  106. function _tpl_load(&$handle)
  107. {
  108. if (! isset($this->filename[$handle]))
  109. {
  110. trigger_error('ChamiloTemplate->_tpl_load(): No file specified for handle' . $handle, E_USER_ERROR);
  111. }
  112. $filename = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.php';
  113. $recompile = false;
  114. if (! file_exists($filename) || @filesize($filename) === 0)
  115. {
  116. $recompile = true;
  117. }
  118. else
  119. {
  120. $reload_templates = PlatformSetting :: get('reload_templates');
  121. if ($reload_templates)
  122. {
  123. $recompile = (@filemtime($filename) < filemtime($this->files[$handle])) ? true : false;
  124. }
  125. }
  126. // Recompile page if the original template is newer, otherwise load the compiled version
  127. if (! $recompile)
  128. {
  129. return $filename;
  130. }
  131. $compile = new ChamiloTemplateCompiler($this);
  132. // If we don't have a file assigned to this handle, die.
  133. if (! isset($this->files[$handle]))
  134. {
  135. trigger_error('ChamiloTemplate->_tpl_load(): No file specified for handle ' . $handle, E_USER_ERROR);
  136. }
  137. $compile->_tpl_load_file($handle);
  138. return false;
  139. }
  140. /**
  141. * Include a separate template
  142. * @access private
  143. */
  144. function _tpl_include($filename, $include = true)
  145. {
  146. $handle = $filename;
  147. $this->filename[$handle] = $filename;
  148. $this->files[$handle] = $this->root . '/' . $filename;
  149. $filename = $this->_tpl_load($handle);
  150. if ($include)
  151. {
  152. if ($filename)
  153. {
  154. include ($filename);
  155. return;
  156. }
  157. eval(' ?>' . $this->compiled_code[$handle] . '<?php
  158. namespace common\libraries; ');
  159. }
  160. }
  161. /**
  162. * Include a php-file
  163. * @access private
  164. */
  165. function _php_include($filename)
  166. {
  167. $file = Path :: get(SYS_PATH) . $filename;
  168. if (! file_exists($file))
  169. {
  170. // trigger_error cannot be used here, as the output already started
  171. echo 'ChamiloTemplate->_php_include(): File ' . htmlspecialchars($file) . ' does not exist or is empty';
  172. return;
  173. }
  174. include ($file);
  175. }
  176. function get_path($path_type)
  177. {
  178. switch ($path_type)
  179. {
  180. case WEB_TPL_PATH :
  181. return Path :: get(WEB_LAYOUT_PATH) . $this->get_theme() . '/templates/';
  182. case SYS_TPL_PATH :
  183. return Path :: get(SYS_LAYOUT_PATH) . $this->get_theme() . '/templates/';
  184. }
  185. }
  186. /**
  187. * Get the path to the theme's template folder.
  188. */
  189. function get_template_path()
  190. {
  191. return $this->get_path(SYS_TPL_PATH);
  192. }
  193. function get_cache_path()
  194. {
  195. return Path :: get_cache_path() . 'layout/' . $this->get_theme() . '/';
  196. }
  197. function set_theme($theme)
  198. {
  199. $this->theme = $theme;
  200. }
  201. function get_theme()
  202. {
  203. return $this->theme;
  204. }
  205. function reset()
  206. {
  207. $this->set_template();
  208. }
  209. }
  210. ?>