PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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