PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/system/t3/includes/joomla25/layout/file.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 441 lines | 198 code | 61 blank | 182 comment | 22 complexity | da0a499a8ccca8eb83d357bf180602b2 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage Layout
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. /**
  11. * Base class for rendering a display layout
  12. * loaded from from a layout file
  13. *
  14. * @package Joomla.Libraries
  15. * @subpackage Layout
  16. * @see http://docs.joomla.org/Sharing_layouts_across_views_or_extensions_with_JLayout
  17. * @since 3.0
  18. */
  19. class JLayoutFile extends JLayoutBase
  20. {
  21. /**
  22. * @var string Dot separated path to the layout file, relative to base path
  23. * @since 3.0
  24. */
  25. protected $layoutId = '';
  26. /**
  27. * @var string Base path to use when loading layout files
  28. * @since 3.0
  29. */
  30. protected $basePath = null;
  31. /**
  32. * @var string Full path to actual layout files, after possible template override check
  33. * @since 3.0.3
  34. */
  35. protected $fullPath = null;
  36. /**
  37. * Paths to search for layouts
  38. *
  39. * @var array
  40. * @since 3.2
  41. */
  42. protected $includePaths = array();
  43. /**
  44. * Method to instantiate the file-based layout.
  45. *
  46. * @param string $layoutId Dot separated path to the layout file, relative to base path
  47. * @param string $basePath Base path to use when loading layout files
  48. * @param mixed $options Optional custom options to load. JRegistry or array format [@since 3.2]
  49. *
  50. * @since 3.0
  51. */
  52. public function __construct($layoutId, $basePath = null, $options = null)
  53. {
  54. // Initialise / Load options
  55. $this->setOptions($options);
  56. // Main properties
  57. $this->setLayout($layoutId);
  58. $this->basePath = $basePath;
  59. // Init Enviroment
  60. $this->setComponent($this->options->get('component', 'auto'));
  61. $this->setClient($this->options->get('client', 'auto'));
  62. }
  63. /**
  64. * Method to render the layout.
  65. *
  66. * @param object $displayData Object which properties are used inside the layout file to build displayed output
  67. *
  68. * @return string The necessary HTML to display the layout
  69. *
  70. * @since 3.0
  71. */
  72. public function render($displayData)
  73. {
  74. $layoutOutput = '';
  75. // Check possible overrides, and build the full path to layout file
  76. $path = $this->getPath();
  77. if ($this->options->get('debug', false))
  78. {
  79. echo "<pre>" . $this->renderDebugMessages() . "</pre>";
  80. }
  81. // If there exists such a layout file, include it and collect its output
  82. if (!empty($path))
  83. {
  84. ob_start();
  85. include $path;
  86. $layoutOutput = ob_get_contents();
  87. ob_end_clean();
  88. }
  89. return $layoutOutput;
  90. }
  91. /**
  92. * Method to finds the full real file path, checking possible overrides
  93. *
  94. * @return string The full path to the layout file
  95. *
  96. * @since 3.0
  97. */
  98. protected function getPath()
  99. {
  100. JLoader::import('joomla.filesystem.path');
  101. if (is_null($this->fullPath) && !empty($this->layoutId))
  102. {
  103. $this->addDebugMessage('<strong>Layout:</strong> ' . $this->layoutId);
  104. // Refresh paths
  105. $this->refreshIncludePaths();
  106. $this->addDebugMessage('<strong>Include Paths:</strong> ' . print_r($this->includePaths, true));
  107. $suffixes = $this->options->get('suffixes', array());
  108. // Search for suffixed versions. Example: tags.j31.php
  109. if (!empty($suffixes))
  110. {
  111. $this->addDebugMessage('<strong>Suffixes:</strong> ' . print_r($suffixes, true));
  112. foreach ($suffixes as $suffix)
  113. {
  114. $rawPath = str_replace('.', '/', $this->layoutId) . '.' . $suffix . '.php';
  115. $this->addDebugMessage('<strong>Searching layout for:</strong> ' . $rawPath);
  116. if ($this->fullPath = JPath::find($this->includePaths, $rawPath))
  117. {
  118. $this->addDebugMessage('<strong>Found layout:</strong> ' . $this->fullPath);
  119. return $this->fullPath;
  120. }
  121. }
  122. }
  123. // Standard version
  124. $rawPath = str_replace('.', '/', $this->layoutId) . '.php';
  125. $this->addDebugMessage('<strong>Searching layout for:</strong> ' . $rawPath);
  126. $this->fullPath = JPath::find($this->includePaths, $rawPath);
  127. if ($this->fullPath = JPath::find($this->includePaths, $rawPath))
  128. {
  129. $this->addDebugMessage('<strong>Found layout:</strong> ' . $this->fullPath);
  130. }
  131. }
  132. return $this->fullPath;
  133. }
  134. /**
  135. * Add one path to include in layout search. Proxy of addIncludePaths()
  136. *
  137. * @param string $path The path to search for layouts
  138. *
  139. * @return void
  140. *
  141. * @since 3.2
  142. */
  143. public function addIncludePath($path)
  144. {
  145. $this->addIncludePaths($path);
  146. }
  147. /**
  148. * Add one or more paths to include in layout search
  149. *
  150. * @param string $paths The path or array of paths to search for layouts
  151. *
  152. * @return void
  153. *
  154. * @since 3.2
  155. */
  156. public function addIncludePaths($paths)
  157. {
  158. if (!empty($paths))
  159. {
  160. if (is_array($paths))
  161. {
  162. $this->includePaths = array_unique(array_merge($paths, $this->includePaths));
  163. }
  164. else
  165. {
  166. array_unshift($this->includePaths, $paths);
  167. }
  168. }
  169. }
  170. /**
  171. * Remove one path from the layout search
  172. *
  173. * @param string $path The path to remove from the layout search
  174. *
  175. * @return void
  176. *
  177. * @since 3.2
  178. */
  179. public function removeIncludePath($path)
  180. {
  181. $this->removeIncludePaths($path);
  182. }
  183. /**
  184. * Remove one or more paths to exclude in layout search
  185. *
  186. * @param string $paths The path or array of paths to remove for the layout search
  187. *
  188. * @return void
  189. *
  190. * @since 3.2
  191. */
  192. public function removeIncludePaths($paths)
  193. {
  194. if (!empty($paths))
  195. {
  196. $paths = (array) $paths;
  197. $this->includePaths = array_diff($this->includePaths, $paths);
  198. }
  199. }
  200. /**
  201. * Validate that the active component is valid
  202. *
  203. * @param string $option URL Option of the component. Example: com_content
  204. *
  205. * @return boolean
  206. *
  207. * @since 3.2
  208. */
  209. protected function validComponent($option = null)
  210. {
  211. // By default we will validate the active component
  212. $component = ($option !== null) ? $option : $this->options->get('component', null);
  213. if (!empty($component))
  214. {
  215. // Valid option format
  216. if (substr_count($component, 'com_'))
  217. {
  218. // Latest check: component exists and is enabled
  219. return JComponentHelper::isEnabled($component);
  220. }
  221. }
  222. return false;
  223. }
  224. /**
  225. * Method to change the component where search for layouts
  226. *
  227. * @param string $option URL Option of the component. Example: com_content
  228. *
  229. * @return mixed Component option string | null for none
  230. *
  231. * @since 3.2
  232. */
  233. public function setComponent($option)
  234. {
  235. $component = null;
  236. switch ((string) $option)
  237. {
  238. case 'none':
  239. $component = null;
  240. break;
  241. case 'auto':
  242. if (defined('JPATH_COMPONENT'))
  243. {
  244. $parts = explode('/', JPATH_COMPONENT);
  245. $component = end($parts);
  246. }
  247. break;
  248. default:
  249. $component = $option;
  250. break;
  251. }
  252. // Extra checks
  253. if (!$this->validComponent($component))
  254. {
  255. $component = null;
  256. }
  257. $this->options->set('component', $component);
  258. // Refresh include paths
  259. $this->refreshIncludePaths();
  260. }
  261. /**
  262. * Function to initialise the application client
  263. *
  264. * @param mixed $client Frontend: 'site' or 0 | Backend: 'admin' or 1
  265. *
  266. * @return void
  267. *
  268. * @since 3.2
  269. */
  270. public function setClient($client)
  271. {
  272. // Force string conversion to avoid unexpected states
  273. switch ((string) $client)
  274. {
  275. case 'site':
  276. case '0':
  277. $client = 0;
  278. break;
  279. case 'admin':
  280. case '1':
  281. $client = 1;
  282. break;
  283. default:
  284. $client = (int) JFactory::getApplication()->isAdmin();
  285. break;
  286. }
  287. $this->options->set('client', $client);
  288. // Refresh include paths
  289. $this->refreshIncludePaths();
  290. }
  291. /**
  292. * Change the layout
  293. *
  294. * @param string $layoutId Layout to render
  295. *
  296. * @return void
  297. *
  298. * @since 3.2
  299. */
  300. public function setLayout($layoutId)
  301. {
  302. $this->layoutId = $layoutId;
  303. $this->fullPath = null;
  304. }
  305. /**
  306. * Refresh the list of include paths
  307. *
  308. * @return void
  309. *
  310. * @since 3.2
  311. */
  312. protected function refreshIncludePaths()
  313. {
  314. // Reset includePaths
  315. $this->includePaths = array();
  316. // (1 - lower priority) Frontend base layouts
  317. $this->addIncludePaths(JPATH_ROOT . '/layouts');
  318. // (2.1) - T3 base layout overridden
  319. $this->addIncludePaths(T3_PATH . '/html/layouts');
  320. // (2) Standard Joomla! layouts overriden
  321. $this->addIncludePaths(JPATH_THEMES . '/' . JFactory::getApplication()->getTemplate() . '/html/layouts');
  322. // (2.1) - user custom layout overridden
  323. if (!defined('T3_LOCAL_DISABLED')) $this->addIncludePaths(T3_LOCAL_PATH . '/html/layouts');
  324. // Component layouts & overrides if exist
  325. $component = $this->options->get('component', null);
  326. if (!empty($component))
  327. {
  328. // (3) Component path
  329. if ($this->options->get('client') == 0)
  330. {
  331. $this->addIncludePaths(JPATH_SITE . '/components/' . $component . '/layouts');
  332. }
  333. else
  334. {
  335. $this->addIncludePaths(JPATH_ADMINISTRATOR . '/components/' . $component . '/layouts');
  336. }
  337. // (4) Component template overrides path
  338. $this->addIncludePath(JPATH_THEMES . '/' . JFactory::getApplication()->getTemplate() . '/html/layouts/' . $component);
  339. }
  340. // (5 - highest priority) Received a custom high priority path ?
  341. if (!is_null($this->basePath))
  342. {
  343. $this->addIncludePath(rtrim($this->basePath, DIRECTORY_SEPARATOR));
  344. }
  345. }
  346. /**
  347. * Change the debug mode
  348. *
  349. * @param boolean $debug Enable / Disable debug
  350. *
  351. * @return void
  352. *
  353. * @since 3.2
  354. */
  355. public function setDebug($debug)
  356. {
  357. $this->options->set('debug', (boolean) $debug);
  358. }
  359. /**
  360. * Render a layout with the same include paths & options
  361. *
  362. * @param object $layoutId Object which properties are used inside the layout file to build displayed output
  363. * @param mixed $displayData Data to be rendered
  364. *
  365. * @return string The necessary HTML to display the layout
  366. *
  367. * @since 3.2
  368. */
  369. public function sublayout($layoutId, $displayData)
  370. {
  371. // Sublayouts are searched in a subfolder with the name of the current layout
  372. if (!empty($this->layoutId))
  373. {
  374. $layoutId = $this->layoutId . '.' . $layoutId;
  375. }
  376. $sublayout = new static($layoutId, $this->basePath, $this->options);
  377. $sublayout->includePaths = $this->includePaths;
  378. return $sublayout->render($displayData);
  379. }
  380. }