[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_config
   6   *
   7   * @copyright   (C) 2019 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\Api\Controller;
  12  
  13  use Joomla\CMS\Access\Exception\NotAllowed;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Extension\ExtensionHelper;
  16  use Joomla\CMS\Form\Form;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\MVC\Controller\ApiController;
  19  use Joomla\Component\Config\Administrator\Model\ComponentModel;
  20  use Joomla\Component\Config\Api\View\Component\JsonapiView;
  21  use Tobscure\JsonApi\Exception\InvalidParameterException;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * The component controller
  29   *
  30   * @since  4.0.0
  31   */
  32  class ComponentController extends ApiController
  33  {
  34      /**
  35       * The content type of the item.
  36       *
  37       * @var    string
  38       * @since  4.0.0
  39       */
  40      protected $contentType = 'component';
  41  
  42      /**
  43       * The default view for the display method.
  44       *
  45       * @var    string
  46       * @since  3.0
  47       */
  48      protected $default_view = 'component';
  49  
  50      /**
  51       * Basic display of a list view
  52       *
  53       * @return  static  A \JControllerLegacy object to support chaining.
  54       *
  55       * @since   4.0.0
  56       */
  57      public function displayList()
  58      {
  59          $viewType = $this->app->getDocument()->getType();
  60          $viewLayout = $this->input->get('layout', 'default', 'string');
  61  
  62          try {
  63              /** @var JsonapiView $view */
  64              $view = $this->getView(
  65                  $this->default_view,
  66                  $viewType,
  67                  '',
  68                  ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
  69              );
  70          } catch (\Exception $e) {
  71              throw new \RuntimeException($e->getMessage());
  72          }
  73  
  74          /** @var ComponentModel $model */
  75          $model = $this->getModel($this->contentType);
  76  
  77          if (!$model) {
  78              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
  79          }
  80  
  81          // Push the model into the view (as default)
  82          $view->setModel($model, true);
  83          $view->set('component_name', $this->input->get('component_name'));
  84  
  85          $view->document = $this->app->getDocument();
  86          $view->displayList();
  87  
  88          return $this;
  89      }
  90  
  91      /**
  92       * Method to edit an existing record.
  93       *
  94       * @return  static  A \JControllerLegacy object to support chaining.
  95       *
  96       * @since   4.0.0
  97       */
  98      public function edit()
  99      {
 100          /** @var ComponentModel $model */
 101          $model = $this->getModel($this->contentType);
 102  
 103          if (!$model) {
 104              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
 105          }
 106  
 107          // Access check.
 108          if (!$this->allowEdit()) {
 109              throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403);
 110          }
 111  
 112          $option = $this->input->get('component_name');
 113  
 114          // @todo: Not the cleanest thing ever but it works...
 115          Form::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
 116  
 117          // Must load after serving service-requests
 118          $form = $model->getForm();
 119  
 120          $data = json_decode($this->input->json->getRaw(), true);
 121  
 122          $component = ComponentHelper::getComponent($option);
 123          $oldData   = $component->getParams()->toArray();
 124          $data      = array_replace($oldData, $data);
 125  
 126          // Validate the posted data.
 127          $validData = $model->validate($form, $data);
 128  
 129          if ($validData === false) {
 130              $errors   = $model->getErrors();
 131              $messages = [];
 132  
 133              for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) {
 134                  if ($errors[$i] instanceof \Exception) {
 135                      $messages[] = "{$errors[$i]->getMessage()}";
 136                  } else {
 137                      $messages[] = "{$errors[$i]}";
 138                  }
 139              }
 140  
 141              throw new InvalidParameterException(implode("\n", $messages));
 142          }
 143  
 144          // Attempt to save the configuration.
 145          $data = [
 146              'params' => $validData,
 147              'id'     => ExtensionHelper::getExtensionRecord($option, 'component')->extension_id,
 148              'option' => $option,
 149          ];
 150  
 151          if (!$model->save($data)) {
 152              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SERVER'), 500);
 153          }
 154  
 155          return $this;
 156      }
 157  }


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