From 05ad5886d2a71131b875668d4ff72bfc55daff33 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jul 2023 18:00:31 +0200 Subject: [PATCH] add: quarantine: when "adding", can copy metadata from the selected record --- .../Controller/Prod/LazaretController.php | 51 ++++- templates/web/prod/upload/lazaret.html.twig | 183 ++++++++++-------- 2 files changed, 148 insertions(+), 86 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/LazaretController.php b/lib/Alchemy/Phrasea/Controller/Prod/LazaretController.php index 7fcddb0b28..634d69b028 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/LazaretController.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/LazaretController.php @@ -124,6 +124,39 @@ public function addElement(Request $request, $file_id) /** @var LazaretManipulator $lazaretManipulator */ $lazaretManipulator = $this->app['manipulator.lazaret']; + //Check if the chosen record is eligible to the substitution + $recordId = $request->request->get('record_id'); + /** @var LazaretFile $lazaretFile */ + $lazaretFile = $this->getLazaretFileRepository()->find($file_id); + + $metadatasToSet = []; + if(!!$request->request->get('copy_meta', false)) { + + $substitutedRecord = null; + foreach ($lazaretFile->getRecordsToSubstitute($this->app) as $r) { + if ($r->getRecordId() === (int)$recordId) { + $substitutedRecord = $r; + break; + } + } + if (!$substitutedRecord) { + $ret['message'] = $this->app->trans('The destination record provided is not allowed'); + + return $this->app->json($ret); + } + + $fieldsToCopy = []; + foreach ($substitutedRecord->getDatabox()->get_meta_structure() as $df) { + if(!$df->is_readonly()) { + $fieldsToCopy[] = $df->get_name(); + } + } + $metadatas = []; + foreach ($substitutedRecord->getCaption($fieldsToCopy) as $k=>$v) { + $metadatasToSet[] = ['field_name' => $k, 'value' => $v]; + } + } + $ret = $lazaretManipulator->add($file_id, $keepAttributes, $attributesToKeep); try{ @@ -132,7 +165,13 @@ public function addElement(Request $request, $file_id) $postStatus = (array) $request->request->get('status'); // update status $this->updateRecordStatus($record, $postStatus); - }catch(\Exception $e){ + + if(!empty($metadatasToSet)) { + $actions = json_decode(json_encode(['metadatas' => $metadatasToSet])); + $record->setMetadatasByActions($actions); + } + } + catch(\Exception $e){ $ret['message'] = $this->app->trans('An error occured when wanting to change status!'); } @@ -209,16 +248,14 @@ public function acceptElement(Request $request, $file_id) return $this->app->json($ret); } - $found = false; //Check if the chosen record is eligible to the substitution + $found = false; foreach ($lazaretFile->getRecordsToSubstitute($this->app) as $record) { - if ($record->getRecordId() !== (int) $recordId) { - continue; + if ($record->getRecordId() === (int) $recordId) { + $found = true; + break; } - - $found = true; - break; } if (!$found) { diff --git a/templates/web/prod/upload/lazaret.html.twig b/templates/web/prod/upload/lazaret.html.twig index 5c8796ddd2..27e009d97d 100644 --- a/templates/web/prod/upload/lazaret.html.twig +++ b/templates/web/prod/upload/lazaret.html.twig @@ -33,8 +33,9 @@