- md5
- 049d2a65b8b8f804bc965786057de4f4
- sha1
- 139d595ddf23ad36a299291761d91b64e6f7794e
- sha256
- 9be259fcf3c9cf25ee7877b15da0225b03afcde231783d139ebdfd529466e87b
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\MVC\View;
defined('_JEXEC') or die;
use Joomla\CMS\Document\JsonapiDocument;
use Joomla\CMS\Router\Exception\RouteNotFoundException;
use Joomla\CMS\Serializer\JoomlaSerializer;
use Joomla\CMS\Uri\Uri;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Resource;
/**
* Base class for a Joomla Json List View
*
* Class holding methods for displaying presentation data.
*
* @since 4.0.0
*/
abstract class JsonApiView extends JsonView
{
/**
* The active document object (Redeclared for typehinting)
*
* @var JsonapiDocument
* @since 3.0
*/
public $document;
/**
* The content type
*
* @var string
*/
protected $type;
/**
* The fields to render in the documents
*
* @var string
*/
protected $fieldsToRender = [];
/**
* Constructor.
*
* @param array $config A named configuration array for object construction.
* contentType: the name (optional) of the content type to use for the serialization
*
* @since 4.0.0
*/
public function __construct($config = array())
{
if (array_key_exists('contentType', $config))
{
$this->type = $config['contentType'];
}
parent::__construct($config);
}
/**
* Execute and display a template script.
*
* @return string
*
* @since 4.0.0
*/
public function displayList()
{
/** @var \Joomla\CMS\MVC\Model\ListModel $model */
$model = $this->getModel();
$items = $model->getItems();
$pagination = $model->getPagination();
// Check for errors.
if (count($errors = $this->get('Errors')))
{
throw new GenericDataException(implode("\n", $errors), 500);
}
if ($this->type === null)
{
throw new \RuntimeException('Content type missing');
}
// Set up links for pagination
$currentUrl = Uri::getInstance();
$currentPageDefaultInformation = array('offset' => $pagination->limitstart, 'limit' => $pagination->limit);
$currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
$totalPagesAvailable = ($pagination->pagesTotal * $pagination->limit);
$firstPage = clone $currentUrl;
$firstPageQuery = $currentPageQuery;
$firstPageQuery['offset'] = 0;
$firstPage->setVar('page', $firstPageQuery);
$nextPage = clone $currentUrl;
$nextPageQuery = $currentPageQuery;
$nextOffset = $currentPageQuery['offset'] + $pagination->limit;
$nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $pagination->limit)) ? $totalPagesAvailable - $pagination->limit : $nextOffset;
$nextPage->setVar('page', $nextPageQuery);
$previousPage = clone $currentUrl;
$previousPageQuery = $currentPageQuery;
$previousOffset = $currentPageQuery['offset'] - $pagination->limit;
$previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0;
$previousPage->setVar('page', $previousPageQuery);
$lastPage = clone $currentUrl;
$lastPageQuery = $currentPageQuery;
$lastPageQuery['offset'] = $totalPagesAvailable - $pagination->limit;
$lastPage->setVar('page', $lastPageQuery);
$collection = (new Collection($items, new JoomlaSerializer($this->type)))
->fields([$this->type => $this->fieldsToRender]);
// Set the data into the document and render it
$this->document->addMeta('total-pages', $pagination->pagesTotal)
->setData($collection)
->addLink('self', (string) $currentUrl)
->addLink('first', (string) $firstPage)
->addLink('next', (string) $nextPage)
->addLink('previous', (string) $previousPage)
->addLink('last', (string) $lastPage);
return $this->document->render();
}
/**
* Execute and display a template script.
*
* @return string
*
* @since 4.0.0
*/
public function displayItem()
{
/** @var \Joomla\CMS\MVC\Model\AdminModel $model */
$model = $this->getModel();
$item = $model->getItem();
if ($item->id === null)
{
throw new RouteNotFoundException('Item does not exist');
}
// Check for errors.
if (count($errors = $this->get('Errors')))
{
throw new GenericDataException(implode("\n", $errors), 500);
}
if ($this->type === null)
{
throw new \RuntimeException('Content type missing');
}
$serializer = new JoomlaSerializer($this->type);
$element = (new Resource($item, $serializer))
->fields([$this->type => $this->fieldsToRender]);
$this->document->setData($element);
$this->document->addLink('self', Uri::current());
return $this->document->render();
}
}
The same content is in 3 releases, Joomla 4.0.0-alpha9 to 4.0.0-alpha11. See them all, or every version of this file.