diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1.php b/lib/Alchemy/Phrasea/Controller/Api/V1.php index 1e794cdf5f..58ee7b7049 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1.php @@ -34,7 +34,8 @@ class V1 implements ControllerProviderInterface /** * Api Service - * @var Closure + * + * @return \API_V1_adapter */ $app['api'] = function () use ($app) { return new \API_V1_adapter($app); @@ -44,7 +45,7 @@ class V1 implements ControllerProviderInterface * oAuth token verification process * - Check if oauth_token exists && is valid * - Check if request comes from phraseanet Navigator && phraseanet Navigator - * is enbale on current instance + * is enable on current instance * - restore user session * * @ throws \API_V1_exception_unauthorized @@ -165,6 +166,9 @@ class V1 implements ControllerProviderInterface ); }); + /** + * Close session + */ $controllers->after(function () use ($app) { $app['authentication']->closeAccount(); }); @@ -177,7 +181,7 @@ class V1 implements ControllerProviderInterface }; /** - * Check wether the current user is Admin or not + * Check whether the current user is Admin or not */ $mustBeAdmin = function (Request $request) use ($app) { $user = $app['token']->get_account()->get_user(); @@ -186,6 +190,71 @@ class V1 implements ControllerProviderInterface } }; + /** + * Check whether the current user has access to databox + */ + $hasAccessToDatabox = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + $databox = $app['phraseanet.appbox']->get_databox($request->attributes->get('databox_id')); + if (!$user->ACL()->has_access_to_sbas($databox->get_sbas_id())) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + + /** + * Check whether the current user has access to the record + */ + $hasAccessToRecord = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + $record = $app['phraseanet.appbox']->get_databox($request->attributes->get('databox_id'))->get_record($request->attributes->get('record_id')); + if (!$user->ACL()->has_access_to_record($record)) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + + /** + * Check whether the current user can modify the record + */ + $canModifyRecord = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + if (!$user->ACL()->has_right('modifyrecord')) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + + /** + * Check whether the current user can modify the record status + */ + $canModifyRecordStatus = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + $record = $app['phraseanet.appbox']->get_databox($request->attributes->get('databox_id'))->get_record($request->attributes->get('record_id')); + if (!$user->ACL()->has_right_on_base($record->get_base_id(), 'chgstatus')) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + + /** + * Check whether the current user can see databox structure + */ + $canSeeDataboxStructure = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + $databox = $app['phraseanet.appbox']->get_databox($request->attributes->get('databox_id')); + if (!$user->ACL()->has_right_on_sbas($databox->get_sbas_id(), 'bas_modify_struct')) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + + /** + * Check whether the current user can move record from a collection to an other + */ + $canMoveRecord = function (Request $request) use ($app) { + $user = $app['token']->get_account()->get_user(); + $record = $app['phraseanet.appbox']->get_databox($request->attributes->get('databox_id'))->get_record($request->attributes->get('record_id')); + if ((!$user->ACL()->has_right('addrecord') && !$user->ACL()->has_right('deleterecord')) || !$user->ACL()->has_right_on_base($record->get_base_id(), 'candeleterecord')) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + }; + /** * Get scheduler informations * @@ -309,7 +378,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_databox_collections($app['request'], $databox_id) ->get_response(); - })->assert('databox_id', '\d+'); + })->before($hasAccessToDatabox)->assert('databox_id', '\d+'); $controllers->get('/databoxes/{any_id}/collections/', $bad_request_exception); @@ -326,7 +395,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_databox_status($app['request'], $databox_id) ->get_response(); - })->assert('databox_id', '\d+'); + })->before($hasAccessToDatabox)->before($canSeeDataboxStructure)->assert('databox_id', '\d+'); $controllers->get('/databoxes/{any_id}/status/', $bad_request_exception); @@ -342,7 +411,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_databox_metadatas($app['request'], $databox_id) ->get_response(); - })->assert('databox_id', '\d+'); + })->before($hasAccessToDatabox)->before($canSeeDataboxStructure)->assert('databox_id', '\d+'); $controllers->get('/databoxes/{any_id}/metadatas/', $bad_request_exception); @@ -362,14 +431,31 @@ class V1 implements ControllerProviderInterface $controllers->get('/databoxes/{any_id}/termsOfUse/', $bad_request_exception); + /** + * Route /quarantine/list/ + * + * Method : GET + * + * Parameters ; + */ $controllers->get('/quarantine/list/', function (SilexApplication $app, Request $request) { return $app['api']->list_quarantine($app, $request)->get_response(); }); + /** + * Route /quarantine/item/{lazaret_id}/ + * + * Method : GET + * + * Parameters ; + * LAZARET_ID : required INT + */ $controllers->get('/quarantine/item/{lazaret_id}/', function ($lazaret_id, SilexApplication $app, Request $request) { return $app['api']->list_quarantine_item($lazaret_id, $app, $request)->get_response(); }); + $controllers->get('/quarantine/item/{any_id}/', $bad_request_exception); + /** * Route : /records/add/ * @@ -422,11 +508,18 @@ class V1 implements ControllerProviderInterface return $app['api']->search_records($app['request'])->get_response(); }); + /** + * Route : /records/{databox_id}/{record_id}/caption/ + * + * Parameters ; + * DATABOX_ID : required INT + * RECORD_ID : required INT + */ $controllers->get('/records/{databox_id}/{record_id}/caption/', function (SilexApplication $app, $databox_id, $record_id) { return $app['api'] ->caption_records($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/caption/', $bad_request_exception); @@ -444,7 +537,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_record_metadatas($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/metadatas/', $bad_request_exception); @@ -462,7 +555,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_record_status($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/status/', $bad_request_exception); @@ -480,7 +573,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_record_related($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/related/', $bad_request_exception); @@ -498,7 +591,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->get_record_embed($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/embed/', $bad_request_exception); @@ -516,7 +609,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->set_record_metadatas($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->before($canModifyRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->post('/records/{any_id}/{anyother_id}/setmetadatas/', $bad_request_exception); @@ -534,7 +627,7 @@ class V1 implements ControllerProviderInterface return $app['api'] ->set_record_status($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->before($canModifyRecord)->before($canModifyRecordStatus)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->post('/records/{any_id}/{anyother_id}/setstatus/', $bad_request_exception); @@ -552,15 +645,25 @@ class V1 implements ControllerProviderInterface return $app['api'] ->set_record_collection($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->before($canMoveRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->post('/records/{wrong_databox_id}/{wrong_record_id}/setcollection/', $bad_request_exception); + /** + * Route : /records/DATABOX_ID/RECORD_ID + * + * Method : GET + * + * Parameters : + * DATABOX_ID : required INT + * RECORD_ID : required INT + * + */ $controllers->get('/records/{databox_id}/{record_id}/', function (SilexApplication $app, $databox_id, $record_id) { return $app['api'] ->get_record($app['request'], $databox_id, $record_id) ->get_response(); - })->assert('databox_id', '\d+')->assert('record_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/records/{any_id}/{anyother_id}/', $bad_request_exception); @@ -666,12 +769,29 @@ class V1 implements ControllerProviderInterface ->get_response(); }); + /** + * Route : /feeds/content/ + * + * Method : GET + * + * Parameters : + * + */ $controllers->get('/feeds/content/', function (SilexApplication $app) { return $app['api'] ->get_publications($app['request'], $app['authentication']->getUser()) ->get_response(); }); + /** + * Route : /feeds/entry/{entry_id}/ + * + * Method : GET + * + * Parameters : + * ENTRY_ID : required INT + * + */ $controllers->get('/feeds/entry/{entry_id}/', function (SilexApplication $app, $entry_id) { return $app['api'] ->get_feed_entry($app['request'], $entry_id, $app['authentication']->getUser()) @@ -704,33 +824,44 @@ class V1 implements ControllerProviderInterface * * Parameters : * DATABOX_ID : required INT - * RECORD_ID : required INT + * STORY_ID : required INT * */ - $controllers->get('/stories/{databox_id}/{story_id}/embed/', function ($databox_id, $story_id) use ($app) { - $result = $app['api']->get_story_embed($app['request'], $databox_id, $story_id); + $controllers->get('/stories/{databox_id}/{record_id}/embed/', function ($databox_id, $record_id) use ($app) { + $result = $app['api']->get_story_embed($app['request'], $databox_id, $record_id); return $result->get_response(); } - )->assert('databox_id', '\d+')->assert('story_id', '\d+'); + )->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/stories/{any_id}/{anyother_id}/embed/', $bad_request_exception); - $controllers->get('/stories/{databox_id}/{story_id}/', function ($databox_id, $story_id) use ($app) { - $result = $app['api']->get_story($app['request'], $databox_id, $story_id); + /** + * Route : /stories/DATABOX_ID/RECORD_ID/ + * + * Method : GET + * + * Parameters : + * DATABOX_ID : required INT + * STORY_ID : required INT + * + */ + $controllers->get('/stories/{databox_id}/{record_id}/', function ($databox_id, $record_id) use ($app) { + $result = $app['api']->get_story($app['request'], $databox_id, $record_id); return $result->get_response(); - })->assert('databox_id', '\d+')->assert('story_id', '\d+'); + })->before($hasAccessToRecord)->assert('databox_id', '\d+')->assert('record_id', '\d+'); $controllers->get('/stories/{any_id}/{anyother_id}/', $bad_request_exception); - $controllers->get('/stories/{databox_id}/{story_id}/', function ($databox_id, $story_id) use ($app) { - $result = $app['api']->get_story($app['request'], $databox_id, $story_id); - - return $result->get_response(); - })->assert('databox_id', '\d+')->assert('story_id', '\d+'); - $controllers->get('/stories/{any_id}/{anyother_id}/', $bad_request_exception); - + /** + * Route : /me/ + * + * Method : GET + * + * Parameters : + * + */ $controllers->get('/me/', function (SilexApplication $app, Request $request) { $result = $app['api']->get_current_user($app, $request); diff --git a/lib/Alchemy/Phrasea/SearchEngine/SearchEngineOptions.php b/lib/Alchemy/Phrasea/SearchEngine/SearchEngineOptions.php index f9130cb8c4..2e1730fd94 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/SearchEngineOptions.php +++ b/lib/Alchemy/Phrasea/SearchEngine/SearchEngineOptions.php @@ -625,7 +625,12 @@ class SearchEngineOptions if (is_array($request->get('bases'))) { $bas = array_map(function ($base_id) use ($app) { - return \collection::get_from_base_id($app, $base_id); + try { + return \collection::get_from_base_id($app, $base_id); + } catch (\Exception_Databox_CollectionNotFound $e) { + return null; + } + }, $request->get('bases')); } elseif (!$app['authentication']->isAuthenticated()) { $bas = $app->getOpenCollections(); diff --git a/lib/classes/API/V1/Interface.php b/lib/classes/API/V1/Interface.php index fe0b3ba68d..19e2deded0 100644 --- a/lib/classes/API/V1/Interface.php +++ b/lib/classes/API/V1/Interface.php @@ -292,7 +292,7 @@ interface API_V1_Interface * USER_ID : required INT * */ - public function get_user_acces(Request $request, $usr_id); + public function get_user_access(Request $request, $usr_id); public function add_record(Application $app, Request $request); diff --git a/lib/classes/API/V1/adapter.php b/lib/classes/API/V1/adapter.php index eecc531b62..b65289c1cc 100644 --- a/lib/classes/API/V1/adapter.php +++ b/lib/classes/API/V1/adapter.php @@ -155,27 +155,13 @@ class API_V1_adapter extends API_V1_Abstract return $result; } - protected function list_task(\task_abstract $task) - { - return array( - 'id' => $task->getID(), - 'name' => $task->getName(), - 'state' => $task->getState(), - 'pid' => $task->getPID(), - 'title' => $task->getTitle(), - 'last_exec_time' => $task->getLastExecTime() ? $task->getLastExecTime()->format(DATE_ATOM) : null, - 'auto_start' => !!$task->isActive(), - 'runner' => $task->getRunner(), - 'crash_counter' => $task->getCrashCounter() - ); - } - /** - * Get informations about an identified task + * Get information about an identified task * - * @param \Silex\Application $app The API silex application - * @param integer $taskId - * @return \API_V1_result + * @param Application $app + * @param $taskId + * + * @return API_V1_result */ public function get_task(Application $app, $taskId) { @@ -195,9 +181,10 @@ class API_V1_adapter extends API_V1_Abstract /** * Start a specified task * - * @param \Silex\Application $app The API silex application - * @param integer $taskId The task id - * @return \API_V1_result + * @param Application $app + * @param $taskId + * + * @return API_V1_result */ public function start_task(Application $app, $taskId) { @@ -218,9 +205,10 @@ class API_V1_adapter extends API_V1_Abstract /** * Stop a specified task * - * @param \Silex\Application $app The API silex application - * @param integer $taskId The task id - * @return \API_V1_result + * @param Application $app + * @param $taskId + * + * @return API_V1_result */ public function stop_task(Application $app, $taskId) { @@ -242,10 +230,11 @@ class API_V1_adapter extends API_V1_Abstract * - name * - autostart * - * @param \Silex\Application $app Silex application - * @param integer $taskId the task id - * @return \API_V1_result - * @throws \API_V1_exception_badrequest + * @param Application $app + * @param $taskId + * + * @return API_V1_result + * @throws API_V1_exception_badrequest */ public function set_task_property(Application $app, $taskId) { @@ -275,10 +264,1739 @@ class API_V1_adapter extends API_V1_Abstract return $result; } + /** + * Provide + * - cache information + * - global values information + * - configuration information + * + * @param Application $app + * + * @return API_V1_result + */ + public function get_phraseanet_monitor(Application $app) + { + $result = new API_V1_result($app, $app['request'], $this); + + $result->set_datas(array_merge($this->get_config_info($app), $this->get_cache_info($app), $this->get_gv_info($app))); + + return $result; + } + + /** + * Get detailed about databoxes + * + * @param Request $request + * + * @return API_V1_result + */ + public function get_databoxes(Request $request) + { + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas(array("databoxes" => $this->list_databoxes())); + + return $result; + } + + /** + * Get an API_V1_result containing the collections of a databox + * + * @param Request $request + * @param int $databox_id + * + * @return API_V1_result + */ + public function get_databox_collections(Request $request, $databox_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas( + array( + "collections" => $this->list_databox_collections( + $this->app['phraseanet.appbox']->get_databox($databox_id) + ) + ) + ); + + return $result; + } + + /** + * Get an API_V1_result containing the status of a databox + * + * @param Request $request + * @param int $databox_id + * + * @return API_V1_result + */ + public function get_databox_status(Request $request, $databox_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas( + array( + "status" => $this->list_databox_status($this->app['phraseanet.appbox']->get_databox($databox_id)->get_statusbits()) + ) + ); + + return $result; + } + + /** + * Get an API_V1_result containing the metadatas of a databox + * + * @param Request $request + * @param int $databox_id + * + * @return API_V1_result + */ + public function get_databox_metadatas(Request $request, $databox_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas( + array( + "document_metadatas" => + $this->list_databox_metadatas_fields( + $this->app['phraseanet.appbox']->get_databox($databox_id) + ->get_meta_structure() + ) + ) + ); + + return $result; + } + + /** + * Get an API_V1_result containing the terms of use of a databox + * + * @param Request $request + * @param int $databox_id + * + * @return API_V1_result + */ + public function get_databox_terms(Request $request, $databox_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas(array( + "termsOfUse" => $this->list_databox_terms($this->app['phraseanet.appbox']->get_databox($databox_id)) + )); + + return $result; + } + + /** + * Get detailed information about a record caption + * + * @param Request $request + * @param $databox_id + * @param $record_id + * + * @return API_V1_result + */ + public function caption_records(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); + $fields = $record->get_caption()->get_fields(); + + $ret = array('caption_metadatas' => array()); + + foreach ($fields as $field) { + $ret['caption_metadatas'][] = array( + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'value' => $field->get_serialized_values(";"), + ); + } + + $result->set_datas($ret); + + return $result; + } + + /** + * Upload a record + * + * @param Application $app + * @param Request $request + * + * @return API_V1_result + * @throws API_V1_exception_forbidden + * @throws API_V1_exception_badrequest + */ + public function add_record(Application $app, Request $request) + { + if (count($request->files->get('file')) == 0) { + throw new API_V1_exception_badrequest('Missing file parameter'); + } + + if (!$request->files->get('file') instanceof Symfony\Component\HttpFoundation\File\UploadedFile) { + throw new API_V1_exception_badrequest('You can upload one file at time'); + } + + $file = $request->files->get('file'); + /* @var $file Symfony\Component\HttpFoundation\File\UploadedFile */ + + if (!$file->isValid()) { + throw new API_V1_exception_badrequest('Datas corrupted, please try again'); + } + + if (!$request->get('base_id')) { + throw new API_V1_exception_badrequest('Missing base_id parameter'); + } + + $collection = \collection::get_from_base_id($this->app, $request->get('base_id')); + + if (!$app['authentication']->getUser()->ACL()->has_right_on_base($request->get('base_id'), 'canaddrecord')) { + throw new API_V1_exception_forbidden(sprintf('You do not have access to collection %s', $collection->get_label($this->app['locale.I18n']))); + } + + $media = $app['mediavorus']->guess($file->getPathname()); + + $Package = new File($this->app, $media, $collection, $file->getClientOriginalName()); + + if ($request->get('status')) { + $Package->addAttribute(new Status($app, $request->get('status'))); + } + + $session = new Entities\LazaretSession(); + $session->setUsrId($app['authentication']->getUser()->get_id()); + + $app['EM']->persist($session); + $app['EM']->flush(); + + $reasons = $output = null; + + $callback = function ($element, $visa, $code) use (&$reasons, &$output) { + if (!$visa->isValid()) { + $reasons = array(); + + foreach ($visa->getResponses() as $response) { + $reasons[] = $response->getMessage(); + } + } + + $output = $element; + }; + + switch ($request->get('forceBehavior')) { + case '0' : + $behavior = BorderManager::FORCE_RECORD; + break; + case '1' : + $behavior = BorderManager::FORCE_LAZARET; + break; + case null: + $behavior = null; + break; + default: + throw new API_V1_exception_badrequest(sprintf('Invalid forceBehavior value `%s`', $request->get('forceBehavior'))); + break; + } + + $app['border-manager']->process($session, $Package, $callback, $behavior); + + $ret = array( + 'entity' => null, + ); + + if ($output instanceof \record_adapter) { + $ret['entity'] = '0'; + $ret['url'] = '/records/' . $output->get_sbas_id() . '/' . $output->get_record_id() . '/'; + $app['phraseanet.SE']->addRecord($output); + } + if ($output instanceof \Entities\LazaretFile) { + $ret['entity'] = '1'; + $ret['url'] = '/quarantine/item/' . $output->getId() . '/'; + } + + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas($ret); + + return $result; + } + + /** + * List quarantined items + * + * @param Application $app + * @param Request $request + * + * @return API_V1_result + */ + public function list_quarantine(Application $app, Request $request) + { + $offset_start = max($request->get('offset_start', 0), 0); + $per_page = min(max($request->get('per_page', 10), 1), 20); + + $baseIds = array_keys($app['authentication']->getUser()->ACL()->get_granted_base(array('canaddrecord'))); + + $lazaretFiles = array(); + + if (count($baseIds) > 0) { + $lazaretRepository = $app['EM']->getRepository('Entities\LazaretFile'); + $lazaretFiles = $lazaretRepository->findPerPage($baseIds, $offset_start, $per_page); + } + + $ret = array(); + + foreach ($lazaretFiles as $lazaretFile) { + $ret[] = $this->list_lazaret_file($lazaretFile); + } + + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas(array( + 'offset_start' => $offset_start, + 'per_page' => $per_page, + 'quarantine_items' => $ret, + )); + + return $result; + } + + /** + * Retrieve detailed information about a quarantined item + * + * @param $lazaret_id + * @param Application $app + * @param Request $request + * + * @return API_V1_result + * @throws API_V1_exception_forbidden + * @throws API_V1_exception_notfound + */ + public function list_quarantine_item($lazaret_id, Application $app, Request $request) + { + $lazaretFile = $app['EM']->find('Entities\LazaretFile', $lazaret_id); + + if (null === $lazaretFile) { + throw new \API_V1_exception_notfound(sprintf('Lazaret file id %d not found', $lazaret_id)); + } + + if (!$app['authentication']->getUser()->ACL()->has_right_on_base($lazaretFile->getBaseId(), 'canaddrecord')) { + throw new \API_V1_exception_forbidden('You do not have access to this quarantine item'); + } + + $ret = array('quarantine_item' => $this->list_lazaret_file($lazaretFile)); + + $result = new API_V1_result($this->app, $request, $this); + + $result->set_datas($ret); + + return $result; + } + + /** + * Search for results + * + * @param Request $request + * @return \API_V1_result + */ + public function search(Request $request) + { + $result = new API_V1_result($this->app, $request, $this); + + list($ret, $search_result) = $this->prepare_search_request($request); + + $ret['results'] = array('records' => array(), 'stories' => array()); + + foreach ($search_result->getResults() as $record) { + if ($record->is_grouping()) { + $ret['results']['stories'][] = $this->list_story($record, $request->get('_extended')); + } else { + $ret['results']['records'][] = $this->list_record($record, $request->get('_extended')); + } + } + + /** + * @todo donner des highlights + */ + $result->set_datas($ret); + + return $result; + } + + /** + * Get an API_V1_result containing the results of a records search + * + * Deprecated in favor of search + * + * @param Request $request + * + * @return API_V1_result + */ + public function search_records(Request $request) + { + $result = new API_V1_result($this->app, $request, $this); + + list($ret, $search_result) = $this->prepare_search_request($request); + + foreach ($search_result->getResults() as $record) { + $ret['results'][] = $this->list_record($record, $request->get('_extended')); + } + + /** + * @todo donner des highlights + */ + $result->set_datas($ret); + + return $result; + } + + /** + * Get an API_V1_result containing the baskets where the record is in + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * + * @return API_V1_result + */ + public function get_record_related(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $that = $this; + $baskets = array_map(function ($basket) use ($that) { + return $that->list_basket($basket); + }, (array) $this->app['phraseanet.appbox'] + ->get_databox($databox_id) + ->get_record($record_id) + ->get_container_baskets($this->app['EM'], $this->app['authentication']->getUser()) + ); + + $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); + + $stories = array_map(function ($story) use ($that, $request) { + return $that->list_story($story, $request->get('_extended')); + }, array_values($record->get_grouping_parents()->get_elements())); + + $result->set_datas(array( + "baskets" => $baskets, + "stories" => $stories, + )); + + return $result; + } + + /** + * Get an API_V1_result containing the record metadatas + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * + * @return API_V1_result + */ + public function get_record_metadatas(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); + + $result->set_datas(array( + "record_metadatas" => $this->list_record_caption($record->get_caption()) + )); + + return $result; + } + + /** + * Get an API_V1_result containing the record status + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * + * @return API_V1_result + */ + public function get_record_status(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); + $record = $databox->get_record($record_id); + + $result->set_datas(array("status" => $this->list_record_status($databox, $record->get_status()))); + + return $result; + } + + /** + * Get an API_V1_result containing the record embed files + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * + * @return API_V1_result + */ + public function get_record_embed(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); + + $ret = array(); + + $devices = $request->get('devices', array()); + $mimes = $request->get('mimes', array()); + + foreach ($record->get_embedable_medias($devices, $mimes) as $name => $media) { + if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { + $ret[] = $subdef; + } + } + + $result->set_datas(array("embed" => $ret)); + + return $result; + } + + /** + * Get an API_V1_result containing the story embed files + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * + * @return API_V1_result + */ + public function get_story_embed(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $record = $this->app['phraseanet.appbox'] + ->get_databox($databox_id) + ->get_record($record_id); + + $ret = array(); + + $devices = $request->get('devices', array()); + $mimes = $request->get('mimes', array()); + + foreach ($record->get_embedable_medias($devices, $mimes) as $name => $media) { + if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { + $ret[] = $subdef; + } + } + + $result->set_datas(array("embed" => $ret)); + + return $result; + } + + public function set_record_metadatas(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); + + try { + $metadatas = $request->get('metadatas'); + + if (!is_array($metadatas)) { + throw new Exception('Metadata should be an array'); + } + + foreach ($metadatas as $metadata) { + if (!is_array($metadata)) { + throw new Exception('Each Metadata value should be an array'); + } + } + + $record->set_metadatas($metadatas); + $result->set_datas(array("record_metadatas" => $this->list_record_caption($record->get_caption()))); + } catch (\Exception $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred')); + } + + return $result; + } + + /** + * Set status to a record + * + * @param Request $request + * @param $databox_id + * @param $record_id + * + * @return API_V1_result + */ + public function set_record_status(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); + $record = $databox->get_record($record_id); + $status_bits = $databox->get_statusbits(); + + try { + $status = $request->get('status'); + + $data = strrev($record->get_status()); + + if (!is_array($status)) { + throw new API_V1_exception_badrequest(); + } + foreach ($status as $n => $value) { + if ($n > 31 || $n < 4) { + throw new API_V1_exception_badrequest(); + } + if (!in_array($value, array('0', '1'))) { + throw new API_V1_exception_badrequest(); + } + if (!isset($status_bits[$n])) { + throw new API_V1_exception_badrequest (); + } + + $data = substr($data, 0, ($n)) . $value . substr($data, ($n + 2)); + } + $data = strrev($data); + + $record->set_binary_status($data); + + $this->app['phraseanet.SE']->updateRecord($record); + + $result->set_datas(array("status" => $this->list_record_status($databox, $record->get_status()))); + } catch (\Exception $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred')); + } + + return $result; + } + + /** + * Move a record to another collection + * + * @param Request $request + * @param $databox_id + * @param $record_id + * + * @return API_V1_result + * @throws API_V1_exception_unauthorized + */ + public function set_record_collection(Request $request, $databox_id, $record_id) + { + if (!$this->app['authentication']->getUser()->ACL()->has_right_on_base($request->get('base_id'), 'canaddrecord')) { + throw new \API_V1_exception_unauthorized('You are not authorized'); + } + $result = new API_V1_result($this->app, $request, $this); + $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); + $record = $databox->get_record($record_id); + + try { + $collection = collection::get_from_base_id($this->app, $request->get('base_id')); + $record->move_to_collection($collection, $this->app['phraseanet.appbox']); + $result->set_datas(array("record" => $this->list_record($record, $request->get('_extended')))); + } catch (\Exception $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, $e->getMessage()); + } + + return $result; + } + + /** + * Return detailed information about one record + * + * @param Request $request + * @param int $databox_id + * @param int $record_id + * @return API_V1_result + */ + public function get_record(Request $request, $databox_id, $record_id) + { + $result = new API_V1_result($this->app, $request, $this); + $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); + try { + $record = $databox->get_record($record_id); + $result->set_datas(array('record' => $this->list_record($record, $request->get('_extended')))); + } catch (NotFoundHttpException $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Record Not Found')); + } catch (\Exception $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred')); + } + + return $result; + } + + /** + * Return detailed information about one story + * + * @param Request $request + * @param int $databox_id + * @param int $story_id + * @return API_V1_result + */ + public function get_story(Request $request, $databox_id, $story_id) + { + $result = new API_V1_result($this->app, $request, $this); + $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); + try { + $story = $databox->get_record($story_id); + $result->set_datas(array('story' => $this->list_story($story, $request->get('_extended')))); + } catch (NotFoundHttpException $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Story Not Found')); + } catch (\Exception $e) { + $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred')); + } + + return $result; + } + + /** + * Return the baskets list of the authenticated user + * + * @param Request $request + * @return API_V1_result + */ + public function search_baskets(Request $request) + { + $result = new API_V1_result($this->app, $request, $this); + + $usr_id = $session = $this->app['authentication']->getUser()->get_id(); + + $result->set_datas(array('baskets' => $this->list_baskets($usr_id))); + + return $result; + } + + /** + * Return a baskets list + * + * @param int $usr_id + * @return array + */ + protected function list_baskets($usr_id) + { + $repo = $this->app['EM']->getRepository('\Entities\Basket'); + /* @var $repo \Repositories\BasketRepository */ + + $baskets = $repo->findActiveByUser($this->app['authentication']->getUser()); + + $ret = array(); + foreach ($baskets as $basket) { + $ret[] = $this->list_basket($basket); + } + + return $ret; + } + + /** + * Create a new basket + * + * @param Request $request + * + * @return API_V1_result + * @throws API_V1_exception_badrequest + */ + public function create_basket(Request $request) + { + $result = new API_V1_result($this->app, $request, $this); + + $name = $request->get('name'); + + if (trim(strip_tags($name)) === '') { + throw new API_V1_exception_badrequest('Missing basket name parameter'); + } + + $Basket = new \Entities\Basket(); + $Basket->setOwner($this->app['authentication']->getUser()); + $Basket->setName($name); + + $this->app['EM']->persist($Basket); + $this->app['EM']->flush(); + + $result->set_datas(array("basket" => $this->list_basket($Basket))); + + return $result; + } + + /** + * Delete a basket + * + * @param Request $request + * @param int $basket_id + * @return array + */ + public function delete_basket(Request $request, $basket_id) + { + $repository = $this->app['EM']->getRepository('\Entities\Basket'); + + /* @var $repository \Repositories\BasketRepository */ + + $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); + $this->app['EM']->remove($Basket); + $this->app['EM']->flush(); + + return $this->search_baskets($request); + } + + /** + * Retrieve a basket + * + * @param Request $request + * @param int $basket_id + * @return API_V1_result + */ + public function get_basket(Request $request, $basket_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $repository = $this->app['EM']->getRepository('\Entities\Basket'); + + /* @var $repository \Repositories\BasketRepository */ + + $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), false); + + $result->set_datas( + array( + "basket" => $this->list_basket($Basket), + "basket_elements" => $this->list_basket_content($Basket, $request->get('_extended')) + ) + ); + + return $result; + } + + /** + * Retrieve elements of one basket + * + * @param \Entities\Basket $Basket + * @param bool $extended + * + * @return array + */ + protected function list_basket_content(\Entities\Basket $Basket, $extended = false) + { + $ret = array(); + + foreach ($Basket->getElements() as $basket_element) { + $ret[] = $this->list_basket_element($basket_element, $extended); + } + + return $ret; + } + + /** + * Retrieve detailed information about a basket element + * + * @param \Entities\BasketElement $basket_element + * @param bool $extended + * + * @return array + */ + protected function list_basket_element(\Entities\BasketElement $basket_element, $extended = false) + { + $ret = array( + 'basket_element_id' => $basket_element->getId(), + 'order' => $basket_element->getOrd(), + 'record' => $this->list_record($basket_element->getRecord($this->app), $extended), + 'validation_item' => null != $basket_element->getBasket()->getValidation(), + ); + + if ($basket_element->getBasket()->getValidation()) { + $choices = array(); + $agreement = null; + $note = ''; + + foreach ($basket_element->getValidationDatas() as $validation_datas) { + $participant = $validation_datas->getParticipant(); + $user = $participant->getUser($this->app); + /* @var $validation_datas Entities\ValidationData */ + $choices[] = array( + 'validation_user' => array( + 'usr_id' => $user->get_id(), + 'usr_name' => $user->get_display_name(), + 'confirmed' => $participant->getIsConfirmed(), + 'can_agree' => $participant->getCanAgree(), + 'can_see_others' => $participant->getCanSeeOthers(), + 'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(), + 'user' => $this->list_user($user), + ), + 'agreement' => $validation_datas->getAgreement(), + 'updated_on' => $validation_datas->getUpdated()->format(DATE_ATOM), + 'note' => null === $validation_datas->getNote() ? '' : $validation_datas->getNote(), + ); + + if ($user->get_id() == $this->app['authentication']->getUser()->get_id()) { + $agreement = $validation_datas->getAgreement(); + $note = null === $validation_datas->getNote() ? '' : $validation_datas->getNote(); + } + + $ret['validation_choices'] = $choices; + } + + $ret['agreement'] = $agreement; + $ret['note'] = $note; + } + + return $ret; + } + + /** + * Change the name of one basket + * + * @param Request $request + * @param int $basket_id + * @return API_V1_result + */ + public function set_basket_title(Request $request, $basket_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $name = $request->get('name'); + + $repository = $this->app['EM']->getRepository('\Entities\Basket'); + + /* @var $repository \Repositories\BasketRepository */ + + $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); + $Basket->setName($name); + + $this->app['EM']->merge($Basket); + $this->app['EM']->flush(); + + $result->set_datas(array("basket" => $this->list_basket($Basket))); + + return $result; + } + + /** + * Change the description of one basket + * + * @param Request $request + * @param type $basket_id + * @return API_V1_result + */ + public function set_basket_description(Request $request, $basket_id) + { + $result = new API_V1_result($this->app, $request, $this); + + $desc = $request->get('description'); + + $repository = $this->app['EM']->getRepository('\Entities\Basket'); + + /* @var $repository \Repositories\BasketRepository */ + + $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); + $Basket->setDescription($desc); + + $this->app['EM']->merge($Basket); + $this->app['EM']->flush(); + + $result->set_datas(array("basket" => $this->list_basket($Basket))); + + return $result; + } + + /** + * List all available feeds + * + * @param Request $request + * @param User_Adapter $user + * @return API_V1_result + */ + public function search_publications(Request $request, User_Adapter $user) + { + $result = new API_V1_result($this->app, $request, $this); + + $coll = Feed_Collection::load($this->app, $user); + + $data = array(); + foreach ($coll->get_feeds() as $feed) { + $data[] = $this->list_publication($feed, $user); + } + + $result->set_datas(array("feeds" => $data)); + + return $result; + } + + /** + * @todo + * + * @param Request $request + * @param int $publication_id + */ + public function remove_publications(Request $request, $publication_id) + { + + } + + /** + * Retrieve one feed + * + * @param Request $request + * @param int $publication_id + * @param User_Adapter $user + * @return API_V1_result + */ + public function get_publication(Request $request, $publication_id, User_Adapter $user) + { + $result = new API_V1_result($this->app, $request, $this); + + $feed = Feed_Adapter::load_with_user($this->app, $user, $publication_id); + + $offset_start = (int) $request->get('offset_start', 0); + $per_page = (int) $request->get('per_page', 5); + + $per_page = (($per_page >= 1) && ($per_page <= 100)) ? $per_page : 100; + + $data = array( + 'feed' => $this->list_publication($feed, $user), + 'offset_start' => $offset_start, + 'per_page' => $per_page, + 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), + ); + + $result->set_datas($data); + + return $result; + } + + /** + * Aggregate feed entries + * + * @param Request $request + * @param User_Adapter $user + * + * @return API_V1_result + */ + public function get_publications(Request $request, User_Adapter $user) + { + $result = new API_V1_result($this->app, $request, $this); + + $restrictions = (array) ($request->get('feeds') ? : array()); + + $feed = Feed_Aggregate::load_with_user($this->app, $user, $restrictions); + + $offset_start = (int) ($request->get('offset_start') ? : 0); + $per_page = (int) ($request->get('per_page') ? : 5); + + $per_page = (($per_page >= 1) && ($per_page <= 20)) ? $per_page : 5; + + $result->set_datas(array( + 'total_entries' => $feed->get_count_total_entries(), + 'offset_start' => $offset_start, + 'per_page' => $per_page, + 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), + )); + + return $result; + } + + /** + * Retrieve information a feed entry + * + * @param Request $request + * @param $entry_id + * @param User_Adapter $user + * + * @return API_V1_result + * @throws API_V1_exception_forbidden + */ + public function get_feed_entry(Request $request, $entry_id, User_Adapter $user) + { + $result = new API_V1_result($this->app, $request, $this); + + $entry = Feed_Entry_Adapter::load_from_id($this->app, $entry_id); + + $collection = $entry->get_feed()->get_collection(); + + if (null !== $collection && !$user->ACL()->has_access_to_base($collection->get_base_id())) { + throw new \API_V1_exception_forbidden('You have not access to the parent feed'); + } + + $data = array('entry' => $this->list_publication_entry($entry),); + + $result->set_datas($data); + + return $result; + } + + /** + * Retrieve detailed information about one feed + * + * @param Feed_Adapter $feed + * @param type $user + * @return array + */ + protected function list_publication(Feed_Adapter $feed, $user) + { + return array( + 'id' => $feed->get_id(), + 'title' => $feed->get_title(), + 'subtitle' => $feed->get_subtitle(), + 'total_entries' => $feed->get_count_total_entries(), + 'icon' => $feed->get_icon_url(), + 'public' => $feed->is_public(), + 'readonly' => !$feed->is_publisher($user), + 'deletable' => $feed->is_owner($user), + 'created_on' => $feed->get_created_on()->format(DATE_ATOM), + 'updated_on' => $feed->get_updated_on()->format(DATE_ATOM), + ); + } + + /** + * Retrieve all entries of one feed + * + * @param Feed_Abstract $feed + * @param bool $extended + * @param int $offset_start + * @param int $how_many + * + * @return array + */ + protected function list_publications_entries(Feed_Abstract $feed, $extended = false, $offset_start = 0, $how_many = 5) + { + $entries = $feed->get_entries($offset_start, $how_many)->get_entries(); + + $out = array(); + foreach ($entries as $entry) { + $out[] = $this->list_publication_entry($entry, $extended); + } + + return $out; + } + + /** + * Retrieve detailed information about one feed entry + * + * @param Feed_Entry_Adapter $entry + * @param bool $extended + * + * @return array + */ + protected function list_publication_entry(Feed_Entry_Adapter $entry, $extended = false) + { + $items = array(); + foreach ($entry->get_content() as $item) { + $items[] = $this->list_publication_entry_item($item, $extended); + } + + return array( + 'id' => $entry->get_id(), + 'author_email' => $entry->get_author_email(), + 'author_name' => $entry->get_author_name(), + 'created_on' => $entry->get_created_on()->format(DATE_ATOM), + 'updated_on' => $entry->get_updated_on()->format(DATE_ATOM), + 'title' => $entry->get_title(), + 'subtitle' => $entry->get_subtitle(), + 'items' => $items, + 'feed_id' => $entry->get_feed()->get_id(), + 'feed_title' => $entry->get_feed()->get_title(), + 'feed_url' => '/feeds/' . $entry->get_feed()->get_id() . '/content/', + 'url' => '/feeds/entry/' . $entry->get_id() . '/', + ); + } + + /** + * Retrieve detailed information about one feed entry item + * + * @param Feed_Entry_Item $item + * @param bool $extended + * + * @return array + */ + protected function list_publication_entry_item(Feed_Entry_Item $item, $extended = false) + { + $data = array( + 'item_id' => $item->get_id(), + 'record' => $this->list_record($item->get_record(), $extended) + ); + + return $data; + } + + /** + * @todo + * @param Request $request + */ + public function search_users(Request $request) + { + + } + + /** + * @todo + * @param Request $request + * @param int $usr_id + */ + public function get_user_access(Request $request, $usr_id) + { + + } + + /** + * @todo + * @param Request $request + */ + public function add_user(Request $request) + { + + } + + /** + * Retrieve detailed information about one sub definition + * + * @param record_adapter $record + * @param media_subdef $media + * @param registryInterface $registry + * + * @return array|null + */ + protected function list_embedable_media(\record_adapter $record, media_subdef $media, registryInterface $registry) + { + if (!$media->is_physically_present()) { + return null; + } + + if ($this->app['authentication']->isAuthenticated()) { + if ($media->get_name() !== 'document' && false === $this->app['authentication']->getUser()->ACL()->has_access_to_subdef($record, $media->get_name())) { + return null; + } else if ($media->get_name() === 'document' + && !$this->app['authentication']->getUser()->ACL()->has_right_on_base($record->get_base_id(), 'candwnldhd') + && !$this->app['authentication']->getUser()->ACL()->has_hd_grant($record)) { + return null; + } + } + + if ($media->get_permalink() instanceof media_Permalink_Adapter) { + $permalink = $this->list_permalink($media->get_permalink(), $registry); + } else { + $permalink = null; + } + + return array( + 'name' => $media->get_name(), + 'permalink' => $permalink, + 'height' => $media->get_height(), + 'width' => $media->get_width(), + 'filesize' => $media->get_size(), + 'devices' => $media->getDevices(), + 'player_type' => $media->get_type(), + 'mime_type' => $media->get_mime(), + ); + } + + /** + * Retrieve detailed information about one permalink + * + * @param media_Permalink_Adapter $permalink + * @param registryInterface $registry + * @return type + */ + protected function list_permalink(media_Permalink_Adapter $permalink, registryInterface $registry) + { + $downloadUrl = $permalink->get_url(); + $downloadUrl->getQuery()->set('download', '1'); + + return array( + 'created_on' => $permalink->get_created_on()->format(DATE_ATOM), + 'id' => $permalink->get_id(), + 'is_activated' => $permalink->get_is_activated(), + 'label' => $permalink->get_label(), + 'updated_on' => $permalink->get_last_modified()->format(DATE_ATOM), + 'page_url' => $permalink->get_page(), + 'download_url' => (string) $downloadUrl, + 'url' => (string) $permalink->get_url() + ); + } + + /** + * Retrieve detailed information about one status + * + * @param databox $databox + * @param string $status + * @return array + */ + protected function list_record_status(databox $databox, $status) + { + $status = strrev($status); + $ret = array(); + foreach ($databox->get_statusbits() as $bit => $status_datas) { + $ret[] = array('bit' => $bit, 'state' => !!substr($status, ($bit - 1), 1)); + } + + return $ret; + } + + /** + * List all field about a specified caption + * + * @param caption_record $caption + * @return array + */ + protected function list_record_caption(caption_record $caption) + { + $ret = array(); + foreach ($caption->get_fields() as $field) { + foreach ($field->get_values() as $value) { + $ret[] = $this->list_record_caption_field($value, $field); + } + } + + return $ret; + } + + /** + * Retrieve information about a caption field + * + * @param caption_Field_Value $value + * @param caption_field $field + * + * @return array + */ + protected function list_record_caption_field(caption_Field_Value $value, caption_field $field) + { + return array( + 'meta_id' => $value->getId(), + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'labels' => array( + 'fr' => $field->get_databox_field()->get_label('fr'), + 'en' => $field->get_databox_field()->get_label('en'), + 'de' => $field->get_databox_field()->get_label('de'), + 'nl' => $field->get_databox_field()->get_label('nl'), + ), + 'value' => $value->getValue(), + ); + } + + /** + * Retrieve information about one basket + * + * @param \Entities\Basket $basket + * @return array + */ + public function list_basket(\Entities\Basket $basket) + { + $ret = array( + 'basket_id' => $basket->getId(), + 'owner' => $this->list_user($basket->getOwner($this->app)), + 'created_on' => $basket->getCreated()->format(DATE_ATOM), + 'description' => (string) $basket->getDescription(), + 'name' => $basket->getName(), + 'pusher_usr_id' => $basket->getPusherId(), + 'pusher' => $basket->getPusher($this->app) ? $this->list_user($basket->getPusher($this->app)) : null, + 'updated_on' => $basket->getUpdated()->format(DATE_ATOM), + 'unread' => !$basket->getIsRead(), + 'validation_basket' => !!$basket->getValidation() + ); + + if ($basket->getValidation()) { + $users = array(); + + foreach ($basket->getValidation()->getParticipants() as $participant) { + /* @var $participant \Entities\ValidationParticipant */ + $user = $participant->getUser($this->app); + + $users[] = array( + 'usr_id' => $user->get_id(), + 'usr_name' => $user->get_display_name(), + 'confirmed' => $participant->getIsConfirmed(), + 'can_agree' => $participant->getCanAgree(), + 'can_see_others' => $participant->getCanSeeOthers(), + 'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(), + 'user' => $this->list_user($user), + ); + } + + $expires_on_atom = $basket->getValidation()->getExpires(); + + if ($expires_on_atom instanceof DateTime) { + $expires_on_atom = $expires_on_atom->format(DATE_ATOM); + } + + $ret = array_merge( + array( + 'validation_users' => $users, + 'expires_on' => $expires_on_atom, + 'validation_infos' => $basket->getValidation()->getValidationString($this->app, $this->app['authentication']->getUser()), + 'validation_confirmed' => $basket->getValidation()->getParticipant($this->app['authentication']->getUser(), $this->app)->getIsConfirmed(), + 'validation_initiator' => $basket->getValidation()->isInitiator($this->app['authentication']->getUser()), + 'validation_initiator_user' => $this->list_user($basket->getValidation()->getInitiator($this->app)), + ), $ret + ); + } + + return $ret; + } + + /** + * Retrieve detailed information about one record + * + * @param record_adapter $record + * @param bool $extended + * + * @return array + */ + public function list_record(record_adapter $record, $extended = false) + { + $technicalInformation = array(); + foreach ($record->get_technical_infos() as $name => $value) { + $technicalInformation[] = array( + 'name' => $name, + 'value' => $value + ); + } + + $data = array( + 'databox_id' => $record->get_sbas_id(), + 'record_id' => $record->get_record_id(), + 'mime_type' => $record->get_mime(), + 'title' => $record->get_title(), + 'original_name' => $record->get_original_name(), + 'updated_on' => $record->get_modification_date()->format(DATE_ATOM), + 'created_on' => $record->get_creation_date()->format(DATE_ATOM), + 'collection_id' => phrasea::collFromBas($this->app, $record->get_base_id()), + 'sha256' => $record->get_sha256(), + 'thumbnail' => $this->list_embedable_media($record, $record->get_thumbnail(), $this->app['phraseanet.registry']), + 'technical_informations' => $technicalInformation, + 'phrasea_type' => $record->get_type(), + 'uuid' => $record->get_uuid(), + ); + + if ($extended) { + $subdefs = $caption = array(); + + foreach ($record->get_embedable_medias(array(), array()) as $name => $media) { + if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { + $subdefs[] = $subdef; + } + } + + foreach ($record->get_caption()->get_fields() as $field) { + $caption[] = array( + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'value' => $field->get_serialized_values(';'), + ); + } + + $extendedData = array( + 'subdefs' => $subdefs, + 'metadata' => $this->list_record_caption($record->get_caption()), + 'status' => $this->list_record_status($record->get_databox(), $record->get_status()), + 'caption' => $caption + ); + + $data = array_merge($data, $extendedData); + } + + return $data; + } + + /** + * Retrieve detailed information about one story + * + * @param record_adapter $story + * @param bool $extended + * + * @return array + * @throws API_V1_exception_notfound + */ + public function list_story(record_adapter $story, $extended = false) + { + if (!$story->is_grouping()) { + throw new \API_V1_exception_notfound('Story not found'); + } + + $that = $this; + $records = array_map(function (\record_adapter $record) use ($that, $extended) { + return $that->list_record($record, $extended); + }, array_values($story->get_children()->get_elements())); + + $caption = $story->get_caption(); + + $format = function (caption_record $caption, $dcField) { + + $field = $caption->get_dc_field($dcField); + + if (!$field) { + return null; + } + + return $field->get_serialized_values(); + }; + + return array( + '@entity@' => self::OBJECT_TYPE_STORY, + 'databox_id' => $story->get_sbas_id(), + 'story_id' => $story->get_record_id(), + 'updated_on' => $story->get_modification_date()->format(DATE_ATOM), + 'created_on' => $story->get_creation_date()->format(DATE_ATOM), + 'collection_id' => phrasea::collFromBas($this->app, $story->get_base_id()), + 'thumbnail' => $this->list_embedable_media($story, $story->get_thumbnail(), $this->app['phraseanet.registry']), + 'uuid' => $story->get_uuid(), + 'metadatas' => array( + '@entity@' => self::OBJECT_TYPE_STORY_METADATA_BAG, + 'dc:contributor' => $format($caption, databox_Field_DCESAbstract::Contributor), + 'dc:coverage' => $format($caption, databox_Field_DCESAbstract::Coverage), + 'dc:creator' => $format($caption, databox_Field_DCESAbstract::Creator), + 'dc:date' => $format($caption, databox_Field_DCESAbstract::Date), + 'dc:description' => $format($caption, databox_Field_DCESAbstract::Description), + 'dc:format' => $format($caption, databox_Field_DCESAbstract::Format), + 'dc:identifier' => $format($caption, databox_Field_DCESAbstract::Identifier), + 'dc:language' => $format($caption, databox_Field_DCESAbstract::Language), + 'dc:publisher' => $format($caption, databox_Field_DCESAbstract::Publisher), + 'dc:relation' => $format($caption, databox_Field_DCESAbstract::Relation), + 'dc:rights' => $format($caption, databox_Field_DCESAbstract::Rights), + 'dc:source' => $format($caption, databox_Field_DCESAbstract::Source), + 'dc:subject' => $format($caption, databox_Field_DCESAbstract::Subject), + 'dc:title' => $format($caption, databox_Field_DCESAbstract::Title), + 'dc:type' => $format($caption, databox_Field_DCESAbstract::Type), + ), + 'records' => $records, + ); + } + + /** + * Retrieve information about current authenticated user + * + * @param Application $app + * @param Request $request + * + * @return API_V1_result + */ + public function get_current_user(Application $app, Request $request) + { + $result = new API_V1_result($app, $request, $this); + $result->set_datas(array('user' => $this->list_user($app['authentication']->getUser()))); + + return $result; + } + + private function list_user(\User_Adapter $user) + { + switch ($user->get_gender()) { + case 2; + $gender = 'Mr'; + break; + case 1; + $gender = 'Mrs'; + break; + case 0; + $gender = 'Miss'; + break; + } + + return array( + '@entity@' => self::OBJECT_TYPE_USER, + 'id' => $user->get_id(), + 'email' => $user->get_email() ?: null, + 'login' => $user->get_login() ?: null, + 'first_name' => $user->get_firstname() ?: null, + 'last_name' => $user->get_lastname() ?: null, + 'display_name' => $user->get_display_name() ?: null, + 'gender' => $gender, + 'address' => $user->get_address() ?: null, + 'zip_code' => $user->get_zipcode() ?: null, + 'city' => $user->get_city() ?: null, + 'country' => $user->get_country() ?: null, + 'phone' => $user->get_tel() ?: null, + 'fax' => $user->get_fax() ?: null, + 'job' => $user->get_job() ?: null, + 'position' => $user->get_position() ?: null, + 'company' => $user->get_company() ?: null, + 'geoname_id' => $user->get_geonameid() ?: null, + 'last_connection' => $user->get_last_connection() ? $user->get_last_connection()->format(DATE_ATOM) : null, + 'created_on' => $user->get_creation_date() ? $user->get_creation_date()->format(DATE_ATOM) : null, + 'updated_on' => $user->get_modification_date() ? $user->get_modification_date()->format(DATE_ATOM) : null, + 'locale' => $user->get_locale() ?: null, + ); + } + + /** + * List all databoxes of the current appbox + * + * @return array + */ + protected function list_databoxes() + { + $ret = array(); + foreach ($this->app['phraseanet.appbox']->get_databoxes() as $databox) { + if (!$this->app['authentication']->getUser()->ACL()->has_access_to_sbas($databox->get_sbas_id())) { + continue; + } + $ret[] = $this->list_databox($databox); + } + + return $ret; + } + + /** + * Retrieve CGU's for the specified databox + * + * @param databox $databox + * @return array + */ + protected function list_databox_terms(databox $databox) + { + $ret = array(); + foreach ($databox->get_cgus() as $locale => $array_terms) { + $ret[] = array('locale' => $locale, 'terms' => $array_terms['value']); + } + + return $ret; + } + + /** + * Retrieve detailed information about one databox + * + * @param databox $databox + * @return array + */ + protected function list_databox(databox $databox) + { + $ret = array(); + + $ret['databox_id'] = $databox->get_sbas_id(); + $ret['name'] = $databox->get_dbname(); + $ret['viewname'] = $databox->get_viewname(); + $ret['labels'] = array( + 'en' => $databox->get_label('en'), + 'de' => $databox->get_label('de'), + 'fr' => $databox->get_label('fr'), + 'nl' => $databox->get_label('nl'), + ); + $ret['version'] = $databox->get_version(); + + return $ret; + } + + /** + * List all available collections for a specified databox + * + * @param databox $databox + * @return array + */ + protected function list_databox_collections(databox $databox) + { + $ret = array(); + + foreach ($databox->get_collections() as $collection) { + if (!$this->app['authentication']->getUser()->ACL()->has_access_to_base($collection->get_base_id())) { + continue; + } + $ret[] = $this->list_collection($collection); + } + + return $ret; + } + + /** + * Retrieve detailed information about one collection + * + * @param collection $collection + * @return array + */ + protected function list_collection(collection $collection) + { + $ret = array( + 'base_id' => $collection->get_base_id(), + 'collection_id' => $collection->get_coll_id(), + 'name' => $collection->get_name(), + 'labels' => array( + 'fr' => $collection->get_label('fr'), + 'en' => $collection->get_label('en'), + 'de' => $collection->get_label('de'), + 'nl' => $collection->get_label('nl'), + ), + 'record_amount' => $collection->get_record_amount(), + ); + + return $ret; + } + + /** + * Retrieve information for a list of status + * + * @param array $status + * @return array + */ + protected function list_databox_status(array $status) + { + $ret = array(); + foreach ($status as $n => $data) { + $ret[] = array( + 'bit' => $n, + 'label_on' => $data['labelon'], + 'label_off' => $data['labeloff'], + 'labels' => array( + 'en' => $data['labels_on_i18n']['en'], + 'fr' => $data['labels_on_i18n']['fr'], + 'de' => $data['labels_on_i18n']['de'], + 'nl' => $data['labels_on_i18n']['nl'], + ), + 'img_on' => $data['img_on'], + 'img_off' => $data['img_off'], + 'searchable' => !!$data['searchable'], + 'printable' => !!$data['printable'], + ); + } + + return $ret; + } + + /** + * List all metadatas field using a databox meta structure + * + * @param databox_descriptionStructure $meta_struct + * @return array + */ + protected function list_databox_metadatas_fields(databox_descriptionStructure $meta_struct) + { + $ret = array(); + foreach ($meta_struct as $meta) { + $ret[] = $this->list_databox_metadata_field_properties($meta); + } + + return $ret; + } + + /** + * Retrieve information about one databox metadata field + * + * @param databox_field $databox_field + * @return array + */ + protected function list_databox_metadata_field_properties(databox_field $databox_field) + { + $ret = array( + 'id' => $databox_field->get_id(), + 'namespace' => $databox_field->get_tag()->getGroupName(), + 'source' => $databox_field->get_tag()->getTagname(), + 'tagname' => $databox_field->get_tag()->getName(), + 'name' => $databox_field->get_name(), + 'labels' => array( + 'fr' => $databox_field->get_label('fr'), + 'en' => $databox_field->get_label('en'), + 'de' => $databox_field->get_label('de'), + 'nl' => $databox_field->get_label('nl'), + ), + 'separator' => $databox_field->get_separator(), + 'thesaurus_branch' => $databox_field->get_tbranch(), + 'type' => $databox_field->get_type(), + 'indexable' => $databox_field->is_indexable(), + 'multivalue' => $databox_field->is_multi(), + 'readonly' => $databox_field->is_readonly(), + 'required' => $databox_field->is_required(), + ); + + return $ret; + } + /** * Get Information the cache system used by the instance * - * @param \Silex\Application $app the silex application + * @param Application $app + * * @return array */ protected function get_cache_info(Application $app) @@ -308,10 +2026,55 @@ class API_V1_adapter extends API_V1_Abstract return $ret; } + /** + * Retrieve detailed information about a lazaret file + * + * @param \Entities\LazaretFile $file + * + * @return array + */ + protected function list_lazaret_file(\Entities\LazaretFile $file) + { + $checks = array(); + + if ($file->getChecks()) { + foreach ($file->getChecks() as $checker) { + + $checks[] = $checker->getMessage(); + } + } + + $usr_id = $user = null; + if ($file->getSession()->getUser($this->app)) { + $user = $file->getSession()->getUser($this->app); + $usr_id = $user->get_id(); + } + + $session = array( + 'id' => $file->getSession()->getId(), + 'usr_id' => $usr_id, + 'user' => $user ? $this->list_user($user) : null, + ); + + return array( + 'id' => $file->getId(), + 'quarantine_session' => $session, + 'base_id' => $file->getBaseId(), + 'original_name' => $file->getOriginalName(), + 'sha256' => $file->getSha256(), + 'uuid' => $file->getUuid(), + 'forced' => $file->getForced(), + 'checks' => $file->getForced() ? array() : $checks, + 'created_on' => $file->getCreated()->format(DATE_ATOM), + 'updated_on' => $file->getUpdated()->format(DATE_ATOM), + ); + } + /** * Provide information about phraseanet configuration * - * @param \Silex\Application $app the silex application + * @param Application $app + * * @return array */ protected function get_config_info(Application $app) @@ -333,8 +2096,32 @@ class API_V1_adapter extends API_V1_Abstract } /** - * Provide phraseanet global values - * @param \Silex\Application $app the silex application + * Get detailed information for a task + * + * @param task_abstract $task + * + * @return array + */ + protected function list_task(\task_abstract $task) + { + return array( + 'id' => $task->getID(), + 'name' => $task->getName(), + 'state' => $task->getState(), + 'pid' => $task->getPID(), + 'title' => $task->getTitle(), + 'last_exec_time' => $task->getLastExecTime() ? $task->getLastExecTime()->format(DATE_ATOM) : null, + 'auto_start' => !!$task->isActive(), + 'runner' => $task->getRunner(), + 'crash_counter' => $task->getCrashCounter() + ); + } + + /** + * Provide phraseanet global values + * + * @param Application $app + * * @return array */ protected function get_gv_info(Application $app) @@ -497,402 +2284,12 @@ class API_V1_adapter extends API_V1_Abstract } /** - * Provide - * - cache information - * - global values informations - * - configuration informations - * - * @param \Silex\Application $app the silex application - * @return \API_V1_result - */ - public function get_phraseanet_monitor(Application $app) - { - $result = new API_V1_result($app, $app['request'], $this); - - $ret = array_merge( - $this->get_config_info($app), $this->get_cache_info($app), $this->get_gv_info($app) - ); - - $result->set_datas($ret); - - return $result; - } - - /** - * Get an API_V1_result containing the databoxes + * Prepare search request * * @param Request $request * - * @return API_V1_result + * @return array */ - public function get_databoxes(Request $request) - { - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas(array("databoxes" => $this->list_databoxes())); - - return $result; - } - - /** - * Get an API_V1_result containing the collections of a databox - * - * @param Request $request - * @param int $databox_id - * - * @return API_V1_result - */ - public function get_databox_collections(Request $request, $databox_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas( - array( - "collections" => $this->list_databox_collections( - $this->app['phraseanet.appbox']->get_databox($databox_id) - ) - ) - ); - - return $result; - } - - /** - * Get an API_V1_result containing the status of a databox - * - * @param Request $request - * @param int $databox_id - * - * @return API_V1_result - */ - public function get_databox_status(Request $request, $databox_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas( - array( - "status" => - $this->list_databox_status( - $this->app['phraseanet.appbox']->get_databox($databox_id)->get_statusbits() - ) - ) - ); - - return $result; - } - - /** - * Get an API_V1_result containing the metadatas of a databox - * - * @param Request $request - * @param int $databox_id - * - * @return API_V1_result - */ - public function get_databox_metadatas(Request $request, $databox_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas( - array( - "document_metadatas" => - $this->list_databox_metadatas_fields( - $this->app['phraseanet.appbox']->get_databox($databox_id) - ->get_meta_structure() - ) - ) - ); - - return $result; - } - - /** - * Get an API_V1_result containing the terms of use of a databox - * - * @param Request $request - * @param int $databox_id - * - * @return API_V1_result - */ - public function get_databox_terms(Request $request, $databox_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas( - array( - "termsOfUse" => - $this->list_databox_terms($this->app['phraseanet.appbox']->get_databox($databox_id)) - ) - ); - - return $result; - } - - public function caption_records(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - $fields = $record->get_caption()->get_fields(); - - $ret = array('caption_metadatas' => array()); - - foreach ($fields as $field) { - $ret['caption_metadatas'][] = array( - 'meta_structure_id' => $field->get_meta_struct_id(), - 'name' => $field->get_name(), - 'value' => $field->get_serialized_values(";"), - ); - } - - $result->set_datas($ret); - - return $result; - } - - public function add_record(Application $app, Request $request) - { - if (count($request->files->get('file')) == 0) { - throw new API_V1_exception_badrequest('Missing file parameter'); - } - - if (!$request->files->get('file') instanceof Symfony\Component\HttpFoundation\File\UploadedFile) { - throw new API_V1_exception_badrequest('You can upload one file at time'); - } - - $file = $request->files->get('file'); - /* @var $file Symfony\Component\HttpFoundation\File\UploadedFile */ - - if (!$file->isValid()) { - throw new API_V1_exception_badrequest('Datas corrupted, please try again'); - } - - if (!$request->get('base_id')) { - throw new API_V1_exception_badrequest('Missing base_id parameter'); - } - - $collection = \collection::get_from_base_id($this->app, $request->get('base_id')); - - if (!$app['authentication']->getUser()->ACL()->has_right_on_base($request->get('base_id'), 'canaddrecord')) { - throw new API_V1_exception_forbidden(sprintf('You do not have access to collection %s', $collection->get_label($this->app['locale.I18n']))); - } - - $media = $app['mediavorus']->guess($file->getPathname()); - - $Package = new File($this->app, $media, $collection, $file->getClientOriginalName()); - - if ($request->get('status')) { - $Package->addAttribute(new Status($app, $request->get('status'))); - } - - $session = new Entities\LazaretSession(); - $session->setUsrId($app['authentication']->getUser()->get_id()); - - $app['EM']->persist($session); - $app['EM']->flush(); - - $reasons = $output = null; - - $callback = function ($element, $visa, $code) use (&$reasons, &$output) { - if (!$visa->isValid()) { - $reasons = array(); - - foreach ($visa->getResponses() as $response) { - $reasons[] = $response->getMessage(); - } - } - - $output = $element; - }; - - switch ($request->get('forceBehavior')) { - case '0' : - $behavior = BorderManager::FORCE_RECORD; - break; - case '1' : - $behavior = BorderManager::FORCE_LAZARET; - break; - case null: - $behavior = null; - break; - default: - throw new API_V1_exception_badrequest(sprintf('Invalid forceBehavior value `%s`', $request->get('forceBehavior'))); - break; - } - - $app['border-manager']->process($session, $Package, $callback, $behavior); - - $ret = array( - 'entity' => null, - ); - - if ($output instanceof \record_adapter) { - $ret['entity'] = '0'; - $ret['url'] = '/records/' . $output->get_sbas_id() . '/' . $output->get_record_id() . '/'; - $app['phraseanet.SE']->addRecord($output); - } - if ($output instanceof \Entities\LazaretFile) { - $ret['entity'] = '1'; - $ret['url'] = '/quarantine/item/' . $output->getId() . '/'; - } - - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas($ret); - - return $result; - } - - public function list_quarantine(Application $app, Request $request) - { - $offset_start = max($request->get('offset_start', 0), 0); - $per_page = min(max($request->get('per_page', 10), 1), 20); - - $baseIds = array_keys($app['authentication']->getUser()->ACL()->get_granted_base(array('canaddrecord'))); - - $lazaretFiles = array(); - - if (count($baseIds) > 0) { - $lazaretRepository = $app['EM']->getRepository('Entities\LazaretFile'); - - $lazaretFiles = $lazaretRepository->findPerPage( - $baseIds, $offset_start, $per_page - ); - } - - $ret = array(); - - foreach ($lazaretFiles as $lazaretFile) { - $ret[] = $this->list_lazaret_file($lazaretFile); - } - - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas(array( - 'offset_start' => $offset_start, - 'per_page' => $per_page, - 'quarantine_items' => $ret, - )); - - return $result; - } - - public function list_quarantine_item($lazaret_id, Application $app, Request $request) - { - $lazaretFile = $app['EM']->find('Entities\LazaretFile', $lazaret_id); - - /* @var $lazaretFile \Entities\LazaretFile */ - if (null === $lazaretFile) { - throw new \API_V1_exception_notfound(sprintf('Lazaret file id %d not found', $lazaret_id)); - } - - if (!$app['authentication']->getUser()->ACL()->has_right_on_base($lazaretFile->getBaseId(), 'canaddrecord')) { - throw new \API_V1_exception_forbidden('You do not have access to this quarantine item'); - } - - $ret = array('quarantine_item' => $this->list_lazaret_file($lazaretFile)); - - $result = new API_V1_result($this->app, $request, $this); - - $result->set_datas($ret); - - return $result; - } - - protected function list_lazaret_file(\Entities\LazaretFile $file) - { - $checks = array(); - - if ($file->getChecks()) { - foreach ($file->getChecks() as $checker) { - - $checks[] = $checker->getMessage(); - } - } - - $usr_id = $user = null; - if ($file->getSession()->getUser($this->app)) { - $user = $file->getSession()->getUser($this->app); - $usr_id = $user->get_id(); - } - - $session = array( - 'id' => $file->getSession()->getId(), - 'usr_id' => $usr_id, - 'user' => $user ? $this->list_user($user) : null, - ); - - return array( - 'id' => $file->getId(), - 'quarantine_session' => $session, - 'base_id' => $file->getBaseId(), - 'original_name' => $file->getOriginalName(), - 'sha256' => $file->getSha256(), - 'uuid' => $file->getUuid(), - 'forced' => $file->getForced(), - 'checks' => $file->getForced() ? array() : $checks, - 'created_on' => $file->getCreated()->format(DATE_ATOM), - 'updated_on' => $file->getUpdated()->format(DATE_ATOM), - ); - } - - /** - * Search for results - * - * @param Request $request - * @return \API_V1_result - */ - public function search(Request $request) - { - $result = new API_V1_result($this->app, $request, $this); - - list($ret, $search_result) = $this->prepare_search_request($request); - - $ret['results'] = array('records' => array(), 'stories' => array()); - - foreach ($search_result->getResults() as $record) { - if ($record->is_grouping()) { - $ret['results']['stories'][] = $this->list_story($record, $request->get('_extended')); - } else { - $ret['results']['records'][] = $this->list_record($record, $request->get('_extended')); - } - } - - /** - * @todo donner des highlights - */ - $result->set_datas($ret); - - return $result; - } - - /** - * Get an API_V1_result containing the results of a records search - * - * Deprecated in favor of search - * - * @param Request $request - * - * @return API_V1_result - */ - public function search_records(Request $request) - { - $result = new API_V1_result($this->app, $request, $this); - - list($ret, $search_result) = $this->prepare_search_request($request); - - foreach ($search_result->getResults() as $record) { - $ret['results'][] = $this->list_record($record, $request->get('_extended')); - } - - /** - * @todo donner des highlights - */ - $result->set_datas($ret); - - return $result; - } - private function prepare_search_request(Request $request) { $options = SearchEngineOptions::fromRequest($this->app, $request); @@ -937,1296 +2334,4 @@ class API_V1_adapter extends API_V1_Abstract return array($ret, $search_result); } - - /** - * Get an API_V1_result containing the baskets where the record is in - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * - * @return API_V1_result - */ - public function get_record_related(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $that = $this; - $baskets = array_map(function ($basket) use ($that) { - return $that->list_basket($basket); - }, (array) $this->app['phraseanet.appbox'] - ->get_databox($databox_id) - ->get_record($record_id) - ->get_container_baskets($this->app['EM'], $this->app['authentication']->getUser()) - ); - - $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - - $stories = array_map(function ($story) use ($that, $request) { - return $that->list_story($story, $request->get('_extended')); - }, array_values($record->get_grouping_parents()->get_elements())); - - $result->set_datas(array( - "baskets" => $baskets, - "stories" => $stories, - )); - - return $result; - } - - /** - * Get an API_V1_result containing the record metadatas - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * - * @return API_V1_result - */ - public function get_record_metadatas(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - - $result->set_datas( - array( - "record_metadatas" => $this->list_record_caption($record->get_caption()) - ) - ); - - return $result; - } - - /** - * Get an API_V1_result containing the record status - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * - * @return API_V1_result - */ - public function get_record_status(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $record = $this->app['phraseanet.appbox'] - ->get_databox($databox_id) - ->get_record($record_id); - - $result->set_datas( - array( - "status" => - $this->list_record_status( - $this->app['phraseanet.appbox']->get_databox($databox_id) - , $record->get_status() - ) - ) - ); - - return $result; - } - - /** - * Get an API_V1_result containing the record embed files - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * - * @return API_V1_result - */ - public function get_record_embed(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - - $ret = array(); - - $devices = $request->get('devices', array()); - $mimes = $request->get('mimes', array()); - - foreach ($record->get_embedable_medias($devices, $mimes) as $name => $media) { - if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { - $ret[] = $subdef; - } - } - - $result->set_datas(array("embed" => $ret)); - - return $result; - } - - /** - * Get an API_V1_result containing the story embed files - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * - * @return API_V1_result - */ - public function get_story_embed(Request $request, $databox_id, $record_id) - { - - $result = new API_V1_result($this->app, $request, $this); - - $record = $this->app['phraseanet.appbox'] - ->get_databox($databox_id) - ->get_record($record_id); - - $ret = array(); - - $devices = $request->get('devices', array()); - $mimes = $request->get('mimes', array()); - - foreach ($record->get_embedable_medias($devices, $mimes) as $name => $media) { - if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { - $ret[] = $subdef; - } - } - - $result->set_datas(array("embed" => $ret)); - - return $result; - } - - public function set_record_metadatas(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - $record = $this->app['phraseanet.appbox']->get_databox($databox_id)->get_record($record_id); - - try { - $metadatas = $request->get('metadatas'); - - if (!is_array($metadatas)) { - throw new Exception('Metadatas should be an array'); - } - - foreach ($metadatas as $metadata) { - if (!is_array($metadata)) { - throw new Exception('Each Metadata value should be an array'); - } - } - - $record->set_metadatas($metadatas); - $result->set_datas(array("record_metadatas" => $this->list_record_caption($record->get_caption()))); - } catch (\Exception $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occured')); - } - - return $result; - } - - public function set_record_status(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); - $record = $databox->get_record($record_id); - $status_bits = $databox->get_statusbits(); - - try { - $status = $request->get('status'); - - $datas = strrev($record->get_status()); - - if (!is_array($status)) { - throw new API_V1_exception_badrequest(); - } - foreach ($status as $n => $value) { - if ($n > 31 || $n < 4) { - throw new API_V1_exception_badrequest(); - } - if (!in_array($value, array('0', '1'))) { - throw new API_V1_exception_badrequest(); - } - if (!isset($status_bits[$n])) { - throw new API_V1_exception_badrequest (); - } - - $datas = substr($datas, 0, ($n)) . $value . substr($datas, ($n + 2)); - } - $datas = strrev($datas); - - $record->set_binary_status($datas); - - $this->app['phraseanet.SE']->updateRecord($record); - - $result->set_datas(array( - "status" => - $this->list_record_status($databox, $record->get_status()) - ) - ); - } catch (\Exception $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occured')); - } - - return $result; - } - - /** - * Move a record to another collection - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * @return API_V1_result - */ - public function set_record_collection(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); - $record = $databox->get_record($record_id); - - try { - $collection = collection::get_from_base_id($this->app, $request->get('base_id')); - - $record->move_to_collection($collection, $this->app['phraseanet.appbox']); - $result->set_datas(array("record" => $this->list_record($record, $request->get('_extended')))); - } catch (\Exception $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, $e->getMessage()); - } - - return $result; - } - - /** - * Return detailed informations about one record - * - * @param Request $request - * @param int $databox_id - * @param int $record_id - * @return API_V1_result - */ - public function get_record(Request $request, $databox_id, $record_id) - { - $result = new API_V1_result($this->app, $request, $this); - $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); - try { - $record = $databox->get_record($record_id); - $result->set_datas(array('record' => $this->list_record($record, $request->get('_extended')))); - } catch (NotFoundHttpException $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Record Not Found')); - } catch (\Exception $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occured')); - } - - return $result; - } - - /** - * Return detailed informations about one story - * - * @param Request $request - * @param int $databox_id - * @param int $story_id - * @return API_V1_result - */ - public function get_story(Request $request, $databox_id, $story_id) - { - $result = new API_V1_result($this->app, $request, $this); - $databox = $this->app['phraseanet.appbox']->get_databox($databox_id); - try { - $story = $databox->get_record($story_id); - $result->set_datas(array('story' => $this->list_story($story, $request->get('_extended')))); - } catch (NotFoundHttpException $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('Story Not Found')); - } catch (\Exception $e) { - $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occured')); - } - - return $result; - } - - /** - * Return the baskets list of the authenticated user - * - * @param Request $request - * @return API_V1_result - */ - public function search_baskets(Request $request) - { - $result = new API_V1_result($this->app, $request, $this); - - $usr_id = $session = $this->app['authentication']->getUser()->get_id(); - - $result->set_datas(array('baskets' => $this->list_baskets($usr_id))); - - return $result; - } - - /** - * Return a baskets list - * - * @param int $usr_id - * @return array - */ - protected function list_baskets($usr_id) - { - $repo = $this->app['EM']->getRepository('\Entities\Basket'); - /* @var $repo \Repositories\BasketRepository */ - - $baskets = $repo->findActiveByUser($this->app['authentication']->getUser()); - - $ret = array(); - foreach ($baskets as $basket) { - $ret[] = $this->list_basket($basket); - } - - return $ret; - } - - /** - * Create a new basket - * - * @param Request $request - * @return API_V1_result - */ - public function create_basket(Request $request) - { - $result = new API_V1_result($this->app, $request, $this); - - $name = $request->get('name'); - - if (trim(strip_tags($name)) === '') { - throw new API_V1_exception_badrequest('Missing basket name parameter'); - } - - $Basket = new \Entities\Basket(); - $Basket->setOwner($this->app['authentication']->getUser()); - $Basket->setName($name); - - $this->app['EM']->persist($Basket); - $this->app['EM']->flush(); - - $result->set_datas(array("basket" => $this->list_basket($Basket))); - - return $result; - } - - /** - * Delete a basket - * - * @param Request $request - * @param int $basket_id - * @return array - */ - public function delete_basket(Request $request, $basket_id) - { - $repository = $this->app['EM']->getRepository('\Entities\Basket'); - - /* @var $repository \Repositories\BasketRepository */ - - $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); - $this->app['EM']->remove($Basket); - $this->app['EM']->flush(); - - return $this->search_baskets($request); - } - - /** - * Retrieve a basket - * - * @param Request $request - * @param int $basket_id - * @return API_V1_result - */ - public function get_basket(Request $request, $basket_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $repository = $this->app['EM']->getRepository('\Entities\Basket'); - - /* @var $repository \Repositories\BasketRepository */ - - $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), false); - - $result->set_datas( - array( - "basket" => $this->list_basket($Basket), - "basket_elements" => $this->list_basket_content($Basket, $request->get('_extended')) - ) - ); - - return $result; - } - - /** - * Retrieve elements of one basket - * - * @param \Entities\Basket $Basket - * @return type - */ - protected function list_basket_content(\Entities\Basket $Basket, $extended = false) - { - $ret = array(); - - foreach ($Basket->getElements() as $basket_element) { - $ret[] = $this->list_basket_element($basket_element, $extended); - } - - return $ret; - } - - /** - * Retrieve detailled informations about a basket element - * - * @param \Entities\BasketElement $basket_element - * @return type - */ - protected function list_basket_element(\Entities\BasketElement $basket_element, $extended = false) - { - $ret = array( - 'basket_element_id' => $basket_element->getId(), - 'order' => $basket_element->getOrd(), - 'record' => $this->list_record($basket_element->getRecord($this->app), $extended), - 'validation_item' => null != $basket_element->getBasket()->getValidation(), - ); - - if ($basket_element->getBasket()->getValidation()) { - $choices = array(); - $agreement = null; - $note = ''; - - foreach ($basket_element->getValidationDatas() as $validation_datas) { - $participant = $validation_datas->getParticipant(); - $user = $participant->getUser($this->app); - /* @var $validation_datas Entities\ValidationData */ - $choices[] = array( - 'validation_user' => array( - 'usr_id' => $user->get_id(), - 'usr_name' => $user->get_display_name(), - 'confirmed' => $participant->getIsConfirmed(), - 'can_agree' => $participant->getCanAgree(), - 'can_see_others' => $participant->getCanSeeOthers(), - 'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(), - 'user' => $this->list_user($user), - ), - 'agreement' => $validation_datas->getAgreement(), - 'updated_on' => $validation_datas->getUpdated()->format(DATE_ATOM), - 'note' => null === $validation_datas->getNote() ? '' : $validation_datas->getNote(), - ); - - if ($user->get_id() == $this->app['authentication']->getUser()->get_id()) { - $agreement = $validation_datas->getAgreement(); - $note = null === $validation_datas->getNote() ? '' : $validation_datas->getNote(); - } - - $ret['validation_choices'] = $choices; - } - - $ret['agreement'] = $agreement; - $ret['note'] = $note; - } - - return $ret; - } - - /** - * Change the name of one basket - * - * @param Request $request - * @param int $basket_id - * @return API_V1_result - */ - public function set_basket_title(Request $request, $basket_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $name = $request->get('name'); - - $repository = $this->app['EM']->getRepository('\Entities\Basket'); - - /* @var $repository \Repositories\BasketRepository */ - - $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); - $Basket->setName($name); - - $this->app['EM']->merge($Basket); - $this->app['EM']->flush(); - - $result->set_datas(array("basket" => $this->list_basket($Basket))); - - return $result; - } - - /** - * Change the description of one basket - * - * @param Request $request - * @param type $basket_id - * @return API_V1_result - */ - public function set_basket_description(Request $request, $basket_id) - { - $result = new API_V1_result($this->app, $request, $this); - - $desc = $request->get('description'); - - $repository = $this->app['EM']->getRepository('\Entities\Basket'); - - /* @var $repository \Repositories\BasketRepository */ - - $Basket = $repository->findUserBasket($this->app, $basket_id, $this->app['authentication']->getUser(), true); - $Basket->setDescription($desc); - - $this->app['EM']->merge($Basket); - $this->app['EM']->flush(); - - $result->set_datas(array("basket" => $this->list_basket($Basket))); - - return $result; - } - - /** - * List all avalaible feeds - * - * @param Request $request - * @param User_Adapter $user - * @return API_V1_result - */ - public function search_publications(Request $request, User_Adapter $user) - { - $result = new API_V1_result($this->app, $request, $this); - - $coll = Feed_Collection::load($this->app, $user); - - $data = array(); - foreach ($coll->get_feeds() as $feed) { - $data[] = $this->list_publication($feed, $user); - } - - $result->set_datas(array("feeds" => $data)); - - return $result; - } - - /** - * @param Request $request - * @param int $publication_id - */ - public function remove_publications(Request $request, $publication_id) - { - - } - - /** - * Retrieve one feed - * - * @param Request $request - * @param int $publication_id - * @param User_Adapter $user - * @return API_V1_result - */ - public function get_publication(Request $request, $publication_id, User_Adapter $user) - { - $result = new API_V1_result($this->app, $request, $this); - - $feed = Feed_Adapter::load_with_user($this->app, $user, $publication_id); - - $offset_start = (int) $request->get('offset_start', 0); - $per_page = (int) $request->get('per_page', 5); - - $per_page = (($per_page >= 1) && ($per_page <= 100)) ? $per_page : 100; - - $datas = array( - 'feed' => $this->list_publication($feed, $user), - 'offset_start' => $offset_start, - 'per_page' => $per_page, - 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), - ); - - $result->set_datas($datas); - - return $result; - } - - public function get_publications(Request $request, User_Adapter $user) - { - $result = new API_V1_result($this->app, $request, $this); - - $restrictions = (array) ($request->get('feeds') ? : array()); - - $feed = Feed_Aggregate::load_with_user($this->app, $user, $restrictions); - - $offset_start = (int) ($request->get('offset_start') ? : 0); - $per_page = (int) ($request->get('per_page') ? : 5); - - $per_page = (($per_page >= 1) && ($per_page <= 20)) ? $per_page : 5; - - $result->set_datas(array( - 'total_entries' => $feed->get_count_total_entries(), - 'offset_start' => $offset_start, - 'per_page' => $per_page, - 'entries' => $this->list_publications_entries($feed, $request->get('_extended'), $offset_start, $per_page), - )); - - return $result; - } - - public function get_feed_entry(Request $request, $entry_id, User_Adapter $user) - { - $result = new API_V1_result($this->app, $request, $this); - - $entry = Feed_Entry_Adapter::load_from_id($this->app, $entry_id); - - $collection = $entry->get_feed()->get_collection(); - - if (null !== $collection && !$user->ACL()->has_access_to_base($collection->get_base_id())) { - throw new \API_V1_exception_forbidden('You have not access to the parent feed'); - } - - $datas = array( - 'entry' => $this->list_publication_entry($entry), - ); - - $result->set_datas($datas); - - return $result; - } - - /** - * Retrieve detailled informations about one feed - * - * @param Feed_Adapter $feed - * @param type $user - * @return array - */ - protected function list_publication(Feed_Adapter $feed, $user) - { - return array( - 'id' => $feed->get_id(), - 'title' => $feed->get_title(), - 'subtitle' => $feed->get_subtitle(), - 'total_entries' => $feed->get_count_total_entries(), - 'icon' => $feed->get_icon_url(), - 'public' => $feed->is_public(), - 'readonly' => !$feed->is_publisher($user), - 'deletable' => $feed->is_owner($user), - 'created_on' => $feed->get_created_on()->format(DATE_ATOM), - 'updated_on' => $feed->get_updated_on()->format(DATE_ATOM), - ); - } - - /** - * Retrieve all entries of one feed - * - * @param Feed_Adapter $feed - * @param int $offset_start - * @param int $how_many - * @return array - */ - protected function list_publications_entries(Feed_Abstract $feed, $extended = false, $offset_start = 0, $how_many = 5) - { - $entries = $feed->get_entries($offset_start, $how_many)->get_entries(); - - $out = array(); - foreach ($entries as $entry) { - $out[] = $this->list_publication_entry($entry, $extended); - } - - return $out; - } - - /** - * Retrieve detailled information about one feed entry - * - * @param Feed_Entry_Adapter $entry - * @return array - */ - protected function list_publication_entry(Feed_Entry_Adapter $entry, $extended = false) - { - $items = array(); - foreach ($entry->get_content() as $item) { - $items[] = $this->list_publication_entry_item($item, $extended); - } - - return array( - 'id' => $entry->get_id(), - 'author_email' => $entry->get_author_email(), - 'author_name' => $entry->get_author_name(), - 'created_on' => $entry->get_created_on()->format(DATE_ATOM), - 'updated_on' => $entry->get_updated_on()->format(DATE_ATOM), - 'title' => $entry->get_title(), - 'subtitle' => $entry->get_subtitle(), - 'items' => $items, - 'feed_id' => $entry->get_feed()->get_id(), - 'feed_title' => $entry->get_feed()->get_title(), - 'feed_url' => '/feeds/' . $entry->get_feed()->get_id() . '/content/', - 'url' => '/feeds/entry/' . $entry->get_id() . '/', - ); - } - - /** - * Retrieve detailled informations about one feed entry item - * - * @param Feed_Entry_Item $item - * @return array - */ - protected function list_publication_entry_item(Feed_Entry_Item $item, $extended = false) - { - $datas = array( - 'item_id' => $item->get_id(), - 'record' => $this->list_record($item->get_record(), $extended) - ); - - return $datas; - } - - /** - * @todo - * @param Request $request - */ - public function search_users(Request $request) - { - - } - - /** - * @param Request $request - * @param int $usr_id - */ - public function get_user_acces(Request $request, $usr_id) - { - - } - - /** - * @todo - * @param Request $request - */ - public function add_user(Request $request) - { - - } - - /** - * @retrieve detailled informations about one suddef - * - * @param media_subdef $media - * @return array - */ - protected function list_embedable_media(\record_adapter $record, media_subdef $media, registryInterface $registry) - { - if (!$media->is_physically_present()) { - return null; - } - - if ($this->app['authentication']->isAuthenticated()) { - if ($media->get_name() !== 'document' && false === $this->app['authentication']->getUser()->ACL()->has_access_to_subdef($record, $media->get_name())) { - return null; - } else if ($media->get_name() === 'document' - && !$this->app['authentication']->getUser()->ACL()->has_right_on_base($record->get_base_id(), 'candwnldhd') - && !$this->app['authentication']->getUser()->ACL()->has_hd_grant($record)) { - return null; - } - } - - if ($media->get_permalink() instanceof media_Permalink_Adapter) { - $permalink = $this->list_permalink($media->get_permalink(), $registry); - } else { - $permalink = null; - } - - return array( - 'name' => $media->get_name(), - 'permalink' => $permalink, - 'height' => $media->get_height(), - 'width' => $media->get_width(), - 'filesize' => $media->get_size(), - 'devices' => $media->getDevices(), - 'player_type' => $media->get_type(), - 'mime_type' => $media->get_mime(), - ); - } - - /** - * Retrieve detailled information about one permalink - * - * @param media_Permalink_Adapter $permalink - * @param registryInterface $registry - * @return type - */ - protected function list_permalink(media_Permalink_Adapter $permalink, registryInterface $registry) - { - $downloadUrl = $permalink->get_url(); - $downloadUrl->getQuery()->set('download', '1'); - - return array( - 'created_on' => $permalink->get_created_on()->format(DATE_ATOM), - 'id' => $permalink->get_id(), - 'is_activated' => $permalink->get_is_activated(), - 'label' => $permalink->get_label(), - 'updated_on' => $permalink->get_last_modified()->format(DATE_ATOM), - 'page_url' => $permalink->get_page(), - 'download_url' => (string) $downloadUrl, - 'url' => (string) $permalink->get_url() - ); - } - - /** - * Retrieve detailled information about one status - * - * @param databox $databox - * @param string $status - * @return array - */ - protected function list_record_status(databox $databox, $status) - { - $status = strrev($status); - $ret = array(); - foreach ($databox->get_statusbits() as $bit => $status_datas) { - $ret[] = array('bit' => $bit, 'state' => !!substr($status, ($bit - 1), 1)); - } - - return $ret; - } - - /** - * List all field about a specified caption - * - * @param caption_record $caption - * @return array - */ - protected function list_record_caption(caption_record $caption) - { - $ret = array(); - foreach ($caption->get_fields() as $field) { - foreach ($field->get_values() as $value) { - $ret[] = $this->list_record_caption_field($value, $field); - } - } - - return $ret; - } - - /** - * Retrieve information about a caption field - * - * @param caption_field $field - * @return array - */ - protected function list_record_caption_field(caption_Field_Value $value, caption_field $field) - { - return array( - 'meta_id' => $value->getId(), - 'meta_structure_id' => $field->get_meta_struct_id(), - 'name' => $field->get_name(), - 'labels' => array( - 'fr' => $field->get_databox_field()->get_label('fr'), - 'en' => $field->get_databox_field()->get_label('en'), - 'de' => $field->get_databox_field()->get_label('de'), - 'nl' => $field->get_databox_field()->get_label('nl'), - ), - 'value' => $value->getValue(), - ); - } - - /** - * Retirve information about one basket - * - * @param \Entities\Basket $basket - * @return array - */ - public function list_basket(\Entities\Basket $basket) - { - $ret = array( - 'basket_id' => $basket->getId(), - 'owner' => $this->list_user($basket->getOwner($this->app)), - 'created_on' => $basket->getCreated()->format(DATE_ATOM), - 'description' => (string) $basket->getDescription(), - 'name' => $basket->getName(), - 'pusher_usr_id' => $basket->getPusherId(), - 'pusher' => $basket->getPusher($this->app) ? $this->list_user($basket->getPusher($this->app)) : null, - 'updated_on' => $basket->getUpdated()->format(DATE_ATOM), - 'unread' => !$basket->getIsRead(), - 'validation_basket' => !!$basket->getValidation() - ); - - if ($basket->getValidation()) { - $users = array(); - - foreach ($basket->getValidation()->getParticipants() as $participant) { - /* @var $participant \Entities\ValidationParticipant */ - $user = $participant->getUser($this->app); - - $users[] = array( - 'usr_id' => $user->get_id(), - 'usr_name' => $user->get_display_name(), - 'confirmed' => $participant->getIsConfirmed(), - 'can_agree' => $participant->getCanAgree(), - 'can_see_others' => $participant->getCanSeeOthers(), - 'readonly' => $user->get_id() != $this->app['authentication']->getUser()->get_id(), - 'user' => $this->list_user($user), - ); - } - - $expires_on_atom = $basket->getValidation()->getExpires(); - - if ($expires_on_atom instanceof DateTime) { - $expires_on_atom = $expires_on_atom->format(DATE_ATOM); - } - - $ret = array_merge( - array( - 'validation_users' => $users, - 'expires_on' => $expires_on_atom, - 'validation_infos' => $basket->getValidation()->getValidationString($this->app, $this->app['authentication']->getUser()), - 'validation_confirmed' => $basket->getValidation()->getParticipant($this->app['authentication']->getUser(), $this->app)->getIsConfirmed(), - 'validation_initiator' => $basket->getValidation()->isInitiator($this->app['authentication']->getUser()), - 'validation_initiator_user' => $this->list_user($basket->getValidation()->getInitiator($this->app)), - ), $ret - ); - } - - return $ret; - } - - /** - * Retrieve detailled informations about one record - * - * @param record_adapter $record - * @return array - */ - public function list_record(record_adapter $record, $extended = false) - { - $technicalInformation = array(); - foreach ($record->get_technical_infos() as $name => $value) { - $technicalInformation[] = array( - 'name' => $name, - 'value' => $value - ); - } - - $data = array( - 'databox_id' => $record->get_sbas_id(), - 'record_id' => $record->get_record_id(), - 'mime_type' => $record->get_mime(), - 'title' => $record->get_title(), - 'original_name' => $record->get_original_name(), - 'updated_on' => $record->get_modification_date()->format(DATE_ATOM), - 'created_on' => $record->get_creation_date()->format(DATE_ATOM), - 'collection_id' => phrasea::collFromBas($this->app, $record->get_base_id()), - 'sha256' => $record->get_sha256(), - 'thumbnail' => $this->list_embedable_media($record, $record->get_thumbnail(), $this->app['phraseanet.registry']), - 'technical_informations' => $technicalInformation, - 'phrasea_type' => $record->get_type(), - 'uuid' => $record->get_uuid(), - ); - - if ($extended) { - $subdefs = $caption = array(); - - foreach ($record->get_embedable_medias(array(), array()) as $name => $media) { - if (null !== $subdef = $this->list_embedable_media($record, $media, $this->app['phraseanet.registry'])) { - $subdefs[] = $subdef; - } - } - - foreach ($record->get_caption()->get_fields() as $field) { - $caption[] = array( - 'meta_structure_id' => $field->get_meta_struct_id(), - 'name' => $field->get_name(), - 'value' => $field->get_serialized_values(';'), - ); - } - - $extendedData = array( - 'subdefs' => $subdefs, - 'metadata' => $this->list_record_caption($record->get_caption()), - 'status' => $this->list_record_status($record->get_databox(), $record->get_status()), - 'caption' => $caption - ); - - $data = array_merge($data, $extendedData); - } - - return $data; - } - - /** - * Retrieve detailled informations about one story - * - * @param record_adapter $story - * - * @return array - */ - public function list_story(record_adapter $story, $extended = false) - { - if (!$story->is_grouping()) { - throw new \API_V1_exception_notfound('Story not found'); - } - - $that = $this; - $records = array_map(function (\record_adapter $record) use ($that, $extended) { - return $that->list_record($record, $extended); - }, array_values($story->get_children()->get_elements())); - - $caption = $story->get_caption(); - - $format = function (caption_record $caption, $dcField) { - - $field = $caption->get_dc_field($dcField); - - if (!$field) { - return null; - } - - return $field->get_serialized_values(); - }; - - return array( - '@entity@' => self::OBJECT_TYPE_STORY, - 'databox_id' => $story->get_sbas_id(), - 'story_id' => $story->get_record_id(), - 'updated_on' => $story->get_modification_date()->format(DATE_ATOM), - 'created_on' => $story->get_creation_date()->format(DATE_ATOM), - 'collection_id' => phrasea::collFromBas($this->app, $story->get_base_id()), - 'thumbnail' => $this->list_embedable_media($story, $story->get_thumbnail(), $this->app['phraseanet.registry']), - 'uuid' => $story->get_uuid(), - 'metadatas' => array( - '@entity@' => self::OBJECT_TYPE_STORY_METADATA_BAG, - 'dc:contributor' => $format($caption, databox_Field_DCESAbstract::Contributor), - 'dc:coverage' => $format($caption, databox_Field_DCESAbstract::Coverage), - 'dc:creator' => $format($caption, databox_Field_DCESAbstract::Creator), - 'dc:date' => $format($caption, databox_Field_DCESAbstract::Date), - 'dc:description' => $format($caption, databox_Field_DCESAbstract::Description), - 'dc:format' => $format($caption, databox_Field_DCESAbstract::Format), - 'dc:identifier' => $format($caption, databox_Field_DCESAbstract::Identifier), - 'dc:language' => $format($caption, databox_Field_DCESAbstract::Language), - 'dc:publisher' => $format($caption, databox_Field_DCESAbstract::Publisher), - 'dc:relation' => $format($caption, databox_Field_DCESAbstract::Relation), - 'dc:rights' => $format($caption, databox_Field_DCESAbstract::Rights), - 'dc:source' => $format($caption, databox_Field_DCESAbstract::Source), - 'dc:subject' => $format($caption, databox_Field_DCESAbstract::Subject), - 'dc:title' => $format($caption, databox_Field_DCESAbstract::Title), - 'dc:type' => $format($caption, databox_Field_DCESAbstract::Type), - ), - 'records' => $records, - ); - } - - public function get_current_user(Application $app, Request $request) - { - $result = new API_V1_result($app, $request, $this); - $result->set_datas(array('user' => $this->list_user($app['authentication']->getUser()))); - - return $result; - } - - private function list_user(\User_Adapter $user) - { - switch ($user->get_gender()) { - case 2; - $gender = 'Mr'; - break; - case 1; - $gender = 'Mrs'; - break; - case 0; - $gender = 'Miss'; - break; - } - - return array( - '@entity@' => self::OBJECT_TYPE_USER, - 'id' => $user->get_id(), - 'email' => $user->get_email() ?: null, - 'login' => $user->get_login() ?: null, - 'first_name' => $user->get_firstname() ?: null, - 'last_name' => $user->get_lastname() ?: null, - 'display_name' => $user->get_display_name() ?: null, - 'gender' => $gender, - 'address' => $user->get_address() ?: null, - 'zip_code' => $user->get_zipcode() ?: null, - 'city' => $user->get_city() ?: null, - 'country' => $user->get_country() ?: null, - 'phone' => $user->get_tel() ?: null, - 'fax' => $user->get_fax() ?: null, - 'job' => $user->get_job() ?: null, - 'position' => $user->get_position() ?: null, - 'company' => $user->get_company() ?: null, - 'geoname_id' => $user->get_geonameid() ?: null, - 'last_connection' => $user->get_last_connection() ? $user->get_last_connection()->format(DATE_ATOM) : null, - 'created_on' => $user->get_creation_date() ? $user->get_creation_date()->format(DATE_ATOM) : null, - 'updated_on' => $user->get_modification_date() ? $user->get_modification_date()->format(DATE_ATOM) : null, - 'locale' => $user->get_locale() ?: null, - ); - } - - /** - * List all databoxes of the current appbox - * - * @return array - */ - protected function list_databoxes() - { - $ret = array(); - foreach ($this->app['phraseanet.appbox']->get_databoxes() as $databox) { - $ret[] = $this->list_databox($databox); - } - - return $ret; - } - - /** - * Retrieve CGU's for the specified databox - * - * @param databox $databox - * @return array - */ - protected function list_databox_terms(databox $databox) - { - $ret = array(); - foreach ($databox->get_cgus() as $locale => $array_terms) { - $ret[] = array('locale' => $locale, 'terms' => $array_terms['value']); - } - - return $ret; - } - - /** - * Retrieve detailled informations about one databox - * @param databox $databox - * @return array - */ - protected function list_databox(databox $databox) - { - $ret = array(); - - $ret['databox_id'] = $databox->get_sbas_id(); - $ret['name'] = $databox->get_dbname(); - $ret['viewname'] = $databox->get_viewname(); - $ret['labels'] = array( - 'en' => $databox->get_label('en'), - 'de' => $databox->get_label('de'), - 'fr' => $databox->get_label('fr'), - 'nl' => $databox->get_label('nl'), - ); - $ret['version'] = $databox->get_version(); - - return $ret; - } - - /** - * List all available collections for a specified databox - * - * @param databox $databox - * @return array - */ - protected function list_databox_collections(databox $databox) - { - $ret = array(); - - foreach ($databox->get_collections() as $collection) { - $ret[] = $this->list_collection($collection); - } - - return $ret; - } - - /** - * Retrieve detailled informations about one collection - * - * @param collection $collection - * @return array - */ - protected function list_collection(collection $collection) - { - $ret = array( - 'base_id' => $collection->get_base_id(), - 'collection_id' => $collection->get_coll_id(), - 'name' => $collection->get_name(), - 'labels' => array( - 'fr' => $collection->get_label('fr'), - 'en' => $collection->get_label('en'), - 'de' => $collection->get_label('de'), - 'nl' => $collection->get_label('nl'), - ), - 'record_amount' => $collection->get_record_amount(), - ); - - return $ret; - } - - /** - * Retrieve informations for a list of status - * - * @param array $status - * @return array - */ - protected function list_databox_status(array $status) - { - $ret = array(); - foreach ($status as $n => $datas) { - $ret[] = array( - 'bit' => $n, - 'label_on' => $datas['labelon'], - 'label_off' => $datas['labeloff'], - 'labels' => array( - 'en' => $datas['labels_on_i18n']['en'], - 'fr' => $datas['labels_on_i18n']['fr'], - 'de' => $datas['labels_on_i18n']['de'], - 'nl' => $datas['labels_on_i18n']['nl'], - ), - 'img_on' => $datas['img_on'], - 'img_off' => $datas['img_off'], - 'searchable' => !!$datas['searchable'], - 'printable' => !!$datas['printable'], - ); - } - - return $ret; - } - - /** - * List all metadatas field using a databox meta structure - * - * @param databox_descriptionStructure $meta_struct - * @return array - */ - protected function list_databox_metadatas_fields(databox_descriptionStructure $meta_struct) - { - $ret = array(); - foreach ($meta_struct as $meta) { - $ret[] = $this->list_databox_metadata_field_properties($meta); - } - - return $ret; - } - - /** - * Retirve informations about one databox metadata field - * - * @param databox_field $databox_field - * @return array - */ - protected function list_databox_metadata_field_properties(databox_field $databox_field) - { - $ret = array( - 'id' => $databox_field->get_id(), - 'namespace' => $databox_field->get_tag()->getGroupName(), - 'source' => $databox_field->get_tag()->getTagname(), - 'tagname' => $databox_field->get_tag()->getName(), - 'name' => $databox_field->get_name(), - 'labels' => array( - 'fr' => $databox_field->get_label('fr'), - 'en' => $databox_field->get_label('en'), - 'de' => $databox_field->get_label('de'), - 'nl' => $databox_field->get_label('nl'), - ), - 'separator' => $databox_field->get_separator(), - 'thesaurus_branch' => $databox_field->get_tbranch(), - 'type' => $databox_field->get_type(), - 'indexable' => $databox_field->is_indexable(), - 'multivalue' => $databox_field->is_multi(), - 'readonly' => $databox_field->is_readonly(), - 'required' => $databox_field->is_required(), - ); - - return $ret; - } - } diff --git a/lib/classes/databox/subdefsStructure.php b/lib/classes/databox/subdefsStructure.php index 749185c741..c16fa0e154 100644 --- a/lib/classes/databox/subdefsStructure.php +++ b/lib/classes/databox/subdefsStructure.php @@ -130,17 +130,22 @@ class databox_subdefsStructure implements IteratorAggregate, Countable } /** + * @param $subdef_type + * @param $subdef_name * - * @param type $subdef_type - * @param type $subdef_name - * @return databox_subdef + * @return mixed + * @throws Exception_Databox_SubdefNotFound */ public function get_subdef($subdef_type, $subdef_name) { - if (isset($this->AvSubdefs[$subdef_type]) && isset($this->AvSubdefs[$subdef_type][$subdef_name])) { - return $this->AvSubdefs[$subdef_type][$subdef_name]; + $type = strtolower($subdef_type); + $name = strtolower($subdef_name); + + if (isset($this->AvSubdefs[$type]) && isset($this->AvSubdefs[$type][$name])) { + return $this->AvSubdefs[$type][$name]; } - throw new Exception_Databox_SubdefNotFound(sprintf('Databox subdef name `%s` of type `%s` not found', $subdef_name, $subdef_type)); + + throw new Exception_Databox_SubdefNotFound(sprintf('Databox subdef name `%s` of type `%s` not found', $name, $type)); } /** diff --git a/tests/classes/api/v1/api_v1_adapterTest.php b/tests/classes/api/v1/api_v1_adapterTest.php index 124fe1a188..fa734e57bd 100644 --- a/tests/classes/api/v1/api_v1_adapterTest.php +++ b/tests/classes/api/v1/api_v1_adapterTest.php @@ -474,7 +474,23 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract { $app = self::$DI['app']; $stub = $this->getMock("API_V1_adapter", array("list_record"), array($app)); - $databox = self::$DI['record_1']->get_databox(); + self::$DI['app']['authentication']->setUser($this->getMockBuilder('\User_Adapter') + ->setMethods(array('ACL')) + ->disableOriginalConstructor() + ->getMock()); + + $stubbedACL = $this->getMockBuilder('\ACL') + ->disableOriginalConstructor() + ->getMock(); + + //has_right_on_base return true + $stubbedACL->expects($this->any()) + ->method('has_right_on_base') + ->will($this->returnValue(true)); + + self::$DI['app']['authentication']->getUser()->expects($this->any()) + ->method('ACL') + ->will($this->returnValue($stubbedACL)); $request = new Request(array("salut" => "salut c'est la fete"), array(), array(), array(), array(), array('HTTP_Accept' => 'application/json')); $result = $stub->set_record_collection($request, self::$DI['record_1']->get_sbas_id(), self::$DI['record_1']->get_record_id()); diff --git a/tests/db-ref.sqlite b/tests/db-ref.sqlite index 9cb2220a77..36b9d5aa01 100644 Binary files a/tests/db-ref.sqlite and b/tests/db-ref.sqlite differ