/components/com_jcomments/libraries/joomlatune/template.php

https://github.com/Shigaru/shigaru · PHP · 446 lines · 178 code · 44 blank · 224 comment · 13 complexity · fa1fde62fff4992b1e69e8e4c1c27a7e MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Template class
  5. *
  6. * @static
  7. * @version 1.0
  8. * @package JoomlaTune.Framework
  9. * @author Sergey M. Litvinov (smart@joomlatune.ru)
  10. * @copyright (C) 2006-2009 by Sergey M. Litvinov (http://www.joomlatune.ru)
  11. * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
  12. *
  13. **/
  14. // define directory separator short constant
  15. if (!defined( 'DS' )) {
  16. define( 'DS', DIRECTORY_SEPARATOR );
  17. }
  18. /**
  19. * JoomlaTune base template class
  20. *
  21. * @abstract
  22. *
  23. */
  24. class JoomlaTuneTemplate {
  25. /**
  26. * A hack to support __construct() on PHP 4
  27. * Hint: descendant classes have no PHP4 class_name() constructors,
  28. * so this constructor gets called first and calls the top-layer __construct()
  29. * which (if present) should call parent::__construct()
  30. *
  31. * @return Object
  32. */
  33. function JoomlaTuneTemplate()
  34. {
  35. $args = func_get_args();
  36. call_user_func_array(array(&$this, '__construct'), $args);
  37. }
  38. /**
  39. * Class constructor
  40. *
  41. * @access protected
  42. */
  43. function __construct()
  44. {
  45. $this->_vars = array();
  46. }
  47. /**
  48. * Render template into string
  49. *
  50. * @abstract Implement in child classes
  51. * @access public
  52. * @return string
  53. */
  54. function render()
  55. {
  56. }
  57. /**
  58. * Sets global variables
  59. *
  60. * @access public
  61. * @param array $value array list of global variables
  62. * @return void
  63. */
  64. function setGlobalVars( &$value )
  65. {
  66. $this->_globals =& $value;
  67. }
  68. /**
  69. * Fetches and returns a given variable.
  70. *
  71. * @access private
  72. * @param string $name Variable name
  73. * @param string $default Default value if the variable does not exist
  74. * @return mixed Requested variable
  75. */
  76. function getVar( $name, $default = null )
  77. {
  78. if (isset($this->_vars[$name])) {
  79. // fetch variable from local variables list
  80. return $this->_vars[$name];
  81. } else if (isset($this->_globals[$name])) {
  82. // fetch variable from global variables list
  83. return $this->_globals[$name];
  84. } else {
  85. // return default value
  86. return $default;
  87. }
  88. }
  89. /**
  90. * Set a template variable, creating it if it doesn't exist
  91. *
  92. * @access public
  93. * @param string $name The name of the variable
  94. * @param mixed $value The value of the variable
  95. * @return void
  96. */
  97. function setVar( $name, $value )
  98. {
  99. $this->_vars[$name] = $value;
  100. }
  101. }
  102. /**
  103. *
  104. * JoomlaTune template renderer class
  105. *
  106. */
  107. class JoomlaTuneTemplateRender {
  108. var $_root = null;
  109. var $_uri = null;
  110. var $_globals = null;
  111. var $_templates = null;
  112. /**
  113. * A hack to support __construct() on PHP 4
  114. * Hint: descendant classes have no PHP4 class_name() constructors,
  115. * so this constructor gets called first and calls the top-layer __construct()
  116. * which (if present) should call parent::__construct()
  117. *
  118. * @return Object
  119. */
  120. function JoomlaTuneTemplateRender()
  121. {
  122. $args = func_get_args();
  123. call_user_func_array(array(&$this, '__construct'), $args);
  124. }
  125. /**
  126. * Class constructor
  127. *
  128. * @access protected
  129. */
  130. function __construct()
  131. {
  132. $this->_globals = array();
  133. $this->_templates = array();
  134. //set root template directory
  135. $this->setRoot(dirname(__FILE__). DS. 'tpl' );
  136. }
  137. /**
  138. * Returns a reference to the global JoomlaTuneTemplateRender object, only creating it
  139. * if it doesn't already exist.
  140. *
  141. * This method must be invoked as:
  142. * <pre> $tmpl = &JoomlaTuneTemplateRender::getInstance();</pre>
  143. *
  144. * @static
  145. * @access public
  146. * @return JoomlaTuneTemplate A template object
  147. */
  148. function &getInstance()
  149. {
  150. static $instance = null;
  151. if (!is_object( $instance )) {
  152. $instance = new JoomlaTuneTemplateRender();
  153. }
  154. return $instance;
  155. }
  156. /**
  157. * Sets root base for the template
  158. *
  159. * The parameter depends on the reader you are using.
  160. *
  161. * @access public
  162. * @param string root base of the templates
  163. * @return void
  164. */
  165. function setRoot( $root )
  166. {
  167. $this->_root = $root ? $root : dirname(__FILE__).DS.'tpl';
  168. }
  169. /**
  170. * Gets name of root base for the templates
  171. *
  172. * @access public
  173. * @return string
  174. */
  175. function getRoot()
  176. {
  177. return $this->_root;
  178. }
  179. /**
  180. * Sets base url for the template images and css
  181. *
  182. * The parameter depends on the reader you are using.
  183. *
  184. * @access public
  185. * @param string base url of the templates
  186. * @return void
  187. */
  188. function setBaseURI( $uri )
  189. {
  190. $this->_uri = $uri;
  191. }
  192. /**
  193. * Gets name of root base for the templates
  194. *
  195. * @access public
  196. * @return string
  197. */
  198. function getBaseURI()
  199. {
  200. return $this->_uri;
  201. }
  202. /**
  203. * Load template class
  204. *
  205. * @access public
  206. * @param string $template name of the template
  207. * @return boolean
  208. */
  209. function load( $template ) {
  210. $templateFileName = $this->getRoot().DS.$template.'.php';
  211. if (is_file($templateFileName)) {
  212. ob_start();
  213. include_once( $templateFileName );
  214. ob_end_clean();
  215. $templateClass = 'jtt_' . $template;
  216. if (!class_exists($templateClass)) {
  217. $this->riseError( 'Template class not found in: ' . $template );
  218. return false;
  219. }
  220. ob_start();
  221. $tmpl =& new $templateClass;
  222. $isValidTemplate = false;
  223. if ((version_compare( phpversion(), '4.2.0' ) < 0)) {
  224. // PHP < 4.2.0 (function is_a not exists)
  225. $isValidTemplate = is_subclass_of($tmpl, 'JoomlaTuneTemplate');
  226. } else {
  227. // PHP > 4.2.0 (using is_a)
  228. $isValidTemplate = is_a( $tmpl, 'JoomlaTuneTemplate' );
  229. }
  230. ob_end_clean();
  231. if (!$isValidTemplate) {
  232. unset( $tmpl );
  233. $this->riseError( 'Incorrect template: ' . $template );
  234. return false;
  235. }
  236. ob_start();
  237. $tmpl->setGlobalVars( $this->_globals );
  238. $this->_templates[$template] =& $tmpl;
  239. ob_end_clean();
  240. return true;
  241. }
  242. return false;
  243. }
  244. /**
  245. * Adds a global variable
  246. *
  247. * Global variables are valid in all templates of this object.
  248. *
  249. * @access public
  250. * @param string $name name of the global variable
  251. * @param string $value value of the variable
  252. * @return void
  253. * @see addVar()
  254. */
  255. function addGlobalVar( $name, $value )
  256. {
  257. $this->_globals[strtolower( $name )] = ( string )$value;
  258. }
  259. /**
  260. * Add a variable to a template
  261. *
  262. * @access public
  263. * @param string $template name of the template
  264. * @param string $name name of the variable
  265. * @param mixed $value value of the variable
  266. * @return void
  267. * @see addGlobalVar()
  268. */
  269. function addVar( $template, $name, $value )
  270. {
  271. $this->_templates[$template]->_vars[$name] = $value;
  272. }
  273. /**
  274. * Add a object variable to a template
  275. *
  276. * @access public
  277. * @param string $template name of the template
  278. * @param string $name name of the variable
  279. * @param mixed $value value of the variable
  280. * @return void
  281. * @see addVar(), addGlobalVar()
  282. */
  283. function addObject( $template, $name, $value )
  284. {
  285. $this->_templates[$template]->_vars[$name] = $value;
  286. }
  287. /**
  288. * Fetches and returns a given variable from template.
  289. *
  290. * @access public
  291. * @param string $template name of the template
  292. * @param string $name name of the variable
  293. * @return mixed
  294. */
  295. function getVar( $template, $name )
  296. {
  297. if (!$this->exists($template))
  298. {
  299. $this->riseError( 'Unknown template: ' . $template );
  300. return null;
  301. }
  302. ob_start();
  303. $result = $this->_templates[$template]->getVar($name);
  304. ob_end_clean();
  305. return $result;
  306. }
  307. /**
  308. * Renders template and return result as string
  309. *
  310. * @access public
  311. * @param string $template name of the template
  312. * @return string
  313. * @see displayTemplate()
  314. */
  315. function renderTemplate( $template )
  316. {
  317. if (!$this->exists($template)) {
  318. $this->riseError( 'Unknown template: ' . $template );
  319. return null;
  320. }
  321. ob_start();
  322. $this->_templates[$template]->render();
  323. $result = ob_get_contents();
  324. ob_end_clean();
  325. return $result;
  326. }
  327. /**
  328. * Renders template and displays output
  329. *
  330. * @access public
  331. * @param string $template name of the template
  332. * @return void
  333. * @see renderTemplate()
  334. */
  335. function displayTemplate( $template )
  336. {
  337. echo $this->renderTemplate($template);
  338. }
  339. /**
  340. * Frees a template
  341. *
  342. * All memory consumed by the template will be freed.
  343. *
  344. * @access public
  345. * @param string $template name of the template
  346. * @return void
  347. * @see freeAllTemplates()
  348. */
  349. function freeTemplate( $template )
  350. {
  351. unset($this->_templates[$template]);
  352. }
  353. /**
  354. * Frees all templates
  355. *
  356. * All memory consumed by the templates will be freed.
  357. *
  358. * @access public
  359. * @return void
  360. * @see freeTemplate()
  361. */
  362. function freeAllTemplates()
  363. {
  364. $this->_templates = array();
  365. $this->_globals = array();
  366. $this->_vars = array();
  367. }
  368. /**
  369. * Checks wether a template exists
  370. *
  371. * @access public
  372. * @param string $name name of the template
  373. * @return boolean true, if the template exists (loaded), false otherwise
  374. * @see load()
  375. */
  376. function exists( $name )
  377. {
  378. return isset($this->_templates[$name]);
  379. }
  380. /**
  381. * Displays error-message and die
  382. *
  383. * @access private
  384. * @param string $message error message
  385. * @param string $type type of error (die or warning)
  386. * @return void
  387. */
  388. function riseError( $message, $type = 'die' )
  389. {
  390. switch($type)
  391. {
  392. case 'warning':
  393. echo( 'JoomlaTuneTemplateWarning: ' . $message );
  394. break;
  395. case 'die':
  396. default:
  397. die( 'JoomlaTuneTemplateError: ' . $message );
  398. break;
  399. }
  400. }
  401. }
  402. ?>