diff --git a/code/view/json.php b/code/view/json.php index abecf6bb23..1aebfa1068 100644 --- a/code/view/json.php +++ b/code/view/json.php @@ -135,46 +135,53 @@ protected function _actionRender(ViewContext $context) */ protected function _fetchData(ViewContext $context) { - $output = new \ArrayObject(array( - 'jsonapi' => array('version' => $this->_version), - 'links' => array('self' => $this->getUrl()->toString()), - 'data' => array() - )); + if ($content = $this->getContent()) { + $context->content = $content; + } + else + { + $output = new \ArrayObject(array( + 'jsonapi' => array('version' => $this->_version), + 'links' => array('self' => $this->getUrl()->toString()), + 'data' => array() + )); - $model = $this->getModel(); - $url = $this->getUrl(); + $model = $this->getModel(); + $url = $this->getUrl(); - if ($this->isCollection()) - { - foreach ($model->fetch() as $entity) { - $output['data'][] = $this->_createResource($entity); - } + if ($this->isCollection()) + { + foreach ($model->fetch() as $entity) { + $output['data'][] = $this->_createResource($entity); + } + + $total = $model->count(); + $limit = (int) $model->getState()->limit; + $offset = (int) $model->getState()->offset; - $total = $model->count(); - $limit = (int) $model->getState()->limit; - $offset = (int) $model->getState()->offset; + $output['meta'] = array( + 'offset' => $offset, + 'limit' => $limit, + 'total' => $total + ); - $output['meta'] = array( - 'offset' => $offset, - 'limit' => $limit, - 'total' => $total - ); + if ($limit && $total-($limit + $offset) > 0) { + $output['links']['next'] = $url->setQuery(array('offset' => $limit+$offset), true)->toString(); + } - if ($limit && $total-($limit + $offset) > 0) { - $output['links']['next'] = $url->setQuery(array('offset' => $limit+$offset), true)->toString(); + if ($limit && $offset && $offset >= $limit) { + $output['links']['prev'] = $url->setQuery(array('offset' => max($offset-$limit, 0)), true)->toString(); + } } + else $output['data'] = $this->_createResource($model->fetch()->getIterator()->current()); - if ($limit && $offset && $offset >= $limit) { - $output['links']['prev'] = $url->setQuery(array('offset' => max($offset-$limit, 0)), true)->toString(); + if ($this->_included_resources) { + $output['included'] = array_values($this->_included_resources); } - } - else $output['data'] = $this->_createResource($model->fetch()->getIterator()->current()); - if ($this->_included_resources) { - $output['included'] = array_values($this->_included_resources); + $context->content = $output; } - $context->content = $output; } /**