[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_templates/src/Controller/ -> StylesController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_templates
   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\Templates\Api\Controller;
  12  
  13  use Joomla\CMS\MVC\Controller\ApiController;
  14  use Joomla\String\Inflector;
  15  use Tobscure\JsonApi\Exception\InvalidParameterException;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * The styles controller
  23   *
  24   * @since  4.0.0
  25   */
  26  class StylesController extends ApiController
  27  {
  28      /**
  29       * The content type of the item.
  30       *
  31       * @var    string
  32       * @since  4.0.0
  33       */
  34      protected $contentType = 'styles';
  35  
  36      /**
  37       * The default view for the display method.
  38       *
  39       * @var    string
  40       * @since  3.0
  41       */
  42      protected $default_view = 'styles';
  43  
  44      /**
  45       * Basic display of an item view
  46       *
  47       * @param   integer  $id  The primary key to display. Leave empty if you want to retrieve data from the request
  48       *
  49       * @return  static  A \JControllerLegacy object to support chaining.
  50       *
  51       * @since   4.0.0
  52       */
  53      public function displayItem($id = null)
  54      {
  55          $this->modelState->set('client_id', $this->getClientIdFromInput());
  56  
  57          return parent::displayItem($id);
  58      }
  59  
  60      /**
  61       * Basic display of a list view
  62       *
  63       * @return  static  A \JControllerLegacy object to support chaining.
  64       *
  65       * @since   4.0.0
  66       */
  67      public function displayList()
  68      {
  69          $this->modelState->set('client_id', $this->getClientIdFromInput());
  70  
  71          return parent::displayList();
  72      }
  73  
  74      /**
  75       * Method to allow extended classes to manipulate the data to be saved for an extension.
  76       *
  77       * @param   array  $data  An array of input data.
  78       *
  79       * @return  array
  80       *
  81       * @since   4.0.0
  82       * @throws  InvalidParameterException
  83       */
  84      protected function preprocessSaveData(array $data): array
  85      {
  86          $data['client_id'] = $this->getClientIdFromInput();
  87  
  88          // If we are updating an item the template is a readonly property based on the ID
  89          if ($this->input->getMethod() === 'PATCH') {
  90              if (\array_key_exists('template', $data)) {
  91                  throw new InvalidParameterException('The template property cannot be modified for an existing style');
  92              }
  93  
  94              $model = $this->getModel(Inflector::singularize($this->contentType), '', ['ignore_request' => true]);
  95              $data['template'] = $model->getItem($this->input->getInt('id'))->template;
  96          }
  97  
  98          return $data;
  99      }
 100  
 101      /**
 102       * Get client id from input
 103       *
 104       * @return string
 105       *
 106       * @since 4.0.0
 107       */
 108      private function getClientIdFromInput()
 109      {
 110          return $this->input->exists('client_id') ? $this->input->get('client_id') : $this->input->post->get('client_id');
 111      }
 112  }


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