[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_plugins/models/ -> plugin.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // No direct access.
   8  defined('_JEXEC') or die;
   9  
  10  jimport('joomla.application.component.modeladmin');
  11  
  12  /**
  13   * Plugin model.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_plugins
  17   * @since        1.6
  18   */
  19  class PluginsModelPlugin extends JModelAdmin
  20  {
  21      /**
  22       * @var        string    The help screen key for the module.
  23       * @since    1.6
  24       */
  25      protected $helpKey = 'JHELP_EXTENSIONS_PLUGIN_MANAGER_EDIT';
  26  
  27      /**
  28       * @var        string    The help screen base URL for the module.
  29       * @since    1.6
  30       */
  31      protected $helpURL;
  32  
  33      protected $_cache;
  34  
  35      /**
  36       * @var        string    The event to trigger after saving the data.
  37       * @since    1.6
  38       */
  39      protected $event_after_save = 'onExtensionAfterSave';
  40  
  41      /**
  42       * @var        string    The event to trigger after before the data.
  43       * @since    1.6
  44       */
  45      protected $event_before_save = 'onExtensionBeforeSave';
  46  
  47      /**
  48       * Method to get the record form.
  49       *
  50       * @param    array    $data        Data for the form.
  51       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
  52       * @return    JForm    A JForm object on success, false on failure
  53       * @since    1.6
  54       */
  55  	public function getForm($data = array(), $loadData = true)
  56      {
  57          // The folder and element vars are passed when saving the form.
  58          if (empty($data)) {
  59              $item        = $this->getItem();
  60              $folder        = $item->folder;
  61              $element    = $item->element;
  62          } else {
  63              $folder        = JArrayHelper::getValue($data, 'folder', '', 'cmd');
  64              $element    = JArrayHelper::getValue($data, 'element', '', 'cmd');
  65          }
  66  
  67          // These variables are used to add data from the plugin XML files.
  68          $this->setState('item.folder',    $folder);
  69          $this->setState('item.element',    $element);
  70  
  71          // Get the form.
  72          $form = $this->loadForm('com_plugins.plugin', 'plugin', array('control' => 'jform', 'load_data' => $loadData));
  73          if (empty($form)) {
  74              return false;
  75          }
  76  
  77          // Modify the form based on access controls.
  78          if (!$this->canEditState((object) $data)) {
  79              // Disable fields for display.
  80              $form->setFieldAttribute('ordering', 'disabled', 'true');
  81              $form->setFieldAttribute('enabled', 'disabled', 'true');
  82  
  83              // Disable fields while saving.
  84              // The controller has already verified this is a record you can edit.
  85              $form->setFieldAttribute('ordering', 'filter', 'unset');
  86              $form->setFieldAttribute('enabled', 'filter', 'unset');
  87          }
  88  
  89          return $form;
  90      }
  91  
  92      /**
  93       * Method to get the data that should be injected in the form.
  94       *
  95       * @return    mixed    The data for the form.
  96       * @since    1.6
  97       */
  98  	protected function loadFormData()
  99      {
 100          // Check the session for previously entered form data.
 101          $data = JFactory::getApplication()->getUserState('com_plugins.edit.plugin.data', array());
 102  
 103          if (empty($data)) {
 104              $data = $this->getItem();
 105          }
 106  
 107          return $data;
 108      }
 109  
 110      /**
 111       * Method to get a single record.
 112       *
 113       * @param    integer    The id of the primary key.
 114       *
 115       * @return    mixed    Object on success, false on failure.
 116       */
 117  	public function getItem($pk = null)
 118      {
 119          // Initialise variables.
 120          $pk = (!empty($pk)) ? $pk : (int) $this->getState('plugin.id');
 121  
 122          if (!isset($this->_cache[$pk])) {
 123              $false    = false;
 124  
 125              // Get a row instance.
 126              $table = $this->getTable();
 127  
 128              // Attempt to load the row.
 129              $return = $table->load($pk);
 130  
 131              // Check for a table object error.
 132              if ($return === false && $table->getError()) {
 133                  $this->setError($table->getError());
 134                  return $false;
 135              }
 136  
 137              // Convert to the JObject before adding other data.
 138              $properties = $table->getProperties(1);
 139              $this->_cache[$pk] = JArrayHelper::toObject($properties, 'JObject');
 140  
 141              // Convert the params field to an array.
 142              $registry = new JRegistry;
 143              $registry->loadString($table->params);
 144              $this->_cache[$pk]->params = $registry->toArray();
 145  
 146              // Get the plugin XML.
 147              $path = JPath::clean(JPATH_PLUGINS.'/'.$table->folder.'/'.$table->element.'/'.$table->element.'.xml');
 148  
 149              if (file_exists($path)) {
 150                  $this->_cache[$pk]->xml = JFactory::getXML($path);
 151              } else {
 152                  $this->_cache[$pk]->xml = null;
 153              }
 154          }
 155  
 156          return $this->_cache[$pk];
 157      }
 158  
 159      /**
 160       * Returns a reference to the a Table object, always creating it.
 161       *
 162       * @param    type    The table type to instantiate
 163       * @param    string    A prefix for the table class name. Optional.
 164       * @param    array    Configuration array for model. Optional.
 165       * @return    JTable    A database object
 166      */
 167  	public function getTable($type = 'Extension', $prefix = 'JTable', $config = array())
 168      {
 169          return JTable::getInstance($type, $prefix, $config);
 170      }
 171  
 172      /**
 173       * Auto-populate the model state.
 174       *
 175       * Note. Calling getState in this method will result in recursion.
 176       *
 177       * @return    void
 178       * @since    1.6
 179       */
 180  	protected function populateState()
 181      {
 182          // Execute the parent method.
 183          parent::populateState();
 184  
 185          $app = JFactory::getApplication('administrator');
 186  
 187          // Load the User state.
 188          $pk = (int) JRequest::getInt('extension_id');
 189          $this->setState('plugin.id', $pk);
 190      }
 191  
 192      /**
 193       * @param    object    A form object.
 194       * @param    mixed    The data expected for the form.
 195       * @return    mixed    True if successful.
 196       * @throws    Exception if there is an error in the form event.
 197       * @since    1.6
 198       */
 199  	protected function preprocessForm(JForm $form, $data, $group = 'content')
 200      {
 201          jimport('joomla.filesystem.file');
 202          jimport('joomla.filesystem.folder');
 203  
 204          // Initialise variables.
 205          $folder        = $this->getState('item.folder');
 206          $element    = $this->getState('item.element');
 207          $lang        = JFactory::getLanguage();
 208          $client        = JApplicationHelper::getClientInfo(0);
 209  
 210      // Load the core and/or local language sys file(s) for the ordering field.
 211          $db = JFactory::getDbo();
 212          $query = 'SELECT element' .
 213                  ' FROM #__extensions' .
 214                  ' WHERE (type =' .$db->Quote('plugin'). 'AND folder='. $db->Quote($folder) . ')';
 215          $db->setQuery($query);
 216          $elements = $db->loadColumn();
 217  
 218          foreach ($elements as $elementa)
 219          {
 220                  $lang->load('plg_'.$folder.'_'.$elementa.'.sys', JPATH_ADMINISTRATOR, null, false, false)
 221              ||    $lang->load('plg_'.$folder.'_'.$elementa.'.sys', JPATH_PLUGINS.'/'.$folder.'/'.$elementa, null, false, false)
 222              ||    $lang->load('plg_'.$folder.'_'.$elementa.'.sys', JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
 223              ||    $lang->load('plg_'.$folder.'_'.$elementa.'.sys', JPATH_PLUGINS.'/'.$folder.'/'.$elementa, $lang->getDefault(), false, false);
 224          }
 225  
 226          if (empty($folder) || empty($element)) {
 227              $app = JFactory::getApplication();
 228              $app->redirect(JRoute::_('index.php?option=com_plugins&view=plugins', false));
 229          }
 230          // Try 1.6 format: /plugins/folder/element/element.xml
 231          $formFile = JPath::clean(JPATH_PLUGINS.'/'.$folder.'/'.$element.'/'.$element.'.xml');
 232          if (!file_exists($formFile)) {
 233              // Try 1.5 format: /plugins/folder/element/element.xml
 234              $formFile = JPath::clean(JPATH_PLUGINS.'/'.$folder.'/'.$element.'.xml');
 235              if (!file_exists($formFile)) {
 236                  throw new Exception(JText::sprintf('COM_PLUGINS_ERROR_FILE_NOT_FOUND', $element.'.xml'));
 237                  return false;
 238              }
 239          }
 240  
 241          // Load the core and/or local language file(s).
 242              $lang->load('plg_'.$folder.'_'.$element, JPATH_ADMINISTRATOR, null, false, false)
 243          ||    $lang->load('plg_'.$folder.'_'.$element, JPATH_PLUGINS.'/'.$folder.'/'.$element, null, false, false)
 244          ||    $lang->load('plg_'.$folder.'_'.$element, JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
 245          ||    $lang->load('plg_'.$folder.'_'.$element, JPATH_PLUGINS.'/'.$folder.'/'.$element, $lang->getDefault(), false, false);
 246  
 247  
 248          if (file_exists($formFile)) {
 249              // Get the plugin form.
 250              if (!$form->loadFile($formFile, false, '//config')) {
 251                  throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
 252              }
 253          }
 254  
 255          // Attempt to load the xml file.
 256          if (!$xml = simplexml_load_file($formFile)) {
 257              throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
 258          }
 259  
 260          // Get the help data from the XML file if present.
 261          $help = $xml->xpath('/extension/help');
 262          if (!empty($help)) {
 263              $helpKey = trim((string) $help[0]['key']);
 264              $helpURL = trim((string) $help[0]['url']);
 265  
 266              $this->helpKey = $helpKey ? $helpKey : $this->helpKey;
 267              $this->helpURL = $helpURL ? $helpURL : $this->helpURL;
 268          }
 269  
 270          // Trigger the default form events.
 271          parent::preprocessForm($form, $data, $group);
 272      }
 273  
 274      /**
 275       * A protected method to get a set of ordering conditions.
 276       *
 277       * @param    object    A record object.
 278       * @return    array    An array of conditions to add to add to ordering queries.
 279       * @since    1.6
 280       */
 281  	protected function getReorderConditions($table)
 282      {
 283          $condition = array();
 284          $condition[] = 'type = '. $this->_db->Quote($table->type);
 285          $condition[] = 'folder = '. $this->_db->Quote($table->folder);
 286          return $condition;
 287      }
 288  
 289      /**
 290       * Override method to save the form data.
 291       *
 292       * @param    array    The form data.
 293       * @return    boolean    True on success.
 294       * @since    1.6
 295       */
 296  	public function save($data)
 297      {
 298          // Load the extension plugin group.
 299          JPluginHelper::importPlugin('extension');
 300  
 301          // Setup type
 302          $data['type'] = 'plugin';
 303  
 304          return parent::save($data);
 305      }
 306  
 307      /**
 308       * Get the necessary data to load an item help screen.
 309       *
 310       * @return    object    An object with key, url, and local properties for loading the item help screen.
 311       * @since    1.6
 312       */
 313  	public function getHelp()
 314      {
 315          return (object) array('key' => $this->helpKey, 'url' => $this->helpURL);
 316      }
 317  
 318      /**
 319       * Custom clean cache method, plugins are cached in 2 places for different clients
 320       *
 321       * @since    1.6
 322       */
 323  	protected function cleanCache($group = null, $client_id = 0) {
 324          parent::cleanCache('com_plugins', 0);
 325          parent::cleanCache('com_plugins', 1);
 326      }
 327  }


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