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

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

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 301 lines | 182 code | 44 blank | 75 comment | 28 complexity | f74ef788f70910937b1842ac6cf589e0 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: helper.php 10034 2008-02-13 21:06:54Z ian $
  4. * @package Joomla.Framework
  5. * @subpackage Application
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. // Import library dependencies
  17. jimport('joomla.application.component.helper');
  18. /**
  19. * Module helper class
  20. *
  21. * @static
  22. * @author Johan Janssens <johan.janssens@joomla.org>
  23. * @package Joomla.Framework
  24. * @subpackage Application
  25. * @since 1.5
  26. */
  27. class JModuleHelper
  28. {
  29. /**
  30. * Get module by name (real, eg 'Breadcrumbs' or folder, eg 'mod_breadcrumbs')
  31. *
  32. * @access public
  33. * @param string $name The name of the module
  34. * @return object The Module object
  35. */
  36. function &getModule($name)
  37. {
  38. $result = null;
  39. $modules =& JModuleHelper::_load();
  40. $total = count($modules);
  41. for ($i = 0; $i < $total; $i++)
  42. {
  43. if ($modules[$i]->name == $name)
  44. {
  45. $result =& $modules[$i];
  46. break;
  47. }
  48. }
  49. // if we didn't find it, and the name is mod_something, create a dummy object
  50. if (is_null( $result ) && substr( $name, 0, 4 ) == 'mod_')
  51. {
  52. $result = new stdClass;
  53. $result->id = 0;
  54. $result->title = '';
  55. $result->module = $name;
  56. $result->position = '';
  57. $result->content = '';
  58. $result->showtitle = 0;
  59. $result->control = '';
  60. $result->params = '';
  61. $result->user = 0;
  62. }
  63. return $result;
  64. }
  65. /**
  66. * Get modules by position
  67. *
  68. * @access public
  69. * @param string $position The position of the module
  70. * @return array An array of module objects
  71. */
  72. function &getModules($position)
  73. {
  74. $position = strtolower( $position );
  75. $result = array();
  76. $modules =& JModuleHelper::_load();
  77. $total = count($modules);
  78. for($i = 0; $i < $total; $i++) {
  79. if($modules[$i]->position == $position) {
  80. $result[] =& $modules[$i];
  81. }
  82. }
  83. if(count($result) == 0) {
  84. if(JRequest::getBool('tp')) {
  85. $result[0] = JModuleHelper::getModule( 'mod_'.$position );
  86. $result[0]->title = $position;
  87. $result[0]->content = $position;
  88. $result[0]->position = $position;
  89. }
  90. }
  91. return $result;
  92. }
  93. /**
  94. * Checks if a module is enabled
  95. *
  96. * @access public
  97. * @param string $module The module name
  98. * @return boolean
  99. */
  100. function isEnabled( $module )
  101. {
  102. $result = &JModuleHelper::getModule( $module);
  103. return (!is_null($result));
  104. }
  105. function renderModule($module, $attribs = array())
  106. {
  107. static $chrome;
  108. global $mainframe, $option;
  109. $scope = $mainframe->scope; //record the scope
  110. $mainframe->scope = $module->module; //set scope to component name
  111. // Handle legacy globals if enabled
  112. if ($mainframe->getCfg('legacy'))
  113. {
  114. // Include legacy globals
  115. global $my, $database, $acl, $mosConfig_absolute_path;
  116. // Get the task variable for local scope
  117. $task = JRequest::getString('task');
  118. // For backwards compatibility extract the config vars as globals
  119. $registry =& JFactory::getConfig();
  120. foreach (get_object_vars($registry->toObject()) as $k => $v) {
  121. $name = 'mosConfig_'.$k;
  122. $$name = $v;
  123. }
  124. $contentConfig = &JComponentHelper::getParams( 'com_content' );
  125. foreach (get_object_vars($contentConfig->toObject()) as $k => $v)
  126. {
  127. $name = 'mosConfig_'.$k;
  128. $$name = $v;
  129. }
  130. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  131. foreach (get_object_vars($usersConfig->toObject()) as $k => $v)
  132. {
  133. $name = 'mosConfig_'.$k;
  134. $$name = $v;
  135. }
  136. }
  137. // Get module parameters
  138. $params = new JParameter( $module->params );
  139. // Get module path
  140. $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
  141. $path = JPATH_BASE.DS.'modules'.DS.$module->module.DS.$module->module.'.php';
  142. // Load the module
  143. if (!$module->user && file_exists( $path ) && empty($module->content))
  144. {
  145. $lang =& JFactory::getLanguage();
  146. $lang->load($module->module);
  147. $content = '';
  148. ob_start();
  149. require $path;
  150. $module->content = ob_get_contents().$content;
  151. ob_end_clean();
  152. }
  153. // Load the module chrome functions
  154. if (!$chrome) {
  155. $chrome = array();
  156. }
  157. require_once (JPATH_BASE.DS.'templates'.DS.'system'.DS.'html'.DS.'modules.php');
  158. $chromePath = JPATH_BASE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.'modules.php';
  159. if (!isset( $chrome[$chromePath]))
  160. {
  161. if (file_exists($chromePath)) {
  162. require_once ($chromePath);
  163. }
  164. $chrome[$chromePath] = true;
  165. }
  166. //make sure a style is set
  167. if(!isset($attribs['style'])) {
  168. $attribs['style'] = 'none';
  169. }
  170. //dynamically add outline style
  171. if(JRequest::getBool('tp')) {
  172. $attribs['style'] .= ' outline';
  173. }
  174. foreach(explode(' ', $attribs['style']) as $style)
  175. {
  176. $chromeMethod = 'modChrome_'.$style;
  177. // Apply chrome and render module
  178. if (function_exists($chromeMethod))
  179. {
  180. $module->style = $attribs['style'];
  181. ob_start();
  182. $chromeMethod($module, $params, $attribs);
  183. $module->content = ob_get_contents();
  184. ob_end_clean();
  185. }
  186. }
  187. $mainframe->scope = $scope; //revert the scope
  188. return $module->content;
  189. }
  190. /**
  191. * Get the path to a layout for a module
  192. *
  193. * @static
  194. * @param string $module The name of the module
  195. * @param string $layout The name of the module layout
  196. * @return string The path to the module layout
  197. * @since 1.5
  198. */
  199. function getLayoutPath($module, $layout = 'default')
  200. {
  201. global $mainframe;
  202. // Build the template and base path for the layout
  203. $tPath = JPATH_BASE.DS.'templates'.DS.$mainframe->getTemplate().DS.'html'.DS.$module.DS.$layout.'.php';
  204. $bPath = JPATH_BASE.DS.'modules'.DS.$module.DS.'tmpl'.DS.$layout.'.php';
  205. // If the template has a layout override use it
  206. if (file_exists($tPath)) {
  207. return $tPath;
  208. } else {
  209. return $bPath;
  210. }
  211. }
  212. /**
  213. * Load published modules
  214. *
  215. * @access private
  216. * @return array
  217. */
  218. function &_load()
  219. {
  220. global $mainframe, $Itemid;
  221. static $modules;
  222. if (isset($modules)) {
  223. return $modules;
  224. }
  225. $user =& JFactory::getUser();
  226. $db =& JFactory::getDBO();
  227. $aid = $user->get('aid', 0);
  228. $modules = array();
  229. $wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
  230. $query = 'SELECT id, title, module, position, content, showtitle, control, params'
  231. . ' FROM #__modules AS m'
  232. . ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
  233. . ' WHERE m.published = 1'
  234. . ' AND m.access <= '. (int)$aid
  235. . ' AND m.client_id = '. (int)$mainframe->getClientId()
  236. . $wheremenu
  237. . ' ORDER BY position, ordering';
  238. $db->setQuery( $query );
  239. if (null === ($modules = $db->loadObjectList())) {
  240. JError::raiseWarning( 'SOME_ERROR_CODE', JText::_( 'Error Loading Modules' ) . $db->getErrorMsg());
  241. return false;
  242. }
  243. $total = count($modules);
  244. for($i = 0; $i < $total; $i++)
  245. {
  246. //determine if this is a custom module
  247. $file = $modules[$i]->module;
  248. $custom = substr( $file, 0, 4 ) == 'mod_' ? 0 : 1;
  249. $modules[$i]->user = $custom;
  250. // CHECK: custom module name is given by the title field, otherwise it's just 'om' ??
  251. $modules[$i]->name = $custom ? $modules[$i]->title : substr( $file, 4 );
  252. $modules[$i]->style = null;
  253. $modules[$i]->position = strtolower($modules[$i]->position);
  254. }
  255. return $modules;
  256. }
  257. }