[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_config/src/Controller/ -> ComponentController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_config
   6   *
   7   * @copyright   (C) 2017 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\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\FormController;
  16  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  17  use Joomla\CMS\Router\Route;
  18  use Joomla\CMS\Uri\Uri;
  19  use Joomla\Input\Input;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Note: this view is intended only to be opened in a popup
  27   *
  28   * @since  1.5
  29   */
  30  class ComponentController extends FormController
  31  {
  32      /**
  33       * Constructor.
  34       *
  35       * @param   array                $config   An optional associative array of configuration settings.
  36       * Recognized key values include 'name', 'default_task', 'model_path', and
  37       * 'view_path' (this list is not meant to be comprehensive).
  38       * @param   MVCFactoryInterface  $factory  The factory.
  39       * @param   CMSApplication       $app      The Application for the dispatcher
  40       * @param   Input                $input    Input
  41       *
  42       * @since   3.0
  43       */
  44      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  45      {
  46          parent::__construct($config, $factory, $app, $input);
  47  
  48          // Map the apply task to the save method.
  49          $this->registerTask('apply', 'save');
  50      }
  51  
  52      /**
  53       * Method to save component configuration.
  54       *
  55       * @param   string  $key     The name of the primary key of the URL variable.
  56       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  57       *
  58       * @return  boolean
  59       *
  60       * @since   3.2
  61       */
  62      public function save($key = null, $urlVar = null)
  63      {
  64          // Check for request forgeries.
  65          $this->checkToken();
  66  
  67          $data    = $this->input->get('jform', [], 'ARRAY');
  68          $id      = $this->input->get('id', null, 'INT');
  69          $option  = $this->input->get('component');
  70          $user    = $this->app->getIdentity();
  71          $context = "$this->option.edit.$this->context.$option";
  72  
  73          /** @var \Joomla\Component\Config\Administrator\Model\ComponentModel $model */
  74          $model = $this->getModel('Component', 'Administrator');
  75          $model->setState('component.option', $option);
  76          $form  = $model->getForm();
  77  
  78          // Make sure com_joomlaupdate and com_privacy can only be accessed by SuperUser
  79          if (\in_array(strtolower($option), ['com_joomlaupdate', 'com_privacy'], true) && !$user->authorise('core.admin')) {
  80              $this->setRedirect(Route::_('index.php', false), Text::_('JERROR_ALERTNOAUTHOR'), 'error');
  81          }
  82  
  83          // Check if the user is authorised to do this.
  84          if (!$user->authorise('core.admin', $option) && !$user->authorise('core.options', $option)) {
  85              $this->setRedirect(Route::_('index.php', false), Text::_('JERROR_ALERTNOAUTHOR'), 'error');
  86          }
  87  
  88          // Remove the permissions rules data if user isn't allowed to edit them.
  89          if (!$user->authorise('core.admin', $option) && isset($data['params']) && isset($data['params']['rules'])) {
  90              unset($data['params']['rules']);
  91          }
  92  
  93          $returnUri = $this->input->post->get('return', null, 'base64');
  94  
  95          $redirect = '';
  96  
  97          if (!empty($returnUri)) {
  98              $redirect = '&return=' . urlencode($returnUri);
  99          }
 100  
 101          // Validate the posted data.
 102          $return = $model->validate($form, $data);
 103  
 104          // Check for validation errors.
 105          if ($return === false) {
 106              // Save the data in the session.
 107              $this->app->setUserState($context . '.data', $data);
 108  
 109              // Redirect back to the edit screen.
 110              $this->setRedirect(
 111                  Route::_('index.php?option=com_config&view=component&component=' . $option . $redirect, false),
 112                  $model->getError(),
 113                  'error'
 114              );
 115  
 116              return false;
 117          }
 118  
 119          // Attempt to save the configuration.
 120          $data = [
 121              'params' => $return,
 122              'id'     => $id,
 123              'option' => $option,
 124          ];
 125  
 126          try {
 127              $model->save($data);
 128          } catch (\RuntimeException $e) {
 129              // Save the data in the session.
 130              $this->app->setUserState($context . '.data', $data);
 131  
 132              // Save failed, go back to the screen and display a notice.
 133              $this->setRedirect(
 134                  Route::_('index.php?option=com_config&view=component&component=' . $option . $redirect, false),
 135                  Text::_('JERROR_SAVE_FAILED', $e->getMessage()),
 136                  'error'
 137              );
 138  
 139              return false;
 140          }
 141  
 142          // Clear session data.
 143          $this->app->setUserState($context . '.data', null);
 144  
 145          // Set the redirect based on the task.
 146          switch ($this->input->get('task')) {
 147              case 'apply':
 148                  $this->setRedirect(
 149                      Route::_('index.php?option=com_config&view=component&component=' . $option . $redirect, false),
 150                      Text::_('COM_CONFIG_SAVE_SUCCESS'),
 151                      'message'
 152                  );
 153  
 154                  break;
 155  
 156              case 'save':
 157                  $this->setMessage(Text::_('COM_CONFIG_SAVE_SUCCESS'), 'message');
 158  
 159                  // No break
 160  
 161              default:
 162                  $redirect = 'index.php?option=' . $option;
 163  
 164                  if (!empty($returnUri)) {
 165                      $redirect = base64_decode($returnUri);
 166                  }
 167  
 168                  // Don't redirect to an external URL.
 169                  if (!Uri::isInternal($redirect)) {
 170                      $redirect = Uri::base();
 171                  }
 172  
 173                  $this->setRedirect(Route::_($redirect, false));
 174          }
 175  
 176          return true;
 177      }
 178  
 179      /**
 180       * Method to cancel global configuration component.
 181       *
 182       * @param   string  $key  The name of the primary key of the URL variable.
 183       *
 184       * @return  boolean
 185       *
 186       * @since   3.2
 187       */
 188      public function cancel($key = null)
 189      {
 190          $component = $this->input->get('component');
 191  
 192          // Clear session data.
 193          $this->app->setUserState("$this->option.edit.$this->context.$component.data", null);
 194  
 195          // Calculate redirect URL
 196          $returnUri = $this->input->post->get('return', null, 'base64');
 197  
 198          $redirect = 'index.php?option=' . $component;
 199  
 200          if (!empty($returnUri)) {
 201              $redirect = base64_decode($returnUri);
 202          }
 203  
 204          // Don't redirect to an external URL.
 205          if (!Uri::isInternal($redirect)) {
 206              $redirect = Uri::base();
 207          }
 208  
 209          $this->setRedirect(Route::_($redirect, false));
 210  
 211          return true;
 212      }
 213  }


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