From 877bd5b4bc974522a843cbab0a870c59472e1de7 Mon Sep 17 00:00:00 2001 From: Jebb Domingo Date: Tue, 23 Jul 2019 11:09:30 +0800 Subject: [PATCH 1/2] #202 Do not override existing content --- code/view/json.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/code/view/json.php b/code/view/json.php index abecf6bb23..b0e1c68d83 100644 --- a/code/view/json.php +++ b/code/view/json.php @@ -135,6 +135,12 @@ protected function _actionRender(ViewContext $context) */ protected function _fetchData(ViewContext $context) { + $content = $this->getContent(); + + if (!empty($content)) { + return; + } + $output = new \ArrayObject(array( 'jsonapi' => array('version' => $this->_version), 'links' => array('self' => $this->getUrl()->toString()), From 59f2c2587c879d43f7b5f1a9023802fe23aa4d04 Mon Sep 17 00:00:00 2001 From: Ercan Ozkaya Date: Fri, 26 Jul 2019 15:46:11 +0300 Subject: [PATCH 2/2] #202: Use view content if it is set explicitly --- code/view/json.php | 67 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/code/view/json.php b/code/view/json.php index b0e1c68d83..1aebfa1068 100644 --- a/code/view/json.php +++ b/code/view/json.php @@ -135,52 +135,53 @@ protected function _actionRender(ViewContext $context) */ protected function _fetchData(ViewContext $context) { - $content = $this->getContent(); - - if (!empty($content)) { - return; + 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() + )); - $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; } /**