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

Added removing related assets when remove attachment #128

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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