[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   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\Site\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Client\ClientHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\Router\Route;
  19  use Joomla\CMS\Uri\Uri;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Component Controller
  27   *
  28   * @since  1.5
  29   */
  30  class ConfigController extends BaseController
  31  {
  32      /**
  33       * @param   array                         $config   An optional associative array of configuration settings.
  34       *                                                  Recognized key values include 'name', 'default_task', 'model_path', and
  35       *                                                  'view_path' (this list is not meant to be comprehensive).
  36       * @param   MVCFactoryInterface|null      $factory  The factory.
  37       * @param   CMSApplication|null           $app      The JApplication for the dispatcher
  38       * @param   \Joomla\CMS\Input\Input|null  $input    The Input object for the request
  39       *
  40       * @since   1.6
  41       */
  42      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  43      {
  44          parent::__construct($config, $factory, $app, $input);
  45  
  46          $this->registerTask('apply', 'save');
  47      }
  48  
  49      /**
  50       * Method to handle cancel
  51       *
  52       * @return  void
  53       *
  54       * @since   3.2
  55       */
  56      public function cancel()
  57      {
  58          // Redirect back to home(base) page
  59          $this->setRedirect(Uri::base());
  60      }
  61  
  62      /**
  63       * Method to save global configuration.
  64       *
  65       * @return  boolean  True on success.
  66       *
  67       * @since   3.2
  68       */
  69      public function save()
  70      {
  71          // Check for request forgeries.
  72          $this->checkToken();
  73  
  74          // Check if the user is authorized to do this.
  75          if (!$this->app->getIdentity()->authorise('core.admin')) {
  76              $this->app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'));
  77              $this->app->redirect('index.php');
  78          }
  79  
  80          // Set FTP credentials, if given.
  81          ClientHelper::setCredentialsFromRequest('ftp');
  82  
  83          $model = $this->getModel();
  84  
  85          $form  = $model->getForm();
  86          $data  = $this->app->input->post->get('jform', array(), 'array');
  87  
  88          // Validate the posted data.
  89          $return = $model->validate($form, $data);
  90  
  91          // Check for validation errors.
  92          if ($return === false) {
  93              /*
  94               * The validate method enqueued all messages for us, so we just need to redirect back.
  95               */
  96  
  97              // Save the data in the session.
  98              $this->app->setUserState('com_config.config.global.data', $data);
  99  
 100              // Redirect back to the edit screen.
 101              $this->app->redirect(Route::_('index.php?option=com_config&view=config', false));
 102          }
 103  
 104          // Attempt to save the configuration.
 105          $data = $return;
 106  
 107          // Access backend com_config
 108          $saveClass = $this->factory->createController('Application', 'Administrator', [], $this->app, $this->input);
 109  
 110          // Get a document object
 111          $document = $this->app->getDocument();
 112  
 113          // Set backend required params
 114          $document->setType('json');
 115  
 116          // Execute backend controller
 117          $return = $saveClass->save();
 118  
 119          // Reset params back after requesting from service
 120          $document->setType('html');
 121  
 122          // Check the return value.
 123          if ($return === false) {
 124              /*
 125               * The save method enqueued all messages for us, so we just need to redirect back.
 126               */
 127  
 128              // Save the data in the session.
 129              $this->app->setUserState('com_config.config.global.data', $data);
 130  
 131              // Save failed, go back to the screen and display a notice.
 132              $this->app->redirect(Route::_('index.php?option=com_config&view=config', false));
 133          }
 134  
 135          // Redirect back to com_config display
 136          $this->app->enqueueMessage(Text::_('COM_CONFIG_SAVE_SUCCESS'));
 137          $this->app->redirect(Route::_('index.php?option=com_config&view=config', false));
 138  
 139          return true;
 140      }
 141  }


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