Source for file helper.php

Documentation is available at helper.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Plugin
  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.  * Plugin helper class
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  Plugin
  17.  * @since       1.5
  18.  */
  19. abstract class JPluginHelper
  20. {
  21.     /**
  22.      * A persistent cache of the loaded plugins.
  23.      *
  24.      * @var    array 
  25.      * @since  1.7
  26.      */
  27.     protected static $plugins null;
  28.  
  29.     /**
  30.      * Get the path to a layout from a Plugin
  31.      *
  32.      * @param   string  $type    Plugin type
  33.      * @param   string  $name    Plugin name
  34.      * @param   string  $layout  Layout name
  35.      *
  36.      * @return  string  Layout path
  37.      *
  38.      * @since   3.0
  39.      */
  40.     public static function getLayoutPath($type$name$layout 'default')
  41.     {
  42.         $template JFactory::getApplication()->getTemplate();
  43.         $defaultLayout $layout;
  44.  
  45.         if (strpos($layout':'!== false)
  46.         {
  47.             // Get the template and file name from the string
  48.             $temp explode(':'$layout);
  49.             $template ($temp[0== '_'$template $temp[0];
  50.             $layout $temp[1];
  51.             $defaultLayout ($temp[1]$temp[1'default';
  52.         }
  53.  
  54.         // Build the template and base path for the layout
  55.         $tPath JPATH_THEMES '/' $template '/html/plg_' $type '_' $name '/' $layout '.php';
  56.         $bPath JPATH_BASE '/plugins/' $type '/' $name '/tmpl/' $defaultLayout '.php';
  57.         $dPath JPATH_BASE '/plugins/' $type '/' $name '/tmpl/default.php';
  58.  
  59.         // If the template has a layout override use it
  60.         if (file_exists($tPath))
  61.         {
  62.             return $tPath;
  63.         }
  64.         elseif (file_exists($bPath))
  65.         {
  66.             return $bPath;
  67.         }
  68.         else
  69.         {
  70.             return $dPath;
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Get the plugin data of a specific type if no specific plugin is specified
  76.      * otherwise only the specific plugin data is returned.
  77.      *
  78.      * @param   string  $type    The plugin type, relates to the sub-directory in the plugins directory.
  79.      * @param   string  $plugin  The plugin name.
  80.      *
  81.      * @return  mixed  An array of plugin data objects, or a plugin data object.
  82.      *
  83.      * @since   1.5
  84.      */
  85.     public static function getPlugin($type$plugin null)
  86.     {
  87.         $result array();
  88.         $plugins static::load();
  89.  
  90.         // Find the correct plugin(s) to return.
  91.         if (!$plugin)
  92.         {
  93.             foreach ($plugins as $p)
  94.             {
  95.                 // Is this the right plugin?
  96.                 if ($p->type == $type)
  97.                 {
  98.                     $result[$p;
  99.                 }
  100.             }
  101.         }
  102.         else
  103.         {
  104.             foreach ($plugins as $p)
  105.             {
  106.                 // Is this plugin in the right group?
  107.                 if ($p->type == $type && $p->name == $plugin)
  108.                 {
  109.                     $result $p;
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.  
  115.         return $result;
  116.     }
  117.  
  118.     /**
  119.      * Checks if a plugin is enabled.
  120.      *
  121.      * @param   string  $type    The plugin type, relates to the sub-directory in the plugins directory.
  122.      * @param   string  $plugin  The plugin name.
  123.      *
  124.      * @return  boolean 
  125.      *
  126.      * @since   1.5
  127.      */
  128.     public static function isEnabled($type$plugin null)
  129.     {
  130.         $result static::getPlugin($type$plugin);
  131.  
  132.         return (!empty($result));
  133.     }
  134.  
  135.     /**
  136.      * Loads all the plugin files for a particular type if no specific plugin is specified
  137.      * otherwise only the specific plugin is loaded.
  138.      *
  139.      * @param   string            $type        The plugin type, relates to the sub-directory in the plugins directory.
  140.      * @param   string            $plugin      The plugin name.
  141.      * @param   boolean           $autocreate  Autocreate the plugin.
  142.      * @param   JEventDispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
  143.      *
  144.      * @return  boolean  True on success.
  145.      *
  146.      * @since   1.5
  147.      */
  148.     public static function importPlugin($type$plugin null$autocreate trueJEventDispatcher $dispatcher null)
  149.     {
  150.         static $loaded array();
  151.  
  152.         // Check for the default args, if so we can optimise cheaply
  153.         $defaults false;
  154.  
  155.         if (is_null($plugin&& $autocreate == true && is_null($dispatcher))
  156.         {
  157.             $defaults true;
  158.         }
  159.  
  160.         if (!isset($loaded[$type]|| !$defaults)
  161.         {
  162.             $results null;
  163.  
  164.             // Load the plugins from the database.
  165.             $plugins static::load();
  166.  
  167.             // Get the specified plugin(s).
  168.             for ($i 0$t count($plugins)$i $t$i++)
  169.             {
  170.                 if ($plugins[$i]->type == $type && ($plugin === null || $plugins[$i]->name == $plugin))
  171.                 {
  172.                     static::import($plugins[$i]$autocreate$dispatcher);
  173.                     $results true;
  174.                 }
  175.             }
  176.  
  177.             // Bail out early if we're not using default args
  178.             if (!$defaults)
  179.             {
  180.                 return $results;
  181.             }
  182.  
  183.             $loaded[$type$results;
  184.         }
  185.  
  186.         return $loaded[$type];
  187.     }
  188.  
  189.     /**
  190.      * Loads the plugin file.
  191.      *
  192.      * @param   object            $plugin      The plugin.
  193.      * @param   boolean           $autocreate  True to autocreate.
  194.      * @param   JEventDispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
  195.      *
  196.      * @return  void 
  197.      *
  198.      * @since   1.5
  199.      * @deprecated  4.0  Use JPluginHelper::import() instead
  200.      */
  201.     protected static function _import($plugin$autocreate trueJEventDispatcher $dispatcher null)
  202.     {
  203.         static::import($plugin$autocreate$dispatcher);
  204.     }
  205.  
  206.     /**
  207.      * Loads the plugin file.
  208.      *
  209.      * @param   object            $plugin      The plugin.
  210.      * @param   boolean           $autocreate  True to autocreate.
  211.      * @param   JEventDispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
  212.      *
  213.      * @return  void 
  214.      *
  215.      * @since   3.2
  216.      */
  217.     protected static function import($plugin$autocreate trueJEventDispatcher $dispatcher null)
  218.     {
  219.         static $paths array();
  220.  
  221.         $plugin->type preg_replace('/[^A-Z0-9_\.-]/i'''$plugin->type);
  222.         $plugin->name preg_replace('/[^A-Z0-9_\.-]/i'''$plugin->name);
  223.  
  224.         $path JPATH_PLUGINS '/' $plugin->type '/' $plugin->name '/' $plugin->name '.php';
  225.  
  226.         if (!isset($paths[$path]))
  227.         {
  228.             if (file_exists($path))
  229.             {
  230.                 if (!isset($paths[$path]))
  231.                 {
  232.                     require_once $path;
  233.                 }
  234.  
  235.                 $paths[$pathtrue;
  236.  
  237.                 if ($autocreate)
  238.                 {
  239.                     // Makes sure we have an event dispatcher
  240.                     if (!is_object($dispatcher))
  241.                     {
  242.                         $dispatcher JEventDispatcher::getInstance();
  243.                     }
  244.  
  245.                     $className 'Plg' $plugin->type $plugin->name;
  246.  
  247.                     if (class_exists($className))
  248.                     {
  249.                         // Load the plugin from the database.
  250.                         if (!isset($plugin->params))
  251.                         {
  252.                             // Seems like this could just go bye bye completely
  253.                             $plugin static::getPlugin($plugin->type$plugin->name);
  254.                         }
  255.  
  256.                         // Instantiate and register the plugin.
  257.                         new $className($dispatcher(array) ($plugin));
  258.                     }
  259.                 }
  260.             }
  261.             else
  262.             {
  263.                 $paths[$pathfalse;
  264.             }
  265.         }
  266.     }
  267.  
  268.     /**
  269.      * Loads the published plugins.
  270.      *
  271.      * @return  array  An array of published plugins
  272.      *
  273.      * @since   1.5
  274.      * @deprecated  4.0  Use JPluginHelper::load() instead
  275.      */
  276.     protected static function _load()
  277.     {
  278.         return static::load();
  279.     }
  280.  
  281.     /**
  282.      * Loads the published plugins.
  283.      *
  284.      * @return  array  An array of published plugins
  285.      *
  286.      * @since   3.2
  287.      */
  288.     protected static function load()
  289.     {
  290.         if (static::$plugins !== null)
  291.         {
  292.             return static::$plugins;
  293.         }
  294.  
  295.         $user JFactory::getUser();
  296.         $cache JFactory::getCache('com_plugins''');
  297.  
  298.         $levels implode(','$user->getAuthorisedViewLevels());
  299.  
  300.         if (!static::$plugins $cache->get($levels))
  301.         {
  302.             $db JFactory::getDbo();
  303.             $query $db->getQuery(true)
  304.                 ->select('folder AS type, element AS name, params')
  305.                 ->from('#__extensions')
  306.                 ->where('enabled >= 1')
  307.                 ->where('type =' $db->quote('plugin'))
  308.                 ->where('state >= 0')
  309.                 ->where('access IN (' $levels ')')
  310.                 ->order('ordering');
  311.  
  312.             static::$plugins $db->setQuery($query)->loadObjectList();
  313.  
  314.             $cache->store(static::$plugins$levels);
  315.         }
  316.  
  317.         return static::$plugins;
  318.     }
  319. }

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