[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


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