[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/plugin/ -> helper.php (source)

   1  <?php
   2  /**
   3  * @version        $Id: helper.php 17261 2010-05-25 15:06:51Z ian $
   4  * @package        Joomla.Framework
   5  * @subpackage    Plugin
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19  * Plugin helper class
  20  *
  21  * @static
  22  * @package        Joomla.Framework
  23  * @subpackage    Plugin
  24  * @since        1.5
  25  */
  26  class JPluginHelper
  27  {
  28      /**
  29       * Get the plugin data of a specific type if no specific plugin is specified
  30       * otherwise only the specific plugin data is returned
  31       *
  32       * @access public
  33       * @param string     $type     The plugin type, relates to the sub-directory in the plugins directory
  34       * @param string     $plugin    The plugin name
  35       * @return mixed     An array of plugin data objects, or a plugin data object
  36       */
  37      function &getPlugin($type, $plugin = null)
  38      {
  39          $result = array();
  40  
  41          $plugins = JPluginHelper::_load();
  42  
  43          $total = count($plugins);
  44          for($i = 0; $i < $total; $i++)
  45          {
  46              if(is_null($plugin))
  47              {
  48                  if($plugins[$i]->type == $type) {
  49                      $result[] = $plugins[$i];
  50                  }
  51              }
  52              else
  53              {
  54                  if($plugins[$i]->type == $type && $plugins[$i]->name == $plugin) {
  55                      $result = $plugins[$i];
  56                      break;
  57                  }
  58              }
  59  
  60          }
  61  
  62          return $result;
  63      }
  64  
  65      /**
  66       * Checks if a plugin is enabled
  67       *
  68       * @access    public
  69       * @param string     $type     The plugin type, relates to the sub-directory in the plugins directory
  70       * @param string     $plugin    The plugin name
  71       * @return    boolean
  72       */
  73  	function isEnabled( $type, $plugin = null )
  74      {
  75          $result = &JPluginHelper::getPlugin( $type, $plugin);
  76          return (!empty($result));
  77      }
  78  
  79      /**
  80      * Loads all the plugin files for a particular type if no specific plugin is specified
  81      * otherwise only the specific pugin is loaded.
  82      *
  83      * @access public
  84      * @param string     $type     The plugin type, relates to the sub-directory in the plugins directory
  85      * @param string     $plugin    The plugin name
  86      * @return boolean True if success
  87      */
  88  	function importPlugin($type, $plugin = null, $autocreate = true, $dispatcher = null)
  89      {
  90          $result = false;
  91  
  92          $plugins = JPluginHelper::_load();
  93  
  94          $total = count($plugins);
  95          for($i = 0; $i < $total; $i++) {
  96              if($plugins[$i]->type == $type && ($plugins[$i]->name == $plugin ||  $plugin === null)) {
  97                  JPluginHelper::_import( $plugins[$i], $autocreate, $dispatcher );
  98                  $result = true;
  99              }
 100          }
 101  
 102          return $result;
 103      }
 104  
 105      /**
 106       * Loads the plugin file
 107       *
 108       * @access private
 109       * @return boolean True if success
 110       */
 111  	function _import( &$plugin, $autocreate = true, $dispatcher = null )
 112      {
 113          static $paths;
 114  
 115          if (!$paths) {
 116              $paths = array();
 117          }
 118  
 119          $result    = false;
 120          $plugin->type = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->type);
 121          $plugin->name  = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->name);
 122  
 123          $path    = JPATH_PLUGINS.DS.$plugin->type.DS.$plugin->name.'.php';
 124  
 125          if (!isset( $paths[$path] ))
 126          {
 127              if (file_exists( $path ))
 128              {
 129                  //needed for backwards compatibility
 130                  global $_MAMBOTS, $mainframe;
 131  
 132                  jimport('joomla.plugin.plugin');
 133                  require_once( $path );
 134                  $paths[$path] = true;
 135  
 136                  if($autocreate)
 137                  {
 138                      // Makes sure we have an event dispatcher
 139                      if(!is_object($dispatcher)) {
 140                          $dispatcher = & JDispatcher::getInstance();
 141                      }
 142  
 143                      $className = 'plg'.$plugin->type.$plugin->name;
 144                      if(class_exists($className))
 145                      {
 146                          // load plugin parameters
 147                          $plugin =& JPluginHelper::getPlugin($plugin->type, $plugin->name);
 148  
 149                          // create the plugin
 150                          $instance = new $className($dispatcher, (array)($plugin));
 151                      }
 152                  }
 153              }
 154              else
 155              {
 156                  $paths[$path] = false;
 157              }
 158          }
 159      }
 160  
 161      /**
 162       * Loads the published plugins
 163       *
 164       * @access private
 165       */
 166  	function _load()
 167      {
 168          static $plugins;
 169  
 170          if (isset($plugins)) {
 171              return $plugins;
 172          }
 173  
 174          $db        =& JFactory::getDBO();
 175          $user    =& JFactory::getUser();
 176  
 177          if (isset($user))
 178          {
 179              $aid = $user->get('aid', 0);
 180  
 181              $query = 'SELECT folder AS type, element AS name, params'
 182                  . ' FROM #__plugins'
 183                  . ' WHERE published >= 1'
 184                  . ' AND access <= ' . (int) $aid
 185                  . ' ORDER BY ordering';
 186          }
 187          else
 188          {
 189              $query = 'SELECT folder AS type, element AS name, params'
 190                  . ' FROM #__plugins'
 191                  . ' WHERE published >= 1'
 192                  . ' ORDER BY ordering';
 193          }
 194  
 195          $db->setQuery( $query );
 196  
 197          if (!($plugins = $db->loadObjectList())) {
 198              JError::raiseWarning( 'SOME_ERROR_CODE', "Error loading Plugins: " . $db->getErrorMsg());
 199              return false;
 200          }
 201  
 202          return $plugins;
 203      }
 204  
 205  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1