PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/amet17/webstar
PHP | 475 lines | 189 code | 47 blank | 239 comment | 14 complexity | 0277606e4b710951ecd553824e4fef91 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-2010 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 JoomlaTuneTemplate
  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 mixed $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 $_default = null;
  110. var $_uri = null;
  111. var $_globals = null;
  112. var $_templates = null;
  113. /**
  114. * A hack to support __construct() on PHP 4
  115. * Hint: descendant classes have no PHP4 class_name() constructors,
  116. * so this constructor gets called first and calls the top-layer __construct()
  117. * which (if present) should call parent::__construct()
  118. *
  119. * @return JoomlaTuneTemplateRender
  120. */
  121. function JoomlaTuneTemplateRender()
  122. {
  123. $args = func_get_args();
  124. call_user_func_array(array(&$this, '__construct'), $args);
  125. }
  126. /**
  127. * Class constructor
  128. *
  129. * @access protected
  130. */
  131. function __construct()
  132. {
  133. $this->_globals = array();
  134. $this->_templates = array();
  135. //set root template directory
  136. $this->setRoot(dirname(__FILE__). DS. 'tpl' );
  137. }
  138. /**
  139. * Returns a reference to the global JoomlaTuneTemplateRender object, only creating it
  140. * if it doesn't already exist.
  141. *
  142. * This method must be invoked as:
  143. * <pre> $tmpl = &JoomlaTuneTemplateRender::getInstance();</pre>
  144. *
  145. * @static
  146. * @access public
  147. * @return JoomlaTuneTemplate A template object
  148. */
  149. function &getInstance()
  150. {
  151. static $instance = null;
  152. if (!is_object( $instance )) {
  153. $instance = new JoomlaTuneTemplateRender();
  154. }
  155. return $instance;
  156. }
  157. /**
  158. * Sets root base for the template
  159. *
  160. * The parameter depends on the reader you are using.
  161. *
  162. * @access public
  163. * @param string $root root base of the templates
  164. * @return void
  165. */
  166. function setRoot( $path )
  167. {
  168. $this->_root = $path ? $path : dirname(__FILE__).DS.'tpl';
  169. }
  170. /**
  171. * Gets name of root base for the templates
  172. *
  173. * @access public
  174. * @return string
  175. */
  176. function getRoot()
  177. {
  178. return $this->_root;
  179. }
  180. /**
  181. * Sets default base for the template
  182. *
  183. * The parameter depends on the reader you are using.
  184. *
  185. * @access public
  186. * @param string $path default base of the templates
  187. * @return void
  188. */
  189. function setDefaultRoot( $path )
  190. {
  191. $this->_default = $path ? $path : $this->getRoot();
  192. }
  193. /**
  194. * Gets name of default base for the templates
  195. *
  196. * @access public
  197. * @return string
  198. */
  199. function getDefaultRoot()
  200. {
  201. return $this->_default;
  202. }
  203. /**
  204. * Sets base url for the template images and css
  205. *
  206. * The parameter depends on the reader you are using.
  207. *
  208. * @access public
  209. * @param string $uri The base url of the templates
  210. * @return void
  211. */
  212. function setBaseURI( $uri )
  213. {
  214. $this->_uri = $uri;
  215. }
  216. /**
  217. * Gets name of root base for the templates
  218. *
  219. * @access public
  220. * @return string
  221. */
  222. function getBaseURI()
  223. {
  224. return $this->_uri;
  225. }
  226. /**
  227. * Load template class
  228. *
  229. * @access public
  230. * @param string $template name of the template
  231. * @return boolean
  232. */
  233. function load( $template ) {
  234. $templateFileName = $this->getRoot().DS.$template.'.php';
  235. if (!is_file($templateFileName)) {
  236. $templateFileName = $this->getDefaultRoot().DS.$template.'.php';
  237. }
  238. if (is_file($templateFileName)) {
  239. ob_start();
  240. include_once( $templateFileName );
  241. ob_end_clean();
  242. $templateClass = 'jtt_' . $template;
  243. if (!class_exists($templateClass)) {
  244. $this->riseError( 'Template class not found in: ' . $template );
  245. return false;
  246. }
  247. ob_start();
  248. $tmpl = new $templateClass;
  249. $isValidTemplate = false;
  250. if ((version_compare( phpversion(), '4.2.0' ) < 0)) {
  251. // PHP < 4.2.0 (function is_a not exists)
  252. $isValidTemplate = is_subclass_of($tmpl, 'JoomlaTuneTemplate');
  253. } else {
  254. // PHP > 4.2.0 (using is_a)
  255. $isValidTemplate = is_a( $tmpl, 'JoomlaTuneTemplate' );
  256. }
  257. ob_end_clean();
  258. if (!$isValidTemplate) {
  259. unset( $tmpl );
  260. $this->riseError( 'Incorrect template: ' . $template );
  261. return false;
  262. }
  263. ob_start();
  264. $tmpl->setGlobalVars( $this->_globals );
  265. $this->_templates[$template] =& $tmpl;
  266. ob_end_clean();
  267. return true;
  268. }
  269. return false;
  270. }
  271. /**
  272. * Adds a global variable
  273. *
  274. * Global variables are valid in all templates of this object.
  275. *
  276. * @access public
  277. * @param string $name name of the global variable
  278. * @param mixed $value value of the variable
  279. * @return void
  280. * @see addVar()
  281. */
  282. function addGlobalVar( $name, $value )
  283. {
  284. $this->_globals[strtolower( $name )] = ( string )$value;
  285. }
  286. /**
  287. * Add a variable to a template
  288. *
  289. * @access public
  290. * @param string $template name of the template
  291. * @param string $name name of the variable
  292. * @param mixed $value value of the variable
  293. * @return void
  294. * @see addGlobalVar()
  295. */
  296. function addVar( $template, $name, $value )
  297. {
  298. $this->_templates[$template]->_vars[$name] = $value;
  299. }
  300. /**
  301. * Add a object variable to a template
  302. *
  303. * @access public
  304. * @param string $template name of the template
  305. * @param string $name name of the variable
  306. * @param mixed $value value of the variable
  307. * @return void
  308. * @see addVar(), addGlobalVar()
  309. */
  310. function addObject( $template, $name, $value )
  311. {
  312. $this->_templates[$template]->_vars[$name] = $value;
  313. }
  314. /**
  315. * Fetches and returns a given variable from template.
  316. *
  317. * @access public
  318. * @param string $template name of the template
  319. * @param string $name name of the variable
  320. * @return mixed
  321. */
  322. function getVar( $template, $name )
  323. {
  324. if (!$this->exists($template))
  325. {
  326. $this->riseError( 'Unknown template: ' . $template );
  327. return null;
  328. }
  329. ob_start();
  330. $result = $this->_templates[$template]->getVar($name);
  331. ob_end_clean();
  332. return $result;
  333. }
  334. /**
  335. * Renders template and return result as string
  336. *
  337. * @access public
  338. * @param string $template name of the template
  339. * @return string
  340. * @see displayTemplate()
  341. */
  342. function renderTemplate( $template )
  343. {
  344. if (!$this->exists($template)) {
  345. $this->riseError( 'Unknown template: ' . $template );
  346. return null;
  347. }
  348. ob_start();
  349. $this->_templates[$template]->render();
  350. $result = ob_get_contents();
  351. ob_end_clean();
  352. return $result;
  353. }
  354. /**
  355. * Renders template and displays output
  356. *
  357. * @access public
  358. * @param string $template name of the template
  359. * @return void
  360. * @see renderTemplate()
  361. */
  362. function displayTemplate( $template )
  363. {
  364. echo $this->renderTemplate($template);
  365. }
  366. /**
  367. * Frees a template
  368. *
  369. * All memory consumed by the template will be freed.
  370. *
  371. * @access public
  372. * @param string $template name of the template
  373. * @return void
  374. * @see freeAllTemplates()
  375. */
  376. function freeTemplate( $template )
  377. {
  378. unset($this->_templates[$template]);
  379. }
  380. /**
  381. * Frees all templates
  382. *
  383. * All memory consumed by the templates will be freed.
  384. *
  385. * @access public
  386. * @return void
  387. * @see freeTemplate()
  388. */
  389. function freeAllTemplates()
  390. {
  391. $this->_templates = array();
  392. $this->_globals = array();
  393. }
  394. /**
  395. * Checks if template exists
  396. *
  397. * @access public
  398. * @param string $name name of the template
  399. * @return boolean true, if template exists (loaded), false otherwise
  400. * @see load()
  401. */
  402. function exists( $name )
  403. {
  404. return isset($this->_templates[$name]);
  405. }
  406. /**
  407. * Displays error-message and die
  408. *
  409. * @access private
  410. * @param string $message error message
  411. * @param string $type type of error (die or warning)
  412. * @return void
  413. */
  414. function riseError( $message, $type = 'die' )
  415. {
  416. switch($type)
  417. {
  418. case 'warning':
  419. echo( 'JoomlaTuneTemplateWarning: ' . $message );
  420. break;
  421. case 'die':
  422. default:
  423. die( 'JoomlaTuneTemplateError: ' . $message );
  424. break;
  425. }
  426. }
  427. }
  428. ?>