From 8d467052e41fd83fe62bf6a6a2b2d81f258c2a40 Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Mon, 31 Jul 2023 18:59:50 +0100 Subject: [PATCH 1/4] . --- app/Services/Asset.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/app/Services/Asset.php b/app/Services/Asset.php index fa1e2ab..41c3583 100644 --- a/app/Services/Asset.php +++ b/app/Services/Asset.php @@ -160,6 +160,9 @@ public function createEntity($data) $entity = $this->getRepository()->get(); $entity->set($data); + // validate files + $this->validateAttachments($entity); + // Are all required fields filled ? $this->checkRequiredFields($entity, $data); @@ -170,6 +173,37 @@ public function createEntity($data) return parent::createEntity($data); } + protected function beforeUpdateEntity(Entity $entity, $data) + { + $this->validateAttachments($entity); + parent::beforeUpdateEntity($entity, $data); // TODO: Change the autogenerated stub + } + + protected function validateAttachments($entity) + { + $filesIds = $this->isMassCreating($entity) ? $entity->get('filesIds') : [$entity->get('fileId')]; + foreach ($filesIds as $fileId) { + $this->getInjection(AssetValidator::class)->validateViaTypes($this->getTypeValue($entity), $this->getEntityManager()->getEntity('Attachment', $fileId)); + } + } + + public function getTypeValue($entity) + { + $defs = $this->getMetadata()->get(['entityDefs', $this->entityName, 'fields', 'type'], []); + $options = []; + foreach ($entity->get('type') as $optionId) { + $key = array_search($optionId, $defs['optionsIds']); + if ($key === false) { + // for create or massupload + $options[] = $optionId; + } else { + // for update + $options[] = $defs['options'][$key]; + } + } + return $options; + } + /** * @param \Dam\Entities\Asset $asset */ From 05ee88cf1db1e90f4fd0f3ef2a931e3bfdc55ba4 Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Mon, 31 Jul 2023 19:02:33 +0100 Subject: [PATCH 2/4] . --- app/Services/Asset.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/Asset.php b/app/Services/Asset.php index 41c3583..80ddc3e 100644 --- a/app/Services/Asset.php +++ b/app/Services/Asset.php @@ -176,7 +176,7 @@ public function createEntity($data) protected function beforeUpdateEntity(Entity $entity, $data) { $this->validateAttachments($entity); - parent::beforeUpdateEntity($entity, $data); // TODO: Change the autogenerated stub + parent::beforeUpdateEntity($entity, $data); } protected function validateAttachments($entity) From c7bca617ecbcd4b83cb02830e79735b9a70c0418 Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Tue, 1 Aug 2023 08:34:14 +0100 Subject: [PATCH 3/4] . --- app/Services/Asset.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/Services/Asset.php b/app/Services/Asset.php index 80ddc3e..d1d64e8 100644 --- a/app/Services/Asset.php +++ b/app/Services/Asset.php @@ -160,9 +160,6 @@ public function createEntity($data) $entity = $this->getRepository()->get(); $entity->set($data); - // validate files - $this->validateAttachments($entity); - // Are all required fields filled ? $this->checkRequiredFields($entity, $data); @@ -173,17 +170,22 @@ public function createEntity($data) return parent::createEntity($data); } + protected function beforeCreateEntity(Entity $entity, $data) + { + $this->validateAttachment($entity, $data); + parent::beforeCreateEntity($entity, $data); + } + protected function beforeUpdateEntity(Entity $entity, $data) { - $this->validateAttachments($entity); + $this->validateAttachment($entity, $data); parent::beforeUpdateEntity($entity, $data); } - protected function validateAttachments($entity) + protected function validateAttachment($entity, $data) { - $filesIds = $this->isMassCreating($entity) ? $entity->get('filesIds') : [$entity->get('fileId')]; - foreach ($filesIds as $fileId) { - $this->getInjection(AssetValidator::class)->validateViaTypes($this->getTypeValue($entity), $this->getEntityManager()->getEntity('Attachment', $fileId)); + if (!empty($entity->get('fileId')) && property_exists($data, 'type')) { + $this->getInjection(AssetValidator::class)->validateViaTypes($this->getTypeValue($data->type), $this->getEntityManager()->getEntity('Attachment', $entity->get('fileId'))); } } From 325babdb25651ef066a5f9aceaa08e89423318bf Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Tue, 1 Aug 2023 08:39:16 +0100 Subject: [PATCH 4/4] . --- app/Services/Asset.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Services/Asset.php b/app/Services/Asset.php index d1d64e8..32e0713 100644 --- a/app/Services/Asset.php +++ b/app/Services/Asset.php @@ -189,11 +189,15 @@ protected function validateAttachment($entity, $data) } } - public function getTypeValue($entity) + public function getTypeValue($type) { + if (empty($type)) { + return []; + } + $defs = $this->getMetadata()->get(['entityDefs', $this->entityName, 'fields', 'type'], []); $options = []; - foreach ($entity->get('type') as $optionId) { + foreach ($type as $optionId) { $key = array_search($optionId, $defs['optionsIds']); if ($key === false) { // for create or massupload