[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_languages/src/Controller/ -> OverridesController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_languages
   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\Languages\Api\Controller;
  12  
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\ApiController;
  16  use Joomla\CMS\MVC\Controller\Exception;
  17  use Joomla\String\Inflector;
  18  use Tobscure\JsonApi\Exception\InvalidParameterException;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * The overrides controller
  26   *
  27   * @since  4.0.0
  28   */
  29  class OverridesController extends ApiController
  30  {
  31      /**
  32       * The content type of the item.
  33       *
  34       * @var    string
  35       * @since  4.0.0
  36       */
  37      protected $contentType = 'overrides';
  38  
  39      /**
  40       * The default view for the display method.
  41       *
  42       * @var    string
  43       * @since  3.0
  44       */
  45      protected $default_view = 'overrides';
  46  
  47      /**
  48       * Basic display of an item view
  49       *
  50       * @param   integer  $id  The primary key to display. Leave empty if you want to retrieve data from the request
  51       *
  52       * @return  static  A \JControllerLegacy object to support chaining.
  53       *
  54       * @since   4.0.0
  55       */
  56      public function displayItem($id = null)
  57      {
  58          $this->modelState->set('filter.language', $this->getLanguageFromInput());
  59          $this->modelState->set('filter.client', $this->getClientFromInput());
  60  
  61          return parent::displayItem($id);
  62      }
  63  
  64      /**
  65       * Basic display of a list view
  66       *
  67       * @return  static  A \JControllerLegacy object to support chaining.
  68       *
  69       * @since   4.0.0
  70       */
  71      public function displayList()
  72      {
  73          $this->modelState->set('filter.language', $this->getLanguageFromInput());
  74          $this->modelState->set('filter.client', $this->getClientFromInput());
  75  
  76          return parent::displayList();
  77      }
  78  
  79      /**
  80       * Method to save a record.
  81       *
  82       * @param   integer  $recordKey  The primary key of the item (if exists)
  83       *
  84       * @return  integer  The record ID on success, false on failure
  85       *
  86       * @since   4.0.0
  87       */
  88      protected function save($recordKey = null)
  89      {
  90          /** @var \Joomla\CMS\MVC\Model\AdminModel $model */
  91          $model = $this->getModel(Inflector::singularize($this->contentType));
  92  
  93          if (!$model) {
  94              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
  95          }
  96  
  97          $model->setState('filter.language', $this->input->post->get('lang_code'));
  98          $model->setState('filter.client', $this->input->post->get('app'));
  99  
 100          $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
 101  
 102          // @todo: Not the cleanest thing ever but it works...
 103          Form::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/forms');
 104  
 105          // Validate the posted data.
 106          $form = $model->getForm($data, false);
 107  
 108          if (!$form) {
 109              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_FORM_CREATE'));
 110          }
 111  
 112          // Test whether the data is valid.
 113          $validData = $model->validate($form, $data);
 114  
 115          // Check for validation errors.
 116          if ($validData === false) {
 117              $errors   = $model->getErrors();
 118              $messages = [];
 119  
 120              for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) {
 121                  if ($errors[$i] instanceof \Exception) {
 122                      $messages[] = "{$errors[$i]->getMessage()}";
 123                  } else {
 124                      $messages[] = "{$errors[$i]}";
 125                  }
 126              }
 127  
 128              throw new InvalidParameterException(implode("\n", $messages));
 129          }
 130  
 131          if (!isset($validData['tags'])) {
 132              $validData['tags'] = [];
 133          }
 134  
 135          if (!$model->save($validData)) {
 136              throw new Exception\Save(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
 137          }
 138  
 139          return $validData['key'];
 140      }
 141  
 142      /**
 143       * Removes an item.
 144       *
 145       * @param   integer  $id  The primary key to delete item.
 146       *
 147       * @return  void
 148       *
 149       * @since   4.0.0
 150       */
 151      public function delete($id = null)
 152      {
 153          $id = $this->input->get('id', '', 'string');
 154  
 155          $this->input->set('model', $this->contentType);
 156  
 157          $this->modelState->set('filter.language', $this->getLanguageFromInput());
 158          $this->modelState->set('filter.client', $this->getClientFromInput());
 159  
 160          parent::delete($id);
 161      }
 162  
 163      /**
 164       * Get client code from input
 165       *
 166       * @return string
 167       *
 168       * @since 4.0.0
 169       */
 170      private function getClientFromInput()
 171      {
 172          return $this->input->exists('app') ? $this->input->get('app') : $this->input->post->get('app');
 173      }
 174  
 175      /**
 176       * Get language code from input
 177       *
 178       * @return string
 179       *
 180       * @since 4.0.0
 181       */
 182      private function getLanguageFromInput()
 183      {
 184          return $this->input->exists('lang_code') ?
 185              $this->input->get('lang_code') : $this->input->post->get('lang_code');
 186      }
 187  }


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