Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #128 from atrocore/attachments-removing
Browse files Browse the repository at this point in the history
Added removing related assets when remove attachment
  • Loading branch information
rratsun committed Dec 20, 2023
2 parents 37dac0c + 80d1aaa commit 56b84d5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/Repositories/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,6 +90,12 @@ protected function init()
parent::init();

$this->addDependency(AssetValidator::class);
$this->addDependency('pseudoTransactionManager');
}

protected function getPseudoTransactionManager(): PseudoTransactionManager
{
return $this->getInjection('pseudoTransactionManager');
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 56b84d5

Please sign in to comment.