PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/jsntplframework/libraries/joomlashine/template/helper.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 659 lines | 409 code | 111 blank | 139 comment | 93 complexity | 5e94458117c297415b581e70a1b8c44a MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package JSNExtension
  5. * @subpackage TPLFramework
  6. * @author JoomlaShine Team <support@joomlashine.com>
  7. * @copyright Copyright (C) 2012 JoomlaShine.com. All Rights Reserved.
  8. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  9. *
  10. * Websites: http://www.joomlashine.com
  11. * Technical Support: Feedback - http://www.joomlashine.com/contact-us/get-support.html
  12. */
  13. // No direct access to this file
  14. defined('_JEXEC') or die('Restricted access');
  15. class JSNTplTemplateHelper
  16. {
  17. private static $_instance;
  18. private $_templateName;
  19. private $_templateXml = array();
  20. private $_templateOptions = array();
  21. private $_defaultParams = array(
  22. // Logo settings
  23. 'logoFile' => '',
  24. 'logoLink' => 'index.php',
  25. 'logoSlogan' => '',
  26. 'logoColored' => false,
  27. // Layout settings
  28. 'layoutWidth' => 'narrow',
  29. 'layoutNarrowWidth' => 960,
  30. 'layoutWideWidth' => 1150,
  31. 'layoutFloatWidth' => 90,
  32. 'showFrontpage' => true,
  33. // Mobile settings
  34. 'mobileSupport' => true,
  35. 'menuSticky' => true,
  36. 'desktopSwitcher' => true,
  37. 'mobileLogo' => '',
  38. // Color & Style settings
  39. 'templateColor' => '',
  40. 'templateStyle' => 'business',
  41. 'textSize' => 'medium',
  42. 'useSpecialFont' => true,
  43. 'useCSS3Effect' => true,
  44. // Menu & Sitetools settings
  45. 'mainMenuWidth' => 200,
  46. 'sideMenuWidth' => 200,
  47. 'sitetoolStyle' => 'menu',
  48. 'textSizeSelector' => true,
  49. 'widthSelector' => true,
  50. 'colorSelector' => true,
  51. 'sitetoolsColors' => array(),
  52. // SEO & System settings
  53. 'enableH1' => true,
  54. 'gotoTop' => true,
  55. 'autoIconLink' => false,
  56. 'printOptimize' => false,
  57. 'codePosition' => 0,
  58. 'codeAnalytic' => '',
  59. 'cssFiles' => '',
  60. 'compression' => 0,
  61. 'useSqueezeBox' => false
  62. );
  63. private $_overrideAttributes = array(
  64. 'width' => array('type' => array('narrow', 'wide', 'float'), 'name' => 'layoutWidth'),
  65. 'textstyle' => array('type' => array('business', 'personal', 'news'), 'name' => 'templateStyle'),
  66. 'textsize' => array('type' => array('small', 'medium', 'big'), 'name' => 'textSize'),
  67. 'direction' => array('type' => array('ltr', 'rtl'), 'name' => 'direction'),
  68. 'color' => array('type' => 'string', 'name' => 'templateColor'),
  69. 'specialfont' => array('type' => 'boolean', 'name' => 'useSpecialFont'),
  70. 'mobile' => array('type' => 'boolean', 'name' => 'mobileView'),
  71. 'preset' => ''
  72. );
  73. private static $_manifest;
  74. private $_loadTemplateCSS = true;
  75. private $_loadTemplateJS = true;
  76. /**
  77. * Constructor
  78. *
  79. * @param string $name Name of the template
  80. */
  81. private function __construct ()
  82. {
  83. $this->_document = JFactory::getDocument();
  84. }
  85. /**
  86. * Get active instance of template helper object
  87. *
  88. * @param string $name Name of the template
  89. *
  90. * @return JSNTplTemplateHelper
  91. */
  92. public static function getInstance ()
  93. {
  94. if (self::$_instance == null) {
  95. self::$_instance = new JSNTplTemplateHelper();
  96. }
  97. return self::$_instance;
  98. }
  99. /**
  100. * Alias of _prepare method
  101. *
  102. * @return void
  103. */
  104. public static function prepare ($loadTemplateCss = true, $loadTemplateJs = true)
  105. {
  106. self::getInstance()->_prepare($loadTemplateCss, $loadTemplateJs);
  107. }
  108. /**
  109. * Load template parameters
  110. *
  111. * @param array $override Parameters override
  112. *
  113. * @return array
  114. */
  115. public function loadParams ($override = null, $templateName)
  116. {
  117. if (!is_array($override)) {
  118. $override = array();
  119. }
  120. $xml = JSNTplHelper::getManifest($templateName);
  121. // $options = $this->loadOptions($templateName);
  122. $templateUrl = JUri::root(true) . 'templates/' . $templateName;
  123. // Load default template Parameters
  124. foreach ($xml->xpath('//defaults/option') as $parameter)
  125. {
  126. $name = (string) $parameter['name'];
  127. $value = (string) $parameter['value'];
  128. if ($name == 'cssFiles') {
  129. $files = array();
  130. foreach ($parameter->children() as $item) {
  131. $files[] = (string) $item;
  132. }
  133. $this->_defaultParams[$name] = implode("\r\n", $files);
  134. continue;
  135. }
  136. $type = isset($this->_defaultParams[$name]) ? gettype($this->_defaultParams[$name]) : 'string';
  137. $this->_defaultParams[$name] = $this->convertType($value, $type);
  138. if ($type == 'string') {
  139. $this->_defaultParams[$name] = str_replace('{templateUrl}', $templateUrl, $this->_defaultParams[$name]);
  140. }
  141. }
  142. if (!isset($this->_defaultParams['logoFile']) || empty($this->_defaultParams['logoFile'])) {
  143. $this->_defaultParams['logoFile'] = 'templates/' . $templateName . '/images/logo.png';
  144. }
  145. return array_merge($this->_defaultParams, $override);
  146. }
  147. /**
  148. * Calculate the number of modules in positions
  149. *
  150. * @param array $positions Positions to get number of modules
  151. * @return int
  152. */
  153. public function countPositions ()
  154. {
  155. $this->_document = JFactory::getDocument();
  156. $positionCount = 0;
  157. foreach (func_get_args() as $position) {
  158. if ($this->_document->countModules($position)) {
  159. $positionCount++;
  160. }
  161. }
  162. return $positionCount;
  163. }
  164. /**
  165. * Preparing template parameters for the template
  166. *
  167. * @return void
  168. */
  169. private function _prepare ($loadTemplateCSS, $loadTemplateJS)
  170. {
  171. $this->_loadTemplateCSS = $loadTemplateCSS;
  172. $this->_loadTemplateJS = $loadTemplateJS;
  173. $templateParams = $this->_document->params;
  174. $templateName = $this->_document->template;
  175. if (empty($templateParams) || empty($templateName))
  176. {
  177. $templateDetails = JFactory::getApplication()->getTemplate(true);
  178. $templateParams = $templateDetails->params;
  179. $templateName = $templateDetails->template;
  180. }
  181. // Update show content on frontpage parameter
  182. $app = JFactory::getApplication();
  183. $menu = $app->getMenu()->getActive();
  184. $lang = JFactory::getLanguage();
  185. $lang->load('plg_system_jsntplframework', JPATH_ADMINISTRATOR);
  186. $manifest = JSNTplHelper::getManifest($templateName);
  187. $manifestCache = JSNTplHelper::getManifestCache($templateName);
  188. $this->_document->app = JFactory::getApplication();
  189. $this->_document->template = $templateName;
  190. $this->_document->version = $manifestCache->version;
  191. $this->_document->isFree = !isset($manifest->edition) || $manifest->edition == 'FREE';
  192. $this->_document->uri = JFactory::getUri();
  193. $this->_document->rootUrl = $this->_document->uri->root(true);
  194. $this->_document->templateUrl = $this->_document->rootUrl . '/templates/' . $this->_document->template;
  195. $columns = array(
  196. 'columnPromoLeft',
  197. 'columnPromoRight',
  198. 'columnLeft',
  199. 'columnRight',
  200. 'columnInnerleft',
  201. 'columnInnerright'
  202. );
  203. // Find customizable columns
  204. $customColumns = $manifest->xpath('//fieldset[@name="jsn-columns-size"]');
  205. if (count($customColumns) > 0) {
  206. $columns = array();
  207. foreach (end($customColumns)->children() as $column) {
  208. $columns[] = (string) $column['name'];
  209. }
  210. }
  211. // Add columns to overriable parameter list
  212. foreach ($columns as $column) {
  213. $className = $column;
  214. if (strpos($column, 'column') === 0) {
  215. $className = substr($column, 6);
  216. }
  217. $this->_overrideAttributes[strtolower($className . 'width')] = array('type' => 'integer', 'name' => $column);
  218. }
  219. // Load template parameters
  220. $params = $this->loadParams($templateParams->toArray(), $templateName, true);
  221. // Detect browser information
  222. $this->_document->browserInfo = JSNTplUtils::getInstance()->getBrowserInfo();
  223. $this->_document->isIE = @$this->_document->browserInfo['browser'] == 'msie';
  224. $this->_document->isIE7 = @$this->_document->browserInfo['browser'] == 'msie' && (int) @$this->_document->browserInfo['version'] == 7;
  225. // Custom direction from url parameter
  226. $direction = JFactory::getApplication()->input->getCmd('jsn_setdirection', $this->_document->direction);
  227. $this->_document->direction = $direction;
  228. // Apply custom params
  229. $params = $this->_overrideCustomParams($params);
  230. $params['showFrontpage'] = (is_object($menu) && $menu->home == 1) ? $params['showFrontpage'] == 1 : true;
  231. if ($this->_document->isFree === true) {
  232. $params['mobileSupport'] = false;
  233. $params['useCSS3Effect'] = false;
  234. }
  235. // Preparing logo parameter
  236. if ($params['logoColored']) {
  237. $params['logoFile'] = "templates/{$templateName}/images/colors/{$params['templateColor']}/logo.png";
  238. }
  239. if ($params['mobileSupport'] == false) {
  240. $params['desktopSwitcher'] = false;
  241. }
  242. if (!preg_match('/^[a-zA-Z]+:\/\//i', $params['logoFile'])) {
  243. $params['logoFile'] = JUri::root(true) . '/' . $params['logoFile'];
  244. }
  245. $this->_document->params = new JRegistry();
  246. // Binding parameters to document object
  247. foreach ($params as $key => $value) {
  248. $this->_document->params->set($key, $value);
  249. $this->_document->{$key} = $value;
  250. }
  251. // Assign helper object
  252. $this->_document->helper = $this;
  253. $this->_document->attributes = $this->_overrideAttributes;
  254. $this->_document->templatePrefix = $this->_document->template . '_';
  255. $this->_document->templateColors = array();
  256. // Prepare body class
  257. $this->_prepareBodyClass();
  258. // Prepare template styles
  259. $this->_prepareHead();
  260. }
  261. /**
  262. * Generate class for body element
  263. *
  264. * @return string
  265. */
  266. private function _prepareBodyClass ()
  267. {
  268. // Generate body class
  269. $bodyClass = array();
  270. $bodyClass[] = "jsn-textstyle-{$this->_document->templateStyle}";
  271. $bodyClass[] = "jsn-textsize-{$this->_document->textSize}";
  272. $bodyClass[] = "jsn-color-{$this->_document->templateColor}";
  273. $bodyClass[] = "jsn-direction-{$this->_document->direction}";
  274. // Special font class
  275. if ($this->_document->useSpecialFont) $bodyClass[] = "jsn-specialfont";
  276. // CSS3 class
  277. if ($this->_document->useCSS3Effect) $bodyClass[] = "jsn-css3";
  278. if ($this->_document->direction == 'rtl') $bodyClass[] = "jsn-direction-rtl";
  279. // Desktop style on mobile
  280. $mobileSupport = $this->_document->mobileSupport;
  281. if (isset($this->_document->mobileView) && $this->_document->mobileView == false) {
  282. $this->_document->mobileSupport = false;
  283. }
  284. // Mobile/desktop class
  285. $bodyClass[] = $this->_document->mobileSupport ? 'jsn-mobile' : 'jsn-desktop';
  286. $bodyClass[] = implode(' ', $this->_getPageClass());
  287. $bodyClass[] = $this->_isJoomla3() ? 'jsn-joomla-30' : 'jsn-joomla-25';
  288. if (isset($this->_document->mobileView) && $this->_document->mobileView == false && $mobileSupport == true) {
  289. $bodyClass[] = 'jsn-desktop-on-mobile';
  290. }
  291. // Add custom class based from query string
  292. $input = $this->_document->app->input;
  293. $option = substr($input->getCmd('option'), 4);
  294. $bodyClass[] = "jsn-com-{$option}";
  295. // View parameter
  296. $view = $input->getCmd('view');
  297. if (!empty($view)) {
  298. $bodyClass[] = "jsn-view-{$view}";
  299. }
  300. // Layout parameter
  301. $layout = $input->getCmd('layout');
  302. if (!empty($layout)) {
  303. $bodyClass[] = "jsn-layout-{$layout}";
  304. }
  305. // Itemid parameter
  306. $itemid = $input->getInt('Itemid');
  307. if (!empty($itemid)) {
  308. $bodyClass[] = "jsn-itemid-{$itemid}";
  309. }
  310. if (is_object($this->_document->activeMenu) && $this->_document->activeMenu->home == 1) {
  311. $bodyClass[] = 'jsn-homepage';
  312. }
  313. // Set body class to document object
  314. $this->_document->bodyClass = implode(' ', $bodyClass);
  315. }
  316. /**
  317. * Preparing head section for the template
  318. *
  319. * @return void
  320. */
  321. private function _prepareHead ()
  322. {
  323. JHTML::_('behavior.framework', true);
  324. if ($this->_loadTemplateCSS == true)
  325. {
  326. if ($this->_isJoomla3())
  327. {
  328. // Add JavaScript Frameworks
  329. JHtml::_('bootstrap.framework');
  330. // Add Css Frameworks
  331. JHtmlBootstrap::loadCss();
  332. // Load optional rtl Bootstrap css and Bootstrap bugfixes
  333. JHtmlBootstrap::loadCss(true, $this->_document->direction);
  334. }
  335. // Print optimize
  336. if ($this->_document->printOptimize) {
  337. $this->_document->addStylesheet($this->_document->templateUrl . "/css/print.css", 'text/css', 'Print');
  338. }
  339. $this->_document->addStylesheet($this->_document->rootUrl . '/templates/system/css/system.css');
  340. $this->_document->addStylesheet($this->_document->rootUrl . '/templates/system/css/general.css');
  341. $this->_document->addStylesheet($this->_document->templateUrl . '/css/template.css');
  342. $this->_document->addStylesheet($this->_document->templateUrl . '/css/colors/' . $this->_document->templateColor . '.css');
  343. $this->_document->addStylesheet($this->_document->templateUrl . '/css/styles/' . $this->_document->templateStyle . '.css');
  344. // Auto icon link stylesheet
  345. if ($this->_document->autoIconLink) {
  346. $this->_document->addStylesheet($this->_document->templateUrl . '/css/jsn_iconlinks.css');
  347. }
  348. // Right to left stylesheet
  349. if ($this->_document->direction == "rtl") {
  350. $this->_document->addStylesheet($this->_document->templateUrl . '/css/jsn_rtl.css');
  351. }
  352. // Mobile support stylesheet
  353. if ($this->_document->mobileSupport) {
  354. $this->_document->addCustomTag('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />');
  355. $this->_document->addStylesheet($this->_document->templateUrl . '/css/jsn_mobile.css');
  356. }
  357. // CSS3 effect stylesheet
  358. if ($this->_document->useCSS3Effect) {
  359. $this->_document->addStylesheet($this->_document->templateUrl . '/css/jsn_css3.css');
  360. }
  361. // IE7 Specific stylesheet
  362. if ($this->_document->isIE7) {
  363. $this->_document->addStylesheet($this->_document->rootUrl . '/css/jsn_fixie7.css');
  364. }
  365. // Squeezebox stylesheet
  366. if ($this->_document->useSqueezeBox) {
  367. $this->_document->addStylesheet($this->_document->rootUrl . '/media/system/css/modal.css');
  368. }
  369. // Template width
  370. $unit = $this->_document->layoutWidth == 'float' ? '%' : 'px';
  371. $this->_document->templateWidth = $this->_document->{'layout' . ucfirst($this->_document->layoutWidth) . 'Width'} . $unit;
  372. // Load custom css that declared by the template
  373. if (is_file(JPATH_ROOT . "/templates/{$this->_document->template}/template_custom.php")) {
  374. include_once JPATH_ROOT . "/templates/{$this->_document->template}/template_custom.php";
  375. }
  376. // Load custom css files from the parameter
  377. foreach (explode("\n", $this->_document->cssFiles) as $file)
  378. {
  379. if (empty($file)) {
  380. continue;
  381. }
  382. preg_match('/^[a-z]+:\/\//i', $file)
  383. ? $this->_document->addStylesheet(trim($file))
  384. : $this->_document->addStylesheet($this->_document->templateUrl . '/css/' . trim($file));
  385. }
  386. }
  387. if ($this->_loadTemplateJS == true)
  388. {
  389. // Load Javascript files
  390. // $this->_document->addScript($this->_document->rootUrl . '/media/system/js/mootools-more.js');
  391. $this->_document->addScript($this->_document->rootUrl . '/plugins/system/jsntplframework/assets/joomlashine/js/noconflict.js');
  392. $this->_document->addScript($this->_document->rootUrl . '/plugins/system/jsntplframework/assets/joomlashine/js/utils.js');
  393. $this->_document->addScript($this->_document->templateUrl . '/js/jsn_template.js');
  394. // Load javascript file for squeezebox
  395. if ($this->_document->useSqueezeBox) {
  396. $this->_document->addScript($this->_document->rootUrl . '/media/system/js/modal.js');
  397. }
  398. // Custom template JS declarations
  399. $this->_document->addScriptDeclaration('
  400. JSNTemplate.initTemplate({
  401. templatePrefix : "' . $this->_document->template . '_",
  402. templatePath : "' . $this->_document->rootUrl . '/templates/' . $this->_document->template . '",
  403. enableRTL : ' . ($this->_document->direction == "rtl" ? 'true' : 'false') . ',
  404. enableGotopLink : ' . ($this->_document->gotoTop ? 'true' : 'false') . ',
  405. enableMobile : ' . ($this->_document->mobileSupport ? 'true' : 'false') . ',
  406. enableMobileMenuSticky : ' . ($this->_document->menuSticky ? 'true' : 'false') .'
  407. });
  408. ');
  409. // load Squeezebox
  410. if ($this->_document->useSqueezeBox) {
  411. $this->_document->addScriptDeclaration('
  412. window.addEvent("domready", function() {
  413. SqueezeBox.initialize({});
  414. SqueezeBox.assign($$("a.modal"), {
  415. parse: "rel"
  416. });
  417. });
  418. ');
  419. }
  420. }
  421. }
  422. /**
  423. * Retrieve parameter pageclass_sfx from active menu
  424. *
  425. * @return string
  426. */
  427. private function _getPageClass ()
  428. {
  429. $pageClass = '';
  430. $notHomePage = true;
  431. $menus = $this->_document->app->getMenu();
  432. $menu = $menus->getActive();
  433. $this->_document->activeMenu = $menu;
  434. if (is_object($menu))
  435. {
  436. // Set page class suffix
  437. $params = JMenu::getInstance('site')->getParams($menu->id);
  438. $pageClass = $params->get('pageclass_sfx', '');
  439. // Set homepage flag
  440. $lang = JFactory::getLanguage();
  441. $defaultMenu = $menus->getDefault($lang->getTag());
  442. if (is_object($defaultMenu)) {
  443. $notHomePage = ($menu->id != $defaultMenu->id);
  444. }
  445. }
  446. return explode(' ', $pageClass);
  447. }
  448. /**
  449. * Override parameters with value retrieved from cookie and body class
  450. *
  451. * @param array $params Template parameters to override
  452. *
  453. * @return array
  454. */
  455. private function _overrideCustomParams ($params)
  456. {
  457. $pageClass = $this->_getPageClass();
  458. $cookieName = $this->_document->template . '_params';
  459. // Override params from page class
  460. foreach ($pageClass as $class)
  461. {
  462. if (strpos($class, 'custom-') !== false && substr_count($class, '-') >= 2)
  463. {
  464. list($prefix, $param, $value) = explode('-', $class);
  465. if (!isset($this->_overrideAttributes[$param])) {
  466. continue;
  467. }
  468. // Update custom parameter
  469. $attribute = $this->_overrideAttributes[$param];
  470. // Checking attribute type
  471. if (is_array($attribute['type']) && in_array($value, $attribute['type'])) {
  472. $params[$attribute['name']] = $value;
  473. }
  474. elseif ($attribute['type'] == 'integer' && is_numeric($value)) {
  475. $params[$attribute['name']] = intval($value);
  476. }
  477. else {
  478. $params[$attribute['name']] = trim($value);
  479. }
  480. }
  481. }
  482. // Check template cookie is existing
  483. if (isset($_COOKIE[$cookieName]))
  484. {
  485. // Decode template cookie
  486. $cookieParams = json_decode($_COOKIE[$cookieName], true);
  487. if (!empty($cookieParams) && is_array($cookieParams))
  488. {
  489. // Override template parameters
  490. foreach ($cookieParams as $key => $value)
  491. {
  492. if (isset($this->_overrideAttributes[$key])) {
  493. $attribute = $this->_overrideAttributes[$key];
  494. // Checking attribute type
  495. if (is_array($attribute['type']) && in_array($value, $attribute['type']))
  496. $params[$attribute['name']] = $value;
  497. elseif ($attribute['type'] == 'integer' && is_numeric($value))
  498. $params[$attribute['name']] = intval($value);
  499. elseif ($attribute['type'] == 'boolean')
  500. $params[$attribute['name']] = $value == 'yes';
  501. else
  502. $params[$attribute['name']] = $value;
  503. }
  504. }
  505. }
  506. }
  507. return $params;
  508. }
  509. /**
  510. * Convert data to specified type
  511. *
  512. * @param mixed $data Data to be converted
  513. * @param string $type Type of the data after converted
  514. *
  515. * @return mixed
  516. */
  517. protected function convertType ($data, $type)
  518. {
  519. switch ($type) {
  520. case 'string':
  521. $data = (string) $data;
  522. break;
  523. case 'integer':
  524. $data = (int) $data;
  525. break;
  526. case 'float':
  527. $data = (float) $data;
  528. break;
  529. case 'boolean':
  530. $data = $data == 'true' || $data == 'yes' || $data == '1';
  531. break;
  532. }
  533. return $data;
  534. }
  535. /**
  536. * Return true if current joomla version is 3.0
  537. *
  538. * @return boolean
  539. */
  540. private function _isJoomla3 ()
  541. {
  542. if (!isset($this->isJoomla3)) {
  543. $version = new JVersion();
  544. $this->isJoomla3 = version_compare($version->getShortVersion(), '3.0', '>=');
  545. }
  546. return $this->isJoomla3;
  547. }
  548. }