[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_categories/src/Controller/ -> CategoriesController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_categories
   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\Categories\Api\Controller;
  12  
  13  use Joomla\CMS\MVC\Controller\ApiController;
  14  use Joomla\CMS\Table\Category;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * The categories controller
  22   *
  23   * @since  4.0.0
  24   */
  25  class CategoriesController extends ApiController
  26  {
  27      /**
  28       * The content type of the item.
  29       *
  30       * @var    string
  31       * @since  4.0.0
  32       */
  33      protected $contentType = 'categories';
  34  
  35      /**
  36       * The default view for the display method.
  37       *
  38       * @var    string
  39       * @since  3.0
  40       */
  41      protected $default_view = 'categories';
  42  
  43      /**
  44       * Method to allow extended classes to manipulate the data to be saved for an extension.
  45       *
  46       * @param   array  $data  An array of input data.
  47       *
  48       * @return  array
  49       *
  50       * @since   4.0.0
  51       */
  52      protected function preprocessSaveData(array $data): array
  53      {
  54          $extension = $this->getExtensionFromInput();
  55          $data['extension'] = $extension;
  56  
  57          // TODO: This is a hack to drop the extension into the global input object - to satisfy how state is built
  58          //       we should be able to improve this in the future
  59          $this->input->set('extension', $extension);
  60  
  61          return $data;
  62      }
  63  
  64      /**
  65       * Method to save a record.
  66       *
  67       * @param   integer  $recordKey  The primary key of the item (if exists)
  68       *
  69       * @return  integer  The record ID on success, false on failure
  70       *
  71       * @since   4.0.6
  72       */
  73      protected function save($recordKey = null)
  74      {
  75          $recordId = parent::save($recordKey);
  76  
  77          if (!$recordId) {
  78              return $recordId;
  79          }
  80  
  81          $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
  82  
  83          if (empty($data['location'])) {
  84              return $recordId;
  85          }
  86  
  87          /** @var Category $category */
  88          $category = $this->getModel('Category')->getTable('Category');
  89          $category->load((int) $recordId);
  90  
  91          $reference = $category->parent_id;
  92  
  93          if (!empty($data['location_reference'])) {
  94              $reference = (int) $data['location_reference'];
  95          }
  96  
  97          $category->setLocation($reference, $data['location']);
  98          $category->store();
  99  
 100          return $recordId;
 101      }
 102  
 103      /**
 104       * Basic display of an item view
 105       *
 106       * @param   integer  $id  The primary key to display. Leave empty if you want to retrieve data from the request
 107       *
 108       * @return  static  A \JControllerLegacy object to support chaining.
 109       *
 110       * @since   4.0.0
 111       */
 112      public function displayItem($id = null)
 113      {
 114          $this->modelState->set('filter.extension', $this->getExtensionFromInput());
 115  
 116          return parent::displayItem($id);
 117      }
 118      /**
 119       * Basic display of a list view
 120       *
 121       * @return  static  A \JControllerLegacy object to support chaining.
 122       *
 123       * @since   4.0.0
 124       */
 125      public function displayList()
 126      {
 127          $this->modelState->set('filter.extension', $this->getExtensionFromInput());
 128  
 129          return parent::displayList();
 130      }
 131  
 132      /**
 133       * Get extension from input
 134       *
 135       * @return string
 136       *
 137       * @since 4.0.0
 138       */
 139      private function getExtensionFromInput()
 140      {
 141          return $this->input->exists('extension') ?
 142              $this->input->get('extension') : $this->input->post->get('extension');
 143      }
 144  }


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