[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_config/src/View/Application/ -> JsonapiView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.API
   5   * @subpackage  com_config
   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\Config\Api\View\Application;
  12  
  13  use Joomla\CMS\Extension\ExtensionHelper;
  14  use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
  15  use Joomla\CMS\Serializer\JoomlaSerializer;
  16  use Joomla\CMS\Uri\Uri;
  17  use Joomla\Component\Config\Administrator\Model\ApplicationModel;
  18  use Tobscure\JsonApi\Collection;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * The application view
  26   *
  27   * @since  4.0.0
  28   */
  29  class JsonapiView extends BaseApiView
  30  {
  31      /**
  32       * Execute and display a template script.
  33       *
  34       * @param   array|null  $items  Array of items
  35       *
  36       * @return  string
  37       *
  38       * @since   4.0.0
  39       */
  40      public function displayList(array $items = null)
  41      {
  42          /** @var ApplicationModel $model */
  43          $model = $this->getModel();
  44          $items = [];
  45  
  46          foreach ($model->getData() as $key => $value) {
  47              $item    = (object) [$key => $value];
  48              $items[] = $this->prepareItem($item);
  49          }
  50  
  51          // Set up links for pagination
  52          $currentUrl = Uri::getInstance();
  53          $currentPageDefaultInformation = ['offset' => 0, 'limit' => 20];
  54          $currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
  55  
  56          $offset              = $currentPageQuery['offset'];
  57          $limit               = $currentPageQuery['limit'];
  58          $totalItemsCount     = \count($items);
  59          $totalPagesAvailable = ceil($totalItemsCount / $limit);
  60  
  61          $items = array_splice($items, $offset, $limit);
  62  
  63          $this->document->addMeta('total-pages', $totalPagesAvailable)
  64              ->addLink('self', (string) $currentUrl);
  65  
  66          // Check for first and previous pages
  67          if ($offset > 0) {
  68              $firstPage = clone $currentUrl;
  69              $firstPageQuery = $currentPageQuery;
  70              $firstPageQuery['offset'] = 0;
  71              $firstPage->setVar('page', $firstPageQuery);
  72  
  73              $previousPage = clone $currentUrl;
  74              $previousPageQuery = $currentPageQuery;
  75              $previousOffset = $currentPageQuery['offset'] - $limit;
  76              $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0;
  77              $previousPage->setVar('page', $previousPageQuery);
  78  
  79              $this->document->addLink('first', $this->queryEncode((string) $firstPage))
  80                  ->addLink('previous', $this->queryEncode((string) $previousPage));
  81          }
  82  
  83          // Check for next and last pages
  84          if ($offset + $limit < $totalItemsCount) {
  85              $nextPage = clone $currentUrl;
  86              $nextPageQuery = $currentPageQuery;
  87              $nextOffset = $currentPageQuery['offset'] + $limit;
  88              $nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $limit)) ? $totalPagesAvailable - $limit : $nextOffset;
  89              $nextPage->setVar('page', $nextPageQuery);
  90  
  91              $lastPage = clone $currentUrl;
  92              $lastPageQuery = $currentPageQuery;
  93              $lastPageQuery['offset'] = ($totalPagesAvailable - 1) * $limit;
  94              $lastPage->setVar('page', $lastPageQuery);
  95  
  96              $this->document->addLink('next', $this->queryEncode((string) $nextPage))
  97                  ->addLink('last', $this->queryEncode((string) $lastPage));
  98          }
  99  
 100          $collection = (new Collection($items, new JoomlaSerializer($this->type)));
 101  
 102          // Set the data into the document and render it
 103          $this->document->setData($collection);
 104  
 105          return $this->document->render();
 106      }
 107  
 108      /**
 109       * Prepare item before render.
 110       *
 111       * @param   object  $item  The model item
 112       *
 113       * @return  object
 114       *
 115       * @since   4.0.0
 116       */
 117      protected function prepareItem($item)
 118      {
 119          $item->id = ExtensionHelper::getExtensionRecord('joomla', 'file')->extension_id;
 120  
 121          return $item;
 122      }
 123  }


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