[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_menus/src/View/Items/ -> JsonapiView.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\View\Items;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  15  use Joomla\CMS\Serializer\JoomlaSerializer;
  16  use Joomla\CMS\Uri\Uri;
  17  use Tobscure\JsonApi\Collection;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * The items view
  25   *
  26   * @since  4.0.0
  27   */
  28  class JsonapiView extends BaseApiView
  29  {
  30      /**
  31       * The fields to render item in the documents
  32       *
  33       * @var  array
  34       * @since  4.0.0
  35       */
  36      protected $fieldsToRenderItem = [
  37          'id',
  38          'parent_id',
  39          'level',
  40          'lft',
  41          'rgt',
  42          'alias',
  43          'typeAlias',
  44          'menutype',
  45          'title',
  46          'note',
  47          'path',
  48          'link',
  49          'type',
  50          'published',
  51          'component_id',
  52          'checked_out',
  53          'checked_out_time',
  54          'browserNav',
  55          'access',
  56          'img',
  57          'template_style_id',
  58          'params',
  59          'home',
  60          'language',
  61          'client_id',
  62          'publish_up',
  63          'publish_down',
  64          'request',
  65          'associations',
  66          'menuordering',
  67      ];
  68  
  69      /**
  70       * The fields to render items in the documents
  71       *
  72       * @var  array
  73       * @since  4.0.0
  74       */
  75      protected $fieldsToRenderList  = [
  76          'id',
  77          'menutype',
  78          'title',
  79          'alias',
  80          'note',
  81          'path',
  82          'link',
  83          'type',
  84          'parent_id',
  85          'level',
  86          'a.published',
  87          'component_id',
  88          'checked_out',
  89          'checked_out_time',
  90          'browserNav',
  91          'access',
  92          'img',
  93          'template_style_id',
  94          'params',
  95          'lft',
  96          'rgt',
  97          'home',
  98          'language',
  99          'client_id',
 100          'enabled',
 101          'publish_up',
 102          'publish_down',
 103          'published',
 104          'language_title',
 105          'language_image',
 106          'language_sef',
 107          'editor',
 108          'componentname',
 109          'access_level',
 110          'menutype_id',
 111          'menutype_title',
 112          'association',
 113          'name',
 114      ];
 115  
 116      /**
 117       * Execute and display a list items types.
 118       *
 119       * @return  string
 120       *
 121       * @since   4.0.0
 122       */
 123      public function displayListTypes()
 124      {
 125          /** @var \Joomla\Component\Menus\Administrator\Model\MenutypesModel $model */
 126          $model = $this->getModel();
 127          $items = [];
 128  
 129          foreach ($model->getTypeOptions() as $type => $data) {
 130              $groupItems = [];
 131  
 132              foreach ($data as $item) {
 133                  $item->id          = implode('/', $item->request);
 134                  $item->title       = Text::_($item->title);
 135                  $item->description = Text::_($item->description);
 136                  $item->group       = Text::_($type);
 137  
 138                  $groupItems[] = $item;
 139              }
 140  
 141              $items = array_merge($items, $groupItems);
 142          }
 143  
 144          // Set up links for pagination
 145          $currentUrl = Uri::getInstance();
 146          $currentPageDefaultInformation = ['offset' => 0, 'limit' => 20];
 147          $currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
 148  
 149          $offset              = $currentPageQuery['offset'];
 150          $limit               = $currentPageQuery['limit'];
 151          $totalItemsCount     = \count($items);
 152          $totalPagesAvailable = ceil($totalItemsCount / $limit);
 153  
 154          $items = array_splice($items, $offset, $limit);
 155  
 156          $firstPage = clone $currentUrl;
 157          $firstPageQuery = $currentPageQuery;
 158          $firstPageQuery['offset'] = 0;
 159          $firstPage->setVar('page', $firstPageQuery);
 160  
 161          $nextPage = clone $currentUrl;
 162          $nextPageQuery = $currentPageQuery;
 163          $nextOffset = $currentPageQuery['offset'] + $limit;
 164          $nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $limit)) ? $totalPagesAvailable - $limit : $nextOffset;
 165          $nextPage->setVar('page', $nextPageQuery);
 166  
 167          $previousPage = clone $currentUrl;
 168          $previousPageQuery = $currentPageQuery;
 169          $previousOffset = $currentPageQuery['offset'] - $limit;
 170          $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0;
 171          $previousPage->setVar('page', $previousPageQuery);
 172  
 173          $lastPage = clone $currentUrl;
 174          $lastPageQuery = $currentPageQuery;
 175          $lastPageQuery['offset'] = $totalPagesAvailable - $limit;
 176          $lastPage->setVar('page', $lastPageQuery);
 177  
 178          $collection = (new Collection($items, new JoomlaSerializer('menutypes')));
 179  
 180          // Set the data into the document and render it
 181          $this->document->addMeta('total-pages', $totalPagesAvailable)
 182              ->setData($collection)
 183              ->addLink('self', (string) $currentUrl)
 184              ->addLink('first', (string) $firstPage)
 185              ->addLink('next', (string) $nextPage)
 186              ->addLink('previous', (string) $previousPage)
 187              ->addLink('last', (string) $lastPage);
 188  
 189          return $this->document->render();
 190      }
 191  
 192      /**
 193       * Prepare item before render.
 194       *
 195       * @param   object  $item  The model item
 196       *
 197       * @return  object
 198       *
 199       * @since   4.0.0
 200       */
 201      protected function prepareItem($item)
 202      {
 203          if (\is_string($item->params)) {
 204              $item->params = json_decode($item->params);
 205          }
 206  
 207          return parent::prepareItem($item);
 208      }
 209  }


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