PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/capsman/framework/classes/template.php

https://github.com/howardlei82/IGSM-Website
PHP | 380 lines | 150 code | 33 blank | 197 comment | 20 complexity | 5339cd1733eac0f9bec2b78bff6ef5bb MD5 | raw file
  1. <?php
  2. /**
  3. * Class to manage simple templates for HTML output.
  4. *
  5. * @version $Rev: 203758 $
  6. * @author Jordi Canals
  7. * @copyright Copyright (C) 2008, 2009, 2010 Jordi Canals
  8. * @license GNU General Public License version 2
  9. * @link http://alkivia.org
  10. * @package Alkivia
  11. * @subpackage Framework
  12. *
  13. Copyright 2008, 2009, 2010 Jordi Canals <devel@jcanals.cat>
  14. This program is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License
  16. version 2 as published by the Free Software Foundation.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * A very simple class for an easy tenplate management.
  26. * This is an abstract class that have to be extended for use.
  27. *
  28. * TODO: Provide some sort of caching template contents.
  29. *
  30. * @package Alkivia
  31. * @subpackage Framework
  32. */
  33. class akTemplate
  34. {
  35. /**
  36. * Templates folder (Slash ended).
  37. * @var string
  38. */
  39. protected $tpl_dir = array();
  40. /**
  41. * Config files folder (Slah ended).
  42. * @var string
  43. */
  44. private $cfg_dir = array();
  45. /**
  46. * Variables and values available to template.
  47. * - 'var_name' => 'value'
  48. * @var array
  49. */
  50. protected $vars = array();
  51. /**
  52. * Data readed from template config files.
  53. * @var array
  54. */
  55. private $config = array();
  56. /**
  57. * Holds notice messages to be shown in output.
  58. * On the template the method displayMessages() must be called.
  59. * @var array
  60. */
  61. private $notices = array();
  62. /**
  63. * Holds error messages to be shown in output.
  64. * On the template the method displayMessages() must be called.
  65. * @var array
  66. */
  67. private $errors = array();
  68. /**
  69. * Class constructor.
  70. *
  71. * @param array|string $tpl_dir Full paths to template folders.
  72. * @param string $cfg_dir Full path to config files.
  73. * @return TemplateAbstract The class object or false if $tpl_dir is not a directory.
  74. */
  75. public function __construct ( $tpl_dir, $cfg_dir = '' )
  76. {
  77. $this->tpl_dir = $this->checkDirectories($tpl_dir);
  78. if ( empty($this->tpl_dir) ) {
  79. wp_die(__('Template class: Received template paths are not valid directories.'));
  80. }
  81. if ( empty($cfg_dir) ) {
  82. $this->cfg_dir = $this->tpl_dir;
  83. } else {
  84. $this->cfg_dir = $this->checkDirectories($cfg_dir);
  85. if ( empty($this->tpl_dir) ) {
  86. wp_die(__('Template class: Received config paths are not valid directories.'));
  87. }
  88. }
  89. }
  90. /**
  91. * Checks an array of paths are valid directories.
  92. *
  93. * @since 0.8
  94. *
  95. * @param array|string $directories Absolute paths array
  96. * @return array An array with only valid directories, wrong directories are removed.
  97. */
  98. private function checkDirectories ( $directories )
  99. {
  100. $valid = array();
  101. foreach ( (array) $directories as $path ) {
  102. if ( is_dir($path) ) {
  103. $valid[] = trailingslashit($path);
  104. }
  105. }
  106. return $valid;
  107. }
  108. /**
  109. * Assigns the translation textDomain as an available variable name.
  110. * This will be available in template as $i18n.
  111. *
  112. * @param $context Translation context textDomain.
  113. * @return void
  114. */
  115. final public function textDomain ( $context )
  116. {
  117. $this->vars['i18n'] = $context;
  118. }
  119. /**
  120. * Assigns a variable name with it's value.
  121. * Reserved vars names: i18n, tpl, cfg.
  122. *
  123. * @param $name Variable name.
  124. * @param $value Value of the variable.
  125. * @return void
  126. */
  127. final public function assign ( $name, $value )
  128. {
  129. $this->checkReserved($name);
  130. $this->vars[$name] = $value;
  131. }
  132. /**
  133. * Assigns a variable name with it's value by reference.
  134. *
  135. * @param $name Variable name.
  136. * @param $value Value of the variable, received by reference.
  137. * @return void
  138. */
  139. public function assignByRef( $name, &$value )
  140. {
  141. $this->checkReserved($name);
  142. $this->vars[$name] =& $value;
  143. }
  144. /**
  145. * Checks if a template var name is reserved and dies if yes.
  146. *
  147. * @since 0.8
  148. *
  149. * @param string $name Variable name to check.
  150. * @return void
  151. */
  152. private function checkReserved ( $name )
  153. {
  154. if ( in_array($name, array('i18n', '_template', '_default', '_config', '_filename')) ) {
  155. wp_die( sprintf(__('Template class: %s is a reserved template variable.'), $name) );
  156. }
  157. }
  158. /**
  159. * Loads an INI file from config folder, and merges the content with previous read files.
  160. *
  161. * @param $file File name (With no extension)
  162. * @return void
  163. */
  164. final public function loadConfig ( $file )
  165. {
  166. foreach ( $this->cfg_dir as $path ) {
  167. $filename = $path . $name . '.ini';
  168. if ( file_exists($filename) ) {
  169. $config = parse_ini_file( $filename, true);
  170. $this->config = array_merge($this->config, $config);
  171. }
  172. }
  173. }
  174. /**
  175. * Sets config values to empty.
  176. * Can be used to start over loading new INI files.
  177. *
  178. * @return void
  179. */
  180. final public function resetConfig ()
  181. {
  182. $this->config = array();
  183. }
  184. /**
  185. * Sets template vars to empty.
  186. * Can be used to start over with a new template.
  187. *
  188. * @return void
  189. */
  190. final public function resetVars ()
  191. {
  192. $this->vars = array();
  193. }
  194. /**
  195. * Sets config and vars to empty.
  196. * Used to start a new clean template.
  197. *
  198. * @return void
  199. */
  200. final public function resetAll ()
  201. {
  202. $this->config = array();
  203. $this->vars = array();
  204. }
  205. /**
  206. * Adds an error to the erros queue.
  207. * Only adds it if error does not already exists on errors queue.
  208. *
  209. * @param string $message Message to be added to the queue.
  210. * @return void
  211. */
  212. final public function addError ( $message )
  213. {
  214. if ( ! empty($message) && ! in_array($message, $this->errors)) {
  215. $this->errors[] = $message;
  216. }
  217. }
  218. /**
  219. * Chechs if errors were found and have to be displayed.
  220. *
  221. * @return boolean Found errors.
  222. */
  223. final public function foundErrors()
  224. {
  225. return ! empty($this->errors);
  226. }
  227. /**
  228. * Adds a notice to the notices queue.
  229. * Only adds the notice if it does not alredy exists on notices queue.
  230. *
  231. * @param string $message Message to be added to the queue.
  232. * @return void
  233. */
  234. final public function addNotice ( $message )
  235. {
  236. if ( ! empty($message) && ! in_array($message, $this->notices)) {
  237. $this->notices[] = $message;
  238. }
  239. }
  240. /**
  241. * Empties all messages queues (notices and errors).
  242. *
  243. * @return void
  244. */
  245. final public function resetMessages ()
  246. {
  247. $this->errors = array();
  248. $this->notices = array();
  249. }
  250. /**
  251. * Displays notices and/or error messages and empties the messages queue.
  252. * This function should only be called within a template.
  253. *
  254. * @return void
  255. */
  256. final public function displayMessages ()
  257. {
  258. if ( ! empty($this->errors) ) {
  259. $errors = implode('<br />' . PHP_EOL, $this->errors);
  260. echo '<div id="message" class="error">' . $errors . '</div>' . PHP_EOL;
  261. }
  262. if ( ! empty($this->notices) ) {
  263. $notices = implode('<br />' . PHP_EOL, $this->notices);
  264. echo '<div id="message" class="notice">' . $notices . '</div>' . PHP_EOL;
  265. }
  266. $this->resetMessages();
  267. }
  268. /**
  269. * Checks if a template file is available.
  270. *
  271. * @since 0.8
  272. *
  273. * @param string $template Template name (With no .php extension)
  274. * @return boolean If template file was found or not.
  275. */
  276. final public function available( $template )
  277. {
  278. return ( false === $this->locateFile($template) ) ? false : true;
  279. }
  280. /**
  281. * Displays a template from the templates folder.
  282. * Inside the template all assigned vars will be available.
  283. * Also 'cfg' and 'tpl' vars will be available:
  284. * - cfg is an array which cointains all config values.
  285. * - tpl is an string cointaining template absolute name.
  286. *
  287. * TODO: Load config file with same name as template.
  288. *
  289. * @param string $_template Template name with no extension.
  290. * @param string $_default Alternate default template name.
  291. * @return void.
  292. */
  293. final public function display ( $_template, $_default = '' )
  294. {
  295. $_filename = $this->locateFile($_template);
  296. if ( false === $_filename && ! empty($_default) ) {
  297. $_filename = $this->locateFile($_default);
  298. }
  299. if ( $_filename ) {
  300. $_config =& $this->config;
  301. extract($this->vars);
  302. include ( $_filename );
  303. } else {
  304. wp_die(sprintf(__('Template file %1$s not found. Default template %2$s not found.'), $_template, $_default));
  305. }
  306. }
  307. /**
  308. * Returns the template contents after processing it.
  309. * Calls to TemplateAbstract::display() for template processing.
  310. *
  311. * @param string $template Template name with no extension.
  312. * @param string $default Alternate default template name.
  313. * @return string|false The template contents or false if failed processing.
  314. */
  315. final public function getDisplay ( $template, $default = '' )
  316. {
  317. if ( ob_start() ) {
  318. $this->display($template, $default);
  319. $content = ob_get_contents();
  320. ob_end_clean();
  321. return $content;
  322. } else {
  323. return false;
  324. }
  325. }
  326. /**
  327. * Locates the path for a template filename.
  328. * If template is not found, returns false.
  329. *
  330. * @param string $name Template name (with no .php extension)
  331. * @return string|false Absolute path to template file. False if not found.
  332. */
  333. private function locateFile( $name )
  334. {
  335. foreach ( $this->tpl_dir as $path ) {
  336. $template = $path . $name . '.php';
  337. if ( file_exists($template) ) {
  338. return $template;
  339. }
  340. }
  341. return false;
  342. }
  343. }