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

/libraries/overrides/application/module/helper.php

https://github.com/Macsmice/Tamka
PHP | 442 lines | 269 code | 66 blank | 107 comment | 50 complexity | e68275b3f20cced07f5e611e1e3a3f6b MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. // Import library dependencies
  11. jimport('joomla.application.component.helper');
  12. /**
  13. * Module helper class
  14. *
  15. * @package Joomla.Platform
  16. * @subpackage Application
  17. * @since 11.1
  18. */
  19. abstract class JModuleHelper
  20. {
  21. /**
  22. * Get module by name (real, eg 'Breadcrumbs' or folder, eg 'mod_breadcrumbs')
  23. *
  24. * @param string The name of the module
  25. * @param string The title of the module, optional
  26. *
  27. * @return object The Module object
  28. */
  29. public static function &getModule($name, $title = null)
  30. {
  31. $result = null;
  32. $modules = JModuleHelper::_load();
  33. $total = count($modules);
  34. for ($i = 0; $i < $total; $i++)
  35. {
  36. // Match the name of the module
  37. if ($modules[$i]->name == $name)
  38. {
  39. // Match the title if we're looking for a specific instance of the module
  40. if (!$title || $modules[$i]->title == $title)
  41. {
  42. $result = &$modules[$i];
  43. break; // Found it
  44. }
  45. }
  46. }
  47. // If we didn't find it, and the name is mod_something, create a dummy object
  48. if (is_null($result) && substr($name, 0, 4) == 'mod_')
  49. {
  50. $result = new stdClass;
  51. $result->id = 0;
  52. $result->title = '';
  53. $result->module = $name;
  54. $result->position = '';
  55. $result->content = '';
  56. $result->showtitle = 0;
  57. $result->control = '';
  58. $result->params = '';
  59. $result->user = 0;
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Get modules by position
  65. *
  66. * @param string $position The position of the module
  67. *
  68. * @return array An array of module objects
  69. */
  70. public static function &getModules($position)
  71. {
  72. $app = JFactory::getApplication();
  73. $position = strtolower($position);
  74. $result = array();
  75. $modules = JModuleHelper::_load();
  76. $total = count($modules);
  77. for ($i = 0; $i < $total; $i++)
  78. {
  79. if ($modules[$i]->position == $position) {
  80. $result[] = &$modules[$i];
  81. }
  82. }
  83. if (count($result) == 0)
  84. {
  85. if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
  86. {
  87. $result[0] = JModuleHelper::getModule('mod_'.$position);
  88. $result[0]->title = $position;
  89. $result[0]->content = $position;
  90. $result[0]->position = $position;
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Checks if a module is enabled
  97. *
  98. * @param string The module name
  99. *
  100. * @return boolean
  101. */
  102. public static function isEnabled($module)
  103. {
  104. $result = JModuleHelper::getModule($module);
  105. return (!is_null($result));
  106. }
  107. /**
  108. * Render the module.
  109. *
  110. * @param object A module object.
  111. * @param array An array of attributes for the module (probably from the XML).
  112. *
  113. * @return string The HTML content of the module output.
  114. */
  115. public static function renderModule($module, $attribs = array())
  116. {
  117. static $chrome;
  118. $option = JRequest::getCmd('option');
  119. $app = JFactory::getApplication();
  120. // Record the scope.
  121. $scope = $app->scope;
  122. // Set scope to component name
  123. $app->scope = $module->module;
  124. // Get module parameters
  125. $params = new JRegistry;
  126. $params->loadJSON($module->params);
  127. // Get module path
  128. $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
  129. $path = JPATH_BASE.'/modules/'.$module->module.'/'.$module->module.'.php';
  130. // Load the module
  131. if (!$module->user && file_exists($path))
  132. {
  133. $lang = JFactory::getLanguage();
  134. // 1.5 or Core then 1.6 3PD
  135. $lang->load($module->module, JPATH_BASE, null, false, false)
  136. || $lang->load($module->module, dirname($path), null, false, false)
  137. || $lang->load($module->module, JPATH_BASE, $lang->getDefault(), false, false)
  138. || $lang->load($module->module, dirname($path), $lang->getDefault(), false, false);
  139. $content = '';
  140. ob_start();
  141. require $path;
  142. $module->content = ob_get_contents().$content;
  143. ob_end_clean();
  144. }
  145. // Load the module chrome functions
  146. if (!$chrome) {
  147. $chrome = array();
  148. }
  149. require_once JPATH_THEMES.'/system/html/modules.php';
  150. $chromePath = JPATH_THEMES.'/'.$app->getTemplate().'/html/modules.php';
  151. if (!isset($chrome[$chromePath]))
  152. {
  153. if (file_exists($chromePath)) {
  154. require_once $chromePath;
  155. }
  156. $chrome[$chromePath] = true;
  157. }
  158. // Make sure a style is set
  159. if (!isset($attribs['style'])) {
  160. $attribs['style'] = 'none';
  161. }
  162. // Dynamically add outline style
  163. if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display')) {
  164. $attribs['style'] .= ' outline';
  165. }
  166. foreach(explode(' ', $attribs['style']) as $style)
  167. {
  168. $chromeMethod = 'modChrome_'.$style;
  169. // Apply chrome and render module
  170. if (function_exists($chromeMethod))
  171. {
  172. $module->style = $attribs['style'];
  173. ob_start();
  174. $chromeMethod($module, $params, $attribs);
  175. $module->content = ob_get_contents();
  176. ob_end_clean();
  177. }
  178. }
  179. $app->scope = $scope; //revert the scope
  180. return $module->content;
  181. }
  182. /**
  183. * Get the path to a layout for a module
  184. *
  185. * @param string $module The name of the module
  186. * @param string $layout The name of the module layout. If alternative layout, in the form template:filename.
  187. * @return string The path to the module layout
  188. * @since 11.1
  189. */
  190. public static function getLayoutPath($module, $layout = 'default')
  191. {
  192. $template = JFactory::getApplication()->getTemplate();
  193. $defaultLayout = $layout;
  194. if (strpos($layout, ':') !== false )
  195. {
  196. // Get the template and file name from the string
  197. $temp = explode(':', $layout);
  198. $template = ($temp[0] == '_') ? $template : $temp[0];
  199. $layout = $temp[1];
  200. $defaultLayout = ($temp[1]) ? $temp[1] : 'default';
  201. }
  202. // Build the template and base path for the layout
  203. $tPath = JPATH_THEMES.'/'.$template.'/html/'.$module.'/'.$layout.'.php';
  204. $bPath = JPATH_BASE.'/modules/'.$module.'/tmpl/'.$defaultLayout.'.php';
  205. // If the template has a layout override use it
  206. if (file_exists($tPath)) {
  207. return $tPath;
  208. }
  209. else {
  210. return $bPath;
  211. }
  212. }
  213. /**
  214. * Load published modules
  215. *
  216. * @return array
  217. */
  218. protected static function &_load()
  219. {
  220. static $clean;
  221. if (isset($clean)) {
  222. return $clean;
  223. }
  224. $Itemid = JRequest::getInt('Itemid');
  225. $app = JFactory::getApplication();
  226. $user = JFactory::getUser();
  227. $groups = implode(',', $user->getAuthorisedViewLevels());
  228. $lang = JFactory::getLanguage()->getTag();
  229. $clientId = (int) $app->getClientId();
  230. $cache = JFactory::getCache ('com_modules', '');
  231. $cacheid = md5(serialize(array($Itemid, $groups, $clientId, $lang)));
  232. if (!($clean = $cache->get($cacheid))) {
  233. $db = JFactory::getDbo();
  234. $query = $db->getQuery(true);
  235. $query->select('id, title, module, position, content, showtitle, params, mm.menuid');
  236. $query->from('#__modules AS m');
  237. $query->join('LEFT','#__modules_menu AS mm ON mm.moduleid = m.id');
  238. $query->where('m.published = 1');
  239. $date = JFactory::getDate();
  240. $now = $date->toMySQL();
  241. $nullDate = $db->getNullDate();
  242. $query->where('(m.publish_up = '.$db->Quote($nullDate).' OR m.publish_up <= '.$db->Quote($now).')');
  243. $query->where('(m.publish_down = '.$db->Quote($nullDate).' OR m.publish_down >= '.$db->Quote($now).')');
  244. $query->where('m.access IN ('.$groups.')');
  245. $query->where('m.client_id = '. $clientId);
  246. $query->where('(mm.menuid = '. (int) $Itemid .' OR mm.menuid <= 0)');
  247. // Filter by language
  248. if ($app->isSite() && $app->getLanguageFilter()) {
  249. $query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
  250. }
  251. $query->order('position, ordering');
  252. // Set the query
  253. $db->setQuery($query);
  254. $modules = $db->loadObjectList();
  255. $clean = array();
  256. if($db->getErrorNum()){
  257. JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $db->getErrorMsg()));
  258. return $clean;
  259. }
  260. // Apply negative selections and eliminate duplicates
  261. $negId = $Itemid ? -(int)$Itemid : false;
  262. $dupes = array();
  263. for ($i = 0, $n = count($modules); $i < $n; $i++)
  264. {
  265. $module = &$modules[$i];
  266. // The module is excluded if there is an explicit prohibition or if
  267. // the Itemid is missing or zero and the module is in exclude mode.
  268. $negHit = ($negId === (int) $module->menuid)
  269. || (!$negId && (int)$module->menuid < 0);
  270. if (isset($dupes[$module->id]))
  271. {
  272. // If this item has been excluded, keep the duplicate flag set,
  273. // but remove any item from the cleaned array.
  274. if ($negHit) {
  275. unset($clean[$module->id]);
  276. }
  277. continue;
  278. }
  279. $dupes[$module->id] = true;
  280. // Only accept modules without explicit exclusions.
  281. if (!$negHit)
  282. {
  283. //determine if this is a custom module
  284. $file = $module->module;
  285. $custom = substr($file, 0, 4) == 'mod_' ? 0 : 1;
  286. $module->user = $custom;
  287. // Custom module name is given by the title field, otherwise strip off "mod_"
  288. $module->name = $custom ? $module->title : substr($file, 4);
  289. $module->style = null;
  290. $module->position = strtolower($module->position);
  291. $clean[$module->id] = $module;
  292. }
  293. }
  294. unset($dupes);
  295. // Return to simple indexing that matches the query order.
  296. $clean = array_values($clean);
  297. $cache->store($clean, $cacheid);
  298. }
  299. return $clean;
  300. }
  301. /**
  302. * Module cache helper
  303. *
  304. * Caching modes:
  305. * To be set in XML:
  306. * 'static' one cache file for all pages with the same module parameters
  307. * 'oldstatic' 1.5. definition of module caching, one cache file for all pages with the same module id and user aid,
  308. * 'itemid' changes on itemid change,
  309. * To be called from inside the module:
  310. * 'safeuri' id created from $cacheparams->modeparams array,
  311. * 'id' module sets own cache id's
  312. *
  313. * @param object $module Module object
  314. * @param object $moduleparams module parameters
  315. * @param object $cacheparams module cache parameters - id or url parameters, depending on the module cache mode
  316. * @param array $params - parameters for given mode - calculated id or an array of safe url parameters and their
  317. * variable types, for valid values see {@link JFilterInput::clean()}.
  318. *
  319. * @since 11.1
  320. */
  321. public static function moduleCache($module, $moduleparams, $cacheparams)
  322. {
  323. if(!isset ($cacheparams->modeparams)) {
  324. $cacheparams->modeparams=null;
  325. }
  326. if(!isset ($cacheparams->cachegroup)) {
  327. $cacheparams->cachegroup = $module->module;
  328. }
  329. $user = JFactory::getUser();
  330. $cache = JFactory::getCache($cacheparams->cachegroup, 'callback');
  331. $conf = JFactory::getConfig();
  332. // Turn cache off for internal callers if parameters are set to off and for all logged in users
  333. if($moduleparams->get('owncache', null) === 0 || $conf->get('caching') == 0 || $user->get('id')) {
  334. $cache->setCaching(false);
  335. }
  336. $cache->setLifeTime($moduleparams->get('cache_time', $conf->get('cachetime') * 60));
  337. $wrkaroundoptions = array (
  338. 'nopathway' => 1,
  339. 'nohead' => 0,
  340. 'nomodules' => 1,
  341. 'modulemode' => 1,
  342. 'mergehead' => 1
  343. );
  344. $wrkarounds = true;
  345. switch ($cacheparams->cachemode) {
  346. case 'id':
  347. $ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $cacheparams->modeparams, $wrkarounds, $wrkaroundoptions);
  348. break;
  349. case 'safeuri':
  350. $secureid=null;
  351. if (is_array($cacheparams->modeparams)) {
  352. $uri = JRequest::get();
  353. $safeuri = new stdClass();
  354. foreach ($cacheparams->modeparams AS $key => $value) {
  355. // Use int filter for id/catid to clean out spamy slugs
  356. if (isset($uri[$key])) {
  357. $safeuri->$key = JRequest::_cleanVar($uri[$key], 0,$value);
  358. }
  359. } }
  360. $secureid = md5(serialize(array($safeuri, $cacheparams->method, $moduleparams)));
  361. $ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->id. $user->get('aid', 0).$secureid, $wrkarounds, $wrkaroundoptions);
  362. break;
  363. case 'static':
  364. $ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->module.md5(serialize($cacheparams->methodparams)), $wrkarounds, $wrkaroundoptions);
  365. break;
  366. case 'oldstatic': // provided for backward compatibility, not really usefull
  367. $ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->id. $user->get('aid', 0), $wrkarounds, $wrkaroundoptions);
  368. break;
  369. case 'itemid':
  370. default:
  371. $ret = $cache->get(array($cacheparams->class, $cacheparams->method), $cacheparams->methodparams, $module->id. $user->get('aid', 0).JRequest::getVar('Itemid',null,'default','INT'), $wrkarounds, $wrkaroundoptions);
  372. break;
  373. }
  374. return $ret;
  375. }
  376. }