diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php index c22cc9f46c..89e398c46d 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1Controller.php @@ -1186,30 +1186,12 @@ class V1Controller extends Controller ]; if ($request->attributes->get('_extended', false)) { - $subdefs = $caption = []; - - foreach ($record->get_embedable_medias([], []) as $name => $media) { - if (null !== $subdef = $this->listEmbeddableMedia($request, $record, $media)) { - $subdefs[] = $subdef; - } - } - - foreach ($record->get_caption()->get_fields() as $field) { - $caption[] = [ - 'meta_structure_id' => $field->get_meta_struct_id(), - 'name' => $field->get_name(), - 'value' => $field->get_serialized_values(';'), - ]; - } - - $extendedData = [ - 'subdefs' => $subdefs, - 'metadata' => $this->listRecordCaption($record->get_caption()), - 'status' => $this->listRecordStatus($record), - 'caption' => $caption - ]; - - $data = array_merge($data, $extendedData); + $data = array_merge($data, [ + 'subdefs' => $this->listRecordEmbeddableMedias($request, $record), + 'metadata' => $this->listRecordMetadata($record), + 'status' => $this->listRecordStatus($record), + 'caption' => $this->listRecordCaption($record), + ]); } return $data; @@ -1314,53 +1296,59 @@ class V1Controller extends Controller public function getRecordMetadataAction(Request $request, $databox_id, $record_id) { $record = $this->findDataboxById($databox_id)->get_record($record_id); - $ret = ["record_metadatas" => $this->listRecordCaption($record->get_caption())]; + $ret = ["record_metadatas" => $this->listRecordMetadata($record)]; return Result::create($request, $ret)->createResponse(); } /** - * List all fields about a specified caption - * - * @param \caption_record $caption + * List all fields of given record * + * @param \record_adapter $record * @return array */ - private function listRecordCaption(\caption_record $caption) + private function listRecordMetadata(\record_adapter $record) + { + $includeBusiness = $this->getAclForUser()->can_see_business_fields($record->getDatabox()); + + return $this->listRecordCaptionFields($record->get_caption()->get_fields(null, $includeBusiness)); + } + + /** + * @param \caption_field[] $fields + * @return array + */ + private function listRecordCaptionFields($fields) { $ret = []; - foreach ($caption->get_fields() as $field) { + + foreach ($fields as $field) { + $databox_field = $field->get_databox_field(); + + $fieldData = [ + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'labels' => [ + 'fr' => $databox_field->get_label('fr'), + 'en' => $databox_field->get_label('en'), + 'de' => $databox_field->get_label('de'), + 'nl' => $databox_field->get_label('nl'), + ], + ]; + foreach ($field->get_values() as $value) { - $ret[] = $this->listRecordCaptionField($value, $field); + $data = [ + 'meta_id' => $value->getId(), + 'value' => $value->getValue(), + ]; + + $ret[] = $fieldData + $data; } } return $ret; } - /** - * Retrieve information about a caption field - * - * @param \caption_Field_Value $value - * @param \caption_field $field - * @return array - */ - private function listRecordCaptionField(\caption_Field_Value $value, \caption_field $field) - { - return [ - 'meta_id' => $value->getId(), - 'meta_structure_id' => $field->get_meta_struct_id(), - 'name' => $field->get_name(), - 'labels' => [ - '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(), - ]; - } - /** * Get a Response containing the record status * @@ -1524,7 +1512,7 @@ class V1Controller extends Controller $record->set_metadatas($metadata); return Result::create($request, [ - "record_metadatas" => $this->listRecordCaption($record->get_caption()), + "record_metadatas" => $this->listRecordMetadata($record), ])->createResponse(); } @@ -2572,4 +2560,43 @@ class V1Controller extends Controller { return $this->app['subdef.substituer']; } + + /** + * @param Request $request + * @param \record_adapter $record + * @return array + */ + private function listRecordEmbeddableMedias(Request $request, \record_adapter $record) + { + $subdefs = []; + + foreach ($record->get_embedable_medias([], []) as $name => $media) { + if (null !== $subdef = $this->listEmbeddableMedia($request, $record, $media)) { + $subdefs[] = $subdef; + } + } + + return $subdefs; + } + + /** + * @param \record_adapter $record + * @return array + */ + private function listRecordCaption(\record_adapter $record) + { + $includeBusiness = $this->getAclForUser()->can_see_business_fields($record->getDatabox()); + + $caption = []; + + foreach ($record->get_caption()->get_fields(null, $includeBusiness) as $field) { + $caption[] = [ + 'meta_structure_id' => $field->get_meta_struct_id(), + 'name' => $field->get_name(), + 'value' => $field->get_serialized_values(';'), + ]; + } + + return $caption; + } } diff --git a/lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php b/lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php index 18116625a6..8335c4db90 100644 --- a/lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php +++ b/lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php @@ -15,6 +15,8 @@ use Assert\Assertion; class RecordReferenceCollection implements \IteratorAggregate { + private $groups; + /** * @param array $records * @return RecordReferenceCollection @@ -48,7 +50,7 @@ class RecordReferenceCollection implements \IteratorAggregate { Assertion::allIsInstanceOf($references, RecordReferenceInterface::class); - $this->references = $references; + $this->references = $references instanceof \Traversable ? iterator_to_array($references) : $references; } public function getIterator() @@ -61,19 +63,29 @@ class RecordReferenceCollection implements \IteratorAggregate */ public function groupPerDataboxId() { - $groups = []; + if (null === $this->groups) { + $this->groups = []; - foreach ($this->references as $index => $reference) { - $databoxId = $reference->getDataboxId(); + foreach ($this->references as $index => $reference) { + $databoxId = $reference->getDataboxId(); - if (!isset($groups[$databoxId])) { - $groups[$databoxId] = []; + if (!isset($this->groups[$databoxId])) { + $this->groups[$databoxId] = []; + } + + $this->groups[$databoxId][$reference->getRecordId()] = $index; } - - $groups[$databoxId][$reference->getRecordId()] = $index; } - return $groups; + return $this->groups; + } + + /** + * @return array + */ + public function getDataboxIds() + { + return array_keys($this->groupPerDataboxId()); } /** diff --git a/lib/classes/caption/record.php b/lib/classes/caption/record.php index 1c6431792c..f769f2dad8 100644 --- a/lib/classes/caption/record.php +++ b/lib/classes/caption/record.php @@ -17,20 +17,17 @@ use Alchemy\Phrasea\SearchEngine\SearchEngineOptions; class caption_record implements caption_interface, cache_cacheableInterface { /** - * * @var array */ protected $fields; /** - * * @var int */ protected $sbas_id; /** - * - * @var record + * @var record_adapter */ protected $record; protected $databox;