[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_config/src/Model/ -> ComponentModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_config
   6   *
   7   * @copyright   (C) 2013 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\Config\Administrator\Model;
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Access\Rules;
  15  use Joomla\CMS\Component\ComponentHelper;
  16  use Joomla\CMS\Factory;
  17  use Joomla\CMS\Filesystem\Path;
  18  use Joomla\CMS\Form\Form;
  19  use Joomla\CMS\Language\Text;
  20  use Joomla\CMS\MVC\Model\FormModel;
  21  use Joomla\CMS\Plugin\PluginHelper;
  22  use Joomla\CMS\Table\Table;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Model for component configuration
  30   *
  31   * @since  3.2
  32   */
  33  class ComponentModel extends FormModel
  34  {
  35      /**
  36       * Method to auto-populate the model state.
  37       *
  38       * Note. Calling getState in this method will result in recursion.
  39       *
  40       * @return  void
  41       *
  42       * @since   3.2
  43       */
  44      protected function populateState()
  45      {
  46          $input = Factory::getApplication()->input;
  47  
  48          // Set the component (option) we are dealing with.
  49          $component = $input->get('component');
  50  
  51          $this->state->set('component.option', $component);
  52  
  53          // Set an alternative path for the configuration file.
  54          if ($path = $input->getString('path')) {
  55              $path = Path::clean(JPATH_SITE . '/' . $path);
  56              Path::check($path);
  57              $this->state->set('component.path', $path);
  58          }
  59      }
  60  
  61      /**
  62       * Method to get a form object.
  63       *
  64       * @param   array    $data      Data for the form.
  65       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  66       *
  67       * @return  mixed  A JForm object on success, false on failure
  68       *
  69       * @since   3.2
  70       */
  71      public function getForm($data = array(), $loadData = true)
  72      {
  73          $state = $this->getState();
  74          $option = $state->get('component.option');
  75  
  76          if ($path = $state->get('component.path')) {
  77              // Add the search path for the admin component config.xml file.
  78              Form::addFormPath($path);
  79          } else {
  80              // Add the search path for the admin component config.xml file.
  81              Form::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
  82          }
  83  
  84          // Get the form.
  85          $form = $this->loadForm(
  86              'com_config.component',
  87              'config',
  88              array('control' => 'jform', 'load_data' => $loadData),
  89              false,
  90              '/config'
  91          );
  92  
  93          if (empty($form)) {
  94              return false;
  95          }
  96  
  97          $lang = Factory::getLanguage();
  98          $lang->load($option, JPATH_BASE)
  99          || $lang->load($option, JPATH_BASE . "/components/$option");
 100  
 101          return $form;
 102      }
 103  
 104      /**
 105       * Method to get the data that should be injected in the form.
 106       *
 107       * @return  array  The default data is an empty array.
 108       *
 109       * @since   4.0.0
 110       */
 111      protected function loadFormData()
 112      {
 113          $option = $this->getState()->get('component.option');
 114  
 115          // Check the session for previously entered form data.
 116          $data = Factory::getApplication()->getUserState('com_config.edit.component.' . $option . '.data', []);
 117  
 118          if (empty($data)) {
 119              return $this->getComponent()->getParams()->toArray();
 120          }
 121  
 122          return $data;
 123      }
 124  
 125      /**
 126       * Get the component information.
 127       *
 128       * @return  object
 129       *
 130       * @since   3.2
 131       */
 132      public function getComponent()
 133      {
 134          $state = $this->getState();
 135          $option = $state->get('component.option');
 136  
 137          // Load common and local language files.
 138          $lang = Factory::getLanguage();
 139          $lang->load($option, JPATH_BASE)
 140          || $lang->load($option, JPATH_BASE . "/components/$option");
 141  
 142          $result = ComponentHelper::getComponent($option);
 143  
 144          return $result;
 145      }
 146  
 147      /**
 148       * Method to save the configuration data.
 149       *
 150       * @param   array  $data  An array containing all global config data.
 151       *
 152       * @return  boolean  True on success, false on failure.
 153       *
 154       * @since   3.2
 155       * @throws  \RuntimeException
 156       */
 157      public function save($data)
 158      {
 159          $table      = Table::getInstance('extension');
 160          $context    = $this->option . '.' . $this->name;
 161          PluginHelper::importPlugin('extension');
 162  
 163          // Check super user group.
 164          if (isset($data['params']) && !Factory::getUser()->authorise('core.admin')) {
 165              $form = $this->getForm(array(), false);
 166  
 167              foreach ($form->getFieldsets() as $fieldset) {
 168                  foreach ($form->getFieldset($fieldset->name) as $field) {
 169                      if (
 170                          $field->type === 'UserGroupList' && isset($data['params'][$field->fieldname])
 171                          && (int) $field->getAttribute('checksuperusergroup', 0) === 1
 172                          && Access::checkGroup($data['params'][$field->fieldname], 'core.admin')
 173                      ) {
 174                          throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
 175                      }
 176                  }
 177              }
 178          }
 179  
 180          // Save the rules.
 181          if (isset($data['params']) && isset($data['params']['rules'])) {
 182              if (!Factory::getUser()->authorise('core.admin', $data['option'])) {
 183                  throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
 184              }
 185  
 186              $rules = new Rules($data['params']['rules']);
 187              $asset = Table::getInstance('asset');
 188  
 189              if (!$asset->loadByName($data['option'])) {
 190                  $root = Table::getInstance('asset');
 191                  $root->loadByName('root.1');
 192                  $asset->name = $data['option'];
 193                  $asset->title = $data['option'];
 194                  $asset->setLocation($root->id, 'last-child');
 195              }
 196  
 197              $asset->rules = (string) $rules;
 198  
 199              if (!$asset->check() || !$asset->store()) {
 200                  throw new \RuntimeException($asset->getError());
 201              }
 202  
 203              // We don't need this anymore
 204              unset($data['option']);
 205              unset($data['params']['rules']);
 206          }
 207  
 208          // Load the previous Data
 209          if (!$table->load($data['id'])) {
 210              throw new \RuntimeException($table->getError());
 211          }
 212  
 213          unset($data['id']);
 214  
 215          // Bind the data.
 216          if (!$table->bind($data)) {
 217              throw new \RuntimeException($table->getError());
 218          }
 219  
 220          // Check the data.
 221          if (!$table->check()) {
 222              throw new \RuntimeException($table->getError());
 223          }
 224  
 225          $result = Factory::getApplication()->triggerEvent('onExtensionBeforeSave', array($context, $table, false));
 226  
 227          // Store the data.
 228          if (in_array(false, $result, true) || !$table->store()) {
 229              throw new \RuntimeException($table->getError());
 230          }
 231  
 232          Factory::getApplication()->triggerEvent('onExtensionAfterSave', array($context, $table, false));
 233  
 234          // Clean the component cache.
 235          $this->cleanCache('_system');
 236  
 237          return true;
 238      }
 239  }


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