From c9356ec206641f3362b9ec0981c2a68f3b4489b5 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 28 Nov 2019 18:15:55 +0400 Subject: [PATCH] max_items depends on header application/vnd.phraseanet.story-paginate --- .../Application/ApiApplicationLoader.php | 3 ++- .../Phrasea/Controller/Api/V1Controller.php | 19 ++++++++++++++----- .../Phrasea/ControllerProvider/Api/V1.php | 2 ++ .../Databox/Record/RecordRepository.php | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/Alchemy/Phrasea/Application/ApiApplicationLoader.php b/lib/Alchemy/Phrasea/Application/ApiApplicationLoader.php index 490554c95e..c2f1a4da3b 100644 --- a/lib/Alchemy/Phrasea/Application/ApiApplicationLoader.php +++ b/lib/Alchemy/Phrasea/Application/ApiApplicationLoader.php @@ -51,7 +51,8 @@ class ApiApplicationLoader extends BaseApplicationLoader ['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'], V1::$extendedContentTypes['json'], V1::$extendedContentTypes['jsonp'], - V1::$extendedContentTypes['yaml'] + V1::$extendedContentTypes['yaml'], + [V1::HEADER_ACCEPT_STORY_PAGINATE] ); $app['phraseanet.content-negotiation.custom_formats'] = [ diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index 7b41bf7f64..dacb48f539 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -1217,12 +1217,18 @@ class V1Controller extends Controller $fractal->setSerializer(new TraceableArraySerializer($this->app['dispatcher'])); $fractal->parseIncludes($this->resolveSearchIncludes($request)); + $story_max_items = null; + // if the header accept story paginate, take account story_max_items parameter + if (in_array(V1::HEADER_ACCEPT_STORY_PAGINATE, $request->getAcceptableContentTypes())) { + $story_max_items = (int)$request->get('story_max_items') ?: 10; + } + $result = $this->doSearch($request); $searchView = $this->buildSearchView( $result, $includeResolver->resolve($fractal), $this->resolveSubdefUrlTTL($request), - (int)$request->get('story_max_items')?:10 + $story_max_items ); $ret = $fractal->createData(new Item($searchView, $searchTransformer))->toArray(); @@ -1676,10 +1682,13 @@ class V1Controller extends Controller return Result::createError($request, 404, 'Story not found')->createResponse(); } - $max_items = (int)$request->get('max_items')?:10; - $page = (int)$request->get('page')?:1; - - $offset = $max_items * ($page - 1) + 1; + $offset = 1; + $max_items = null; + if (in_array(V1::HEADER_ACCEPT_STORY_PAGINATE, $request->getAcceptableContentTypes())) { + $max_items = (int)$request->get('max_items')?:10; + $page = (int)$request->get('page')?:1; + $offset = ($max_items * ($page - 1)) + 1; + } $caption = $story->get_caption(); diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php b/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php index d64689eca2..597e34aec7 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php @@ -25,6 +25,8 @@ class V1 extends Api implements ControllerProviderInterface, ServiceProviderInte { const VERSION = '2.0.0'; + const HEADER_ACCEPT_STORY_PAGINATE = 'application/vnd.phraseanet.story-paginate'; + public static $extendedContentTypes = [ 'json' => ['application/vnd.phraseanet.record-extended+json'], 'yaml' => ['application/vnd.phraseanet.record-extended+yaml'], diff --git a/lib/Alchemy/Phrasea/Databox/Record/RecordRepository.php b/lib/Alchemy/Phrasea/Databox/Record/RecordRepository.php index fb91e05f1f..870339a238 100644 --- a/lib/Alchemy/Phrasea/Databox/Record/RecordRepository.php +++ b/lib/Alchemy/Phrasea/Databox/Record/RecordRepository.php @@ -47,7 +47,7 @@ interface RecordRepository * @param null|int $max_items * @return \set_selection[] */ - public function findChildren(array $storyIds, $user = null, $offset = 0, $max_items = null); + public function findChildren(array $storyIds, $user = null, $offset = 1, $max_items = null); /**