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

/src/legacy/Zikula/View.php

https://github.com/antoniom/core
PHP | 2903 lines | 1206 code | 341 blank | 1356 comment | 142 complexity | 7bddd94170671b1d2b12f6143cb32464 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula_View
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. use Zikula\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\EventDispatcher\EventDispatcher;
  16. use Zikula\Common\I18n\TranslatableInterface;
  17. use Zikula\Core\Event\GenericEvent;
  18. /**
  19. * Zikula_View class.
  20. */
  21. class Zikula_View extends Smarty implements TranslatableInterface
  22. {
  23. const CACHE_DISABLED = 0;
  24. const CACHE_ENABLED = 1;
  25. const CACHE_INDIVIDUAL = 2;
  26. /**
  27. * Translation domain of the calling module.
  28. *
  29. * @var string
  30. */
  31. public $domain;
  32. /**
  33. * Module info array, indexed by module name.
  34. *
  35. * @var array
  36. */
  37. public $module;
  38. /**
  39. * Module info.
  40. *
  41. * @var array
  42. */
  43. public $modinfo;
  44. /**
  45. * Top level module.
  46. *
  47. * @var string
  48. */
  49. public $toplevelmodule;
  50. /**
  51. * Type.
  52. *
  53. * @var integer
  54. */
  55. public $type;
  56. /**
  57. * Function.
  58. *
  59. * @var string
  60. */
  61. public $func;
  62. /**
  63. * Language.
  64. *
  65. * @var string
  66. */
  67. public $language;
  68. /**
  69. * Homepage flag.
  70. *
  71. * @var boolean
  72. */
  73. public $homepage;
  74. /**
  75. * Theme name.
  76. *
  77. * @var string
  78. */
  79. public $theme;
  80. /**
  81. * Theme info.
  82. *
  83. * @var array
  84. */
  85. public $themeinfo;
  86. /**
  87. * Base Url.
  88. *
  89. * @var string
  90. */
  91. public $baseurl;
  92. /**
  93. * Base Uri.
  94. *
  95. * @var string
  96. */
  97. public $baseuri;
  98. /**
  99. * The service manager instance.
  100. *
  101. * @var \Symfony\Component\DependencyInjection\ContainerBuilder
  102. */
  103. protected $container;
  104. /**
  105. * The event manager instance.
  106. *
  107. * @var EventDispatcher
  108. */
  109. protected $dispatcher;
  110. /**
  111. * Request object.
  112. *
  113. * @var \Symfony\Component\HttpFoundation\Request
  114. */
  115. protected $request;
  116. /**
  117. * Zikula controller.
  118. *
  119. * @var Zikula_AbstractController
  120. */
  121. protected $controller;
  122. /**
  123. * Cache Id.
  124. *
  125. * @var string
  126. */
  127. public $cache_id;
  128. /**
  129. * Whether or not to expose template.
  130. *
  131. * @var boolean
  132. */
  133. public $expose_template;
  134. /**
  135. * Template path (populated by fetch).
  136. *
  137. * @var string
  138. */
  139. protected $templatePath;
  140. /**
  141. * Templates.
  142. *
  143. * @var array
  144. */
  145. protected $templateCache = array();
  146. /**
  147. * Constructor.
  148. *
  149. * @param ContainerBuilder $container ServiceManager.
  150. * @param string $moduleName Module name ("zikula" for system plugins).
  151. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  152. */
  153. public function __construct(ContainerBuilder $container, $moduleName = '', $caching = null)
  154. {
  155. $this->container = $container;
  156. $this->dispatcher = $this->container->get('event_dispatcher');
  157. $this->request = $this->container->get('request');
  158. // set the error reporting level
  159. $this->error_reporting = isset($container['error_reporting']) ? $container['error_reporting'] : E_ALL;
  160. $this->allow_php_tag = true;
  161. // get variables from input
  162. $module = $this->request->attributes->get('_module', null);
  163. $type = $this->request->attributes->get('_controller', 'user');
  164. $func = $this->request->attributes->get('_action', 'index');
  165. // set vars based on the module structures
  166. $this->homepage = empty($module) ? true : false;
  167. $this->type = strtolower(!$this->homepage ? $type : System::getVar('starttype'));
  168. $this->func = strtolower(!$this->homepage ? $func : System::getVar('startfunc'));
  169. // Initialize the module property with the name of
  170. // the topmost module. For Hooks, Blocks, API Functions and others
  171. // you need to set this property to the name of the respective module!
  172. $this->toplevelmodule = ModUtil::getName();
  173. if (!$moduleName) {
  174. $moduleName = $this->toplevelmodule;
  175. }
  176. $this->modinfo = ModUtil::getInfoFromName($moduleName);
  177. $this->module = array($moduleName => $this->modinfo);
  178. // initialise environment vars
  179. $this->language = ZLanguage::getLanguageCode();
  180. $this->baseurl = System::getBaseUrl();
  181. $this->baseuri = System::getBaseUri();
  182. // system info
  183. $this->themeinfo = ThemeUtil::getInfo(ThemeUtil::getIDFromName(UserUtil::getTheme()));
  184. $this->theme = $this->themeinfo['directory'];
  185. //---- Plugins handling -----------------------------------------------
  186. // add plugin paths
  187. switch ($this->modinfo['type']) {
  188. case ModUtil::TYPE_MODULE :
  189. $mpluginPath = "modules/" . $this->modinfo['directory'] . "/Resources/views/plugins";
  190. break;
  191. case ModUtil::TYPE_SYSTEM :
  192. $mpluginPath = "system/" . $this->modinfo['directory'] . "/Rsources/views/plugins";
  193. break;
  194. default:
  195. $mpluginPath = "system/" . $this->modinfo['directory'] . "/Rsources/views/plugins";
  196. }
  197. // add standard plugin search path
  198. $this->plugins_dir = array();
  199. $this->addPluginDir('config/Resources/plugins'); // Official override
  200. $this->addPluginDir('config/plugins'); // Official override
  201. $this->addPluginDir(ZIKULA_ROOT.'/../src/legacy/viewplugins'); // Core plugins
  202. $this->addPluginDir("themes/$this->theme/Resources/views/plugins"); // Theme plugins
  203. $this->addPluginDir(SMARTY_DIR.'plugins'); // Smarty core plugins
  204. $this->addPluginDir($mpluginPath); // Plugins for current module
  205. // check if the 'type' parameter in the URL is admin and if yes,
  206. // include system/Admin/templates/plugins to the plugins_dir array
  207. if ($type === 'admin') {
  208. if (!$this instanceof Zikula_View_Theme) {
  209. $this->addPluginDir('system/AdminModule/Resources/views/plugins');
  210. } else {
  211. $this->load_filter('output', 'admintitle');
  212. }
  213. }
  214. //---- Cache handling -------------------------------------------------
  215. if ($caching && in_array((int)$caching, array(0, 1, 2))) {
  216. $this->caching = (int)$caching;
  217. } else {
  218. $this->caching = (int)ModUtil::getVar('Theme', 'render_cache');
  219. }
  220. $this->compile_id = '';
  221. $this->cache_id = '';
  222. // template compilation
  223. $this->compile_dir = CacheUtil::getLocalDir('view_compiled');
  224. $this->compile_check = ModUtil::getVar('Theme', 'render_compile_check');
  225. $this->force_compile = ModUtil::getVar('Theme', 'render_force_compile');
  226. // template caching
  227. $this->cache_dir = CacheUtil::getLocalDir('view_cache');
  228. $this->cache_lifetime = ModUtil::getVar('Theme', 'render_lifetime');
  229. $this->expose_template = (ModUtil::getVar('Theme', 'render_expose_template') == true) ? true : false;
  230. // register resource type 'z' this defines the way templates are searched
  231. // during {include file='my_template.tpl'} this enables us to store selected module
  232. // templates in the theme while others can be kept in the module itself.
  233. $this->register_resource('z', array('Zikula_View_Resource',
  234. 'z_get_template',
  235. 'z_get_timestamp',
  236. 'z_get_secure',
  237. 'z_get_trusted'));
  238. // set 'z' as default resource type
  239. $this->default_resource_type = 'z';
  240. // process some plugins specially when Render cache is enabled
  241. if (!$this instanceof Zikula_View_Theme && $this->caching) {
  242. $this->register_nocache_plugins();
  243. }
  244. // register the 'nocache' block to allow dynamic zones caching templates
  245. $this->register_block('nocache', array('Zikula_View_Resource', 'block_nocache'), false);
  246. // For ajax requests we use the short urls filter to 'fix' relative paths
  247. if (($this->container->get('zikula')->getStage() & \Zikula\Core\Core::STAGE_AJAX) && System::getVar('shorturls')) {
  248. $this->load_filter('output', 'shorturls');
  249. }
  250. // register prefilters
  251. $this->register_prefilter('z_prefilter_add_literal');
  252. $this->register_prefilter('z_prefilter_gettext_params');
  253. // assign some useful settings
  254. $this->assign('homepage', $this->homepage)
  255. ->assign('modinfo', $this->modinfo)
  256. ->assign('module', $moduleName)
  257. ->assign('toplevelmodule', $this->toplevelmodule)
  258. ->assign('type', $this->type)
  259. ->assign('func', $this->func)
  260. ->assign('lang', $this->language)
  261. ->assign('themeinfo', $this->themeinfo)
  262. ->assign('themepath', $this->baseurl . 'themes/' . $this->theme)
  263. ->assign('baseurl', $this->baseurl)
  264. ->assign('baseuri', $this->baseuri);
  265. // for {gt} template plugin to detect gettext domain
  266. if ($this->modinfo['type'] == ModUtil::TYPE_MODULE) {
  267. $this->domain = ZLanguage::getModuleDomain($this->modinfo['name']);
  268. }
  269. // make render object available to modifiers
  270. parent::assign('zikula_view', $this);
  271. // add ServiceManager, EventManager and others to all templates
  272. parent::assign('container', $this->container);
  273. parent::assign('dispatcher', $this->dispatcher);
  274. parent::assign('zikula_core', $this->container->get('zikula'));
  275. parent::assign('request', $this->request);
  276. parent::assign('modvars', ModUtil::getModvars()); // Get all modvars from any modules that have accessed their modvars at least once.
  277. $this->add_core_data();
  278. // metadata for SEO
  279. if (!isset($this->container['zikula_view.metatags'])) {
  280. $this->container['zikula_view.metatags'] = new ArrayObject(array());
  281. }
  282. parent::assign('metatags', $this->container['zikula_view.metatags']);
  283. $event = new GenericEvent($this);
  284. $this->dispatcher->dispatch('view.init', $event);
  285. }
  286. /**
  287. * Setup the current instance of the Zikula_View class and return it back to the module.
  288. *
  289. * @param string $module Module name.
  290. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  291. * @param string $cache_id Cache Id.
  292. *
  293. * @return Zikula_View This instance.
  294. */
  295. public static function getInstance($module = null, $caching = null, $cache_id = null)
  296. {
  297. if (is_null($module)) {
  298. $module = ModUtil::getName();
  299. }
  300. $module = preg_match('/\w+Module$/', $module) ? $module : $module.'Module';
  301. $container = ServiceUtil::getManager();
  302. $serviceId = strtolower(sprintf('zikula.view.%s', $module));
  303. if (!$container->has($serviceId)) {
  304. $view = new self($container, $module, $caching);
  305. $container->set($serviceId, $view);
  306. } else {
  307. $view = $container->get($serviceId);
  308. }
  309. if (!is_null($caching)) {
  310. $view->caching = (int)$caching;
  311. }
  312. if (!is_null($cache_id)) {
  313. $view->cache_id = $cache_id;
  314. }
  315. if (!$module) {
  316. $module = $view->toplevelmodule;
  317. }
  318. // if (!array_key_exists($module, $view->module)) {
  319. $view->module[$module] = ModUtil::getInfoFromName($module);
  320. $view->_add_plugins_dir($module);
  321. // }
  322. // for {gt} template plugin to detect gettext domain
  323. if ($view->module[$module]['type'] == ModUtil::TYPE_MODULE) {
  324. $view->domain = ZLanguage::getModuleDomain($view->module[$module]['name']);
  325. }
  326. return $view;
  327. }
  328. /**
  329. * Get module plugin Zikula_View_Plugin instance.
  330. *
  331. * @param string $modName Module name.
  332. * @param string $pluginName Plugin name.
  333. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  334. * @param string $cache_id Cache Id.
  335. *
  336. * @return Zikula_View_Plugin The plugin instance.
  337. */
  338. public static function getModulePluginInstance($modName, $pluginName, $caching = null, $cache_id = null)
  339. {
  340. return Zikula_View_Plugin::getInstance($modName, $pluginName, $caching, $cache_id);
  341. }
  342. /**
  343. * Get system plugin Zikula_View_Plugin instance.
  344. *
  345. * @param string $pluginName Plugin name.
  346. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  347. * @param string $cache_id Cache Id.
  348. *
  349. * @return Zikula_View_Plugin The plugin instance.
  350. */
  351. public static function getSystemPluginInstance($pluginName, $caching = null, $cache_id = null)
  352. {
  353. $modName = 'zikula';
  354. return Zikula_View_Plugin::getPluginInstance($modName, $pluginName, $caching, $cache_id);
  355. }
  356. /**
  357. * Internal registration of Zikula core's plugins sensible to cache.
  358. *
  359. * Basically the user-based ones and those that has relation with the theme/pagevars.
  360. *
  361. * @return void
  362. */
  363. protected function register_nocache_plugins()
  364. {
  365. // disables the cache for them and do not load them yet
  366. // that happens later when required
  367. $delayed_load = true;
  368. $cacheable = false;
  369. //// blocks
  370. // checkgroup
  371. Zikula_View_Resource::register($this, 'block', 'checkgroup', $delayed_load, $cacheable, array('gid'));
  372. // checkpermissionblock
  373. Zikula_View_Resource::register($this, 'block', 'checkpermissionblock', $delayed_load, $cacheable, array('component', 'instance'));
  374. // pageaddvarblock
  375. Zikula_View_Resource::register($this, 'block', 'pageaddvarblock', $delayed_load, $cacheable, array('name'));
  376. //// plugins
  377. // ajaxheader
  378. Zikula_View_Resource::register($this, 'function', 'ajaxheader', $delayed_load, $cacheable, array('modname', 'filename', 'noscriptaculous', 'validation', 'lightbox', 'imageviewer', 'assign'));
  379. // assign_cache
  380. Zikula_View_Resource::register($this, 'function', 'assign_cache', $delayed_load, $cacheable, array('var', 'value'));
  381. // checkpermission
  382. Zikula_View_Resource::register($this, 'function', 'checkpermission', $delayed_load, $cacheable, array('component', 'instance', 'level', 'assign'));
  383. // formutil_getfieldmarker
  384. Zikula_View_Resource::register($this, 'function', 'formutil_getfieldmarker', $delayed_load, $cacheable, array('objectType', 'validation', 'field', 'assign'));
  385. // formutil_getpassedvalue
  386. Zikula_View_Resource::register($this, 'function', 'formutil_getpassedvalue', $delayed_load, $cacheable, array('assign', 'html', 'key', 'name', 'default', 'source', 'noprocess'));
  387. // formutil_getvalidationerror
  388. Zikula_View_Resource::register($this, 'function', 'formutil_getvalidationerror', $delayed_load, $cacheable, array('objectType', 'field', 'assign'));
  389. // notifydisplayhooks
  390. Zikula_View_Resource::register($this, 'function', 'notifydisplayhooks', $delayed_load, $cacheable, array('eventname', 'id', 'urlobject', 'assign'));
  391. // notifyevent
  392. Zikula_View_Resource::register($this, 'function', 'notifyevent', $delayed_load, $cacheable, array('eventname', 'eventsubject', 'eventdata', 'assign'));
  393. // pageaddvar
  394. Zikula_View_Resource::register($this, 'function', 'pageaddvar', $delayed_load, $cacheable, array('name', 'value'));
  395. // pagegetvar
  396. Zikula_View_Resource::register($this, 'function', 'pagegetvar', $delayed_load, $cacheable, array('name', 'html', 'assign'));
  397. // pager
  398. Zikula_View_Resource::register($this, 'function', 'pager', $delayed_load, $cacheable, array('modname', 'type', 'func', 'rowcount', 'limit', 'posvar', 'owner', 'template', 'includeStylesheet', 'anchorText', 'maxpages', 'display', 'class', 'processDetailLinks', 'processUrls', 'optimize'));
  399. // pageregistervar
  400. Zikula_View_Resource::register($this, 'function', 'pageregistervar', $delayed_load, $cacheable, array('name'));
  401. // pagesetvar
  402. Zikula_View_Resource::register($this, 'function', 'pagesetvar', $delayed_load, $cacheable, array('name', 'value'));
  403. // servergetvar
  404. Zikula_View_Resource::register($this, 'function', 'servergetvar', $delayed_load, $cacheable, array('name', 'default', 'assign'));
  405. // sessiondelvar
  406. Zikula_View_Resource::register($this, 'function', 'sessiondelvar', $delayed_load, $cacheable, array('name', 'path', 'assign'));
  407. // sessiongetvar
  408. Zikula_View_Resource::register($this, 'function', 'sessiongetvar', $delayed_load, $cacheable, array('name', 'assign', 'default', 'path'));
  409. // sessionsetvar
  410. Zikula_View_Resource::register($this, 'function', 'sessionsetvar', $delayed_load, $cacheable, array('name', 'value', 'path', 'assign'));
  411. // setmetatag
  412. Zikula_View_Resource::register($this, 'function', 'setmetatag', $delayed_load, $cacheable, array('name', 'value'));
  413. // themegetvar
  414. Zikula_View_Resource::register($this, 'function', 'themegetvar', $delayed_load, $cacheable, array('name', 'default', 'assign'));
  415. // themesetvar
  416. Zikula_View_Resource::register($this, 'function', 'themesetvar', $delayed_load, $cacheable, array('name', 'value'));
  417. // user
  418. Zikula_View_Resource::register($this, 'function', 'user', $delayed_load, $cacheable);
  419. // useravatar - without uid caching
  420. Zikula_View_Resource::register($this, 'function', 'useravatar', $delayed_load, $cacheable);
  421. // usergetvar
  422. Zikula_View_Resource::register($this, 'function', 'usergetvar', $delayed_load, $cacheable, array('assign', 'default', 'name', 'uid'));
  423. // userlinks
  424. Zikula_View_Resource::register($this, 'function', 'userlinks', $delayed_load, $cacheable, array('start', 'end', 'seperator'));
  425. // userloggedin
  426. Zikula_View_Resource::register($this, 'function', 'userloggedin', $delayed_load, $cacheable, array('assign'));
  427. // userwelcome
  428. Zikula_View_Resource::register($this, 'function', 'userwelcome', $delayed_load, $cacheable);
  429. // zdebug
  430. Zikula_View_Resource::register($this, 'function', 'zdebug', $delayed_load, $cacheable);
  431. }
  432. /**
  433. * Checks whether requested template exists.
  434. *
  435. * @param string $template Template name.
  436. *
  437. * @return boolean
  438. */
  439. public function template_exists($template)
  440. {
  441. return (bool)$this->get_template_path($template);
  442. }
  443. /**
  444. * Checks which path to use for required template.
  445. *
  446. * @param string $template Template name.
  447. *
  448. * @return string Template path.
  449. */
  450. public function get_template_path($template)
  451. {
  452. if (isset($this->templateCache[$template])) {
  453. return $this->templateCache[$template];
  454. }
  455. // the current module
  456. $modname = ModUtil::getName();
  457. foreach ($this->module as $module => $modinfo) {
  458. // prepare the values for OS
  459. $module = $modinfo['name'];
  460. $os_module = DataUtil::formatForOS($module);
  461. $os_dir = $modinfo['type'] == ModUtil::TYPE_MODULE ? 'modules' : 'system';
  462. $ostemplate = DataUtil::formatForOS($template);
  463. if (is_dir("$os_dir/$os_module/Resources/views")) {
  464. $relativepath = "$os_dir/$os_module/Resources/views";
  465. }
  466. $templatefile = "$relativepath/$ostemplate";
  467. $override = self::getTemplateOverride($templatefile);
  468. if ($override === false) {
  469. // no override present
  470. if (is_readable($templatefile)) {
  471. $this->templateCache[$template] = $relativepath;
  472. return $relativepath;
  473. } else {
  474. return false;
  475. }
  476. } else {
  477. if (is_readable($override)) {
  478. $path = substr($override, 0, strrpos($override, $ostemplate));
  479. $this->templateCache[$template] = $path;
  480. return $path;
  481. }
  482. }
  483. }
  484. // when we arrive here, no path was found
  485. return false;
  486. }
  487. /**
  488. * Add core data to the template.
  489. *
  490. * This function adds some basic data to the template depending on the
  491. * current user and the Zikula settings. There is no need to call this as it's
  492. * invoked automatically on instanciation.
  493. *
  494. * @return Zikula_View
  495. */
  496. public function add_core_data()
  497. {
  498. if (!isset($this->container['zikula_view.coredata'])) {
  499. $this->container['zikula_view.coredata'] = new ArrayObject(array());
  500. }
  501. $core = $this->container['zikula_view.coredata'];
  502. $core['version_num'] = \Zikula\Core\Core::VERSION_NUM;
  503. $core['version_id'] = \Zikula\Core\Core::VERSION_ID;
  504. $core['version_sub'] = \Zikula\Core\Core::VERSION_SUB;
  505. $core['logged_in'] = UserUtil::isLoggedIn();
  506. $core['language'] = $this->language;
  507. // add userdata
  508. $core['user'] = UserUtil::getVars($this->request->getSession()->get('uid'));
  509. // Module vars
  510. parent::assign('coredata', $core);
  511. return $this;
  512. }
  513. /**
  514. * Executes & returns the template results.
  515. *
  516. * This returns the template output instead of displaying it.
  517. * Supply a valid template name.
  518. * As an optional second parameter, you can pass a cache id.
  519. * As an optional third parameter, you can pass a compile id.
  520. *
  521. * @param string $template The name of the template.
  522. * @param string $cache_id The cache ID (optional).
  523. * @param string $compile_id The compile ID (optional).
  524. * @param boolean $display Whether or not to display directly (optional).
  525. * @param boolean $reset Reset singleton defaults (optional). deprecated.
  526. *
  527. * @return string The template output.
  528. */
  529. public function fetch($template, $cache_id = null, $compile_id = null, $display = false, $reset = true)
  530. {
  531. $this->_setup_template($template);
  532. if (is_null($cache_id)) {
  533. $cache_id = $this->cache_id;
  534. }
  535. if (is_null($compile_id)) {
  536. $compile_id = $this->compile_id;
  537. }
  538. $this->template = $this->template_dir . '/' . $template;
  539. $output = $this->_fetch($template, $cache_id, $compile_id, $display);
  540. if ($this->expose_template == true) {
  541. $template = DataUtil::formatForDisplay($template);
  542. $output = "\n<!-- Start " . $this->template_dir . "/$template -->\n" . $output . "\n<!-- End " . $this->template_dir . "/$template -->\n";
  543. }
  544. $event = new GenericEvent($this, array('template' => $template), $output);
  545. try {
  546. $this->dispatcher->dispatch('view.postfetch', $event);
  547. $data = $event->getData();
  548. } catch (Exception $e) {
  549. var_dump($e->getMessage());
  550. }
  551. return $data;
  552. }
  553. /**
  554. * Executes & displays the template results.
  555. *
  556. * This displays the template.
  557. * Supply a valid template name.
  558. * As an optional second parameter, you can pass a cache id.
  559. * As an optional third parameter, you can pass a compile id.
  560. *
  561. * @param string $template The name of the template.
  562. * @param string $cache_id The cache ID (optional).
  563. * @param string $compile_id The compile ID (optional).
  564. *
  565. * @return boolean
  566. */
  567. public function display($template, $cache_id = null, $compile_id = null)
  568. {
  569. echo $this->fetch($template, $cache_id, $compile_id);
  570. return true;
  571. }
  572. /**
  573. * Returns an auto_id for auto-file-functions.
  574. *
  575. * @param string $cache_id The cache ID (optional).
  576. * @param string $compile_id The compile ID (optional).
  577. *
  578. * @return string|null The auto_id, or null if neither $cache_id nor $compile_id are set.
  579. */
  580. public function _get_auto_id($cache_id=null, $compile_id=null)
  581. {
  582. if (!empty($cache_id)) {
  583. $this->_filter_auto_id($cache_id);
  584. }
  585. if (!empty($compile_id)) {
  586. $this->_filter_auto_id($compile_id);
  587. }
  588. $auto_id = $cache_id . (!empty($compile_id) ? '/'.$compile_id : '');
  589. $auto_id = trim($auto_id, '/');
  590. return $auto_id;
  591. }
  592. /**
  593. * utility method to filter the IDs of not desired chars.
  594. *
  595. * @param string &$id Cache or compile ID to filter.
  596. *
  597. * @return void
  598. */
  599. protected function _filter_auto_id(&$id)
  600. {
  601. // convert some chars used as separators
  602. $id = str_replace(array(':', '=', ','), '_', $id);
  603. // convert the "Smarty cache groups" | to paths
  604. $id = str_replace('|', '/', $id);
  605. // and remove anything outside the acceptable range
  606. $id = preg_replace('#[^a-zA-Z0-9-_/]+#', '', $id);
  607. }
  608. /**
  609. * Get a concrete filename for automagically created content.
  610. *
  611. * @param string $path The base path.
  612. * @param string $auto_source The file name (optional).
  613. * @param string $auto_id The ID (optional).
  614. *
  615. * @return string The concrete path and file name to the content.
  616. */
  617. public function _get_auto_filename($path, $auto_source = null, $auto_id = null, $themedir = null)
  618. {
  619. // enables a flags to detect when is treating compiled templates
  620. $tocompile = ($path == $this->compile_dir) ? true : false;
  621. // format auto_source for os to make sure that id does not contain 'ugly' characters
  622. $auto_source = DataUtil::formatForOS($auto_source);
  623. // build a hierarchical directory path
  624. $path .= '/' . $this->modinfo['directory'];
  625. if ($this instanceof Zikula_View_Plugin) {
  626. $path .= '_' . $this->pluginName;
  627. }
  628. // add the cache_id path if set
  629. $path .= !empty($auto_id) ? '/' . $auto_id : '';
  630. // takes in account the source subdirectory
  631. $path .= strpos($auto_source, '/') !== false ? '/' . dirname($auto_source) : '';
  632. // make sure the path exists to write the compiled/cached template there
  633. if (!file_exists($path)) {
  634. mkdir($path, $this->container['system.chmod_dir'], true);
  635. }
  636. // if there's a explicit source, it
  637. if ($auto_source) {
  638. $path .= '/';
  639. $extension = FileUtil::getExtension($auto_source);
  640. // isolates the filename on the source path passed
  641. $path .= FileUtil::getFilebase($auto_source);
  642. // add theme and language to our path
  643. if (empty($themedir)) $themedir = $this->themeinfo['directory'];
  644. $path .= '--t_'.$themedir.'-l_' . $this->language;
  645. // if we are not compiling, end with a suffix
  646. if (!$tocompile) {
  647. $path .= ($extension ? ".$extension" : '');
  648. }
  649. }
  650. return $path;
  651. }
  652. /**
  653. * Finds out if a template is already cached.
  654. *
  655. * This returns true if there is a valid cache for this template.
  656. *
  657. * @param string $template The name of the template.
  658. * @param string $cache_id The cache ID (optional).
  659. * @param string $compile_id The compile ID (optional).
  660. *
  661. * @return boolean
  662. */
  663. public function is_cached($template, $cache_id = null, $compile_id = null)
  664. {
  665. if (is_null($cache_id)) {
  666. $cache_id = $this->cache_id;
  667. }
  668. if (is_null($compile_id)) {
  669. $compile_id = $this->compile_id;
  670. }
  671. return parent::is_cached($template, $cache_id, $compile_id);
  672. }
  673. /**
  674. * Internal function to delete cache of templates.
  675. *
  676. * @param string $tplpath Relative template path.
  677. * @param string $template Template partial filename.
  678. * @param integer $expire Expire limit of the cached templates.
  679. *
  680. * @return boolean True on success, false otherwise
  681. */
  682. protected function rmtpl($tplpath, $template, $expire = null)
  683. {
  684. if (!$template || !is_dir($tplpath) || !is_readable($tplpath)) {
  685. return false;
  686. }
  687. $filebase = FileUtil::getFilebase($template);
  688. $dh = opendir($tplpath);
  689. while (($entry = readdir($dh)) !== false) {
  690. if ($entry != '.' && $entry != '..') {
  691. $path = $tplpath . DIRECTORY_SEPARATOR . $entry;
  692. if (is_dir($path)) {
  693. // search recusively
  694. $this->rmtpl($path, $template, $expire);
  695. } elseif (strpos($entry, $filebase) === 0) {
  696. // delete the files that matches the template base filename
  697. $this->_unlink($path, $expire);
  698. }
  699. }
  700. }
  701. closedir($dh);
  702. return true;
  703. }
  704. /**
  705. * Internal function to delete cache directories and files.
  706. *
  707. * @param string $dirname Relative cache directory path.
  708. * @param integer $expire Expire limit of the cached templates.
  709. * @param boolean $rmbase Remove the passed directory too (default: true).
  710. *
  711. * @return boolean True on success, false otherwise.
  712. */
  713. protected function rmdir($dirname, $expire = null, $rmbase = true)
  714. {
  715. if (!is_dir($dirname) || !is_readable($dirname)) {
  716. return false;
  717. }
  718. $dh = opendir($dirname);
  719. while (($entry = readdir($dh)) !== false) {
  720. if ($entry != '.' && $entry != '..' && $entry != 'index.html') {
  721. $path = $dirname . DIRECTORY_SEPARATOR . $entry;
  722. if (is_dir($path)) {
  723. // remove recursively
  724. $this->rmdir($path, $expire, true);
  725. } elseif ($expire !== false) {
  726. // check expiration time of cached templates
  727. $this->_unlink($path, $expire);
  728. } else {
  729. // delete compiled templates directly
  730. unlink($path);
  731. }
  732. }
  733. }
  734. closedir($dh);
  735. if ($rmbase) {
  736. return rmdir($dirname);
  737. }
  738. return true;
  739. }
  740. /**
  741. * Clears a temporary folder for a auto_id and/or template.
  742. *
  743. * This returns true if the operation was successful.
  744. *
  745. * @param string $tmpdir Name of the temporary folder to clear.
  746. * @param string $auto_id The cache and compile ID.
  747. * @param string $template The name of the template.
  748. * @param string $expire Minimum age in sec. the cache file must be before it will get cleared (optional).
  749. *
  750. * @return boolean
  751. */
  752. protected function clear_folder($tmpdir, $auto_id = null, $template = null, $expire = null, $themedir = null)
  753. {
  754. if (!$auto_id && !$template) {
  755. $result = $this->rmdir($tmpdir, $expire, false);
  756. } else {
  757. $autofolder = $this->_get_auto_filename($tmpdir, null, $auto_id, $themedir);
  758. if ($template) {
  759. $result = $this->rmtpl($autofolder, $template, $expire);
  760. } else {
  761. $result = $this->rmdir($autofolder, $expire);
  762. }
  763. }
  764. return $result;
  765. }
  766. /**
  767. * Clears the cache for a specific template or cache_id.
  768. *
  769. * @param string $template The name of the template.
  770. * @param string $cache_id The cache ID (optional).
  771. * @param string $compile_id The compile ID (optional).
  772. * @param string $expire Minimum age in sec. the cache file must be before it will get cleared (optional).
  773. *
  774. * @return boolean True on success, false otherwise.
  775. */
  776. public function clear_cache($template = null, $cache_id = null, $compile_id = null, $expire = null, $themedir = null)
  777. {
  778. if (is_null($compile_id) && $template) {
  779. $compile_id = $this->compile_id;
  780. }
  781. $auto_id = $this->_get_auto_id($cache_id, $compile_id);
  782. return $this->clear_folder($this->cache_dir, $auto_id, $template, $expire, $themedir);
  783. }
  784. /**
  785. * Clears all view cache for a module.
  786. *
  787. * @return boolean True on success, false otherwise.
  788. */
  789. public function clear_cache_module($moduledir = null)
  790. {
  791. if (is_null($moduledir)) {
  792. $moduledir = $this->modinfo['directory'];
  793. }
  794. return $this->clear_folder($this->cache_dir .'/'. $moduledir);
  795. }
  796. /**
  797. * Clear all compiled templates.
  798. *
  799. * Needs to clear the cache too as non cached plugins information will need regeneration too.
  800. *
  801. * @return boolean True if success, false otherwise.
  802. */
  803. public function clear_compiled()
  804. {
  805. if ($this->clear_folder($this->compile_dir, null, null, false)) {
  806. return $this->clear_all_cache();
  807. }
  808. return false;
  809. }
  810. /**
  811. * Clear all cached templates.
  812. *
  813. * @param string $expire Expire time.
  814. *
  815. * @return boolean Results of clear_cache with null parameters.
  816. */
  817. public function clear_all_cache($expire = null)
  818. {
  819. return $this->clear_cache(null, null, null, $expire);
  820. }
  821. /**
  822. * Set up paths for the template.
  823. *
  824. * This function sets the template and the config path according
  825. * to where the template is found (Theme or Module directory)
  826. *
  827. * @param string $template The template name.
  828. *
  829. * @return void
  830. */
  831. public function _setup_template($template)
  832. {
  833. // default directory for templates
  834. $this->template_dir = $this->get_template_path($template);
  835. $this->templatePath = $this->template_dir . '/' . $template;
  836. $this->config_dir = $this->template_dir . '/config';
  837. }
  838. /**
  839. * Add a plugin dir to the search path.
  840. *
  841. * Avoids adding duplicates.
  842. *
  843. * @param string $dir The directory to add.
  844. * @param boolean $push Whether to push the new dir to the bottom of the stack (default: true).
  845. *
  846. * @return Zikula_View This instance.
  847. */
  848. public function addPluginDir($dir, $push=true)
  849. {
  850. if (in_array($dir, $this->plugins_dir) || !@is_dir($dir)) {
  851. return $this;
  852. }
  853. if ($push) {
  854. array_push($this->plugins_dir, $dir);
  855. } else {
  856. $this->plugins_dir = array_merge(array($dir), $this->plugins_dir);
  857. }
  858. return $this;
  859. }
  860. /**
  861. * add a plugins dir to _plugin_dir array
  862. *
  863. * This function takes module name and adds two path two the plugins_dir array
  864. * when existing
  865. *
  866. * @param string $module Well known module name.
  867. *
  868. * @return void
  869. */
  870. protected function _add_plugins_dir($module)
  871. {
  872. if (empty($module)) {
  873. return;
  874. }
  875. $modinfo = ModUtil::getInfoFromName($module);
  876. if (!$modinfo) {
  877. return;
  878. }
  879. $modpath = ($modinfo['type'] == ModUtil::TYPE_SYSTEM) ? 'system' : 'modules';
  880. if (is_dir("$modpath/$modinfo[directory]/Resources/views/plugins")) {
  881. $this->addPluginDir("$modpath/$modinfo[directory]/Resources/views/plugins");
  882. }
  883. }
  884. /**
  885. * Execute a template override event.
  886. *
  887. * @param string $template Path to template.
  888. *
  889. * @throws InvalidArgumentException If event handler returns a non-existent template.
  890. *
  891. * @return mixed String if found, false if no override present.
  892. */
  893. public static function getTemplateOverride($template)
  894. {
  895. $event = new GenericEvent(null, array(), $template);
  896. EventUtil::getManager()->dispatch('zikula_view.template_override', $event);
  897. if ($event->isPropagationStopped()) {
  898. $ostemplate = DataUtil::formatForOS($event->getData());
  899. if (is_readable($ostemplate)) {
  900. return $ostemplate;
  901. } else {
  902. throw new InvalidArgumentException(__f('zikula_view.template_override returned a non-existent template path %s', $ostemplate));
  903. }
  904. }
  905. return false;
  906. }
  907. /**
  908. * Assign variable to template.
  909. *
  910. * @param string $key Variable name.
  911. * @param mixed $value Value.
  912. *
  913. * @return Zikula_View
  914. */
  915. public function assign($key, $value = null)
  916. {
  917. $this->_assign_check($key);
  918. parent::assign($key, $value);
  919. return $this;
  920. }
  921. /**
  922. * Assign variable to template by reference.
  923. *
  924. * @param string $key Variable name.
  925. * @param mixed &$value Value.
  926. *
  927. * @return Zikula_View
  928. */
  929. public function assign_by_ref($key, &$value)
  930. {
  931. $this->_assign_check($key);
  932. parent::assign_by_ref($key, $value);
  933. return $this;
  934. }
  935. /**
  936. * Prevent certain variables from being overwritten.
  937. *
  938. * @param string $key The protected variable key.
  939. *
  940. * @return void
  941. */
  942. protected function _assign_check($key)
  943. {
  944. if (is_array($key)) {
  945. foreach ($key as $v) {
  946. self::_assign_check($v);
  947. }
  948. return;
  949. }
  950. if (is_string($key)) {
  951. switch (strtolower($key)) {
  952. case 'zikula_view':
  953. case 'zikula_core':
  954. case 'modvars':
  955. case 'metatags':
  956. case 'coredata':
  957. case 'servicemanager':
  958. case 'eventmanager':
  959. $this->trigger_error(__f('%s is a protected template variable and may not be assigned', $key));
  960. break;
  961. }
  962. }
  963. }
  964. /**
  965. * Translate.
  966. *
  967. * @param string $msgid String to be translated.
  968. *
  969. * @return string The $msgid translated by gettext.
  970. */
  971. public function __($msgid)
  972. {
  973. return __($msgid, $this->domain);
  974. }
  975. /**
  976. * Translate with sprintf().
  977. *
  978. * @param string $msgid String to be translated.
  979. * @param string|array $params Args for sprintf().
  980. *
  981. * @return string The $msgid translated by gettext.
  982. */
  983. public function __f($msgid, $params)
  984. {
  985. return __f($msgid, $params, $this->domain);
  986. }
  987. /**
  988. * Translate plural string.
  989. *
  990. * @param string $singular Singular instance.
  991. * @param string $plural Plural instance.
  992. * @param string $count Object count.
  993. *
  994. * @return string Translated string.
  995. */
  996. public function _n($singular, $plural, $count)
  997. {
  998. return _n($singular, $plural, $count, $this->domain);
  999. }
  1000. /**
  1001. * Translate plural string with sprintf().
  1002. *
  1003. * @param string $sin Singular instance.
  1004. * @param string $plu Plural instance.
  1005. * @param string $n Object count.
  1006. * @param string|array $params Sprintf() arguments.
  1007. *
  1008. * @return string The $sin or $plu translated by gettext, based on $n.
  1009. */
  1010. public function _fn($sin, $plu, $n, $params)
  1011. {
  1012. return _fn($sin, $plu, $n, $params, $this->domain);
  1013. }
  1014. /**
  1015. * Retrieves the gettext domain for the module, as {@link ZLanguage::getModuleDomain()}.
  1016. *
  1017. * If the module is a system module this is not set.
  1018. *
  1019. * @return string The gettext domain for the module, or null for system modules.
  1020. */
  1021. public function getDomain()
  1022. {
  1023. return $this->domain;
  1024. }
  1025. /**
  1026. * Retrieve an array of module information, indexed by module name.
  1027. *
  1028. * @return array An array containing the module info, indexed by module name.
  1029. */
  1030. public function getModule()
  1031. {
  1032. return $this->module;
  1033. }
  1034. /**
  1035. * Retrieve the module info array for the top-level module (or the module specified in the constructor).
  1036. *
  1037. * @return array The module info array.
  1038. */
  1039. public function getModInfo()
  1040. {
  1041. return $this->modinfo;
  1042. }
  1043. /**
  1044. * Retrieve the name of the top-level module.
  1045. *
  1046. * @return string The name of the top-level module.
  1047. */
  1048. public function getTopLevelModule()
  1049. {
  1050. return $this->toplevelmodule;
  1051. }
  1052. /**
  1053. * Retrieve module name.
  1054. *
  1055. * @return string Module name.
  1056. */
  1057. public function getModuleName()
  1058. {
  1059. return $this->toplevelmodule;
  1060. }
  1061. /**
  1062. * Retrive the name of the controller type.
  1063. *
  1064. * @return string The name of the controller type.
  1065. */
  1066. public function getType()
  1067. {
  1068. return $this->type;
  1069. }
  1070. /**
  1071. * Retrive the name of the controller function.
  1072. *
  1073. * @return string The name of the controller function.
  1074. */
  1075. public function getFunc()
  1076. {
  1077. return $this->func;
  1078. }
  1079. /**
  1080. * Retrieve the current language code.
  1081. *
  1082. * @return string The current language code.
  1083. */
  1084. public function getLanguage()
  1085. {
  1086. return $this->language;
  1087. }
  1088. /**
  1089. * Retrieve the name of the current theme.
  1090. *
  1091. * @return string The name of the current theme.
  1092. */
  1093. public function getTheme()
  1094. {
  1095. return $this->theme;
  1096. }
  1097. /**
  1098. * Retrieve the theme info array for the current theme.
  1099. *
  1100. * @param string $key Field to retrieve of the theme info.
  1101. *
  1102. * @return array The theme info array.
  1103. */
  1104. public function getThemeInfo($key=null)
  1105. {
  1106. if ($key && array_key_exists($key, $this->themeinfo)) {
  1107. return $this->themeinfo[$key];
  1108. }
  1109. return $this->themeinfo;
  1110. }
  1111. /**
  1112. * Set a value or all the theme info array.
  1113. *
  1114. * @param mixed $value Value to assign.
  1115. * @param string $key Field to set on the theme info.
  1116. *
  1117. * @return void
  1118. */
  1119. public function setThemeInfo($value, $key=null)
  1120. {
  1121. if ($key) {
  1122. $this->themeinfo[$key] = $value;
  1123. }
  1124. $this->themeinfo = $value;
  1125. }
  1126. /**
  1127. * Retrieve the site's base URL.
  1128. *
  1129. * The value returned is the same as {@link System::getBaseUrl()}.
  1130. *
  1131. * @return string The base URL.
  1132. */
  1133. public function getBaseUrl()
  1134. {
  1135. return $this->baseurl;
  1136. }
  1137. /**
  1138. * Retrieve the site's base URI.
  1139. *
  1140. * The value returned is the same as {@link System::getBaseUri()}.
  1141. *
  1142. * @return string The base URI.
  1143. */
  1144. public function getBaseUri()
  1145. {
  1146. return $this->baseuri;
  1147. }
  1148. /**
  1149. * Gets DependencyInjection container.
  1150. *
  1151. * @return ContainerBuilder The service manager.
  1152. */
  1153. public function getContainer()
  1154. {
  1155. return $this->container;
  1156. }
  1157. /**
  1158. * Get EventDispatcher.
  1159. *
  1160. * @return EventDispatcher The event manager.
  1161. */
  1162. public function getDispatcher()
  1163. {
  1164. return $this->dispatcher;
  1165. }
  1166. /**
  1167. * Get the request.
  1168. *
  1169. * @return \Symfony\Component\HttpFoundation\Request
  1170. */
  1171. public function getRequest()
  1172. {
  1173. return $this->request;
  1174. }
  1175. /**
  1176. * Get the Zikula controller.
  1177. *
  1178. * @return \Zikula\Framework\Controller\AbstractController
  1179. */
  1180. public function getController()
  1181. {
  1182. return $this->controller;
  1183. }
  1184. /**
  1185. * Set the controller property.
  1186. *
  1187. * @param \Zikula\Framework\Controller\AbstractController $controller Controller to set.
  1188. *
  1189. * @return void
  1190. */
  1191. public function setController(\Zikula\Framework\Controller\AbstractController $controller)
  1192. {
  1193. $this->controller = $controller;
  1194. }
  1195. /**
  1196. * Retrieve the current setting for the 'render_expose_template' Theme module variable.
  1197. *
  1198. * @return boolean True if The 'render_expose_template' Theme module template is true.
  1199. */
  1200. public function getExposeTemplate()
  1201. {
  1202. return $this->expose_template;
  1203. }
  1204. /**
  1205. * Get template path.
  1206. *
  1207. * This is calculated by _setup_template() invoked during fetch().
  1208. *
  1209. * @return string
  1210. */
  1211. public function getTemplatePath()
  1212. {
  1213. return $this->templatePath;
  1214. }
  1215. /**
  1216. * Retrieve the name of the directory where templates are located.
  1217. *
  1218. * @return string The directory name.
  1219. */
  1220. public function getTemplateDir()
  1221. {
  1222. return $this->template_dir;
  1223. }
  1224. /**
  1225. * Retrieve the template variables array ({@link Smarty::$_tpl_vars}).
  1226. *
  1227. * @return array The template variables array.
  1228. */
  1229. public function getTplVars()
  1230. {
  1231. return $this->_tpl_vars;
  1232. }
  1233. /**
  1234. * Get a template variable.
  1235. *
  1236. * @param string $key Key of assigned template variable.
  1237. *
  1238. * @return mixed
  1239. */
  1240. public function getTplVar($key)
  1241. {
  1242. if (!array_key_exists($key, $this->_tpl_vars)) {
  1243. return null;
  1244. }
  1245. return $this->_tpl_vars[$key];
  1246. }
  1247. /**
  1248. * Retrieve the compile ID used to compile different sets of compiled files for the same templates.
  1249. *
  1250. * @return string|null The compile id, or null if none.
  1251. */
  1252. public function getCompileId()
  1253. {
  1254. return $this->compile_id;
  1255. }
  1256. /**
  1257. * Set this if you want different sets of compiled files for the same templates.
  1258. *
  1259. * This is useful for things like different languages.
  1260. * Instead of creating separate sets of templates per language, you
  1261. * set different compile_ids like 'en' and 'de'.
  1262. *
  1263. * @param string|null $compile_id The compile id, or null.
  1264. *
  1265. * @return $this
  1266. */
  1267. public function setCompileId($compile_id)
  1268. {
  1269. $this->compile_id = $compile_id;
  1270. return $this;
  1271. }
  1272. /**
  1273. * Retrieve the directory where compiled templates are located.
  1274. *
  1275. * @return string The directory name.
  1276. */
  1277. public function getCompileDir()
  1278. {
  1279. return $this->compile_dir;
  1280. }
  1281. /**
  1282. * Set the directory where compiled templates are located.
  1283. *
  1284. * @param string $compile_dir The directory name.
  1285. *
  1286. * @return $this
  1287. */
  1288. public function setCompileDir($compile_dir)
  1289. {
  1290. $this->compile_dir = $compile_dir;
  1291. return $this;
  1292. }
  1293. /**
  1294. * Retrieve the flag that controls whether to check for recompiling or not.
  1295. *
  1296. * Recompiling does not need to happen unless a template or config file is changed.
  1297. * Typically you enable this during development, and disable for production.
  1298. *
  1299. * @return boolean True if checked, otherwise false.
  1300. */
  1301. public function getCompileCheck()
  1302. {
  1303. return $this->compile_check;
  1304. }
  1305. /**
  1306. * Set compile check.
  1307. *
  1308. * @param boolean $doCompileCheck If true, checks for compile will be performed.
  1309. *
  1310. * @return $this
  1311. */
  1312. public function setCompileCheck($doCompileCheck)
  1313. {
  1314. $this->compile_check = $doCompileCheck;
  1315. return $this;
  1316. }
  1317. /**
  1318. * Retrieve whether templates are forced to be compiled.
  1319. *
  1320. * @return boolean True if templates are forced to be compiled, otherwise false.
  1321. */
  1322. public function getForceCompile()
  1323. {
  1324. return $this->force_compile;
  1325. }
  1326. /**
  1327. * Set whether templates are forced to be compiled.
  1328. *
  1329. * @param boolean $force_compile True to force compilation, otherwise false.
  1330. *
  1331. * @return $this
  1332. */
  1333. public function setForceCompile($force_compile)
  1334. {
  1335. $this->force_compile = $force_compile;
  1336. return $this;
  1337. }
  1338. /**
  1339. * Retrieve whether caching is enabled.
  1340. *
  1341. * @return integer A code indicating whether caching is enabled.
  1342. */
  1343. public function getCaching()
  1344. {
  1345. return $this->caching;
  1346. }
  1347. /**
  1348. * Set Caching.
  1349. *
  1350. * Possible value:
  1351. * <ul>
  1352. * <li>0 = no caching</li>
  1353. * <li>1 = use class cache_lifetime value</li>
  1354. * <li>2 = use cache_lifetime in cache file</li>
  1355. * </ul>
  1356. *
  1357. * @param integer $caching Cache value to set.
  1358. *
  1359. * @return $this
  1360. */
  1361. public function setCaching($caching)
  1362. {
  1363. $this->caching = (int)$caching;
  1364. return $this;
  1365. }
  1366. /**
  1367. * Retrieve the current cache ID.
  1368. *
  1369. * @return string The current cache ID.
  1370. */
  1371. public function getCacheId()
  1372. {
  1373. return $this->cache_id;
  1374. }
  1375. /**
  1376. * Set cache ID.
  1377. *
  1378. * @param string $id Cache ID.
  1379. *
  1380. * @return $this
  1381. */
  1382. public function setCacheId($id)
  1383. {
  1384. $this->cache_id = $id;
  1385. return $this;
  1386. }
  1387. /**
  1388. * Retrieve the number of seconds cached content will persist.
  1389. *
  1390. * Special values:
  1391. * <ul>
  1392. * <li>0 = always regenerate cache</li>
  1393. * <li>-1 = never expires</li>
  1394. * </ul>
  1395. *
  1396. * @return integer The number of seconds cached content will persist.
  1397. */
  1398. public function getCacheLifetime()
  1399. {
  1400. return $this->cache_lifetime;
  1401. }
  1402. /**
  1403. * Set cache lifetime.
  1404. *
  1405. * @param integer $time Lifetime in seconds.
  1406. *
  1407. * @return $this
  1408. */
  1409. public function setCacheLifetime($time)
  1410. {
  1411. $this->cache_lifetime = $time;
  1412. return $this;
  1413. }
  1414. /**
  1415. * Retrieve the name of the directory for cache files.

Large files files are truncated, but you can click here to view the full file