PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/tmp/busines13_bundle_installer/lib_gantry/core/gantrytemplate.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 829 lines | 495 code | 109 blank | 225 comment | 77 complexity | 61022ce9320be81f8a03769495b7ad26 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package gantry
  4. * @subpackage core
  5. * @version 3.2.22 August 3, 2012
  6. * @author RocketTheme http://www.rockettheme.com
  7. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. *
  10. * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  11. *
  12. */
  13. defined('GANTRY_VERSION') or die();
  14. gantry_import("core.utilities.gantrysimplexmlelement");
  15. gantry_import("core.utilities.gantryregistry");
  16. /**
  17. * Populates the parameters and template configuration form the templateDetails.xml and params.ini
  18. *
  19. * @package gantry
  20. * @subpackage core
  21. */
  22. class GantryTemplate
  23. {
  24. /**
  25. * Template Author
  26. * @access private
  27. * @var string
  28. */
  29. protected $author;
  30. /**
  31. * Template Version
  32. * @access private
  33. * @var string
  34. */
  35. protected $version;
  36. /**
  37. * Template Short Name
  38. * @access private
  39. * @var string
  40. */
  41. protected $name;
  42. /**
  43. * Template license
  44. * @access private
  45. * @var string
  46. */
  47. protected $license;
  48. /**
  49. * Template Full Name
  50. * @access private
  51. * @var string
  52. */
  53. protected $fullname;
  54. /**
  55. * Creation Date
  56. * @access private
  57. * @var string
  58. */
  59. protected $creationDate;
  60. /**
  61. * Template Author Email
  62. * @access private
  63. * @var string
  64. */
  65. protected $authorEmail;
  66. /**
  67. * Template Author Url
  68. * @access private
  69. * @var string
  70. */
  71. protected $authorUrl;
  72. /**
  73. * Template Description
  74. * @access private
  75. * @var string
  76. */
  77. protected $description;
  78. /**
  79. * Template Copyright
  80. * @access private
  81. * @var string
  82. */
  83. protected $copyright;
  84. /**
  85. * @var GantrySimpleXMLElement
  86. */
  87. protected $xml;
  88. /**
  89. * @var GantrySimpleXMLElement
  90. */
  91. protected $options_xml;
  92. /**
  93. * @var array
  94. */
  95. protected $positions = array();
  96. /**
  97. * @var array
  98. */
  99. protected $positionInfo = array();
  100. /**
  101. * @var array
  102. */
  103. protected $params = array();
  104. /**
  105. * @var GantryRegistry
  106. */
  107. protected $_params_reg;
  108. protected $_processors = array();
  109. /**
  110. * @var bool
  111. */
  112. protected $legacycss = true;
  113. /**
  114. * @var bool
  115. */
  116. protected $gridcss = true;
  117. /**
  118. * @var null
  119. */
  120. protected $_params_content;
  121. protected $masterParams;
  122. public function __sleep()
  123. {
  124. return array(
  125. 'author',
  126. 'version',
  127. 'name',
  128. 'license',
  129. 'fullname',
  130. 'creationDate',
  131. 'authorEmail',
  132. 'authorUrl',
  133. 'description',
  134. 'copyright',
  135. 'positions',
  136. 'params',
  137. 'positionInfo',
  138. '_params_content'
  139. );
  140. }
  141. public function GantryTemplate()
  142. {
  143. }
  144. public function init(&$gantry)
  145. {
  146. $this->xml = new GantrySimpleXMLElement($gantry->templatePath . '/templateDetails.xml', null, true);
  147. $this->options_xml = new GantrySimpleXMLElement($gantry->templatePath . '/template-options.xml', null, true);
  148. $this->_loadPositions();
  149. $this->_getTemplateInfo();
  150. $this->_loadProcessors($gantry);
  151. $this->params = $this->_getParams($gantry);
  152. }
  153. public function getParams()
  154. {
  155. return $this->params;
  156. }
  157. protected function & _loadPositions()
  158. {
  159. $positions = array();
  160. $xml_positions = $this->xml->xpath('//positions/position');
  161. foreach ($xml_positions as $position)
  162. {
  163. //$position_name = $position->data();
  164. array_push($this->positions, $position->data());
  165. $shortposition = preg_replace("/(\-[a-f])$/i", "", $position->data());
  166. if (!array_key_exists($shortposition, $this->positionInfo))
  167. {
  168. $positionObject = new stdClass();
  169. $attrs = $position->attributes();
  170. $positionObject->name = $shortposition;
  171. $positionObject->id = $shortposition;
  172. $positionObject->max_positions = 1;
  173. $positionObject->mobile = ((string)$attrs['mobile'] == 'true') ? true : false;
  174. $this->positionInfo[$shortposition] = $positionObject;
  175. }
  176. else
  177. {
  178. $this->positionInfo[$shortposition]->max_positions++;
  179. }
  180. }
  181. return $positions;
  182. }
  183. protected function _loadProcessors(&$gantry){
  184. $processor_path = $gantry->gantryPath.'/core/params/processors';
  185. if (file_exists($processor_path) && is_dir($processor_path)){
  186. $d = dir($gantry->gantryPath.'/core/params/processors');
  187. while (false !== ($entry = $d->read())) {
  188. if($entry != '.' && $entry != '..'){
  189. $processor_name = basename($entry, ".php");
  190. $path = $processor_path.DS.$processor_name.'.php';
  191. $className = 'GantryParamProcessor'.ucfirst($processor_name);
  192. if (!class_exists($className)) {
  193. if (file_exists( $path ))
  194. {
  195. require_once( $path );
  196. if(class_exists($className))
  197. {
  198. $this->_processors[$className] = new $className();
  199. }
  200. }
  201. }
  202. }
  203. }
  204. $d->close();
  205. }
  206. }
  207. protected function _runProcessorPreLoad(&$gantry, $param_name, &$param_element, &$data){
  208. foreach ($this->_processors as $processor){
  209. $processor->preLoad($gantry, $param_name, $param_element, $data);
  210. }
  211. }
  212. protected function _runProcessorPostLoad(&$gantry, $param_name, &$param_element, &$data){
  213. foreach ($this->_processors as $processor){
  214. $processor->postLoad($gantry, $param_name, $param_element, $data);
  215. }
  216. }
  217. public function getUniquePositions()
  218. {
  219. return array_keys($this->positionInfo);
  220. }
  221. public function getPositionInfo($position_name)
  222. {
  223. $shortposition = preg_replace("/(\-[a-f])$/i", "", $position_name);
  224. return $this->positionInfo[$shortposition];
  225. }
  226. public function getPositions()
  227. {
  228. return $this->positions;
  229. }
  230. public function parsePosition($position, $pattern)
  231. {
  232. if (null == $pattern)
  233. {
  234. $pattern = "(-)?";
  235. }
  236. $filtered_positions = array();
  237. if (count($this->positions) > 0)
  238. {
  239. $regpat = "/^" . $position . $pattern . "/";
  240. foreach ($this->positions as $key => $value)
  241. {
  242. if (preg_match($regpat, $value) == 1)
  243. {
  244. $filtered_positions[] = $value;
  245. }
  246. }
  247. }
  248. return $filtered_positions;
  249. }
  250. protected function _getParams(&$gantry)
  251. {
  252. $this->_params_content = "";
  253. $this->_loadParamsContent($gantry);
  254. $data = array();
  255. $params = $this->options_xml->xpath('//form//field|//form//fields[@default]|//form//fields[@value]');
  256. foreach ($params as $param)
  257. {
  258. $attrs = $param->xpath('ancestor::fields[@name][not(@ignore-group=\'true\')]/@name');
  259. $groups = array_map('strval', $attrs ? $attrs : array());
  260. $groups = array_flip($groups);
  261. if (array_key_exists('template-options', $groups)) unset($groups['template-options']);
  262. $groups = array_flip($groups);
  263. $prefix = '';
  264. foreach ($groups as $parent)
  265. {
  266. $prefix .= $parent . "-";
  267. }
  268. $param_name = $prefix . $param['name'];
  269. $this->_getParamInfo($gantry, $param_name, $param, $data);
  270. }
  271. $this->params = $data;
  272. return $data;
  273. }
  274. /**
  275. * Loads the params.ini content
  276. *
  277. * @param $gantry
  278. *
  279. * @return void
  280. */
  281. protected function _loadParamsContent(&$gantry)
  282. {
  283. if ($gantry->isAdmin()) {
  284. $styleId = JRequest::getInt('id', 0);
  285. if ($styleId == 0) {
  286. $template = self::getMasterTemplateStyleByName($gantry->templateName);
  287. $styleId = $template->id;
  288. }
  289. } else {
  290. // TODO: get style ID from front end
  291. $site = JFactory::getApplication();
  292. $template = $site->getTemplate(true);
  293. $styleId = $template->id;
  294. }
  295. $return = false;
  296. if ($gantry->isAdmin()) {
  297. JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_templates/tables');
  298. $table = JTable::getInstance('Style', 'TemplatesTable', array());
  299. // Attempt to load the row.
  300. $return = $table->load($styleId);
  301. $site = JFactory::getApplication();
  302. $registry = new GantryRegistry;
  303. $registry->loadString($table->params);
  304. if ($registry->get('master') != 'true') {
  305. $return = $table->load($registry->get('master'));
  306. $registry = new GantryRegistry;
  307. $registry->loadString($table->params);
  308. }
  309. // Check for a table object error.
  310. if ($return === false && $table->getError()) {
  311. $this->setError($table->getError());
  312. return false;
  313. }
  314. $this->_params_content = $table->params;
  315. } else {
  316. $site = JFactory::getApplication();
  317. $template = self::getTemplateById($site->getTemplate(true)->id);
  318. $master = $template->params->get('master', 'true');
  319. if ($master !== 'true') {
  320. $master_params = self::getTemplateParams((int)$master);
  321. $registry = &$master_params;
  322. $this->masterParams = $master_params->toString();
  323. $master_params->merge($template->params);
  324. $this->_params_content = $master_params->toString();
  325. } else {
  326. $registry = &$template->params;
  327. $this->_params_content = $template->params->toString();
  328. $this->masterParams = $this->_params_content;
  329. }
  330. }
  331. $this->_params_reg = $registry;
  332. return true;
  333. }
  334. public function getParamsHash()
  335. {
  336. return md5($this->_params_content);
  337. }
  338. public function getMasterParamsHash()
  339. {
  340. return md5($this->masterParams);
  341. }
  342. protected function _getParamInfo(&$gantry, $param_name, &$param, &$data, $prefix = "")
  343. {
  344. $attributes = array();
  345. foreach ($param->attributes() as $key => $val)
  346. {
  347. $attributes[$key] = (string)$val;
  348. }
  349. $full_param_name = $prefix . $param_name;
  350. $default = (array_key_exists('default', $attributes)) ? $attributes['default'] : false;
  351. $value = $this->_getParamValue($full_param_name, $default);
  352. // run the preload of the processors
  353. $this->_runProcessorPreLoad($gantry, $full_param_name, $param, $data);
  354. $data[$full_param_name] = array(
  355. 'name' => $prefix . $full_param_name,
  356. 'type' => $attributes['type'],
  357. 'default' => $default,
  358. 'value' => $value,
  359. 'sitebase' => $value,
  360. 'setbyurl' => (array_key_exists('setbyurl', $attributes)) ? ($attributes['setbyurl'] == 'true') ? true : false : false,
  361. 'setbycookie' => (array_key_exists('setbycookie', $attributes)) ? ($attributes['setbycookie'] == 'true') ? true : false : false,
  362. 'setbysession' => (array_key_exists('setbysession', $attributes)) ? ($attributes['setbysession'] == 'true') ? true : false : false,
  363. 'setincookie' => (array_key_exists('setbycookie', $attributes)) ? ($attributes['setbycookie'] == 'true') ? true : false : false,
  364. 'setinsession' => (array_key_exists('setinsession', $attributes)) ? ($attributes['setinsession'] == 'true') ? true : false : false,
  365. 'setinoverride' => (array_key_exists('setinoverride', $attributes)) ? ($attributes['setinoverride'] == 'true') ? true : false : true,
  366. 'setbyoverride' => (array_key_exists('setbyoverride', $attributes)) ? ($attributes['setbyoverride'] == 'true') ? true : false : true,
  367. 'isbodyclass' => (array_key_exists('isbodyclass', $attributes)) ? ($attributes['isbodyclass'] == 'true') ? true : false : false,
  368. 'setclassbytag' => (array_key_exists('setclassbytag', $attributes)) ? $attributes['setclassbytag'] : false,
  369. 'setby' => 'default',
  370. 'attributes' => &$attributes
  371. );
  372. if ($data[$full_param_name]['setbyurl']) $gantry->_setbyurl[] = $full_param_name;
  373. if ($data[$full_param_name]['setbysession']) $gantry->_setbysession[] = $full_param_name;
  374. if ($data[$full_param_name]['setbycookie']) $gantry->_setbycookie[] = $full_param_name;
  375. if ($data[$full_param_name]['setinsession']) $gantry->_setinsession[] = $full_param_name;
  376. if ($data[$full_param_name]['setincookie']) $gantry->_setincookie[] = $full_param_name;
  377. if ($data[$full_param_name]['setinoverride'])
  378. {
  379. $gantry->setinoverride[] = $full_param_name;
  380. }
  381. else
  382. {
  383. $gantry->dontsetinoverride[] = $full_param_name;
  384. }
  385. if ($data[$full_param_name]['setbyoverride']) $gantry->_setbyoverride[] = $full_param_name;
  386. if ($data[$full_param_name]['isbodyclass']) $gantry->_bodyclasses[] = $full_param_name;
  387. if ($data[$full_param_name]['setclassbytag']) $gantry->_classesbytag[$data[$full_param_name]['setclassbytag']][] = $full_param_name;
  388. $this->_runProcessorPostLoad($gantry, $full_param_name, $param, $data);
  389. }
  390. protected function _getParamValue($param_name, $default)
  391. {
  392. $original_param_name =$param_name;
  393. $value = $default;
  394. $exists = false;
  395. if ($this->_params_reg->exists($param_name)) $exists = true;
  396. // try forward dashes
  397. if (!$exists && strstr($param_name, '-') !== false)
  398. {
  399. $param_name_parts = explode('-', $param_name);
  400. $parts_count = count($param_name_parts);
  401. $dots = array();
  402. for ($i = 0; $i < $parts_count; $i++)
  403. {
  404. array_unshift($dots, array_pop($param_name_parts));
  405. $param_name = implode('-', $param_name_parts) . '.' . implode('.', $dots);
  406. if ($this->_params_reg->exists($param_name))
  407. {
  408. $exists = true;
  409. break;
  410. }
  411. }
  412. // try backwards dashes
  413. if (!$exists)
  414. {
  415. $param_name = $original_param_name;
  416. $param_name_parts = explode('-', $param_name);
  417. $parts_count = count($param_name_parts);
  418. $dots = array();
  419. for ($i = 0; $i < $parts_count; $i++)
  420. {
  421. array_unshift($dots, array_pop($param_name_parts));
  422. $param_name = implode('.', $param_name_parts) . '.' . implode('-', $dots);
  423. if ($this->_params_reg->exists($param_name))
  424. {
  425. $exists = true;
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. if ($exists)
  432. $value = $this->_params_reg->get($param_name, $default);
  433. return $value;
  434. }
  435. /**
  436. * load the basic template info
  437. * @return void
  438. */
  439. protected function _getTemplateInfo()
  440. {
  441. if ($this->xml->name) $this->setName((string)$this->xml->name);
  442. if ($this->xml->version) $this->setVersion((string)$this->xml->version);
  443. if ($this->xml->creationDate) $this->setCreationDate((string)$this->xml->creationDate);
  444. if ($this->xml->author) $this->setAuthor((string)$this->xml->author);
  445. if ($this->xml->authorUrl) $this->setAuthorUrl((string)$this->xml->authorUrl);
  446. if ($this->xml->authorEmail) $this->setAuthorEmail((string)$this->xml->authorEmail);
  447. if ($this->xml->copyright) $this->setCopyright((string)$this->xml->copyright);
  448. if ($this->xml->license) $this->setLicense((string)$this->xml->license);
  449. if ($this->xml->description) $this->setDescription((string)$this->xml->description);
  450. if ($this->xml->legacycss) $this->setLegacycss((string)$this->xml->legacycss);
  451. if ($this->xml->gridcss) $this->setGridcss((string)$this->xml->gridcss);
  452. }
  453. public static function getTemplateParams($id)
  454. {
  455. $templates = self::getAllTemplates();
  456. if (array_key_exists($id, $templates))
  457. return $templates[$id]->params;
  458. else
  459. return new GantryRegistry;
  460. }
  461. public static function getTemplateById($id = 0)
  462. {
  463. $templates = self::getAllTemplates();
  464. $template = $templates[$id];
  465. return $template;
  466. }
  467. public static function getAllTemplates()
  468. {
  469. $cache = JFactory::getCache('com_templates', '');
  470. $tag = JFactory::getLanguage()->getTag();
  471. $templates = $cache->get('gantrytemplates0' . $tag);
  472. if ($templates === false)
  473. {
  474. // Load styles
  475. $db = JFactory::getDbo();
  476. $query = $db->getQuery(true);
  477. $query->select('id, home, template, s.params');
  478. $query->from('#__template_styles as s');
  479. $query->where('s.client_id = 0');
  480. $query->where('e.enabled = 1');
  481. $query->leftJoin('#__extensions as e ON e.element=s.template AND e.type='.$db->quote('template').' AND e.client_id=s.client_id');
  482. $db->setQuery($query);
  483. $templates = $db->loadObjectList('id');
  484. foreach ($templates as &$template)
  485. {
  486. $registry = new GantryRegistry;
  487. $registry->loadString($template->params);
  488. $template->params = $registry;
  489. }
  490. $cache->store($templates, 'gantrytemplates0' . $tag);
  491. }
  492. return $templates;
  493. }
  494. public static function getMasterTemplateStyleByName($template_name)
  495. {
  496. $templates = self::getAllTemplates();
  497. foreach ($templates as $template)
  498. {
  499. if ($template->template == $template_name && $template->params->get('master') == 'true')
  500. {
  501. return $template;
  502. }
  503. }
  504. return false;
  505. }
  506. /**
  507. * Gets the version for gantry
  508. * @access public
  509. * @return string
  510. */
  511. public function getVersion()
  512. {
  513. return $this->version;
  514. }
  515. /**
  516. * Sets the version for gantry
  517. * @access public
  518. * @param string $version
  519. */
  520. protected function setVersion($version)
  521. {
  522. $this->version = $version;
  523. }
  524. /**
  525. * Gets the name for gantry
  526. * @access public
  527. * @return string
  528. */
  529. public function getName()
  530. {
  531. return $this->name;
  532. }
  533. /**
  534. * Sets the name for gantry
  535. * @access public
  536. * @param string $name
  537. */
  538. protected function setName($name)
  539. {
  540. $this->name = $name;
  541. }
  542. /**
  543. * Gets the fullname for gantry
  544. * @access public
  545. * @return string
  546. */
  547. public function getFullname()
  548. {
  549. return $this->fullname;
  550. }
  551. /**
  552. * Sets the fullname for gantry
  553. * @access public
  554. * @param string $fullname
  555. */
  556. protected function setFullname($fullname)
  557. {
  558. $this->fullname = $fullname;
  559. }
  560. /**
  561. * Gets the creationDate for gantry
  562. * @access public
  563. * @return string
  564. */
  565. public function getCreationDate()
  566. {
  567. return $this->creationDate;
  568. }
  569. /**
  570. * Sets the creationDate for gantry
  571. * @access public
  572. * @param string $creationDate
  573. */
  574. protected function setCreationDate($creationDate)
  575. {
  576. $this->creationDate = $creationDate;
  577. }
  578. /**
  579. * Gets the authorEmail for gantry
  580. * @access public
  581. * @return string
  582. */
  583. public function getAuthorEmail()
  584. {
  585. return $this->authorEmail;
  586. }
  587. /**
  588. * Sets the authorEmail for gantry
  589. * @access public
  590. * @param string $authorEmail
  591. */
  592. protected function setAuthorEmail($authorEmail)
  593. {
  594. $this->authorEmail = $authorEmail;
  595. }
  596. /**
  597. * Gets the authorUrl for gantry
  598. * @access public
  599. * @return string
  600. */
  601. public function getAuthorUrl()
  602. {
  603. return $this->authorUrl;
  604. }
  605. /**
  606. * Sets the authorUrl for gantry
  607. * @access public
  608. * @param string $authorUrl
  609. */
  610. protected function setAuthorUrl($authorUrl)
  611. {
  612. $this->authorUrl = $authorUrl;
  613. }
  614. /**
  615. * Gets the description for gantry
  616. * @access public
  617. * @return string
  618. */
  619. public function getDescription()
  620. {
  621. return $this->description;
  622. }
  623. /**
  624. * Sets the description for gantry
  625. * @access public
  626. * @param string $description
  627. */
  628. protected function setDescription($description)
  629. {
  630. $this->description = $description;
  631. }
  632. /**
  633. * Gets the copyright for gantry
  634. * @access public
  635. * @return string
  636. */
  637. public function getCopyright()
  638. {
  639. return $this->copyright;
  640. }
  641. /**
  642. * Sets the copyright for gantry
  643. * @access public
  644. * @param string $copyright
  645. */
  646. protected function setCopyright($copyright)
  647. {
  648. $this->copyright = $copyright;
  649. }
  650. /**
  651. * Gets the license for gantry
  652. * @access public
  653. * @return string
  654. */
  655. public function getLicense()
  656. {
  657. return $this->license;
  658. }
  659. /**
  660. * Sets the license for gantry
  661. * @access public
  662. * @param string $license
  663. */
  664. protected function setLicense($license)
  665. {
  666. $this->license = $license;
  667. }
  668. /**
  669. * Gets the author for gantry
  670. * @access public
  671. * @return string
  672. */
  673. public function getAuthor()
  674. {
  675. return $this->author;
  676. }
  677. /**
  678. * Sets the author for gantry
  679. * @access public
  680. * @param string $author
  681. */
  682. protected function setAuthor($author)
  683. {
  684. $this->author = $author;
  685. }
  686. /**
  687. * @param $legacycss
  688. */
  689. public function setLegacycss($legacycss)
  690. {
  691. $set = true;
  692. if ($legacycss == 'false'){
  693. $this->legacycss = false;
  694. }
  695. }
  696. /**
  697. * @return bool
  698. */
  699. public function getLegacycss()
  700. {
  701. return $this->legacycss;
  702. }
  703. /**
  704. * @param boolean $gridcss
  705. */
  706. public function setGridcss($gridcss)
  707. {
  708. $set = true;
  709. if ($gridcss == 'false'){
  710. $this->gridcss = false;
  711. }
  712. }
  713. /**
  714. * @return boolean
  715. */
  716. public function getGridcss()
  717. {
  718. return $this->gridcss;
  719. }
  720. }