[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_menus/src/Controller/ -> ItemsController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_menus
   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\Menus\Api\Controller;
  12  
  13  use Joomla\CMS\Access\Exception\NotAllowed;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\ApiController;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Component\Menus\Api\View\Items\JsonapiView;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * The items controller
  25   *
  26   * @since  4.0.0
  27   */
  28  class ItemsController extends ApiController
  29  {
  30      /**
  31       * The content type of the item.
  32       *
  33       * @var    string
  34       * @since  4.0.0
  35       */
  36      protected $contentType = 'items';
  37  
  38      /**
  39       * The default view for the display method.
  40       *
  41       * @var    string
  42       * @since  3.0
  43       */
  44      protected $default_view = 'items';
  45  
  46      /**
  47       * Basic display of an item view
  48       *
  49       * @param   integer  $id  The primary key to display. Leave empty if you want to retrieve data from the request
  50       *
  51       * @return  static  A \JControllerLegacy object to support chaining.
  52       *
  53       * @since   4.0.0
  54       */
  55      public function displayItem($id = null)
  56      {
  57          $this->modelState->set('filter.client_id', $this->getClientIdFromInput());
  58  
  59          return parent::displayItem($id);
  60      }
  61  
  62      /**
  63       * Basic display of a list view
  64       *
  65       * @return  static  A \JControllerLegacy object to support chaining.
  66       *
  67       * @since   4.0.0
  68       */
  69      public function displayList()
  70      {
  71          $this->modelState->set('filter.client_id', $this->getClientIdFromInput());
  72  
  73          return parent::displayList();
  74      }
  75  
  76      /**
  77       * Method to add a new record.
  78       *
  79       * @return  void
  80       *
  81       * @since   4.0.0
  82       * @throws  NotAllowed
  83       * @throws  \RuntimeException
  84       */
  85      public function add()
  86      {
  87          $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
  88  
  89          if (isset($data['menutype'])) {
  90              $this->input->set('menutype', $data['menutype']);
  91              $this->input->set('com_menus.items.menutype', $data['menutype']);
  92          }
  93  
  94          isset($data['type'])      && $this->input->set('type', $data['type']);
  95          isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
  96          isset($data['link'])      && $this->input->set('link', $data['link']);
  97  
  98          $this->input->set('id', '0');
  99  
 100          parent::add();
 101      }
 102  
 103      /**
 104       * Method to edit an existing record.
 105       *
 106       * @return  static  A \JControllerLegacy object to support chaining.
 107       *
 108       * @since   4.0.0
 109       */
 110      public function edit()
 111      {
 112          $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
 113  
 114          if (isset($data['menutype'])) {
 115              $this->input->set('menutype', $data['menutype']);
 116              $this->input->set('com_menus.items.menutype', $data['menutype']);
 117          }
 118  
 119          isset($data['type'])      && $this->input->set('type', $data['type']);
 120          isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
 121          isset($data['link'])      && $this->input->set('link', $data['link']);
 122  
 123          return parent::edit();
 124      }
 125  
 126      /**
 127       * Return menu items types
 128       *
 129       * @return  static  A \JControllerLegacy object to support chaining.
 130       *
 131       * @since   4.0.0
 132       */
 133      public function getTypes()
 134      {
 135          $viewType   = $this->app->getDocument()->getType();
 136          $viewName   = $this->input->get('view', $this->default_view);
 137          $viewLayout = $this->input->get('layout', 'default', 'string');
 138  
 139          try {
 140              /** @var JsonapiView $view */
 141              $view = $this->getView(
 142                  $viewName,
 143                  $viewType,
 144                  '',
 145                  ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
 146              );
 147          } catch (\Exception $e) {
 148              throw new \RuntimeException($e->getMessage());
 149          }
 150  
 151          /** @var ListModel $model */
 152          $model = $this->getModel('menutypes', '', ['ignore_request' => true]);
 153  
 154          if (!$model) {
 155              throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
 156          }
 157  
 158          $model->setState('client_id', $this->getClientIdFromInput());
 159  
 160          $view->setModel($model, true);
 161  
 162          $view->document = $this->app->getDocument();
 163  
 164          $view->displayListTypes();
 165  
 166          return $this;
 167      }
 168  
 169      /**
 170       * Get client id from input
 171       *
 172       * @return string
 173       *
 174       * @since 4.0.0
 175       */
 176      private function getClientIdFromInput()
 177      {
 178          return $this->input->exists('client_id') ?
 179              $this->input->get('client_id') : $this->input->post->get('client_id');
 180      }
 181  }


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