[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/application/module/ -> helper.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Application
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.application.component.helper');
  13  
  14  /**
  15   * Module helper class
  16   *
  17   * @package     Joomla.Platform
  18   * @subpackage  Application
  19   * @since       11.1
  20   */
  21  abstract class JModuleHelper
  22  {
  23      /**
  24       * Get module by name (real, eg 'Breadcrumbs' or folder, eg 'mod_breadcrumbs')
  25       *
  26       * @param   string  $name   The name of the module
  27       * @param   string  $title  The title of the module, optional
  28       *
  29       * @return  object  The Module object
  30       *
  31       * @since   11.1
  32       */
  33      public static function &getModule($name, $title = null)
  34      {
  35          $result = null;
  36          $modules =& JModuleHelper::_load();
  37          $total = count($modules);
  38  
  39          for ($i = 0; $i < $total; $i++)
  40          {
  41              // Match the name of the module
  42              if ($modules[$i]->name == $name || $modules[$i]->module == $name)
  43              {
  44                  // Match the title if we're looking for a specific instance of the module
  45                  if (!$title || $modules[$i]->title == $title)
  46                  {
  47                      // Found it
  48                      $result = &$modules[$i];
  49                      break; // Found it
  50                  }
  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  
  69          return $result;
  70      }
  71  
  72      /**
  73       * Get modules by position
  74       *
  75       * @param   string  $position  The position of the module
  76       *
  77       * @return  array  An array of module objects
  78       *
  79       * @since   11.1
  80       */
  81      public static function &getModules($position)
  82      {
  83          $position = strtolower($position);
  84          $result = array();
  85  
  86          $modules =& JModuleHelper::_load();
  87  
  88          $total = count($modules);
  89          for ($i = 0; $i < $total; $i++)
  90          {
  91              if ($modules[$i]->position == $position)
  92              {
  93                  $result[] = &$modules[$i];
  94              }
  95          }
  96  
  97          if (count($result) == 0)
  98          {
  99              if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
 100              {
 101                  $result[0] = JModuleHelper::getModule('mod_' . $position);
 102                  $result[0]->title = $position;
 103                  $result[0]->content = $position;
 104                  $result[0]->position = $position;
 105              }
 106          }
 107  
 108          return $result;
 109      }
 110  
 111      /**
 112       * Checks if a module is enabled
 113       *
 114       * @param   string  $module  The module name
 115       *
 116       * @return  boolean
 117       *
 118       * @since   11.1
 119       */
 120  	public static function isEnabled($module)
 121      {
 122          $result = JModuleHelper::getModule($module);
 123  
 124          return !is_null($result);
 125      }
 126  
 127      /**
 128       * Render the module.
 129       *
 130       * @param   object  $module   A module object.
 131       * @param   array   $attribs  An array of attributes for the module (probably from the XML).
 132       *
 133       * @return  string  The HTML content of the module output.
 134       *
 135       * @since   11.1
 136       */
 137  	public static function renderModule($module, $attribs = array())
 138      {
 139          static $chrome;
 140  
 141          if (constant('JDEBUG'))
 142          {
 143              JProfiler::getInstance('Application')->mark('beforeRenderModule ' . $module->module . ' (' . $module->title . ')');
 144          }
 145  
 146          $app = JFactory::getApplication();
 147  
 148          // Record the scope.
 149          $scope = $app->scope;
 150  
 151          // Set scope to component name
 152          $app->scope = $module->module;
 153  
 154          // Get module parameters
 155          $params = new JRegistry;
 156          $params->loadString($module->params);
 157  
 158          // Get module path
 159          $module->module = preg_replace('/[^A-Z0-9_\.-]/i', '', $module->module);
 160          $path = JPATH_BASE . '/modules/' . $module->module . '/' . $module->module . '.php';
 161  
 162          // Load the module
 163          // $module->user is a check for 1.0 custom modules and is deprecated refactoring
 164          if (empty($module->user) && file_exists($path))
 165          {
 166              $lang = JFactory::getLanguage();
 167              // 1.5 or Core then 1.6 3PD
 168              $lang->load($module->module, JPATH_BASE, null, false, false) ||
 169                  $lang->load($module->module, dirname($path), null, false, false) ||
 170                  $lang->load($module->module, JPATH_BASE, $lang->getDefault(), false, false) ||
 171                  $lang->load($module->module, dirname($path), $lang->getDefault(), false, false);
 172  
 173              $content = '';
 174              ob_start();
 175              include $path;
 176              $module->content = ob_get_contents() . $content;
 177              ob_end_clean();
 178          }
 179  
 180          // Load the module chrome functions
 181          if (!$chrome)
 182          {
 183              $chrome = array();
 184          }
 185  
 186          include_once JPATH_THEMES . '/system/html/modules.php';
 187          $chromePath = JPATH_THEMES . '/' . $app->getTemplate() . '/html/modules.php';
 188  
 189          if (!isset($chrome[$chromePath]))
 190          {
 191              if (file_exists($chromePath))
 192              {
 193                  include_once $chromePath;
 194              }
 195  
 196              $chrome[$chromePath] = true;
 197          }
 198  
 199          // Make sure a style is set
 200          if (!isset($attribs['style']))
 201          {
 202              $attribs['style'] = 'none';
 203          }
 204  
 205          // Dynamically add outline style
 206          if (JRequest::getBool('tp') && JComponentHelper::getParams('com_templates')->get('template_positions_display'))
 207          {
 208              $attribs['style'] .= ' outline';
 209          }
 210  
 211          foreach (explode(' ', $attribs['style']) as $style)
 212          {
 213              $chromeMethod = 'modChrome_' . $style;
 214  
 215              // Apply chrome and render module
 216              if (function_exists($chromeMethod))
 217              {
 218                  $module->style = $attribs['style'];
 219  
 220                  ob_start();
 221                  $chromeMethod($module, $params, $attribs);
 222                  $module->content = ob_get_contents();
 223                  ob_end_clean();
 224              }
 225          }
 226  
 227          //revert the scope
 228          $app->scope = $scope;
 229  
 230          if (constant('JDEBUG'))
 231          {
 232              JProfiler::getInstance('Application')->mark('afterRenderModule ' . $module->module . ' (' . $module->title . ')');
 233          }
 234  
 235          return $module->content;
 236      }
 237  
 238      /**
 239       * Get the path to a layout for a module
 240       *
 241       * @param   string  $module  The name of the module
 242       * @param   string  $layout  The name of the module layout. If alternative layout, in the form template:filename.
 243       *
 244       * @return  string  The path to the module layout
 245       *
 246       * @since   11.1
 247       */
 248  	public static function getLayoutPath($module, $layout = 'default')
 249      {
 250          $template = JFactory::getApplication()->getTemplate();
 251          $defaultLayout = $layout;
 252  
 253          if (strpos($layout, ':') !== false)
 254          {
 255              // Get the template and file name from the string
 256              $temp = explode(':', $layout);
 257              $template = ($temp[0] == '_') ? $template : $temp[0];
 258              $layout = $temp[1];
 259              $defaultLayout = ($temp[1]) ? $temp[1] : 'default';
 260          }
 261  
 262          // Build the template and base path for the layout
 263          $tPath = JPATH_THEMES . '/' . $template . '/html/' . $module . '/' . $layout . '.php';
 264          $bPath = JPATH_BASE . '/modules/' . $module . '/tmpl/' . $defaultLayout . '.php';
 265  
 266          // If the template has a layout override use it
 267          if (file_exists($tPath))
 268          {
 269              return $tPath;
 270          }
 271          else
 272          {
 273              return $bPath;
 274          }
 275      }
 276  
 277      /**
 278       * Load published modules.
 279       *
 280       * @return  array
 281       *
 282       * @since   11.1
 283       */
 284      protected static function &_load()
 285      {
 286          static $clean;
 287  
 288          if (isset($clean))
 289          {
 290              return $clean;
 291          }
 292  
 293          $Itemid = JRequest::getInt('Itemid');
 294          $app = JFactory::getApplication();
 295          $user = JFactory::getUser();
 296          $groups = implode(',', $user->getAuthorisedViewLevels());
 297          $lang = JFactory::getLanguage()->getTag();
 298          $clientId = (int) $app->getClientId();
 299  
 300          $cache = JFactory::getCache('com_modules', '');
 301          $cacheid = md5(serialize(array($Itemid, $groups, $clientId, $lang)));
 302  
 303          if (!($clean = $cache->get($cacheid)))
 304          {
 305              $db = JFactory::getDbo();
 306  
 307              $query = $db->getQuery(true);
 308              $query->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid');
 309              $query->from('#__modules AS m');
 310              $query->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id');
 311              $query->where('m.published = 1');
 312  
 313              $query->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id');
 314              $query->where('e.enabled = 1');
 315  
 316              $date = JFactory::getDate();
 317              $now = $date->toSql();
 318              $nullDate = $db->getNullDate();
 319              $query->where('(m.publish_up = ' . $db->Quote($nullDate) . ' OR m.publish_up <= ' . $db->Quote($now) . ')');
 320              $query->where('(m.publish_down = ' . $db->Quote($nullDate) . ' OR m.publish_down >= ' . $db->Quote($now) . ')');
 321  
 322              $query->where('m.access IN (' . $groups . ')');
 323              $query->where('m.client_id = ' . $clientId);
 324              $query->where('(mm.menuid = ' . (int) $Itemid . ' OR mm.menuid <= 0)');
 325  
 326              // Filter by language
 327              if ($app->isSite() && $app->getLanguageFilter())
 328              {
 329                  $query->where('m.language IN (' . $db->Quote($lang) . ',' . $db->Quote('*') . ')');
 330              }
 331  
 332              $query->order('m.position, m.ordering');
 333  
 334              // Set the query
 335              $db->setQuery($query);
 336              $modules = $db->loadObjectList();
 337              $clean = array();
 338  
 339              if ($db->getErrorNum())
 340              {
 341                  JError::raiseWarning(500, JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $db->getErrorMsg()));
 342                  return $clean;
 343              }
 344  
 345              // Apply negative selections and eliminate duplicates
 346              $negId = $Itemid ? -(int) $Itemid : false;
 347              $dupes = array();
 348              for ($i = 0, $n = count($modules); $i < $n; $i++)
 349              {
 350                  $module = &$modules[$i];
 351  
 352                  // The module is excluded if there is an explicit prohibition
 353                  $negHit = ($negId === (int) $module->menuid);
 354  
 355                  if (isset($dupes[$module->id]))
 356                  {
 357                      // If this item has been excluded, keep the duplicate flag set,
 358                      // but remove any item from the cleaned array.
 359                      if ($negHit)
 360                      {
 361                          unset($clean[$module->id]);
 362                      }
 363                      continue;
 364                  }
 365  
 366                  $dupes[$module->id] = true;
 367  
 368                  // Only accept modules without explicit exclusions.
 369                  if (!$negHit)
 370                  {
 371                      // Determine if this is a 1.0 style custom module (no mod_ prefix)
 372                      // This should be eliminated when the class is refactored.
 373                      // $module->user is deprecated.
 374                      $file = $module->module;
 375                      $custom = substr($file, 0, 4) == 'mod_' ?  0 : 1;
 376                      $module->user = $custom;
 377                      // 1.0 style custom module name is given by the title field, otherwise strip off "mod_"
 378                      $module->name = $custom ? $module->module : substr($file, 4);
 379                      $module->style = null;
 380                      $module->position = strtolower($module->position);
 381                      $clean[$module->id] = $module;
 382                  }
 383              }
 384  
 385              unset($dupes);
 386  
 387              // Return to simple indexing that matches the query order.
 388              $clean = array_values($clean);
 389  
 390              $cache->store($clean, $cacheid);
 391          }
 392  
 393          return $clean;
 394      }
 395  
 396      /**
 397       * Module cache helper
 398       *
 399       * Caching modes:
 400       * To be set in XML:
 401       * 'static'      One cache file for all pages with the same module parameters
 402       * 'oldstatic'   1.5 definition of module caching, one cache file for all pages
 403       * with the same module id and user aid,
 404       * 'itemid'      Changes on itemid change, to be called from inside the module:
 405       * 'safeuri'     Id created from $cacheparams->modeparams array,
 406       * 'id'          Module sets own cache id's
 407       *
 408       * @param   object  $module        Module object
 409       * @param   object  $moduleparams  Module parameters
 410       * @param   object  $cacheparams   Module cache parameters - id or url parameters, depending on the module cache mode
 411       *
 412       * @return  string
 413       *
 414       * @since   11.1
 415       *
 416       * @link JFilterInput::clean()
 417       */
 418  	public static function moduleCache($module, $moduleparams, $cacheparams)
 419      {
 420          if (!isset($cacheparams->modeparams))
 421          {
 422              $cacheparams->modeparams = null;
 423          }
 424  
 425          if (!isset($cacheparams->cachegroup))
 426          {
 427              $cacheparams->cachegroup = $module->module;
 428          }
 429  
 430          $user = JFactory::getUser();
 431          $cache = JFactory::getCache($cacheparams->cachegroup, 'callback');
 432          $conf = JFactory::getConfig();
 433  
 434          // Turn cache off for internal callers if parameters are set to off and for all logged in users
 435          if ($moduleparams->get('owncache', null) === '0' || $conf->get('caching') == 0 || $user->get('id'))
 436          {
 437              $cache->setCaching(false);
 438          }
 439  
 440          // module cache is set in seconds, global cache in minutes, setLifeTime works in minutes
 441          $cache->setLifeTime($moduleparams->get('cache_time', $conf->get('cachetime') * 60) / 60);
 442  
 443          $wrkaroundoptions = array('nopathway' => 1, 'nohead' => 0, 'nomodules' => 1, 'modulemode' => 1, 'mergehead' => 1);
 444  
 445          $wrkarounds = true;
 446          $view_levels = md5(serialize($user->getAuthorisedViewLevels()));
 447  
 448          switch ($cacheparams->cachemode)
 449          {
 450              case 'id':
 451                  $ret = $cache->get(
 452                      array($cacheparams->class, $cacheparams->method),
 453                      $cacheparams->methodparams,
 454                      $cacheparams->modeparams,
 455                      $wrkarounds,
 456                      $wrkaroundoptions
 457                  );
 458                  break;
 459  
 460              case 'safeuri':
 461                  $secureid = null;
 462                  if (is_array($cacheparams->modeparams))
 463                  {
 464                      $uri = JRequest::get();
 465                      $safeuri = new stdClass;
 466                      foreach ($cacheparams->modeparams as $key => $value)
 467                      {
 468                          // Use int filter for id/catid to clean out spamy slugs
 469                          if (isset($uri[$key]))
 470                          {
 471                              $safeuri->$key = JRequest::_cleanVar($uri[$key], 0, $value);
 472                          }
 473                      }
 474                  }
 475                  $secureid = md5(serialize(array($safeuri, $cacheparams->method, $moduleparams)));
 476                  $ret = $cache->get(
 477                      array($cacheparams->class, $cacheparams->method),
 478                      $cacheparams->methodparams,
 479                      $module->id . $view_levels . $secureid,
 480                      $wrkarounds,
 481                      $wrkaroundoptions
 482                  );
 483                  break;
 484  
 485              case 'static':
 486                  $ret = $cache->get(
 487                      array($cacheparams->class,
 488                          $cacheparams->method),
 489                      $cacheparams->methodparams,
 490                      $module->module . md5(serialize($cacheparams->methodparams)),
 491                      $wrkarounds,
 492                      $wrkaroundoptions
 493                  );
 494                  break;
 495  
 496              case 'oldstatic': // provided for backward compatibility, not really usefull
 497                  $ret = $cache->get(
 498                      array($cacheparams->class, $cacheparams->method),
 499                      $cacheparams->methodparams,
 500                      $module->id . $view_levels,
 501                      $wrkarounds,
 502                      $wrkaroundoptions
 503                  );
 504                  break;
 505  
 506              case 'itemid':
 507              default:
 508                  $ret = $cache->get(
 509                      array($cacheparams->class, $cacheparams->method),
 510                      $cacheparams->methodparams,
 511                      $module->id . $view_levels . JRequest::getVar('Itemid', null, 'default', 'INT'),
 512                      $wrkarounds,
 513                      $wrkaroundoptions
 514                  );
 515                  break;
 516          }
 517  
 518          return $ret;
 519      }
 520  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1