PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/joebushi/joomla
PHP | 316 lines | 191 code | 41 blank | 84 comment | 36 complexity | 5a7051575a997d20a79e178ff01fd08e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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 $name The name of the module
  25. * @param string $title 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 $module 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.6 3PD
  134. $lang->load($module->module, dirname($path));
  135. // 1.5 or Core
  136. $lang->load($module->module);
  137. $content = '';
  138. ob_start();
  139. require $path;
  140. $module->content = ob_get_contents().$content;
  141. ob_end_clean();
  142. }
  143. // Load the module chrome functions
  144. if (!$chrome) {
  145. $chrome = array();
  146. }
  147. require_once JPATH_BASE.'/templates/system/html/modules.php';
  148. $chromePath = JPATH_BASE.'/templates/'.$app->getTemplate().'/html/modules.php';
  149. if (!isset($chrome[$chromePath]))
  150. {
  151. if (file_exists($chromePath)) {
  152. require_once $chromePath;
  153. }
  154. $chrome[$chromePath] = true;
  155. }
  156. //make sure a style is set
  157. if (!isset($attribs['style'])) {
  158. $attribs['style'] = 'none';
  159. }
  160. //dynamically add outline style
  161. if ($app->getCfg('debug_modules') && JRequest::getBool('tp')) {
  162. $attribs['style'] .= ' outline';
  163. }
  164. foreach(explode(' ', $attribs['style']) as $style)
  165. {
  166. $chromeMethod = 'modChrome_'.$style;
  167. // Apply chrome and render module
  168. if (function_exists($chromeMethod))
  169. {
  170. $module->style = $attribs['style'];
  171. ob_start();
  172. $chromeMethod($module, $params, $attribs);
  173. $module->content = ob_get_contents();
  174. ob_end_clean();
  175. }
  176. }
  177. $app->scope = $scope; //revert the scope
  178. return $module->content;
  179. }
  180. /**
  181. * Get the path to a layout for a module
  182. *
  183. * @static
  184. * @param string $module The name of the module
  185. * @param string $layout The name of the module layout
  186. * @return string The path to the module layout
  187. * @since 1.5
  188. */
  189. public static function getLayoutPath($module, $layout = 'default')
  190. {
  191. $app = JFactory::getApplication();
  192. // Build the template and base path for the layout
  193. $tPath = JPATH_BASE.'/templates/'.$app->getTemplate().'/html/'.$module.'/'.$layout.'.php';
  194. $bPath = JPATH_BASE.'/modules/'.$module.'/tmpl/'.$layout.'.php';
  195. // If the template has a layout override use it
  196. if (file_exists($tPath)) {
  197. return $tPath;
  198. }
  199. else {
  200. return $bPath;
  201. }
  202. }
  203. /**
  204. * Load published modules
  205. *
  206. * @return array
  207. */
  208. protected static function &_load()
  209. {
  210. static $clean;
  211. if (isset($clean)) {
  212. return $clean;
  213. }
  214. $Itemid = JRequest::getInt('Itemid');
  215. $app = JFactory::getApplication();
  216. $user = &JFactory::getUser();
  217. $groups = implode(',', $user->authorisedLevels());
  218. $db = &JFactory::getDbo();
  219. $where = isset($Itemid) ? ' AND (mm.menuid = '. (int) $Itemid .' OR mm.menuid <= 0)' : '';
  220. $db->setQuery(
  221. 'SELECT id, title, module, position, content, showtitle, params, mm.menuid'
  222. . ' FROM #__modules AS m'
  223. . ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
  224. . ' WHERE m.published = 1'
  225. . ' AND m.access IN ('.$groups.')'
  226. . ' AND m.client_id = '. (int) $app->getClientId()
  227. . $where
  228. . ' ORDER BY position, ordering'
  229. );
  230. if (null === ($modules = $db->loadObjectList()))
  231. {
  232. JError::raiseWarning('SOME_ERROR_CODE', JText::_('ERROR_LOADING_MODULES') . $db->getErrorMsg());
  233. return false;
  234. }
  235. // Apply negative selections and eliminate duplicates
  236. $negId = $Itemid ? -(int)$Itemid : false;
  237. $dupes = array();
  238. $clean = array();
  239. for ($i = 0, $n = count($modules); $i < $n; $i++)
  240. {
  241. $module = &$modules[$i];
  242. // The module is excluded if there is an explicit prohibition, or if
  243. // the Itemid is missing or zero and the module is in exclude mode.
  244. $negHit = ($negId === (int) $module->menuid)
  245. || (!$negId && (int)$module->menuid < 0);
  246. if (isset($dupes[$module->id]))
  247. {
  248. // If this item has been excluded, keep the duplicate flag set,
  249. // but remove any item from the cleaned array.
  250. if ($negHit) {
  251. unset($clean[$module->id]);
  252. }
  253. continue;
  254. }
  255. $dupes[$module->id] = true;
  256. // Only accept modules without explicit exclusions.
  257. if (!$negHit)
  258. {
  259. //determine if this is a custom module
  260. $file = $module->module;
  261. $custom = substr($file, 0, 4) == 'mod_' ? 0 : 1;
  262. $module->user = $custom;
  263. // Custom module name is given by the title field, otherwise strip off "com_"
  264. $module->name = $custom ? $module->title : substr($file, 4);
  265. $module->style = null;
  266. $module->position = strtolower($module->position);
  267. $clean[$module->id] = $module;
  268. }
  269. }
  270. unset($dupes);
  271. // Return to simple indexing that matches the query order.
  272. $clean = array_values($clean);
  273. return $clean;
  274. }
  275. }