|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package RedCORE.Plugin |
| 4 | + * @subpackage System.MVCOverride |
| 5 | + * |
| 6 | + * @copyright Copyright (C) 2008 - 2017 redCOMPONENT.com. All rights reserved. |
| 7 | + * @license GNU General Public License version 2 or later; see LICENSE |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Joomla\CMS\Helper; |
| 11 | + |
| 12 | +defined('JPATH_PLATFORM') or die; |
| 13 | + |
| 14 | +use Joomla\CMS\Component\ComponentHelper; |
| 15 | +use Joomla\Registry\Registry; |
| 16 | +use Joomla\Filesystem\Path; |
| 17 | + |
| 18 | +/** |
| 19 | + * Module helper class |
| 20 | + * |
| 21 | + * @since 1.5 |
| 22 | + */ |
| 23 | +abstract class ModuleHelper extends LIB_ModuleHelperDefault |
| 24 | +{ |
| 25 | + /** |
| 26 | + * An array to hold included paths |
| 27 | + * |
| 28 | + * @var array |
| 29 | + * @since 1.5 |
| 30 | + */ |
| 31 | + protected static $includePaths = array(); |
| 32 | + |
| 33 | + /** |
| 34 | + * Render the module. |
| 35 | + * |
| 36 | + * @param object $module A module object. |
| 37 | + * @param array $attribs An array of attributes for the module (probably from the XML). |
| 38 | + * |
| 39 | + * @return string The HTML content of the module output. |
| 40 | + * |
| 41 | + * @since 1.5 |
| 42 | + */ |
| 43 | + public static function renderModule($module, $attribs = array()) |
| 44 | + { |
| 45 | + static $chrome; |
| 46 | + |
| 47 | + // Check that $module is a valid module object |
| 48 | + if (!is_object($module) || !isset($module->module) || !isset($module->params)) |
| 49 | + { |
| 50 | + if (JDEBUG) |
| 51 | + { |
| 52 | + \JLog::addLogger(array('text_file' => 'jmodulehelper.log.php'), \JLog::ALL, array('modulehelper')); |
| 53 | + \JLog::add('ModuleHelper::renderModule($module) expects a module object', \JLog::DEBUG, 'modulehelper'); |
| 54 | + } |
| 55 | + |
| 56 | + return ''; |
| 57 | + } |
| 58 | + |
| 59 | + if (JDEBUG) |
| 60 | + { |
| 61 | + \JProfiler::getInstance('Application')->mark('beforeRenderModule ' . $module->module . ' (' . $module->title . ')'); |
| 62 | + } |
| 63 | + |
| 64 | + $app = \JFactory::getApplication(); |
| 65 | + |
| 66 | + // Record the scope. |
| 67 | + $scope = $app->scope; |
| 68 | + |
| 69 | + // Set scope to component name |
| 70 | + $app->scope = $module->module; |
| 71 | + |
| 72 | + // Get module parameters |
| 73 | + $params = new Registry($module->params); |
| 74 | + |
| 75 | + // Get the template |
| 76 | + $template = $app->getTemplate(); |
| 77 | + |
| 78 | + // Get module path |
| 79 | + $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module); |
| 80 | + $path = JPATH_BASE . '/modules/' . $module->module . '/' . $module->module . '.php'; |
| 81 | + |
| 82 | + // Load the module |
| 83 | + if (file_exists($path)) |
| 84 | + { |
| 85 | + $lang = \JFactory::getLanguage(); |
| 86 | + |
| 87 | + $coreLanguageDirectory = JPATH_BASE; |
| 88 | + $extensionLanguageDirectory = dirname($path); |
| 89 | + |
| 90 | + $langPaths = $lang->getPaths(); |
| 91 | + |
| 92 | + // Only load the module's language file if it hasn't been already |
| 93 | + if (!$langPaths || (!isset($langPaths[$coreLanguageDirectory]) && !isset($langPaths[$extensionLanguageDirectory]))) |
| 94 | + { |
| 95 | + // 1.5 or Core then 1.6 3PD |
| 96 | + $lang->load($module->module, $coreLanguageDirectory, null, false, true) || |
| 97 | + $lang->load($module->module, $extensionLanguageDirectory, null, false, true); |
| 98 | + } |
| 99 | + |
| 100 | + $paths = array(JPATH_BASE . '/modules'); |
| 101 | + $paths = array_merge(self::addIncludePath(), $paths); |
| 102 | + $filePath = Path::find($paths, $module->module . '/' . $module->module . '.php'); |
| 103 | + |
| 104 | + if ($filePath) |
| 105 | + { |
| 106 | + $content = ''; |
| 107 | + ob_start(); |
| 108 | + include $filePath; |
| 109 | + $module->content = ob_get_contents() . $content; |
| 110 | + ob_end_clean(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Load the module chrome functions |
| 115 | + if (!$chrome) |
| 116 | + { |
| 117 | + $chrome = array(); |
| 118 | + } |
| 119 | + |
| 120 | + include_once JPATH_THEMES . '/system/html/modules.php'; |
| 121 | + $chromePath = JPATH_THEMES . '/' . $template . '/html/modules.php'; |
| 122 | + |
| 123 | + if (!isset($chrome[$chromePath])) |
| 124 | + { |
| 125 | + if (file_exists($chromePath)) |
| 126 | + { |
| 127 | + include_once $chromePath; |
| 128 | + } |
| 129 | + |
| 130 | + $chrome[$chromePath] = true; |
| 131 | + } |
| 132 | + |
| 133 | + // Check if the current module has a style param to override template module style |
| 134 | + $paramsChromeStyle = $params->get('style'); |
| 135 | + |
| 136 | + if ($paramsChromeStyle) |
| 137 | + { |
| 138 | + $attribs['style'] = preg_replace('/^(system|' . $template . ')\-/i', '', $paramsChromeStyle); |
| 139 | + } |
| 140 | + |
| 141 | + // Make sure a style is set |
| 142 | + if (!isset($attribs['style'])) |
| 143 | + { |
| 144 | + $attribs['style'] = 'none'; |
| 145 | + } |
| 146 | + |
| 147 | + // Dynamically add outline style |
| 148 | + if ($app->input->getBool('tp') && ComponentHelper::getParams('com_templates')->get('template_positions_display')) |
| 149 | + { |
| 150 | + $attribs['style'] .= ' outline'; |
| 151 | + } |
| 152 | + |
| 153 | + // If the $module is nulled it will return an empty content, otherwise it will render the module normally. |
| 154 | + $app->triggerEvent('onRenderModule', array(&$module, &$attribs)); |
| 155 | + |
| 156 | + if ($module === null || !isset($module->content)) |
| 157 | + { |
| 158 | + return ''; |
| 159 | + } |
| 160 | + |
| 161 | + foreach (explode(' ', $attribs['style']) as $style) |
| 162 | + { |
| 163 | + $chromeMethod = 'modChrome_' . $style; |
| 164 | + |
| 165 | + // Apply chrome and render module |
| 166 | + if (function_exists($chromeMethod)) |
| 167 | + { |
| 168 | + $module->style = $attribs['style']; |
| 169 | + |
| 170 | + ob_start(); |
| 171 | + $chromeMethod($module, $params, $attribs); |
| 172 | + $module->content = ob_get_contents(); |
| 173 | + ob_end_clean(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Revert the scope |
| 178 | + $app->scope = $scope; |
| 179 | + |
| 180 | + $app->triggerEvent('onAfterRenderModule', array(&$module, &$attribs)); |
| 181 | + |
| 182 | + if (JDEBUG) |
| 183 | + { |
| 184 | + \JProfiler::getInstance('Application')->mark('afterRenderModule ' . $module->module . ' (' . $module->title . ')'); |
| 185 | + } |
| 186 | + |
| 187 | + return $module->content; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Get the path to a layout for a module |
| 192 | + * |
| 193 | + * @param string $module The name of the module |
| 194 | + * @param string $layout The name of the module layout. If alternative layout, in the form template:filename. |
| 195 | + * |
| 196 | + * @return string The path to the module layout |
| 197 | + * |
| 198 | + * @since 1.5 |
| 199 | + */ |
| 200 | + public static function getLayoutPath($module, $layout = 'default') |
| 201 | + { |
| 202 | + $template = \JFactory::getApplication()->getTemplate(); |
| 203 | + $defaultLayout = $layout; |
| 204 | + $templatePaths = array(); |
| 205 | + |
| 206 | + if (strpos($layout, ':') !== false) |
| 207 | + { |
| 208 | + // Get the template and file name from the string |
| 209 | + $temp = explode(':', $layout); |
| 210 | + $template = $temp[0] === '_' ? $template : $temp[0]; |
| 211 | + $layout = $temp[1]; |
| 212 | + $defaultLayout = $temp[1] ?: 'default'; |
| 213 | + } |
| 214 | + |
| 215 | + foreach (self::addIncludePath() as $onePath) |
| 216 | + { |
| 217 | + $templatePaths[] = $onePath . '/' . $module; |
| 218 | + } |
| 219 | + |
| 220 | + $templatePaths[] = JPATH_THEMES . '/' . $template . '/html/' . $module; |
| 221 | + $templatePaths[] = JPATH_BASE . '/modules/' . $module; |
| 222 | + |
| 223 | + // If the template has a layout override use it |
| 224 | + if ($tPath = Path::find($templatePaths, $layout . '.php')) |
| 225 | + { |
| 226 | + return $tPath; |
| 227 | + } |
| 228 | + elseif ($tPath = Path::find($templatePaths, 'tmpl/' . $defaultLayout . '.php')) |
| 229 | + { |
| 230 | + return $tPath; |
| 231 | + } |
| 232 | + else |
| 233 | + { |
| 234 | + return JPATH_BASE . '/modules/' . $module . '/tmpl/default.php'; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Add a directory where JModuleHelper should search for module. You may |
| 240 | + * either pass a string or an array of directories. |
| 241 | + * |
| 242 | + * @param string $path A path to search. |
| 243 | + * |
| 244 | + * @return array An array with directory elements |
| 245 | + * |
| 246 | + * @since 1.5 |
| 247 | + */ |
| 248 | + public static function addIncludePath($path = '') |
| 249 | + { |
| 250 | + if ($path) |
| 251 | + { |
| 252 | + // Force path to array |
| 253 | + settype($path, 'array'); |
| 254 | + |
| 255 | + // Loop through the path directories |
| 256 | + foreach ($path as $dir) |
| 257 | + { |
| 258 | + if (!empty($dir) && !in_array(Path::clean($dir), self::$includePaths)) |
| 259 | + { |
| 260 | + array_unshift(self::$includePaths, Path::clean($dir)); |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + return self::$includePaths; |
| 266 | + } |
| 267 | +} |
0 commit comments