PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/gantry/core/gantry.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 1895 lines | 1391 code | 268 blank | 236 comment | 294 complexity | ed934ebee51ddcd01e7d4298604969e7 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT

Large files files are truncated, but you can click here to view the full file

  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.gantrytemplate');
  15. gantry_import('core.gantryini');
  16. gantry_import('core.gantrypositions');
  17. gantry_import('core.gantrystylelink');
  18. gantry_import('core.gantryplatform');
  19. gantry_import('core.gantrybrowser');
  20. /**
  21. * This is the base class for the Gantry framework. It is the primary mechanisim for template definition
  22. *
  23. * @package gantry
  24. * @subpackage core
  25. */
  26. class Gantry {
  27. static $instances = array();
  28. public static function getInstance($template_name)
  29. {
  30. if (!array_key_exists($template_name, self::$instances)) {
  31. self::$instances[$template_name] = new Gantry($template_name);
  32. }
  33. return self::$instances[$template_name];
  34. }
  35. // Cacheable
  36. /**
  37. *
  38. */
  39. var $basePath;
  40. var $baseUrl;
  41. var $templateName;
  42. var $templateUrl;
  43. var $templatePath;
  44. var $templateId;
  45. var $layoutPath;
  46. var $gantryPath;
  47. var $gantryUrl;
  48. var $layoutSchemas = array();
  49. var $mainbodySchemas = array();
  50. var $pushPullSchemas = array();
  51. var $mainbodySchemasCombos = array();
  52. var $default_grid = 12;
  53. var $presets = array();
  54. var $originalPresets = array();
  55. var $customPresets = array();
  56. var $dontsetinoverride = array();
  57. var $defaultMenuItem;
  58. var $currentMenuItem;
  59. var $currentMenuTree;
  60. var $template_prefix;
  61. var $custom_dir;
  62. var $custom_presets_file;
  63. var $positions = array();
  64. var $altindex = false;
  65. var $platform;
  66. // Not cacheable
  67. var $document;
  68. var $browser;
  69. var $language;
  70. var $session;
  71. var $currentUrl;
  72. // Private Vars
  73. /**#@+
  74. * @access private
  75. */
  76. // cacheable privates
  77. var $_template;
  78. var $_aliases = array();
  79. var $_preset_names = array();
  80. var $_param_names = array();
  81. var $_base_params_checksum = null;
  82. var $_setbyurl = array();
  83. var $_setbycookie = array();
  84. var $_setbysession = array();
  85. var $_setinsession = array();
  86. var $_setincookie = array();
  87. var $_setinoverride = array();
  88. var $_setbyoverride = array();
  89. var $_features = array();
  90. var $_ajaxmodels = array();
  91. var $_adminajaxmodels = array();
  92. var $_layouts = array();
  93. var $_bodyclasses = array();
  94. var $_classesbytag = array();
  95. var $_ignoreQueryParams = array('reset-settings');
  96. var $_config_vars = array(
  97. 'layoutschemas'=>'layoutSchemas',
  98. 'mainbodyschemas'=>'mainbodySchemas',
  99. 'mainbodyschemascombos' => 'mainbodySchemasCombos',
  100. 'pushpullschemas'=>'pushPullSchemas',
  101. 'presets'=>'presets',
  102. 'browser_params' => '_browser_params',
  103. 'grid'=>'grid'
  104. );
  105. var $_working_params;
  106. // non cachable privates
  107. var $_bodyId = null;
  108. var $_browser_params = array();
  109. var $_menu_item_params = array();
  110. var $_scripts = array();
  111. var $_styles = array();
  112. var $_styles_available = array();
  113. var $_tmp_vars = array();
  114. var $adminElements = array();
  115. var $_params_hash;
  116. var $_featuresPosition;
  117. var $_featuresInstances = array();
  118. var $_parts_cache = true;
  119. var $_parts_to_cache = array('_featuresPosition', '_styles_available');
  120. var $_parts_cached = false;
  121. var $_browser_hash;
  122. var $_domready_script = '';
  123. var $_loadevent_script = '';
  124. /**#@-*/
  125. var $__cacheables = array(
  126. 'basePath',
  127. 'baseUrl',
  128. 'templateName',
  129. 'templateUrl',
  130. 'templatePath',
  131. 'layoutPath',
  132. 'gantryPath',
  133. 'gantryUrl',
  134. 'layoutSchemas',
  135. 'mainbodySchemas',
  136. 'pushPullSchemas',
  137. 'mainbodySchemasCombos',
  138. 'default_grid',
  139. 'presets',
  140. 'originalPresets',
  141. 'customPresets',
  142. 'dontsetinoverride',
  143. 'defaultMenuItem',
  144. 'currentMenuItem',
  145. 'currentMenuTree',
  146. 'template_prefix',
  147. 'custom_dir',
  148. 'custom_presets_file',
  149. 'positions',
  150. '_template',
  151. '_aliases',
  152. '_preset_names',
  153. '_param_names',
  154. '_base_params_checksum',
  155. '_setbyurl',
  156. '_setbycookie',
  157. '_setbysession',
  158. '_setinsession',
  159. '_setincookie',
  160. '_setinoverride',
  161. '_setbyoverride',
  162. '_features',
  163. '_ajaxmodels',
  164. '_adminajaxmodels',
  165. '_layouts',
  166. '_bodyclasses',
  167. '_classesbytag',
  168. '_ignoreQueryParams',
  169. '_config_vars',
  170. '_working_params',
  171. 'platform'
  172. );
  173. function __sleep() {
  174. return $this->__cacheables;
  175. }
  176. function __wakeup() {
  177. // set the GRID_SYSTEM define;
  178. if (!defined('GRID_SYSTEM')) {
  179. define ('GRID_SYSTEM',$this->get('grid_system',$this->default_grid));
  180. }
  181. }
  182. /**
  183. * Constructor
  184. * @return void
  185. */
  186. function Gantry($template_name = null) {
  187. // load the base gantry path
  188. $this->gantryPath = realpath(dirname( __FILE__ ).DS."..");
  189. // set the base class vars
  190. $doc =& JFactory::getDocument();
  191. $this->document =& $doc;
  192. $this->browser = new GantryBrowser();
  193. $this->platform = new GantryPlatform();
  194. $this->basePath = JPATH_ROOT;
  195. if ($template_name == null){
  196. $this->templateName = $this->_getCurrentTemplate();
  197. }
  198. else {
  199. $this->templateName = $template_name;
  200. }
  201. $this->templatePath = JPATH_ROOT.DS.'templates'.DS.$this->templateName;
  202. $this->layoutPath = $this->templatePath.DS.'html'.DS.'layouts.php';
  203. $this->custom_dir = $this->templatePath.DS.'custom';
  204. $this->custom_presets_file = $this->custom_dir.DS.'presets.ini';
  205. $this->baseUrl = JURI::root(true)."/";
  206. $this->templateUrl = $this->baseUrl.'templates'."/".$this->templateName;
  207. if (version_compare( JVERSION, '1.5', '>=') && version_compare(JVERSION, '1.6', '<')) {
  208. $this->gantryUrl = $this->baseUrl.'components/com_gantry';
  209. }
  210. else if (version_compare(JVERSION, '1.6', '>=')) {
  211. $this->gantryUrl = $this->baseUrl.'libraries/gantry';
  212. }
  213. $this->defaultMenuItem = $this->_getDefaultMenuItem();
  214. $this->currentMenuItem = $this->defaultMenuItem;
  215. $this->_loadConfig();
  216. // Load up the template details
  217. $this->_template = new GantryTemplate();
  218. $this->_template->init($this);
  219. $this->_base_params_checksum = $this->_template->getMasterParamsHash();
  220. // Put a base copy of the saved params in the working params
  221. $this->_working_params = $this->_template->getParams();
  222. $this->_param_names = array_keys($this->_template->getParams());
  223. $this->template_prefix = $this->_working_params['template_prefix']['value'];
  224. // set the GRID_SYSTEM define;
  225. if (!defined('GRID_SYSTEM')) {
  226. define ('GRID_SYSTEM',$this->get('grid_system',$this->default_grid));
  227. }
  228. // process the presets
  229. if (!empty($this->presets)) {
  230. // check for custom presets
  231. $this->_customPresets();
  232. $this->_preset_names = array_keys($this->presets);
  233. //$wp_keys = array_keys($this->_template->params);
  234. //$this->_param_names = array_diff($wp_keys, $this->_preset_names);
  235. }
  236. $this->_loadLayouts();
  237. $this->_loadFeatures();
  238. $this->_loadAjaxModels();
  239. $this->_loadAdminAjaxModels();
  240. $this->_loadStyles();
  241. //$this->_checkAjaxTool();
  242. //$this->_checkLanguageFiles();
  243. // set up the positions object for all gird systems defined
  244. foreach(array_keys($this->mainbodySchemasCombos) as $grid){
  245. $this->positions[$grid] = GantryPositions::getInstance($grid);
  246. }
  247. // add GRID_SYSTEM class to body
  248. $this->addBodyClass("col".GRID_SYSTEM);
  249. }
  250. function adminInit() {
  251. $this->browser = new GantryBrowser();
  252. $this->_browser_hash = md5(serialize($this->browser));
  253. $this->platform = new GantryPlatform();
  254. $doc =& JFactory::getDocument();
  255. $this->document =& $doc;
  256. }
  257. /**
  258. * Initializer.
  259. * This should run when gantry is run from the front end in order and before the template file to
  260. * populate all user session level data
  261. * @return void
  262. */
  263. function init() {
  264. if (defined('GANTRY_INIT')) {
  265. return;
  266. }
  267. // Run the admin init
  268. if ($this->isAdmin()) {
  269. $this->adminInit();
  270. return;
  271. }
  272. define('GANTRY_INIT', "GANTRY_INIT");
  273. $cache = GantryCache::getInstance();
  274. // set the GRID_SYSTEM define;
  275. if (!defined('GRID_SYSTEM')) {
  276. define ('GRID_SYSTEM',$this->get('grid_system',$this->default_grid));
  277. }
  278. // Set the main class vars to match the call
  279. JHTML::_('behavior.mootools');
  280. $doc =& JFactory::getDocument();
  281. $this->document =& $doc;
  282. $this->language = $doc->language;
  283. $this->session =& JFactory::getSession();
  284. $this->baseUrl = JURI::root(true) . "/";
  285. $uri = JURI::getInstance();
  286. $this->currentUrl = $uri->toString();
  287. $this->templateUrl = $this->baseUrl.'templates'."/".$this->templateName;
  288. if (version_compare( JVERSION, '1.5', '>=') && version_compare(JVERSION, '1.6', '<')) {
  289. $this->gantryUrl = $this->baseUrl.'components/com_gantry';
  290. }
  291. else if (version_compare(JVERSION, '1.6', '>=')) {
  292. $this->gantryUrl = $this->baseUrl.'libraries/gantry';
  293. }
  294. // use any menu item level overrides
  295. $menus = &JSite::getMenu();
  296. $menu = $menus->getActive();
  297. $this->currentMenuItem = ($menu != null)?$menu->id : null;
  298. $this->currentMenuTree = ($menu != null)?$menu->tree: array();
  299. // Populate all the params for the session
  300. $this->_populateParams();
  301. $this->browser = new GantryBrowser();
  302. $this->_browser_hash = md5(serialize($this->browser));
  303. $this->platform = new GantryPlatform();
  304. $this->_loadBrowserConfig();
  305. }
  306. function initTemplate(){
  307. $cache = GantryCache::getInstance();
  308. // Init all features
  309. foreach ($this->getFeatures() as $feature) {
  310. $feature_instance = $this->_getFeature($feature);
  311. if ($feature_instance->isEnabled() && method_exists($feature_instance, 'init')) {
  312. $feature_instance->init();
  313. }
  314. }
  315. if (false !== ($parts = $cache->get($this->cacheKey('parts')))) {
  316. $this->_parts_cached = true;
  317. foreach ($parts as $part => $value) {
  318. $this->$part = $value;
  319. }
  320. }
  321. if ($this->_template->getGridcss()) {
  322. //add correct grid system css
  323. $this->addStyle('grid-' . GRID_SYSTEM . '.css', 5);
  324. }
  325. if ($this->_template->getLegacycss()) {
  326. //add default gantry stylesheet
  327. $this->addStyle('gantry.css', 5);
  328. $this->addStyle('joomla.css', 5);
  329. }
  330. }
  331. function adminFinalize()
  332. {
  333. ksort($this->_styles);
  334. foreach ($this->_styles as $priorities) {
  335. foreach ($priorities as $css_file) {
  336. $this->document->addStyleSheet($css_file->url);
  337. }
  338. }
  339. foreach ($this->_scripts as $js_file) {
  340. $this->document->addScript($js_file);
  341. }
  342. $this->renderCombinesInlines();
  343. }
  344. function renderCombinesInlines(){
  345. $lnEnd = "\12";
  346. $tab = "\11";
  347. $tagEnd = ' />';
  348. $strHtml = '';
  349. // Generate domready script
  350. if (isset($this->_domready_script) && count($this->_domready_script)) {
  351. $strHtml .= 'window.addEvent(\'domready\', function() {' . $this->_domready_script . $lnEnd . '});'. $lnEnd;
  352. }
  353. // Generate load script
  354. if (isset($this->_loadevent_script) && count($this->_loadevent_script)) {
  355. $strHtml .= 'window.addEvent(\'load\', function() {' . $this->_loadevent_script . $lnEnd . '});'. $lnEnd;
  356. }
  357. $this->document->addScriptDeclaration($strHtml);
  358. }
  359. function finalize()
  360. {
  361. if (!defined('GANTRY_FINALIZED')) {
  362. // Run the admin init
  363. if ($this->isAdmin()) {
  364. $this->adminFinalize();
  365. return;
  366. }
  367. gantry_import('core.params.overrides.gantrycookieparamoverride');
  368. gantry_import('core.params.overrides.gantrysessionparamoverride');
  369. $cache = GantryCache::getInstance();
  370. if (!$this->_parts_cached) {
  371. $parts_cache = array();
  372. foreach ($this->_parts_to_cache as $part) {
  373. $parts_cache[$part] = $this->$part;
  374. }
  375. if ($parts_cache) {
  376. $cache->set($this->cacheKey('parts'), $parts_cache);
  377. }
  378. }
  379. // Finalize all features
  380. foreach ($this->getFeatures() as $feature) {
  381. $feature_instance = $this->_getFeature($feature);
  382. if ($feature_instance->isEnabled() && method_exists($feature_instance , 'finalize')) {
  383. $feature_instance->finalize();
  384. }
  385. }
  386. $this->renderCombinesInlines();
  387. if (isset($_REQUEST['reset-settings'])) {
  388. GantrySessionParamOverride::clean();
  389. GantryCookieParamOverride::clean();
  390. }
  391. else {
  392. GantrySessionParamOverride::store();
  393. GantryCookieParamOverride::store();
  394. }
  395. if ($this->get("gzipper-enabled",false)) {
  396. gantry_import('core.gantrygzipper');
  397. GantryGZipper::processCSSFiles();
  398. GantryGZipper::processJsFiles();
  399. }
  400. else {
  401. ksort($this->_styles);
  402. foreach($this->_styles as $priorities){
  403. foreach($priorities as $css_file) {
  404. $this->document->addStyleSheet($css_file->url);
  405. }
  406. }
  407. foreach($this->_scripts as $js_file){
  408. $this->document->addScript($js_file);
  409. }
  410. }
  411. define('GANTRY_FINALIZED', true);
  412. }
  413. if ($this->altindex !== false) {
  414. $contents = ob_get_contents();
  415. ob_end_clean();
  416. ob_start();
  417. echo $this->altindex;
  418. }
  419. }
  420. function isAdmin(){
  421. $app =& JFactory::getApplication();
  422. return $app->isAdmin();
  423. }
  424. function get($param = false, $default = "") {
  425. if (array_key_exists($param, $this->_working_params)) $value = $this->_working_params[$param]['value'];
  426. else $value = $default;
  427. return $value;
  428. }
  429. function getDefault($param = false) {
  430. $value = "";
  431. if (array_key_exists($param, $this->_working_params)) $value = $this->_working_params[$param]['default'];
  432. return $value;
  433. }
  434. function getFeatures(){
  435. return array_keys($this->_features);
  436. }
  437. function set($param, $value=false) {
  438. $return = false;
  439. if (array_key_exists($param, $this->_working_params)){
  440. $this->_working_params[$param]['value'] = $value;
  441. $return = true;
  442. }
  443. return $return;
  444. }
  445. function getAjaxModel($model_name, $admin=false){
  446. $model_path = false;
  447. if ($admin) {
  448. if (array_key_exists($model_name, $this->_adminajaxmodels)){
  449. $model_path = $this->_adminajaxmodels[$model_name];
  450. }
  451. }
  452. else {
  453. if (array_key_exists($model_name, $this->_ajaxmodels)){
  454. $model_path = $this->_ajaxmodels[$model_name];
  455. }
  456. }
  457. return $model_path;
  458. }
  459. function getPositions($position = null, $pattern = null) {
  460. if ($position != null) {
  461. $positions = $this->_template->parsePosition($position, $pattern);
  462. return $positions;
  463. }
  464. return $this->_template->getPositions();
  465. }
  466. function getUniquePositions() {
  467. return $this->_template->getUniquePositions();
  468. }
  469. function getPositionInfo($position_name) {
  470. return $this->_template->getPositionInfo($position_name);
  471. }
  472. function getAjaxUrl(){
  473. $url = $this->baseUrl;
  474. $component_path = 'index.php?option=com_gantry&task=ajax&format=raw&template='.$this->templateName;
  475. if ($this->isAdmin()){
  476. $url .= 'administrator/'.$component_path;
  477. }
  478. else{
  479. $url .= $component_path;
  480. }
  481. return $url;
  482. }
  483. function getParams($prefix=null,$remove_prefix=false) {
  484. if (null==$prefix){
  485. return $this->_working_params;
  486. }
  487. $params=array();
  488. foreach ($this->_working_params as $param_name => $param_value){
  489. $matches = array();
  490. if (preg_match("/^".$prefix."-(.*)$/", $param_name, $matches)){
  491. if ($remove_prefix){
  492. $param_name = $matches[1];
  493. }
  494. $params[$param_name] = $param_value;
  495. }
  496. }
  497. return $params;
  498. }
  499. /**
  500. * Gets the current URL and query string and can ready it for more query string vars
  501. * @param array $ignore
  502. * @param bool $qs_preped
  503. * @return mixed|string
  504. */
  505. function getCurrentUrl($ignore=array()){
  506. gantry_import('core.utilities.gantryurl');
  507. $url = GantryUrl::explode($this->currentUrl);
  508. if (!empty($ignore) && array_key_exists('query_params', $url)) {
  509. foreach ($ignore as $k) {
  510. if (array_key_exists($k, $url['query_params'])) unset($url['query_params'][$k]);
  511. }
  512. }
  513. return GantryUrl::implode($url);
  514. }
  515. function addQueryStringParams($url, $params = array()) {
  516. gantry_import('core.utilities.gantryurl');
  517. return GantryUrl::updateParams($url, $params);
  518. }
  519. /**
  520. * @param $positionStub
  521. * @param $pattern
  522. * @return int
  523. */
  524. function countModules($positionStub, $pattern = null)
  525. {
  526. if (defined('GANTRY_FINALIZED')) return 0;
  527. $count = 0;
  528. if (array_key_exists($positionStub, $this->_aliases)) {
  529. return $this->countModules($this->_aliases[$positionStub]);
  530. }
  531. $positions = $this->getPositions($positionStub, $pattern);
  532. foreach ($positions as $position) {
  533. if (!$this->isAdmin()) {
  534. if ($this->document->countModules($position) || count($this->_getFeaturesForPosition($position)) > 0) $count++;
  535. }
  536. else {
  537. if ($this->_adminCountModules($position) || count($this->_getFeaturesForPosition($position)) > 0) $count++;
  538. }
  539. }
  540. return $count;
  541. }
  542. /**
  543. * @param $positionStub
  544. * @param $pattern
  545. * @return int
  546. */
  547. function countSubPositionModules($position, $pattern = null)
  548. {
  549. if (defined('GANTRY_FINALIZED')) return 0;
  550. $count = 0;
  551. if (array_key_exists($position, $this->_aliases)) {
  552. return $this->countSubPositionModules($this->_aliases[$position]);
  553. }
  554. if (!$this->isAdmin()) {
  555. if ($this->document->countModules($position) || count($this->_getFeaturesForPosition($position)) > 0)
  556. {
  557. $count += $this->document->countModules($position);
  558. $count += count($this->_getFeaturesForPosition($position));
  559. }
  560. }
  561. else {
  562. if ($this->_adminCountModules($position) || count($this->_getFeaturesForPosition($position)) > 0)
  563. {
  564. $count += $this->_adminCountModules($position);
  565. $count += count($this->_getFeaturesForPosition($position));
  566. }
  567. }
  568. return $count;
  569. }
  570. // wrapper for mainbody display
  571. function displayMainbody($bodyLayout = 'mainbody', $sidebarLayout = 'sidebar', $sidebarChrome = 'standard', $contentTopLayout = 'standard', $contentTopChrome = 'standard', $contentBottomLayout = 'standard', $contentBottomChrome = 'standard', $gridsize = null) {
  572. if (defined('GANTRY_FINALIZED')) return;
  573. gantry_import('core.renderers.gantrymainbodyrenderer');
  574. return GantryMainBodyRenderer::display($bodyLayout, $sidebarLayout, $sidebarChrome, $contentTopLayout, $contentTopChrome, $contentBottomLayout, $contentBottomChrome, $gridsize);
  575. }
  576. // wrapper for mainbody display
  577. function displayOrderedMainbody($bodyLayout = 'mainbody', $sidebarLayout = 'sidebar', $sidebarChrome = 'standard', $contentTopLayout = 'standard', $contentTopChrome = 'standard', $contentBottomLayout = 'standard', $contentBottomChrome = 'standard', $gridsize = null) {
  578. if (defined('GANTRY_FINALIZED')) return;
  579. gantry_import('core.renderers.gantryorderedmainbodyrenderer');
  580. return GantryOrderedMainBodyRenderer::display($bodyLayout, $sidebarLayout, $sidebarChrome, $contentTopLayout, $contentTopChrome, $contentBottomLayout, $contentBottomChrome, $gridsize);
  581. }
  582. // wrapper for display modules
  583. function displayModules($positionStub, $layout = 'standard', $chrome = 'standard', $gridsize = GRID_SYSTEM, $pattern = null) {
  584. if (defined('GANTRY_FINALIZED')) return;
  585. gantry_import('core.renderers.gantrymodulesrenderer');
  586. return GantryModulesRenderer::display($positionStub, $layout, $chrome, $gridsize, $pattern);
  587. }
  588. // wrapper for display modules
  589. function displayFeature($feature, $layout = 'basic') {
  590. if (defined('GANTRY_FINALIZED')) return;
  591. gantry_import('core.renderers.gantryfeaturerenderer');
  592. return GantryFeatureRenderer::display($feature, $layout);
  593. }
  594. function addTemp($namespace, $varname, &$variable) {
  595. if (defined('GANTRY_FINALIZED')) return;
  596. $this->_tmp_vars[$namespace][$varname] = $variable;
  597. return;
  598. }
  599. function &retrieveTemp($namespace, $varname, $default = null){
  600. if (defined('GANTRY_FINALIZED')) return;
  601. if (!array_key_exists($namespace,$this->_tmp_vars) ||!array_key_exists($varname, $this->_tmp_vars[$namespace])){
  602. return $default;
  603. }
  604. return $this->_tmp_vars[$namespace][$varname];
  605. }
  606. function setBodyId($id = null){
  607. $this->_bodyId = $id;
  608. }
  609. function addBodyClass($class) {
  610. if (defined('GANTRY_FINALIZED')) return;
  611. $this->_bodyclasses[] = $class;
  612. }
  613. function addClassByTag($id , $class) {
  614. if (defined('GANTRY_FINALIZED')) return;
  615. $this->_classesbytag[$id][] = $class;
  616. }
  617. function displayHead() {
  618. if (defined('GANTRY_FINALIZED')) return;
  619. //stuff to output that is needed by joomla
  620. echo '<jdoc:include type="head" />';
  621. }
  622. function displayBodyTag() {
  623. if (defined('GANTRY_FINALIZED')) return;
  624. $body_classes = array();
  625. foreach ($this->_bodyclasses as $param) {
  626. $param_value = $this->get($param);
  627. if ($param_value != "") {
  628. $body_classes[] = strtolower(str_replace(" ","-",$param ."-".$param_value));
  629. } else {
  630. $body_classes[] = strtolower(str_replace(" ","-",$param));
  631. }
  632. }
  633. return $this->renderLayout('doc_body', array('classes'=>implode(" ", $body_classes),'id'=>$this->_bodyId));
  634. }
  635. function displayClassesByTag($tag) {
  636. if (defined('GANTRY_FINALIZED')) return;
  637. $tag_classes = array();
  638. $output = "";
  639. if (array_key_exists($tag,$this->_classesbytag)) {
  640. foreach ($this->_classesbytag[$tag] as $param) {
  641. $param_value = $this->get($param);
  642. if ($param_value != "") {
  643. $tag_classes[] = $param ."-".$param_value;
  644. } else {
  645. $tag_classes[] = $param;
  646. }
  647. }
  648. $output = 'class="'.implode(" ", $tag_classes).'"';
  649. }
  650. return $this->renderLayout('doc_tag', array('classes'=>implode(" ", $tag_classes)));
  651. }
  652. // debug function for body
  653. function debugMainbody($bodyLayout = 'debugmainbody', $sidebarLayout = 'sidebar', $sidebarChrome = 'standard', $grid = null) {
  654. gantry_import('core.renderers.gantrydebugmainbodyrenderer');
  655. return GantryDebugMainBodyRenderer::display($bodyLayout, $sidebarLayout, $sidebarChrome, $grid);
  656. }
  657. /* ------ Stylesheet Funcitons ----------- */
  658. function addStyle($file = '', $priority=10, $template_files_override = false) {
  659. if (is_array($file)) return $this->addStyles($file, $priority);
  660. $type = 'css';
  661. $template_path = $this->templatePath.DS .$type.DS;
  662. $template_url = $this->templateUrl.'/css/';
  663. $gantry_path = $this->gantryPath.DS.$type.DS;
  664. $gantry_url = $this->gantryUrl.'/css/';
  665. $gantry_first_paths = array(
  666. $gantry_url => $gantry_path,
  667. $template_url => $template_path
  668. );
  669. $out_files = array();
  670. $ext = substr($file, strrpos($file, '.'));
  671. $filename = basename($file, $ext);
  672. $base_file = basename($file);
  673. $override_file = $filename . "-override" . $ext;
  674. // get browser checks and remove base files
  675. $checks = $this->_getBrowserBasedChecks(basename($file));
  676. unset($checks[array_search($base_file,$checks)]);
  677. $override_checks = $this->_getBrowserBasedChecks(basename($override_file));
  678. unset($override_checks[array_search($override_file,$override_checks)]);
  679. // check to see if this is a full path file
  680. $dir = dirname($file);
  681. if ($dir != ".") {
  682. // Add full url directly to document
  683. if (preg_match('/^http/', $file)) {
  684. $link = new GantryStyleLink('url','',$file);
  685. $this->_styles[$priority][]=$link;
  686. return;
  687. }
  688. // process a url passed file and browser checks
  689. $url_path = $dir;
  690. $file_path = $this->_getFilePath($file);
  691. $file_parent_path = dirname($file_path);
  692. if (file_exists($file_parent_path) && is_dir($file_parent_path)) {
  693. $base_path = preg_replace("/\?(.*)/", '', $file_parent_path.DS.$base_file);
  694. // load the base file
  695. if (file_exists($base_path) && is_file($base_path) && is_readable($base_path)){
  696. $out_files[$base_path] = new GantryStyleLink('local',$base_path, $file);
  697. }
  698. foreach ($checks as $check) {
  699. $check_path = preg_replace("/\?(.*)/", '', $file_parent_path . DS . $check);
  700. $check_url_path = $url_path . "/" . $check;
  701. if (file_exists($check_path) && is_readable($check_path)) {
  702. $out_files[$check] = new GantryStyleLink('local',$check_path, $check_url_path);
  703. }
  704. }
  705. }
  706. }
  707. else {
  708. $base_override = false;
  709. $checks_override = array();
  710. // Look for an base override file in the template dir
  711. $template_base_override_file = $template_path.$override_file;
  712. if ($this->isStyleAvailable($template_base_override_file)) {
  713. $out_files[$template_base_override_file] = new GantryStyleLink('local',$template_base_override_file, $template_url.$override_file);
  714. $base_override = true;
  715. }
  716. // look for overrides for each of the browser checks
  717. foreach($override_checks as $check_index => $override_check) {
  718. $template_check_override = preg_replace("/\?(.*)/", '', $template_path.$override_check);
  719. $checks_override[$check_index] = false;
  720. if ($this->isStyleAvailable($template_check_override)){
  721. $checks_override[$check_index] = true;
  722. if ($base_override){
  723. $out_files[$template_check_override] = new GantryStyleLink('local',$template_check_override,$template_url.$override_check);
  724. }
  725. }
  726. }
  727. if (!$base_override){
  728. // Add the base files if there is no base -override
  729. foreach ($gantry_first_paths as $base_url => $path) {
  730. // Add the base file
  731. $base_path = preg_replace("/\?(.*)/", '', $path.$base_file);
  732. // load the base file
  733. if ($this->isStyleAvailable($base_path)){
  734. $outfile_key = ($template_files_override)? $base_file : $base_path;
  735. $out_files[$outfile_key] = new GantryStyleLink('local',$base_path,$base_url.$base_file);
  736. }
  737. // Add the browser checked files or its override
  738. foreach($checks as $check_index => $check) {
  739. // replace $check with the override if it exists
  740. if ($checks_override[$check_index]){
  741. $check = $override_checks[$check_index];
  742. }
  743. $check_path = preg_replace("/\?(.*)/", '', $path.$check);
  744. if ($this->isStyleAvailable($check_path)){
  745. $outfile_key = ($template_files_override)? $check : $check_path;
  746. $out_files[$outfile_key] = new GantryStyleLink('local',$check_path,$base_url.$check);
  747. }
  748. }
  749. }
  750. }
  751. }
  752. foreach ($out_files as $link) {
  753. $addit = true;
  754. foreach($this->_styles as $style_priority => $priority_links){
  755. $index = array_search($link, $priority_links);
  756. if ($index !== false){
  757. if ($priority < $style_priority){
  758. unset($this->_styles[$style_priority][$index]);
  759. }
  760. else {
  761. $addit = false;
  762. }
  763. }
  764. }
  765. if ($addit) {
  766. if(!defined('GANTRY_FINALIZED')){
  767. $this->_styles[$priority][] = $link;
  768. }
  769. else{
  770. $this->document->addStyleSheet($link->url);
  771. }
  772. }
  773. }
  774. //clean up styles
  775. foreach($this->_styles as $style_priority => $priority_links){
  776. if (count($priority_links) == 0){
  777. unset($this->_styles[$style_priority]);
  778. }
  779. }
  780. }
  781. function isStyleAvailable($path){
  782. if (isset($this->_styles_available[$path])){
  783. return true;
  784. }
  785. else if (file_exists($path) && is_file($path)){
  786. $this->_styles_available[$path] = $path;
  787. return true;
  788. }
  789. return false;
  790. }
  791. function addStyles($styles = array(),$priority=10) {
  792. if (defined('GANTRY_FINALIZED')) return;
  793. foreach($styles as $style) $this->addStyle($style, $priority);
  794. }
  795. function addInlineStyle($css = '') {
  796. if (defined('GANTRY_FINALIZED')) return;
  797. return $this->document->addStyleDeclaration($css);
  798. }
  799. function addScript($file = '') {
  800. if (is_array($file)) return $this->addScripts($file);
  801. $type = 'js';
  802. // check to see if this is a full path file
  803. $dir = dirname($file);
  804. if ($dir != ".") {
  805. // For remote url just add the url
  806. if (preg_match('/^http/',$file)){
  807. $this->document->addScript($file);
  808. return;
  809. }
  810. // For local url path get the local path based on checks
  811. $url_path = $dir;
  812. $file_path = $this->_getFilePath($file);
  813. $url_file_checks = $this->platform->getJSChecks($file_path, true);
  814. foreach ($url_file_checks as $url_file){
  815. $full_path = realpath($url_file);
  816. if ($full_path !== false && file_exists($full_path)){
  817. $check_url_path = $url_path.'/'.basename($url_file);
  818. if(!defined('GANTRY_FINALIZED'))
  819. $this->_scripts[$full_path] = $check_url_path;
  820. else
  821. $this->document->addScript($check_url_path);
  822. break;
  823. }
  824. }
  825. return;
  826. }
  827. $out_files = array();
  828. $paths = array(
  829. $this->templateUrl => $this->templatePath.DS.$type,
  830. $this->gantryUrl => $this->gantryPath.DS.$type
  831. );
  832. $checks = $this->platform->getJSChecks($file);
  833. foreach($paths as $baseurl => $path){
  834. if (file_exists($path) && is_dir($path)){
  835. foreach($checks as $check) {
  836. $check_path = preg_replace("/\?(.*)/",'',$path.DS.$check);
  837. $check_url_path = $baseurl ."/".$type."/".$check;
  838. if (file_exists($check_path) && is_readable($check_path)){
  839. if(!defined('GANTRY_FINALIZED'))
  840. $this->_scripts[$check_path] = $check_url_path;
  841. else
  842. $this->document->addScript($check_url_path);
  843. break(2);
  844. }
  845. }
  846. }
  847. }
  848. }
  849. function addScripts($scripts = array()) {
  850. if (defined('GANTRY_FINALIZED')) return;
  851. foreach($scripts as $script) $this->addScript($script);
  852. }
  853. function addInlineScript($js = '') {
  854. if (defined('GANTRY_FINALIZED')) return;
  855. return $this->document->addScriptDeclaration($js);
  856. }
  857. function addDomReadyScript($js = '') {
  858. if (defined('GANTRY_FINALIZED')) return;
  859. if (!isset($this->_domready_script)) {
  860. $this->_domready_script = $js;
  861. } else {
  862. $this->_domready_script .= chr(13).$js;
  863. }
  864. }
  865. function addLoadScript($js = '') {
  866. if (defined('GANTRY_FINALIZED')) return;
  867. if (!isset($this->_loadevent_script)) {
  868. $this->_loadevent_script = $js;
  869. } else {
  870. $this->_loadevent_script .= chr(13).$js;
  871. }
  872. }
  873. function repopulateParams(){
  874. if ($this->isAdmin()){
  875. // get a copy of the params for working with on this call
  876. $this->_working_params = $this->_template->getParams();
  877. gantry_import('core.params.overrides.gantrymenuitemparams');
  878. GantryMenuItemParams::populate();
  879. }
  880. }
  881. /**
  882. * @param string $layout the layout name to render
  883. * @param array $params all parameters needed for rendering the layout as an associative array with 'parameter name' => parameter_value
  884. * @return void
  885. */
  886. function renderLayout($layout_name, $params=array()){
  887. $layout = $this->_getLayout($layout_name);
  888. if ($layout === false){
  889. return "<!-- Unable to render layout... can not find layout class for " . $layout_name . " -->";
  890. }
  891. return $layout->render($params);
  892. }
  893. /**#@+
  894. * @access private
  895. */
  896. /**
  897. * @param $url
  898. * @return string
  899. */
  900. function _getFilePath($url) {
  901. $uri =& JURI::getInstance();
  902. $base = $uri->toString( array('scheme', 'host', 'port'));
  903. $path = JURI::Root(true);
  904. if ($url && $base && strpos($url,$base)!==false) $url = preg_replace('|^'.$base.'|',"",$url);
  905. if ($url && $path && strpos($url,$path)!==false) $url = preg_replace('|^'.$path.'|',"",$url);
  906. if (substr($url,0,1) != DS) $url = DS.$url;
  907. $filepath = JPATH_SITE.$url;
  908. return $filepath;
  909. }
  910. /**
  911. * internal util function to get key from schema array
  912. * @param $schemaArray
  913. * @return #Fimplode|?
  914. */
  915. function _getKey($schemaArray) {
  916. $concatArray = array();
  917. foreach ($schemaArray as $key=>$value) {
  918. $concatArray[] = $key . $value;
  919. }
  920. return (implode("-",$concatArray));
  921. }
  922. /**
  923. * @return #M#Vdb.loadResult|#P#Vdefault_item.id|int|?
  924. */
  925. function _getDefaultMenuItem(){
  926. if (!$this->isAdmin()){
  927. $menu =& JSite::getMenu();
  928. $default_item = $menu->getDefault();
  929. return $default_item->id;
  930. }
  931. else
  932. {
  933. $db =& JFactory::getDBO();
  934. $default = 0;
  935. $query = 'SELECT id'
  936. . ' FROM #__menu AS m'
  937. . ' WHERE m.home = 1';
  938. $db->setQuery( $query );
  939. $default = $db->loadResult();
  940. return $default;
  941. }
  942. }
  943. /**
  944. * @return void
  945. */
  946. function _loadConfig() {
  947. // Process the config
  948. $default_config_file = $this->gantryPath.DS.'gantry.config.php';
  949. if (file_exists($default_config_file) && is_readable($default_config_file)){
  950. include_once($default_config_file);
  951. }
  952. $template_config_file = $this->templatePath.DS.'gantry.config.php';
  953. if (file_exists($template_config_file ) && is_readable($template_config_file)){
  954. /** @define "$template_config_file" "VALUE" */
  955. include_once($template_config_file);
  956. }
  957. if (isset($gantry_default_config_mapping)) {
  958. $temp_array = array_merge($this->_config_vars, $gantry_default_config_mapping);
  959. $this->_config_vars = $temp_array;
  960. }
  961. if (isset($gantry_config_mapping)){
  962. $temp_array = array_merge($this->_config_vars, $gantry_config_mapping);
  963. $this->_config_vars = $temp_array;
  964. }
  965. foreach($this->_config_vars as $config_var_name =>$class_var_name){
  966. $default_config_var_name = 'gantry_default_'.$config_var_name;
  967. if (isset($$default_config_var_name)){
  968. $this->$class_var_name = $$default_config_var_name;
  969. $this->__cacheables[] = $class_var_name;
  970. }
  971. $template_config_var_name = 'gantry_'.$config_var_name;
  972. if (isset($$template_config_var_name)){
  973. $this->$class_var_name = $$template_config_var_name;
  974. $this->__cacheables[] = $class_var_name;
  975. }
  976. }
  977. }
  978. /**
  979. * @return void
  980. */
  981. function _loadBrowserConfig() {
  982. $checks = array(
  983. $this->browser->name,
  984. $this->browser->platform,
  985. $this->browser->name . '_' . $this->browser->platform,
  986. $this->browser->name . $this->browser->shortversion,
  987. $this->browser->name . $this->browser->version,
  988. $this->browser->name . $this->browser->shortversion . '_' . $this->browser->platform,
  989. $this->browser->name . $this->browser->version . '_' . $this->browser->platform
  990. );
  991. foreach($checks as $check){
  992. if (array_key_exists($check, $this->_browser_params)){
  993. foreach($this->_browser_params[$check] as $param_name => $param_value) {
  994. $this->set($param_name, $param_value);
  995. }
  996. }
  997. }
  998. }
  999. /**
  1000. * @return void
  1001. */
  1002. function _customPresets() {
  1003. $this->originalPresets = $this->presets;
  1004. if (file_exists($this->custom_presets_file)) {
  1005. $customPresets = GantryINI::read($this->custom_presets_file);
  1006. $this->customPresets = $customPresets;
  1007. $this->originalPresets = $this->presets;
  1008. if (count($customPresets)) {
  1009. $this->presets = $this->_array_merge_replace_recursive($this->presets, $customPresets);
  1010. foreach($this->presets as $key => $preset) {
  1011. uksort($preset, array($this, "_compareKeys"));
  1012. $this->presets[$key] = $preset;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. /**
  1018. * @param $key1
  1019. * @param $key2
  1020. * @return int
  1021. */
  1022. function _compareKeys($key1, $key2) {
  1023. if (strlen($key1) < strlen($key2)) return -1;
  1024. else if (strlen($key1) > strlen($key2)) return 1;
  1025. else {
  1026. if ($key1 < $key2) return -1;
  1027. else return 1;
  1028. }
  1029. }
  1030. /**
  1031. * @param $name
  1032. * @param $preset
  1033. * @return array
  1034. */
  1035. function _getPresetParams($name,$preset){
  1036. $return_params = array();
  1037. if (array_key_exists($preset,$this->presets[$name])){
  1038. $preset_params = $this->presets[$name][$preset];
  1039. foreach ($preset_params as $preset_param_name => $preset_param_value) {
  1040. if (array_key_exists($preset_param_name, $this->_working_params) && $this->_working_params[$preset_param_name]['type'] == 'preset') {
  1041. $return_params = $this->_getPresetParams($preset_param_name,$preset_param_value);
  1042. }
  1043. }
  1044. foreach ($preset_params as $preset_param_name => $preset_param_value) {
  1045. if (array_key_exists($preset_param_name, $this->_working_params) && $this->_working_params[$preset_param_name]['type'] != 'preset') {
  1046. $return_params[$preset_param_name] = $preset_param_value;
  1047. }
  1048. }
  1049. }
  1050. return $return_params;
  1051. }
  1052. /**
  1053. * @return void
  1054. */
  1055. function _populateParams(){
  1056. gantry_import('core.params.overrides.gantryurlparamoverride');
  1057. gantry_import('core.params.overrides.gantrysessionparamoverride');
  1058. gantry_import('core.params.overrides.gantrycookieparamoverride');
  1059. gantry_import('core.params.overrides.gantrymenuitemparamoverride');
  1060. // get a copy of the params for working with on this call
  1061. $this->_working_params = $this->_template->getParams();
  1062. if (!isset($_REQUEST['reset-settings'])){
  1063. GantrySessionParamOverride::populate();
  1064. GantryCookieParamOverride::populate();
  1065. }
  1066. GantryMenuItemParamOverride::populate();
  1067. if (!isset($_REQUEST['reset-settings'])){
  1068. GantryUrlParamOverride::populate();
  1069. }
  1070. $this->_params_hash = md5(serialize($this->_working_params));
  1071. }
  1072. /**
  1073. * @param $position
  1074. * @return array
  1075. */
  1076. function _getFeaturesForPosition($position) {
  1077. if (isset($this->_featuresPosition[$this->cacheKey($position, true)])) {
  1078. return $this->_featuresPosition[$this->cacheKey($position, true)];
  1079. }
  1080. $return = array();
  1081. // Init all features
  1082. foreach($this->getFeatures() as $feature){
  1083. $feature_instance = $this->_getFeature($feature);
  1084. if ($feature_instance->isEnabled() && $feature_instance->isInPosition($position) && method_exists( $feature_instance , 'render')) {
  1085. $return[] = $feature;
  1086. }
  1087. }
  1088. return $this->_featuresPosition[$this->cacheKey($position, true)] = $return;
  1089. }
  1090. /**
  1091. * internal util to get short name from long name
  1092. * @param $longname
  1093. * @return string
  1094. */
  1095. function _getShortName($longname) {
  1096. $shortname = $longname;
  1097. if (strlen($longname)>2) {
  1098. $shortname = substr($longname,0,1) . substr($longname,-1);
  1099. }
  1100. return $shortname;
  1101. }
  1102. /**
  1103. * internal util to get long name from short name
  1104. * @param $shortname
  1105. * @return string
  1106. */
  1107. function _getLongName($shortname) {
  1108. $longname = $shortname;
  1109. switch (substr($shortname,0,1)) {
  1110. case "s":
  1111. default:
  1112. $longname = "sidebar";
  1113. break;
  1114. }
  1115. $longname .= "-".substr($shortname,-1);
  1116. return $longname;
  1117. }
  1118. /**
  1119. * internal util to retrieve the prefix of a position
  1120. * @param $position
  1121. * @return #Fsubstr|?
  1122. */
  1123. function _getPositionPrefix($position) {
  1124. return substr($position, 0, strrpos($position, "-"));
  1125. }
  1126. /**
  1127. * internal util to retrieve the stored position schema
  1128. * @param $position
  1129. * @param $gridsize
  1130. * @param $count
  1131. * @param $index
  1132. * @return #P#CGantry.layoutSchemas|boolean|?
  1133. */
  1134. function _getPositionSchema($position, $gridsize, $count, $index) {
  1135. $param = $this->_getPositionPrefix($position) . '-layout';
  1136. $defaultSchema = false;
  1137. $storedParam = $this->get($param);
  1138. if (!preg_match("/{/", $storedParam)) $storedParam = '';
  1139. $setting = unserialize($storedParam);
  1140. $schema =& $setting[$gridsize][$count][$index];
  1141. if ($this->document->direction == 'rtl' && $this->get('rtl-enabled')) {
  1142. $layout = array_reverse($setting[$gridsize][$count]);
  1143. $schema =& $layout[$index];
  1144. }
  1145. if (isset($schema))
  1146. return $schema;
  1147. else {
  1148. if (count($this->layoutSchemas[$gridsize]) < $count){
  1149. $count = count($this->layoutSchemas[$gridsize]);
  1150. }
  1151. for ($i=$count;$i>0;$i--) {
  1152. $layout = $this->layoutSchemas[$gridsize][$i];
  1153. if ($this->document->direction == 'rtl' && $this->get('rtl-enabled')) {
  1154. $layout = array_reverse($layout);
  1155. }
  1156. if (isset($layout[$index])) {
  1157. $defaultSchema = $layout[$index];
  1158. break;
  1159. }
  1160. }
  1161. return $defaultSchema;
  1162. }
  1163. }
  1164. /**
  1165. * @param $file
  1166. * @return
  1167. */
  1168. function _getBrowserBasedChecks($file, $keep_path=false) {
  1169. $ext = substr($file, strrpos($file, '.'));
  1170. $path = ($keep_path)?dirname($file).DS:'';
  1171. $filename = basename($file, $ext);
  1172. $checks = $this->browser->getChecks($file, $keep_path);
  1173. // check if RTL version needed
  1174. $document =& $this->document;
  1175. if ($document->direction == 'rtl' && $this->get('rtl-enabled')) {
  1176. $checks[] = $path.$filename . '-rtl'.$ext;
  1177. }
  1178. return $checks;
  1179. }
  1180. /**
  1181. * @return
  1182. */
  1183. function _getCurrentTemplate() {
  1184. $session =& JFactory::getSession();
  1185. if (!$this->isAdmin()) {
  1186. $app = &JApplication::getInstance('site', array(), 'J');
  1187. $template = $app->getTemplate();
  1188. }
  1189. else {
  1190. if (array_key_exists('cid',$_REQUEST)){
  1191. $template = $_REQUEST['cid'][0];
  1192. }
  1193. else {
  1194. $template = $session->get('gantry-current-template');
  1195. }
  1196. }
  1197. $session->set('gantry-current-template', $template);
  1198. return $template;
  1199. }
  1200. /**
  1201. * @param $condition
  1202. * @return
  1203. */
  1204. function _adminCountModules($condition)
  1205. {
  1206. $result = '';
  1207. $words = explode(' ', $condition);
  1208. for($i = 0; $i < count($words); $i+=2)
  1209. {
  1210. // odd parts (modules)
  1211. $name = strtolower($words[$i]);
  1212. $words[$i] = ((isset($this->_buffer['modules'][$name])) && ($this->_buffer['modules'][$name] === false)) ? 0 : count($this->_getModulesFromAdmin($name));
  1213. }
  1214. $str = 'return '.implode(' ', $words).';';
  1215. return eval($str);
  1216. }
  1217. /**
  1218. * Get modules by position
  1219. *
  1220. * @param string $position The position of the module
  1221. * @return array An array of module objects
  1222. */
  1223. function &_getModulesFromAdmin($position)
  1224. {
  1225. $position = strtolower( $position );
  1226. $result = array();
  1227. $modules = $this->_loadModulesFromAdmin();
  1228. $total = count($modules);
  1229. for($i = 0; $i < $total; $i++) {
  1230. if($modules[$i]->position == $position) {
  1231. $result[] =& $modules[$i];
  1232. }
  1233. }
  1234. return $result;
  1235. }
  1236. /**
  1237. * Load published modules
  1238. *
  1239. * @return array
  1240. */
  1241. function &_loadModulesFromAdmin()
  1242. {
  1243. static $clean;
  1244. if (isset($clean)) {
  1245. return $clean;
  1246. }
  1247. $db = JFactory::getDbo();
  1248. $query = $db->getQuery(true);
  1249. $query->select('a.id');
  1250. $query->from('#__menu AS a');
  1251. $query->where('a.home = 1');
  1252. $query->where('a.client_id = 0');
  1253. $db->setQuery($query);
  1254. $Itemid = (int)$db->loadResult();
  1255. $app = JFactory::getApplication();
  1256. $user = JFactory::getUser(0);
  1257. $groups = implode(',', $user->getAuthorisedViewLevels());
  1258. $lang = JFactory::getLanguage()->getTag();
  1259. $clientId = 0;
  1260. $cache = JFactory::getCache ('com_modules', '');
  1261. $cacheid = md5(serialize(array($Itemid, $groups, $clientId, $lang)));
  1262. if (!($clean = $cache->get($cacheid))) {
  1263. $query = $db->getQuery(true);
  1264. $query->select('id, title, module, position, content, showtitle, params, mm.menuid');
  1265. $query->from('#__modules AS m');
  1266. $query->join('LEFT','#__modules_menu AS mm ON mm.moduleid = m.id');
  1267. $query->where('m.published = 1');
  1268. $date = JFactory::getDate();
  1269. $now = $date->toMySQL();
  1270. $nullDate = $db->getNullDate();
  1271. $query->where('(m.publish_up = '.$db->Quote($nullDate).' OR m.publish_up <= '.$db->Quote($now).')');
  1272. $query->where('(m.publish_down = '.$db->Quote($nullDate).' OR m.publish_down >= '.$db->Quote($now).')');
  1273. $query->where('m.access IN ('.$groups.')');
  1274. $query->where('m.client_id = 0');
  1275. $query->where('(mm.menuid = '. (int) $Itemid . ' OR mm.menuid <=0)');
  1276. // Filter by language
  1277. if ($app->isSite() && $app->getLanguageFilter()) {
  1278. $query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
  1279. }
  1280. $query->order('position, ordering');
  1281. // Set the query
  1282. $db->setQuery($query);
  1283. if (!($modules = $db->loadObjectList())) {
  1284. JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $db->getErrorMsg()));
  1285. return false;
  1286. }
  1287. // Apply negative selections and eliminate duplicates
  1288. $negId = $Itemid ? -(int)$Itemid : false;
  1289. $dupes = array();
  1290. $clean = array();
  1291. for ($i = 0, $n = count($modules); $i < $n; $i++)
  1292. {
  1293. $module = &$modules[$i];
  1294. // The module is excluded if there is an explicit prohibition, or if
  1295. // the Itemid is missing or zero and the module is in exclude mode.
  1296. $negHit = ($negId === (int) $module->menuid)
  1297. || (!$negId && (int)$module->menuid < 0);
  1298. if (isset($dupes[$module->id]))
  1299. {
  1300. // If this item has been excluded, keep the duplicate flag set,
  1301. // but remove any item from the cleaned array.
  1302. if ($negHit) {
  1303. unset($clean[$module->id]);
  1304. }
  1305. continue;
  1306. }
  1307. $dupes[$module->id] = true;
  1308. // Only accept modules without explicit exclusions.
  1309. if (!$negHit)
  1310. {
  1311. //determine if this is a cu…

Large files files are truncated, but you can click here to view the full file