From 1bcefda9a1ee764a8257f5f6fb713992a002f8c6 Mon Sep 17 00:00:00 2001 From: Thibaud Fabre Date: Tue, 7 Jul 2015 18:52:51 +0200 Subject: [PATCH 1/2] Add API get collection route Conflicts: lib/Alchemy/Phrasea/Controller/Api/V1.php lib/classes/API/V1/adapter.php Conflicts: lib/classes/appbox.php --- lib/Alchemy/Phrasea/Application/Api.php | 4 +++- .../Phrasea/Controller/Api/V1Controller.php | 7 ++++++ .../Phrasea/ControllerProvider/Api/V1.php | 2 ++ lib/classes/appbox.php | 22 ++++++++++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/Alchemy/Phrasea/Application/Api.php b/lib/Alchemy/Phrasea/Application/Api.php index 751fb1d484..dc6d54d18e 100644 --- a/lib/Alchemy/Phrasea/Application/Api.php +++ b/lib/Alchemy/Phrasea/Application/Api.php @@ -92,7 +92,9 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) { if ($request->getRequestFormat(Result::FORMAT_JSON) === Result::FORMAT_JSONP && !$response->isOk() && !$response->isServerError()) { $response->setStatusCode(200); } - // set response content type + // set response content typeReturns available collections on a specified databox. + + if (!$response->headers->get('Content-Type')) { $response->headers->set('Content-Type', $request->getMimeType($request->getRequestFormat(Result::FORMAT_JSON))); } diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index a283220cd2..d3f7d46f8d 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -466,6 +466,13 @@ class V1Controller extends Controller ]; } + public function getDataboxCollectionAction(Request $request, $base_id) + { + return Result::create($request, [ + $this->listCollection($this->app['phraseanet.appbox']->get_collection($base_id)) + ])->createResponse(); + } + /** * Get a Response containing the collections of a \databox * diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php b/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php index edf576915a..58c866ba21 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Api/V1.php @@ -72,6 +72,8 @@ class V1 implements ControllerProviderInterface, ServiceProviderInterface $controllers->get('/monitor/phraseanet/', 'controller.api.v1:showPhraseanetConfigurationAction') ->before('controller.api.v1:ensureAdmin'); + $controllers->get('/collections/{base_id}/', 'controller.api.v1:getDataboxCollectionAction'); + $controllers->get('/databoxes/list/', 'controller.api.v1:listDataboxesAction'); $controllers->get('/databoxes/{databox_id}/collections/', 'controller.api.v1:getDataboxCollectionsAction') diff --git a/lib/classes/appbox.php b/lib/classes/appbox.php index c1230b9864..6d2c7defac 100644 --- a/lib/classes/appbox.php +++ b/lib/classes/appbox.php @@ -367,7 +367,7 @@ class appbox extends base return $this->databoxes; } - + public function get_databox($sbas_id) { $databoxes = $this->get_databoxes(); @@ -379,6 +379,26 @@ class appbox extends base return $databoxes[$sbas_id]; } + public function get_collection($base_id) + { + $sbas_id = phrasea::sbasFromBas($this->app, $base_id); + + if ($sbas_id === false) { + throw new \RuntimeException('Collection not found.'); + } + + $collections = $this->get_databox($sbas_id)->get_collections(); + + foreach ($collections as $collection) { + if ($collection->get_base_id() == $base_id) { + return $collection; + } + } + + // This should not happen, but I'd rather be safe than sorry. + throw new \RuntimeException('Collection not found.'); + } + /** * @param string $option * @return string From da9ec3908ced3777c34617cf29c72b5b473ceb60 Mon Sep 17 00:00:00 2001 From: Thibaud Fabre Date: Fri, 25 Sep 2015 18:06:32 +0200 Subject: [PATCH 2/2] Fix typos and use method to fetch appbox --- lib/Alchemy/Phrasea/Application/Api.php | 3 +-- lib/Alchemy/Phrasea/Controller/Api/V1Controller.php | 2 +- lib/classes/appbox.php | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/Alchemy/Phrasea/Application/Api.php b/lib/Alchemy/Phrasea/Application/Api.php index dc6d54d18e..55aa2e4a06 100644 --- a/lib/Alchemy/Phrasea/Application/Api.php +++ b/lib/Alchemy/Phrasea/Application/Api.php @@ -92,9 +92,8 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) { if ($request->getRequestFormat(Result::FORMAT_JSON) === Result::FORMAT_JSONP && !$response->isOk() && !$response->isServerError()) { $response->setStatusCode(200); } - // set response content typeReturns available collections on a specified databox. - + // set response content type if (!$response->headers->get('Content-Type')) { $response->headers->set('Content-Type', $request->getMimeType($request->getRequestFormat(Result::FORMAT_JSON))); } diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index d3f7d46f8d..f0e113c404 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -469,7 +469,7 @@ class V1Controller extends Controller public function getDataboxCollectionAction(Request $request, $base_id) { return Result::create($request, [ - $this->listCollection($this->app['phraseanet.appbox']->get_collection($base_id)) + $this->listCollection($this->app->getApplicationBox()->get_collection($base_id)) ])->createResponse(); } diff --git a/lib/classes/appbox.php b/lib/classes/appbox.php index 6d2c7defac..e7a9038d7c 100644 --- a/lib/classes/appbox.php +++ b/lib/classes/appbox.php @@ -367,7 +367,7 @@ class appbox extends base return $this->databoxes; } - + public function get_databox($sbas_id) { $databoxes = $this->get_databoxes();