Source for file loadmodule.php

Documentation is available at loadmodule.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Content.loadmodule
  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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Plug-in to enable loading modules into content (e.g. articles)
  14.  * This uses the {loadmodule} syntax
  15.  *
  16.  * @package     Joomla.Plugin
  17.  * @subpackage  Content.loadmodule
  18.  * @since       1.5
  19.  */
  20. class PlgContentLoadmodule extends JPlugin
  21. {
  22.     protected static $modules array();
  23.  
  24.     protected static $mods array();
  25.  
  26.     /**
  27.      * Plugin that loads module positions within content
  28.      *
  29.      * @param   string   $context   The context of the content being passed to the plugin.
  30.      * @param   object   &$article  The article object.  Note $article->text is also available
  31.      * @param   mixed    &$params   The article params
  32.      * @param   integer  $page      The 'page' number
  33.      *
  34.      * @return  mixed   true if there is an error. Void otherwise.
  35.      *
  36.      * @since   1.6
  37.      */
  38.     public function onContentPrepare($context&$article&$params$page 0)
  39.     {
  40.         // Don't run this plugin when the content is being indexed
  41.         if ($context == 'com_finder.indexer')
  42.         {
  43.             return true;
  44.         }
  45.  
  46.         // Simple performance check to determine whether bot should process further
  47.         if (strpos($article->text'loadposition'=== false && strpos($article->text'loadmodule'=== false)
  48.         {
  49.             return true;
  50.         }
  51.  
  52.         // Expression to search for (positions)
  53.         $regex        '/{loadposition\s(.*?)}/i';
  54.         $style        $this->params->def('style''none');
  55.  
  56.         // Expression to search for(modules)
  57.         $regexmod    '/{loadmodule\s(.*?)}/i';
  58.         $stylemod    $this->params->def('style''none');
  59.  
  60.         // Find all instances of plugin and put in $matches for loadposition
  61.         // $matches[0] is full pattern match, $matches[1] is the position
  62.         preg_match_all($regex$article->text$matchesPREG_SET_ORDER);
  63.  
  64.         // No matches, skip this
  65.         if ($matches)
  66.         {
  67.             foreach ($matches as $match)
  68.             {
  69.                 $matcheslist explode(','$match[1]);
  70.  
  71.                 // We may not have a module style so fall back to the plugin default.
  72.                 if (!array_key_exists(1$matcheslist))
  73.                 {
  74.                     $matcheslist[1$style;
  75.                 }
  76.  
  77.                 $position trim($matcheslist[0]);
  78.                 $style    trim($matcheslist[1]);
  79.  
  80.                 $output $this->_load($position$style);
  81.  
  82.                 // We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
  83.                 $article->text preg_replace("|$match[0]|"addcslashes($output'\\$')$article->text1);
  84.                 $style $this->params->def('style''none');
  85.             }
  86.         }
  87.  
  88.         // Find all instances of plugin and put in $matchesmod for loadmodule
  89.         preg_match_all($regexmod$article->text$matchesmodPREG_SET_ORDER);
  90.  
  91.         // If no matches, skip this
  92.         if ($matchesmod)
  93.         {
  94.             foreach ($matchesmod as $matchmod)
  95.             {
  96.                 $matchesmodlist explode(','$matchmod[1]);
  97.  
  98.                 // We may not have a specific module so set to null
  99.                 if (!array_key_exists(1$matchesmodlist))
  100.                 {
  101.                     $matchesmodlist[1null;
  102.                 }
  103.  
  104.                 // We may not have a module style so fall back to the plugin default.
  105.                 if (!array_key_exists(2$matchesmodlist))
  106.                 {
  107.                     $matchesmodlist[2$stylemod;
  108.                 }
  109.  
  110.                 $module trim($matchesmodlist[0]);
  111.                 $name   htmlspecialchars_decode(trim($matchesmodlist[1]));
  112.                 $stylemod  trim($matchesmodlist[2]);
  113.  
  114.                 // $match[0] is full pattern match, $match[1] is the module,$match[2] is the title
  115.                 $output $this->_loadmod($module$name$stylemod);
  116.  
  117.                 // We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
  118.                 $article->text preg_replace("|$matchmod[0]|"addcslashes($output'\\$')$article->text1);
  119.                 $stylemod $this->params->def('style''none');
  120.             }
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Loads and renders the module
  126.      *
  127.      * @param   string  $position  The position assigned to the module
  128.      * @param   string  $style     The style assigned to the module
  129.      *
  130.      * @return  mixed 
  131.      *
  132.      * @since   1.6
  133.      */
  134.     protected function _load($position$style 'none')
  135.     {
  136.         self::$modules[$position'';
  137.         $document    JFactory::getDocument();
  138.         $renderer    $document->loadRenderer('module');
  139.         $modules    JModuleHelper::getModules($position);
  140.         $params        array('style' => $style);
  141.         ob_start();
  142.  
  143.         foreach ($modules as $module)
  144.         {
  145.             echo $renderer->render($module$params);
  146.         }
  147.  
  148.         self::$modules[$positionob_get_clean();
  149.  
  150.         return self::$modules[$position];
  151.     }
  152.  
  153.     /**
  154.      * This is always going to get the first instance of the module type unless
  155.      * there is a title.
  156.      *
  157.      * @param   string  $module  The module title
  158.      * @param   string  $title   The title of the module
  159.      * @param   string  $style   The style of the module
  160.      *
  161.      * @return  mixed 
  162.      *
  163.      * @since   1.6
  164.      */
  165.     protected function _loadmod($module$title$style 'none')
  166.     {
  167.         self::$mods[$module'';
  168.         $document    JFactory::getDocument();
  169.         $renderer    $document->loadRenderer('module');
  170.         $mod        JModuleHelper::getModule($module$title);
  171.  
  172.         // If the module without the mod_ isn't found, try it with mod_.
  173.         // This allows people to enter it either way in the content
  174.         if (!isset($mod))
  175.         {
  176.             $name 'mod_'.$module;
  177.             $mod  JModuleHelper::getModule($name$title);
  178.         }
  179.  
  180.         $params array('style' => $style);
  181.         ob_start();
  182.  
  183.         echo $renderer->render($mod$params);
  184.  
  185.         self::$mods[$moduleob_get_clean();
  186.  
  187.         return self::$mods[$module];
  188.     }
  189. }

Documentation generated on Tue, 19 Nov 2013 15:07:24 +0100 by phpDocumentor 1.4.3