diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3Controller.php new file mode 100644 index 0000000000..fe865f41c2 --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3Controller.php @@ -0,0 +1,76 @@ +attributes->get('databox_id'))) { + $ret = [ + 'databoxes' => $this->listSubdefsStructure([$this->findDataboxById($request->attributes->get('databox_id'))]) + ]; + } else { + $acl = $this->getAclForUser($this->getAuthenticatedUser()); + + // ensure can see databox structure + $ret = [ + 'databoxes' => $this->listSubdefsStructure($acl->get_granted_sbas([\ACL::BAS_MODIFY_STRUCT])) + ]; + } + + return Result::create($request, $ret)->createResponse(); + } + + /** + * List the subdef structure of databoxes + * @param array $databoxes + * @return array + * @throws \Exception + */ + private function listSubdefsStructure(array $databoxes) + { + $ret = []; + + /** @var \databox $databox */ + foreach ($databoxes as $databox) { + $databoxId = $databox->get_sbas_id(); + $subdefStructure = $databox->get_subdef_structure(); + $subdefs = []; + foreach ($subdefStructure as $subGroup) { + /** @var \databox_subdef $sub */ + foreach ($subGroup->getIterator() as $sub) { + $opt = []; + $data = [ + 'name' => $sub->get_name(), + 'databox_id' => $databoxId, + 'class' => $sub->get_class(), + 'preset' => $sub->get_preset(), + 'downloadable' => $sub->isDownloadable(), + 'devices' => $sub->getDevices(), + 'labels' => [ + 'fr' => $sub->get_label('fr'), + 'en' => $sub->get_label('en'), + 'de' => $sub->get_label('de'), + 'nl' => $sub->get_label('nl'), + ], + ]; + $options = $sub->getOptions(); + foreach ($options as $option) { + $opt[$option->getName()] = $option->getValue(); + } + $data['options'] = $opt; + $subdefs[$subGroup->getName()][$sub->get_name()] = $data; + } + } + $ret[$databoxId]['databox_id'] = $databoxId; + $ret[$databoxId]['subdefs'] = $subdefs; + } + + return $ret; + } +} diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php index 011000495d..e7797eaf92 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php @@ -4,6 +4,7 @@ namespace Alchemy\Phrasea\ControllerProvider\Api; use Alchemy\Phrasea\Application as PhraseaApplication; use Alchemy\Phrasea\Controller\Api\V1Controller; +use Alchemy\Phrasea\Controller\Api\V3\V3Controller; use Alchemy\Phrasea\Controller\Api\V3\V3RecordController; use Alchemy\Phrasea\Controller\Api\V3\V3ResultHelpers; use Alchemy\Phrasea\Controller\Api\V3\V3SearchController; @@ -53,6 +54,9 @@ class V3 extends Api implements ControllerProviderInterface, ServiceProviderInte $app['controller.api.v3.stories'] = $app->share(function (PhraseaApplication $app) { return (new V3StoriesController($app)); }); + $app['controller.api.v3'] = $app->share(function (PhraseaApplication $app) { + return (new V3Controller($app)); + }); } public function boot(Application $app) @@ -150,6 +154,16 @@ class V3 extends Api implements ControllerProviderInterface, ServiceProviderInte $controllers->post('/subdefs_service/', 'controller.api.v3.subdefs_service:indexAction_POST'); } + /** + * @uses V3Controller::getDataboxSubdefsAction() + */ + $controllers->get('/databoxes/{databox_id}/subdefs/', 'controller.api.v3:getDataboxSubdefsAction') + ->before('controller.api.v1:ensureAccessToDatabox') + ->before('controller.api.v1:ensureCanSeeDataboxStructure') + ->assert('databox_id', '\d+'); + + $controllers->get('/databoxes/subdefs/', 'controller.api.v3:getDataboxSubdefsAction'); + return $controllers; }