PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/fof/template/utils.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 518 lines | 278 code | 70 blank | 170 comment | 49 complexity | 20ba25503ad6ef280c91ad324e0b80da MD5 | raw file
  1. <?php
  2. /**
  3. * @package FrameworkOnFramework
  4. * @subpackage template
  5. * @copyright Copyright (C) 2010 - 2015 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // Protect from unauthorized access
  9. defined('FOF_INCLUDED') or die;
  10. /**
  11. * A utility class to load view templates, media files and modules.
  12. *
  13. * @package FrameworkOnFramework
  14. * @since 1.0
  15. */
  16. class FOFTemplateUtils
  17. {
  18. /**
  19. * Add a CSS file to the page generated by the CMS
  20. *
  21. * @param string $path A fancy path definition understood by parsePath
  22. *
  23. * @see FOFTemplateUtils::parsePath
  24. *
  25. * @return void
  26. */
  27. public static function addCSS($path)
  28. {
  29. $document = FOFPlatform::getInstance()->getDocument();
  30. if ($document instanceof JDocument)
  31. {
  32. if (method_exists($document, 'addStyleSheet'))
  33. {
  34. $url = self::parsePath($path);
  35. $document->addStyleSheet($url);
  36. }
  37. }
  38. }
  39. /**
  40. * Add a JS script file to the page generated by the CMS.
  41. *
  42. * There are three combinations of defer and async (see http://www.w3schools.com/tags/att_script_defer.asp):
  43. * * $defer false, $async true: The script is executed asynchronously with the rest of the page
  44. * (the script will be executed while the page continues the parsing)
  45. * * $defer true, $async false: The script is executed when the page has finished parsing.
  46. * * $defer false, $async false. (default) The script is loaded and executed immediately. When it finishes
  47. * loading the browser continues parsing the rest of the page.
  48. *
  49. * When you are using $defer = true there is no guarantee about the load order of the scripts. Whichever
  50. * script loads first will be executed first. The order they appear on the page is completely irrelevant.
  51. *
  52. * @param string $path A fancy path definition understood by parsePath
  53. * @param boolean $defer Adds the defer attribute, meaning that your script
  54. * will only load after the page has finished parsing.
  55. * @param boolean $async Adds the async attribute, meaning that your script
  56. * will be executed while the resto of the page
  57. * continues parsing.
  58. *
  59. * @see FOFTemplateUtils::parsePath
  60. *
  61. * @return void
  62. */
  63. public static function addJS($path, $defer = false, $async = false)
  64. {
  65. $document = FOFPlatform::getInstance()->getDocument();
  66. if ($document instanceof JDocument)
  67. {
  68. if (method_exists($document, 'addScript'))
  69. {
  70. $url = self::parsePath($path);
  71. $document->addScript($url, "text/javascript", $defer, $async);
  72. }
  73. }
  74. }
  75. /**
  76. * Compile a LESS file into CSS and add it to the page generated by the CMS.
  77. * This method has integrated cache support. The compiled LESS files will be
  78. * written to the media/lib_fof/compiled directory of your site. If the file
  79. * cannot be written we will use the $altPath, if specified
  80. *
  81. * @param string $path A fancy path definition understood by parsePath pointing to the source LESS file
  82. * @param string $altPath A fancy path definition understood by parsePath pointing to a precompiled CSS file,
  83. * used when we can't write the generated file to the output directory
  84. * @param boolean $returnPath Return the URL of the generated CSS file but do not include it. If it can't be
  85. * generated, false is returned and the alt files are not included
  86. *
  87. * @see FOFTemplateUtils::parsePath
  88. *
  89. * @since 2.0
  90. *
  91. * @return mixed True = successfully included generated CSS, False = the alternate CSS file was used, null = the source file does not exist
  92. */
  93. public static function addLESS($path, $altPath = null, $returnPath = false)
  94. {
  95. // Does the cache directory exists and is writeable
  96. static $sanityCheck = null;
  97. // Get the local LESS file
  98. $localFile = self::parsePath($path, true);
  99. $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
  100. $platformDirs = FOFPlatform::getInstance()->getPlatformBaseDirs();
  101. if (is_null($sanityCheck))
  102. {
  103. // Make sure the cache directory exists
  104. if (!is_dir($platformDirs['public'] . '/media/lib_fof/compiled/'))
  105. {
  106. $sanityCheck = $filesystem->folderCreate($platformDirs['public'] . '/media/lib_fof/compiled/');
  107. }
  108. else
  109. {
  110. $sanityCheck = true;
  111. }
  112. }
  113. // No point continuing if the source file is not there or we can't write to the cache
  114. if (!$sanityCheck || !is_file($localFile))
  115. {
  116. if (!$returnPath)
  117. {
  118. if (is_string($altPath))
  119. {
  120. self::addCSS($altPath);
  121. }
  122. elseif (is_array($altPath))
  123. {
  124. foreach ($altPath as $anAltPath)
  125. {
  126. self::addCSS($anAltPath);
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. // Get the source file's unique ID
  133. $id = md5(filemtime($localFile) . filectime($localFile) . $localFile);
  134. // Get the cached file path
  135. $cachedPath = $platformDirs['public'] . '/media/lib_fof/compiled/' . $id . '.css';
  136. // Get the LESS compiler
  137. $lessCompiler = new FOFLess;
  138. $lessCompiler->formatterName = 'compressed';
  139. // Should I add an alternative import path?
  140. $altFiles = self::getAltPaths($path);
  141. if (isset($altFiles['alternate']))
  142. {
  143. $currentLocation = realpath(dirname($localFile));
  144. $normalLocation = realpath(dirname($altFiles['normal']));
  145. $alternateLocation = realpath(dirname($altFiles['alternate']));
  146. if ($currentLocation == $normalLocation)
  147. {
  148. $lessCompiler->importDir = array($alternateLocation, $currentLocation);
  149. }
  150. else
  151. {
  152. $lessCompiler->importDir = array($currentLocation, $normalLocation);
  153. }
  154. }
  155. // Compile the LESS file
  156. $lessCompiler->checkedCompile($localFile, $cachedPath);
  157. // Add the compiled CSS to the page
  158. $base_url = rtrim(FOFPlatform::getInstance()->URIbase(), '/');
  159. if (substr($base_url, -14) == '/administrator')
  160. {
  161. $base_url = substr($base_url, 0, -14);
  162. }
  163. $url = $base_url . '/media/lib_fof/compiled/' . $id . '.css';
  164. if ($returnPath)
  165. {
  166. return $url;
  167. }
  168. else
  169. {
  170. $document = FOFPlatform::getInstance()->getDocument();
  171. if ($document instanceof JDocument)
  172. {
  173. if (method_exists($document, 'addStyleSheet'))
  174. {
  175. $document->addStyleSheet($url);
  176. }
  177. }
  178. return true;
  179. }
  180. }
  181. /**
  182. * Creates a SEF compatible sort header. Standard Joomla function will add a href="#" tag, so with SEF
  183. * enabled, the browser will follow the fake link instead of processing the onSubmit event; so we
  184. * need a fix.
  185. *
  186. * @param string $text Header text
  187. * @param string $field Field used for sorting
  188. * @param FOFUtilsObject $list Object holding the direction and the ordering field
  189. *
  190. * @return string HTML code for sorting
  191. */
  192. public static function sefSort($text, $field, $list)
  193. {
  194. $sort = JHTML::_('grid.sort', JText::_(strtoupper($text)) . '&nbsp;', $field, $list->order_Dir, $list->order);
  195. return str_replace('href="#"', 'href="javascript:void(0);"', $sort);
  196. }
  197. /**
  198. * Parse a fancy path definition into a path relative to the site's root,
  199. * respecting template overrides, suitable for inclusion of media files.
  200. * For example, media://com_foobar/css/test.css is parsed into
  201. * media/com_foobar/css/test.css if no override is found, or
  202. * templates/mytemplate/media/com_foobar/css/test.css if the current
  203. * template is called mytemplate and there's a media override for it.
  204. *
  205. * The valid protocols are:
  206. * media:// The media directory or a media override
  207. * admin:// Path relative to administrator directory (no overrides)
  208. * site:// Path relative to site's root (no overrides)
  209. *
  210. * @param string $path Fancy path
  211. * @param boolean $localFile When true, it returns the local path, not the URL
  212. *
  213. * @return string Parsed path
  214. */
  215. public static function parsePath($path, $localFile = false)
  216. {
  217. $platformDirs = FOFPlatform::getInstance()->getPlatformBaseDirs();
  218. if ($localFile)
  219. {
  220. $url = rtrim($platformDirs['root'], DIRECTORY_SEPARATOR) . '/';
  221. }
  222. else
  223. {
  224. $url = FOFPlatform::getInstance()->URIroot();
  225. }
  226. $altPaths = self::getAltPaths($path);
  227. $filePath = $altPaths['normal'];
  228. // If JDEBUG is enabled, prefer that path, else prefer an alternate path if present
  229. if (defined('JDEBUG') && JDEBUG && isset($altPaths['debug']))
  230. {
  231. if (file_exists($platformDirs['public'] . '/' . $altPaths['debug']))
  232. {
  233. $filePath = $altPaths['debug'];
  234. }
  235. }
  236. elseif (isset($altPaths['alternate']))
  237. {
  238. if (file_exists($platformDirs['public'] . '/' . $altPaths['alternate']))
  239. {
  240. $filePath = $altPaths['alternate'];
  241. }
  242. }
  243. $url .= $filePath;
  244. return $url;
  245. }
  246. /**
  247. * Parse a fancy path definition into a path relative to the site's root.
  248. * It returns both the normal and alternative (template media override) path.
  249. * For example, media://com_foobar/css/test.css is parsed into
  250. * array(
  251. * 'normal' => 'media/com_foobar/css/test.css',
  252. * 'alternate' => 'templates/mytemplate/media/com_foobar/css//test.css'
  253. * );
  254. *
  255. * The valid protocols are:
  256. * media:// The media directory or a media override
  257. * admin:// Path relative to administrator directory (no alternate)
  258. * site:// Path relative to site's root (no alternate)
  259. *
  260. * @param string $path Fancy path
  261. *
  262. * @return array Array of normal and alternate parsed path
  263. */
  264. public static function getAltPaths($path)
  265. {
  266. $protoAndPath = explode('://', $path, 2);
  267. if (count($protoAndPath) < 2)
  268. {
  269. $protocol = 'media';
  270. }
  271. else
  272. {
  273. $protocol = $protoAndPath[0];
  274. $path = $protoAndPath[1];
  275. }
  276. $path = ltrim($path, '/' . DIRECTORY_SEPARATOR);
  277. switch ($protocol)
  278. {
  279. case 'media':
  280. // Do we have a media override in the template?
  281. $pathAndParams = explode('?', $path, 2);
  282. $ret = array(
  283. 'normal' => 'media/' . $pathAndParams[0],
  284. 'alternate' => FOFPlatform::getInstance()->getTemplateOverridePath('media:/' . $pathAndParams[0], false),
  285. );
  286. break;
  287. case 'admin':
  288. $ret = array(
  289. 'normal' => 'administrator/' . $path
  290. );
  291. break;
  292. default:
  293. case 'site':
  294. $ret = array(
  295. 'normal' => $path
  296. );
  297. break;
  298. }
  299. // For CSS and JS files, add a debug path if the supplied file is compressed
  300. $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
  301. $ext = $filesystem->getExt($ret['normal']);
  302. if (in_array($ext, array('css', 'js')))
  303. {
  304. $file = basename($filesystem->stripExt($ret['normal']));
  305. /*
  306. * Detect if we received a file in the format name.min.ext
  307. * If so, strip the .min part out, otherwise append -uncompressed
  308. */
  309. if (strlen($file) > 4 && strrpos($file, '.min', '-4'))
  310. {
  311. $position = strrpos($file, '.min', '-4');
  312. $filename = str_replace('.min', '.', $file, $position) . $ext;
  313. }
  314. else
  315. {
  316. $filename = $file . '-uncompressed.' . $ext;
  317. }
  318. // Clone the $ret array so we can manipulate the 'normal' path a bit
  319. $t1 = (object) $ret;
  320. $temp = clone $t1;
  321. unset($t1);
  322. $temp = (array)$temp;
  323. $normalPath = explode('/', $temp['normal']);
  324. array_pop($normalPath);
  325. $normalPath[] = $filename;
  326. $ret['debug'] = implode('/', $normalPath);
  327. }
  328. return $ret;
  329. }
  330. /**
  331. * Returns the contents of a module position
  332. *
  333. * @param string $position The position name, e.g. "position-1"
  334. * @param int $style Rendering style; please refer to Joomla!'s code for more information
  335. *
  336. * @return string The contents of the module position
  337. */
  338. public static function loadPosition($position, $style = -2)
  339. {
  340. $document = FOFPlatform::getInstance()->getDocument();
  341. if (!($document instanceof JDocument))
  342. {
  343. return '';
  344. }
  345. if (!method_exists($document, 'loadRenderer'))
  346. {
  347. return '';
  348. }
  349. try
  350. {
  351. $renderer = $document->loadRenderer('module');
  352. }
  353. catch (Exception $exc)
  354. {
  355. return '';
  356. }
  357. $params = array('style' => $style);
  358. $contents = '';
  359. foreach (JModuleHelper::getModules($position) as $mod)
  360. {
  361. $contents .= $renderer->render($mod, $params);
  362. }
  363. return $contents;
  364. }
  365. /**
  366. * Merges the current url with new or changed parameters.
  367. *
  368. * This method merges the route string with the url parameters defined
  369. * in current url. The parameters defined in current url, but not given
  370. * in route string, will automatically reused in the resulting url.
  371. * But only these following parameters will be reused:
  372. *
  373. * option, view, layout, format
  374. *
  375. * Example:
  376. *
  377. * Assuming that current url is:
  378. * http://fobar.com/index.php?option=com_foo&view=cpanel
  379. *
  380. * <code>
  381. * <?php echo FOFTemplateutils::route('view=categories&layout=tree'); ?>
  382. * </code>
  383. *
  384. * Result:
  385. * http://fobar.com/index.php?option=com_foo&view=categories&layout=tree
  386. *
  387. * @param string $route The parameters string
  388. *
  389. * @return string The human readable, complete url
  390. */
  391. public static function route($route = '')
  392. {
  393. $route = trim($route);
  394. // Special cases
  395. if ($route == 'index.php' || $route == 'index.php?')
  396. {
  397. $result = $route;
  398. }
  399. elseif (substr($route, 0, 1) == '&')
  400. {
  401. $url = JURI::getInstance();
  402. $vars = array();
  403. parse_str($route, $vars);
  404. $url->setQuery(array_merge($url->getQuery(true), $vars));
  405. $result = 'index.php?' . $url->getQuery();
  406. }
  407. else
  408. {
  409. $url = JURI::getInstance();
  410. $props = $url->getQuery(true);
  411. // Strip 'index.php?'
  412. if (substr($route, 0, 10) == 'index.php?')
  413. {
  414. $route = substr($route, 10);
  415. }
  416. // Parse route
  417. $parts = array();
  418. parse_str($route, $parts);
  419. $result = array();
  420. // Check to see if there is component information in the route if not add it
  421. if (!isset($parts['option']) && isset($props['option']))
  422. {
  423. $result[] = 'option=' . $props['option'];
  424. }
  425. // Add the layout information to the route only if it's not 'default'
  426. if (!isset($parts['view']) && isset($props['view']))
  427. {
  428. $result[] = 'view=' . $props['view'];
  429. if (!isset($parts['layout']) && isset($props['layout']))
  430. {
  431. $result[] = 'layout=' . $props['layout'];
  432. }
  433. }
  434. // Add the format information to the URL only if it's not 'html'
  435. if (!isset($parts['format']) && isset($props['format']) && $props['format'] != 'html')
  436. {
  437. $result[] = 'format=' . $props['format'];
  438. }
  439. // Reconstruct the route
  440. if (!empty($route))
  441. {
  442. $result[] = $route;
  443. }
  444. $result = 'index.php?' . implode('&', $result);
  445. }
  446. return JRoute::_($result);
  447. }
  448. }