/**
* Execute and display a template script.
*
* @param array|null $items Array of items
*
* @return string
*
* @since 4.0.0
*/
public function displayList(array $items = null)
{
/** @var \Joomla\CMS\MVC\Model\ListModel $model */
$model = $this->getModel();
// Get page query
$currentUrl = Uri::getInstance();
$currentPageDefaultInformation = ['offset' => 0, 'limit' => 20];
$currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
if ($items === null) {
$items = [];
foreach ($model->getItems() as $item) {
$items[] = $this->prepareItem($item);
}
}
$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
$totalItemsCount = $pagination->pagesTotal * $pagination->limit;
$this->document->addMeta('total-pages', $pagination->pagesTotal)->addLink('self', (string) $currentUrl);
// Check for first and previous pages
if ($pagination->limitstart > 0) {
$firstPage = clone $currentUrl;
$firstPageQuery = $currentPageQuery;
$firstPageQuery['offset'] = 0;
$firstPage->setVar('page', $firstPageQuery);
$previousPage = clone $currentUrl;
$previousPageQuery = $currentPageQuery;
$previousOffset = $currentPageQuery['offset'] - $pagination->limit;
$previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0;
$previousPage->setVar('page', $previousPageQuery);
$this->document->addLink('first', $this->queryEncode((string) $firstPage))->addLink('previous', $this->queryEncode((string) $previousPage));
}
// Check for next and last pages
if ($pagination->limitstart + $pagination->limit < $totalItemsCount) {
$nextPage = clone $currentUrl;
$nextPageQuery = $currentPageQuery;
$nextOffset = $currentPageQuery['offset'] + $pagination->limit;
$nextPageQuery['offset'] = $nextOffset > $pagination->pagesTotal * $pagination->limit ? $pagination->pagesTotal - $pagination->limit : $nextOffset;
$nextPage->setVar('page', $nextPageQuery);
$lastPage = clone $currentUrl;
$lastPageQuery = $currentPageQuery;
$lastPageQuery['offset'] = ($pagination->pagesTotal - 1) * $pagination->limit;
$lastPage->setVar('page', $lastPageQuery);
$this->document->addLink('next', $this->queryEncode((string) $nextPage))->addLink('last', $this->queryEncode((string) $lastPage));
}
$eventData = ['type' => OnGetApiFields::LIST, 'fields' => $this->fieldsToRenderList, 'context' => $this->type];
$event = new OnGetApiFields('onApiGetFields', $eventData);
/** @var OnGetApiFields $eventResult */
$eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event);
$collection = (new Collection($items, $this->serializer))->fields([$this->type => $eventResult->getAllPropertiesToRender()]);
if (!empty($this->relationship)) {
$collection->with($this->relationship);
}
// Set the data into the document and render it
$this->document->setData($collection);
return $this->document->render();
}