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

/libraries/joomla/application/module/helper.php

https://github.com/cosmocommerce/joomla
PHP | 338 lines | 199 code | 44 blank | 95 comment | 37 complexity | 91ac922ba6a67ed425ebe9e6d6d2749c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access
  8. defined('JPATH_BASE') or die;
  9. // Import library dependencies
  10. jimport('joomla.application.component.helper');
  11. /**
  12. * Module helper class
  13. *
  14. * @static
  15. * @package Joomla.Framework
  16. * @subpackage Application
  17. * @since 1.5
  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 ($app->getCfg('debug_modules') && JRequest::getBool('tp'))
  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 strign 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 JParameter($module->params);
  126. // Get module path
  127. $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
  128. $path = JPATH_BASE.'/modules/'.$module->module.'/'.$module->module.'.php';
  129. // Load the module
  130. if (!$module->user && file_exists($path))
  131. {
  132. $lang = &JFactory::getLanguage();
  133. // 1.5 or Core then
  134. // 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_BASE.'/templates/system/html/modules.php';
  150. $chromePath = JPATH_BASE.'/templates/'.$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 ($app->getCfg('debug_modules') && JRequest::getBool('tp')) {
  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. * @static
  186. * @param string $module The name of the module
  187. * @param string $layout The name of the module layout
  188. * @return string The path to the module layout
  189. * @since 1.5
  190. */
  191. public static function getLayoutPath($module, $layout = 'default')
  192. {
  193. $app = JFactory::getApplication();
  194. // Build the template and base path for the layout
  195. $tPath = JPATH_BASE.'/templates/'.$app->getTemplate().'/html/'.$module.'/'.$layout.'.php';
  196. $bPath = JPATH_BASE.'/modules/'.$module.'/tmpl/'.$layout.'.php';
  197. // If the template has a layout override use it
  198. if (file_exists($tPath)) {
  199. return $tPath;
  200. }
  201. else {
  202. return $bPath;
  203. }
  204. }
  205. /**
  206. * Load published modules
  207. *
  208. * @return array
  209. */
  210. protected static function &_load()
  211. {
  212. static $clean;
  213. if (isset($clean)) {
  214. return $clean;
  215. }
  216. $Itemid = JRequest::getInt('Itemid');
  217. $app = JFactory::getApplication();
  218. $user = &JFactory::getUser();
  219. $groups = implode(',', $user->authorisedLevels());
  220. $db = &JFactory::getDbo();
  221. $query = new JDatabaseQuery;
  222. $query->select('id, title, module, position, content, showtitle, params, mm.menuid');
  223. $query->from('#__modules AS m');
  224. $query->join('LEFT','#__modules_menu AS mm ON mm.moduleid = m.id');
  225. $query->where('m.published = 1');
  226. $date = JFactory::getDate();
  227. $now = $date->toMySQL();
  228. $nullDate = $db->getNullDate();
  229. $query->where('(m.publish_up = '.$db->Quote($nullDate).' OR m.publish_up <= '.$db->Quote($now).')');
  230. $query->where('(m.publish_down = '.$db->Quote($nullDate).' OR m.publish_down >= '.$db->Quote($now).')');
  231. $query->where('m.access IN ('.$groups.')');
  232. $query->where('m.client_id = '. (int) $app->getClientId());
  233. if (isset($Itemid)) {
  234. $query->where('(mm.menuid = '. (int) $Itemid .' OR mm.menuid <= 0)');
  235. }
  236. $query->order('position, ordering');
  237. $db->setQuery($query);
  238. /* $where = isset($Itemid) ? ' AND (mm.menuid = '. (int) $Itemid .' OR mm.menuid <= 0)' : '';
  239. $db->setQuery(
  240. 'SELECT id, title, module, position, content, showtitle, params, mm.menuid'
  241. . ' FROM #__modules AS m'
  242. . ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
  243. . ' WHERE m.published = 1'
  244. . ' AND m.access IN ('.$groups.')'
  245. . ' AND m.client_id = '. (int) $app->getClientId()
  246. . $where
  247. . ' ORDER BY position, ordering'
  248. );*/
  249. if (null === ($modules = $db->loadObjectList()))
  250. {
  251. JError::raiseWarning('SOME_ERROR_CODE', JText::_('ERROR_LOADING_MODULES') . $db->getErrorMsg());
  252. return false;
  253. }
  254. // Apply negative selections and eliminate duplicates
  255. $negId = $Itemid ? -(int)$Itemid : false;
  256. $dupes = array();
  257. $clean = array();
  258. for ($i = 0, $n = count($modules); $i < $n; $i++)
  259. {
  260. $module = &$modules[$i];
  261. // The module is excluded if there is an explicit prohibition, or if
  262. // the Itemid is missing or zero and the module is in exclude mode.
  263. $negHit = ($negId === (int) $module->menuid)
  264. || (!$negId && (int)$module->menuid < 0);
  265. if (isset($dupes[$module->id]))
  266. {
  267. // If this item has been excluded, keep the duplicate flag set,
  268. // but remove any item from the cleaned array.
  269. if ($negHit) {
  270. unset($clean[$module->id]);
  271. }
  272. continue;
  273. }
  274. $dupes[$module->id] = true;
  275. // Only accept modules without explicit exclusions.
  276. if (!$negHit)
  277. {
  278. //determine if this is a custom module
  279. $file = $module->module;
  280. $custom = substr($file, 0, 4) == 'mod_' ? 0 : 1;
  281. $module->user = $custom;
  282. // Custom module name is given by the title field, otherwise strip off "com_"
  283. $module->name = $custom ? $module->title : substr($file, 4);
  284. $module->style = null;
  285. $module->position = strtolower($module->position);
  286. $clean[$module->id] = $module;
  287. }
  288. }
  289. unset($dupes);
  290. // Return to simple indexing that matches the query order.
  291. $clean = array_values($clean);
  292. return $clean;
  293. }
  294. }