Source for file helper.php

Documentation is available at helper.php

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

Documentation generated on Tue, 19 Nov 2013 15:04:40 +0100 by phpDocumentor 1.4.3