From 64acb116a027086b27f5e1e0a50a070a2abaaced Mon Sep 17 00:00:00 2001 From: Roman Zablodskyi Date: Wed, 20 Dec 2023 11:42:47 +0200 Subject: [PATCH 1/3] Added removing related assets when remove attachment --- app/Services/Attachment.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/app/Services/Attachment.php b/app/Services/Attachment.php index 81e198f..8b641c8 100644 --- a/app/Services/Attachment.php +++ b/app/Services/Attachment.php @@ -267,6 +267,37 @@ public function changeName(\Dam\Entities\Attachment $attachment, string $newName return $this->getRepository()->renameFile($attachment, $newName); } + public function afterDeleteEntity(Entity $entity) + { + parent::afterDeleteEntity($entity); + + if ($this->getMetadata()->get(['entityDefs', 'Asset', 'fields', 'file', 'required'], false)) { + $offset = 0; + $limit = 20; + + while(true) { + $assets = $this + ->getEntityManager() + ->getRepository('Asset') + ->where([ + 'fileId' => $entity->id + ]) + ->limit($offset, $limit) + ->find(); + + if (count($assets) == 0) { + break; + } + + foreach ($assets as $asset) { + $this->getPseudoTransactionManager()->pushDeleteEntityJob('Asset', $asset->id); + } + + $offset += $limit; + } + } + } + /** * @inheritDoc */ From 6fc149e41e447f3be9f761370fa4755c35fe16c7 Mon Sep 17 00:00:00 2001 From: Roman Zablodskyi Date: Wed, 20 Dec 2023 14:31:07 +0200 Subject: [PATCH 2/3] . --- app/Repositories/Attachment.php | 33 +++++++++++++++++++++++++++++++++ app/Services/Attachment.php | 32 -------------------------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/app/Repositories/Attachment.php b/app/Repositories/Attachment.php index dae8369..aba9aad 100644 --- a/app/Repositories/Attachment.php +++ b/app/Repositories/Attachment.php @@ -13,6 +13,7 @@ namespace Dam\Repositories; +use Atro\Core\PseudoTransactionManager; use Dam\Core\AssetValidator; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\Error; @@ -89,6 +90,12 @@ protected function init() parent::init(); $this->addDependency(AssetValidator::class); + $this->addDependency('pseudoTransactionManager'); + } + + protected function getPseudoTransactionManager(): PseudoTransactionManager + { + return $this->getInjection('pseudoTransactionManager'); } /** @@ -173,6 +180,32 @@ protected function afterRemove(Entity $entity, array $options = []) $this->getFileManager()->unlink($dirPath . '/page-1.png'); } + + if ($this->getMetadata()->get(['entityDefs', 'Asset', 'fields', 'file', 'required'], false)) { + $offset = 0; + $limit = 20; + + while(true) { + $assets = $this + ->getEntityManager() + ->getRepository('Asset') + ->where([ + 'fileId' => $entity->id + ]) + ->limit($offset, $limit) + ->find(); + + if (count($assets) == 0) { + break; + } + + foreach ($assets as $asset) { + $this->getPseudoTransactionManager()->pushDeleteEntityJob('Asset', $asset->id); + } + + $offset += $limit; + } + } } protected function isPdf(Entity $entity): bool diff --git a/app/Services/Attachment.php b/app/Services/Attachment.php index 8b641c8..fc22135 100644 --- a/app/Services/Attachment.php +++ b/app/Services/Attachment.php @@ -267,37 +267,6 @@ public function changeName(\Dam\Entities\Attachment $attachment, string $newName return $this->getRepository()->renameFile($attachment, $newName); } - public function afterDeleteEntity(Entity $entity) - { - parent::afterDeleteEntity($entity); - - if ($this->getMetadata()->get(['entityDefs', 'Asset', 'fields', 'file', 'required'], false)) { - $offset = 0; - $limit = 20; - - while(true) { - $assets = $this - ->getEntityManager() - ->getRepository('Asset') - ->where([ - 'fileId' => $entity->id - ]) - ->limit($offset, $limit) - ->find(); - - if (count($assets) == 0) { - break; - } - - foreach ($assets as $asset) { - $this->getPseudoTransactionManager()->pushDeleteEntityJob('Asset', $asset->id); - } - - $offset += $limit; - } - } - } - /** * @inheritDoc */ @@ -305,7 +274,6 @@ protected function init() { parent::init(); - $this->addDependency('fileStorageManager'); $this->addDependency(AssetValidator::class); } From 80d1aaa8ce9bdfd2f765ec88c6ef609f0f3901cd Mon Sep 17 00:00:00 2001 From: Roman Zablodskyi Date: Wed, 20 Dec 2023 14:31:43 +0200 Subject: [PATCH 3/3] . --- app/Services/Attachment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Services/Attachment.php b/app/Services/Attachment.php index fc22135..81e198f 100644 --- a/app/Services/Attachment.php +++ b/app/Services/Attachment.php @@ -274,6 +274,7 @@ protected function init() { parent::init(); + $this->addDependency('fileStorageManager'); $this->addDependency(AssetValidator::class); }