PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/cms/layout/file.php

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