[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_plugins/src/Model/ -> PluginModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_plugins
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Plugins\Administrator\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Filesystem\Path;
  15  use Joomla\CMS\Form\Form;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\MVC\Model\AdminModel;
  19  use Joomla\CMS\Object\CMSObject;
  20  use Joomla\CMS\Router\Route;
  21  use Joomla\CMS\Table\Table;
  22  use Joomla\Registry\Registry;
  23  use Joomla\Utilities\ArrayHelper;
  24  
  25  // phpcs:disable PSR1.Files.SideEffects
  26  \defined('_JEXEC') or die;
  27  // phpcs:enable PSR1.Files.SideEffects
  28  
  29  /**
  30   * Plugin model.
  31   *
  32   * @since  1.6
  33   */
  34  class PluginModel extends AdminModel
  35  {
  36      /**
  37       * @var     string  The help screen key for the module.
  38       * @since   1.6
  39       */
  40      protected $helpKey = 'Plugins:_Name_of_Plugin';
  41  
  42      /**
  43       * @var     string  The help screen base URL for the module.
  44       * @since   1.6
  45       */
  46      protected $helpURL;
  47  
  48      /**
  49       * @var     array  An array of cached plugin items.
  50       * @since   1.6
  51       */
  52      protected $_cache;
  53  
  54      /**
  55       * Constructor.
  56       *
  57       * @param   array                $config   An optional associative array of configuration settings.
  58       * @param   MVCFactoryInterface  $factory  The factory.
  59       *
  60       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  61       * @since   3.2
  62       */
  63      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  64      {
  65          $config = array_merge(
  66              array(
  67                  'event_after_save'  => 'onExtensionAfterSave',
  68                  'event_before_save' => 'onExtensionBeforeSave',
  69                  'events_map'        => array(
  70                      'save' => 'extension'
  71                  )
  72              ),
  73              $config
  74          );
  75  
  76          parent::__construct($config, $factory);
  77      }
  78  
  79      /**
  80       * Method to get the record form.
  81       *
  82       * @param   array    $data      Data for the form.
  83       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  84       *
  85       * @return  Form|bool  A Form object on success, false on failure.
  86       *
  87       * @since   1.6
  88       */
  89      public function getForm($data = array(), $loadData = true)
  90      {
  91          // The folder and element vars are passed when saving the form.
  92          if (empty($data)) {
  93              $item    = $this->getItem();
  94              $folder  = $item->folder;
  95              $element = $item->element;
  96          } else {
  97              $folder  = ArrayHelper::getValue($data, 'folder', '', 'cmd');
  98              $element = ArrayHelper::getValue($data, 'element', '', 'cmd');
  99          }
 100  
 101          // Add the default fields directory
 102          Form::addFieldPath(JPATH_PLUGINS . '/' . $folder . '/' . $element . '/field');
 103  
 104          // These variables are used to add data from the plugin XML files.
 105          $this->setState('item.folder', $folder);
 106          $this->setState('item.element', $element);
 107  
 108          // Get the form.
 109          $form = $this->loadForm('com_plugins.plugin', 'plugin', array('control' => 'jform', 'load_data' => $loadData));
 110  
 111          if (empty($form)) {
 112              return false;
 113          }
 114  
 115          // Modify the form based on access controls.
 116          if (!$this->canEditState((object) $data)) {
 117              // Disable fields for display.
 118              $form->setFieldAttribute('ordering', 'disabled', 'true');
 119              $form->setFieldAttribute('enabled', 'disabled', 'true');
 120  
 121              // Disable fields while saving.
 122              // The controller has already verified this is a record you can edit.
 123              $form->setFieldAttribute('ordering', 'filter', 'unset');
 124              $form->setFieldAttribute('enabled', 'filter', 'unset');
 125          }
 126  
 127          return $form;
 128      }
 129  
 130      /**
 131       * Method to get the data that should be injected in the form.
 132       *
 133       * @return  mixed  The data for the form.
 134       *
 135       * @since   1.6
 136       */
 137      protected function loadFormData()
 138      {
 139          // Check the session for previously entered form data.
 140          $data = Factory::getApplication()->getUserState('com_plugins.edit.plugin.data', array());
 141  
 142          if (empty($data)) {
 143              $data = $this->getItem();
 144          }
 145  
 146          $this->preprocessData('com_plugins.plugin', $data);
 147  
 148          return $data;
 149      }
 150  
 151      /**
 152       * Method to get a single record.
 153       *
 154       * @param   integer  $pk  The id of the primary key.
 155       *
 156       * @return  mixed  Object on success, false on failure.
 157       */
 158      public function getItem($pk = null)
 159      {
 160          $pk = (!empty($pk)) ? $pk : (int) $this->getState('plugin.id');
 161  
 162          $cacheId = $pk;
 163  
 164          if (\is_array($cacheId)) {
 165              $cacheId = serialize($cacheId);
 166          }
 167  
 168          if (!isset($this->_cache[$cacheId])) {
 169              // Get a row instance.
 170              $table = $this->getTable();
 171  
 172              // Attempt to load the row.
 173              $return = $table->load(\is_array($pk) ? $pk : ['extension_id' => $pk, 'type' => 'plugin']);
 174  
 175              // Check for a table object error.
 176              if ($return === false) {
 177                  return false;
 178              }
 179  
 180              // Convert to the \Joomla\CMS\Object\CMSObject before adding other data.
 181              $properties = $table->getProperties(1);
 182              $this->_cache[$cacheId] = ArrayHelper::toObject($properties, CMSObject::class);
 183  
 184              // Convert the params field to an array.
 185              $registry = new Registry($table->params);
 186              $this->_cache[$cacheId]->params = $registry->toArray();
 187  
 188              // Get the plugin XML.
 189              $path = Path::clean(JPATH_PLUGINS . '/' . $table->folder . '/' . $table->element . '/' . $table->element . '.xml');
 190  
 191              if (file_exists($path)) {
 192                  $this->_cache[$cacheId]->xml = simplexml_load_file($path);
 193              } else {
 194                  $this->_cache[$cacheId]->xml = null;
 195              }
 196          }
 197  
 198          return $this->_cache[$cacheId];
 199      }
 200  
 201      /**
 202       * Returns a reference to the Table object, always creating it.
 203       *
 204       * @param   string  $type    The table type to instantiate.
 205       * @param   string  $prefix  A prefix for the table class name. Optional.
 206       * @param   array   $config  Configuration array for model. Optional.
 207       *
 208       * @return  Table   A database object
 209       */
 210      public function getTable($type = 'Extension', $prefix = 'JTable', $config = array())
 211      {
 212          return Table::getInstance($type, $prefix, $config);
 213      }
 214  
 215      /**
 216       * Auto-populate the model state.
 217       *
 218       * Note. Calling getState in this method will result in recursion.
 219       *
 220       * @return  void
 221       *
 222       * @since   1.6
 223       */
 224      protected function populateState()
 225      {
 226          // Execute the parent method.
 227          parent::populateState();
 228  
 229          $app = Factory::getApplication();
 230  
 231          // Load the User state.
 232          $pk = $app->input->getInt('extension_id');
 233          $this->setState('plugin.id', $pk);
 234      }
 235  
 236      /**
 237       * Preprocess the form.
 238       *
 239       * @param   Form    $form   A form object.
 240       * @param   mixed   $data   The data expected for the form.
 241       * @param   string  $group  Cache group name.
 242       *
 243       * @return  mixed  True if successful.
 244       *
 245       * @since   1.6
 246       *
 247       * @throws  \Exception if there is an error in the form event.
 248       */
 249      protected function preprocessForm(Form $form, $data, $group = 'content')
 250      {
 251          $folder  = $this->getState('item.folder');
 252          $element = $this->getState('item.element');
 253          $lang    = Factory::getLanguage();
 254  
 255          // Load the core and/or local language sys file(s) for the ordering field.
 256          $db    = $this->getDatabase();
 257          $query = $db->getQuery(true)
 258              ->select($db->quoteName('element'))
 259              ->from($db->quoteName('#__extensions'))
 260              ->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
 261              ->where($db->quoteName('folder') . ' = :folder')
 262              ->bind(':folder', $folder);
 263          $db->setQuery($query);
 264          $elements = $db->loadColumn();
 265  
 266          foreach ($elements as $elementa) {
 267              $lang->load('plg_' . $folder . '_' . $elementa . '.sys', JPATH_ADMINISTRATOR)
 268              || $lang->load('plg_' . $folder . '_' . $elementa . '.sys', JPATH_PLUGINS . '/' . $folder . '/' . $elementa);
 269          }
 270  
 271          if (empty($folder) || empty($element)) {
 272              $app = Factory::getApplication();
 273              $app->redirect(Route::_('index.php?option=com_plugins&view=plugins', false));
 274          }
 275  
 276          $formFile = Path::clean(JPATH_PLUGINS . '/' . $folder . '/' . $element . '/' . $element . '.xml');
 277  
 278          if (!file_exists($formFile)) {
 279              throw new \Exception(Text::sprintf('COM_PLUGINS_ERROR_FILE_NOT_FOUND', $element . '.xml'));
 280          }
 281  
 282          // Load the core and/or local language file(s).
 283              $lang->load('plg_' . $folder . '_' . $element, JPATH_ADMINISTRATOR)
 284          ||  $lang->load('plg_' . $folder . '_' . $element, JPATH_PLUGINS . '/' . $folder . '/' . $element);
 285  
 286          if (file_exists($formFile)) {
 287              // Get the plugin form.
 288              if (!$form->loadFile($formFile, false, '//config')) {
 289                  throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
 290              }
 291          }
 292  
 293          // Attempt to load the xml file.
 294          if (!$xml = simplexml_load_file($formFile)) {
 295              throw new \Exception(Text::_('JERROR_LOADFILE_FAILED'));
 296          }
 297  
 298          // Get the help data from the XML file if present.
 299          $help = $xml->xpath('/extension/help');
 300  
 301          if (!empty($help)) {
 302              $helpKey = trim((string) $help[0]['key']);
 303              $helpURL = trim((string) $help[0]['url']);
 304  
 305              $this->helpKey = $helpKey ?: $this->helpKey;
 306              $this->helpURL = $helpURL ?: $this->helpURL;
 307          }
 308  
 309          // Trigger the default form events.
 310          parent::preprocessForm($form, $data, $group);
 311      }
 312  
 313      /**
 314       * A protected method to get a set of ordering conditions.
 315       *
 316       * @param   object  $table  A record object.
 317       *
 318       * @return  array  An array of conditions to add to ordering queries.
 319       *
 320       * @since   1.6
 321       */
 322      protected function getReorderConditions($table)
 323      {
 324          $db = $this->getDatabase();
 325  
 326          return [
 327              $db->quoteName('type') . ' = ' . $db->quote($table->type),
 328              $db->quoteName('folder') . ' = ' . $db->quote($table->folder),
 329          ];
 330      }
 331  
 332      /**
 333       * Override method to save the form data.
 334       *
 335       * @param   array  $data  The form data.
 336       *
 337       * @return  boolean  True on success.
 338       *
 339       * @since   1.6
 340       */
 341      public function save($data)
 342      {
 343          // Setup type.
 344          $data['type'] = 'plugin';
 345  
 346          return parent::save($data);
 347      }
 348  
 349      /**
 350       * Get the necessary data to load an item help screen.
 351       *
 352       * @return  object  An object with key, url, and local properties for loading the item help screen.
 353       *
 354       * @since   1.6
 355       */
 356      public function getHelp()
 357      {
 358          return (object) array('key' => $this->helpKey, 'url' => $this->helpURL);
 359      }
 360  
 361      /**
 362       * Custom clean cache method, plugins are cached in 2 places for different clients.
 363       *
 364       * @param   string   $group     Cache group name.
 365       * @param   integer  $clientId  @deprecated   5.0   No longer used.
 366       *
 367       * @return  void
 368       *
 369       * @since   1.6
 370       */
 371      protected function cleanCache($group = null, $clientId = 0)
 372      {
 373          parent::cleanCache('com_plugins');
 374      }
 375  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer