[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


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