file field processing

This commit is contained in:
Yuri Kuznetsov
2021-04-21 12:21:08 +03:00
parent fa758c51ee
commit c004beab02
4 changed files with 228 additions and 75 deletions
@@ -0,0 +1,148 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldProcessing\File;
use Espo\Core\{
ORM\Entity,
ORM\EntityManager,
Utils\Metadata,
};
class SaveProcessor
{
private $entityManager;
private $metadata;
private $fieldListMapCache = [];
public function __construct(
EntityManager $entityManager,
Metadata $metadata
) {
$this->entityManager = $entityManager;
$this->metadata = $metadata;
}
public function process(Entity $entity, array $options): void
{
foreach ($this->getFieldList($entity->getEntityType()) as $name) {
$this->processItem($entity, $name);
}
if ($entity->isNew()) {
return;
}
}
private function processItem(Entity $entity, string $name): void
{
$attribute = $name . 'Id';
if (!$entity->get($attribute)) {
return;
}
if (!$entity->isAttributeChanged($attribute)) {
return;
}
$attachment = $this->entityManager->getEntity('Attachment', $entity->get($attribute));
if (!$attachment) {
return;
}
$attachment->set([
'relatedId' => $entity->getId(),
'relatedType' => $entity->getEntityType(),
]);
$this->entityManager->saveEntity($attachment);
if ($entity->isNew()) {
return;
}
$previousAttachmentId = $entity->getFetched($attribute);
if (!$previousAttachmentId) {
return;
}
$previousAttachment = $this->entityManager->getEntity('Attachment', $previousAttachmentId);
if (!$previousAttachment) {
return;
}
$this->entityManager->removeEntity($previousAttachment);
}
private function getFieldList(string $entityType): array
{
if (array_key_exists($entityType, $this->fieldListMapCache)) {
return $this->fieldListMapCache[$entityType];
}
$entityDefs = $this->entityManager
->getDefs()
->getEntity($entityType);
$list = [];
foreach ($entityDefs->getRelationNameList() as $name) {
$defs = $entityDefs->getRelation($name);
$type = $defs->getType();
if (!$defs->hasForeignEntityType()) {
continue;
}
$foreignEntityType = $defs->getForeignEntityType();
if ($type !== Entity::BELONGS_TO || $foreignEntityType !== 'Attachment') {
continue;
}
if (!$entityDefs->hasAttribute($name . 'Id')) {
continue;
}
$list[] = $name;
}
$this->fieldListMapCache[$entityType] = $list;
return $list;
}
}
@@ -35,6 +35,7 @@ use Espo\Core\{
FieldProcessing\EmailAddress\SaveProcessor as EmailAddressSaveProcessor,
FieldProcessing\PhoneNumber\SaveProcessor as PhoneNumberSaveProcessor,
FieldProcessing\Relation\SaveProcessor as RelationSaveProcessor,
FieldProcessing\File\SaveProcessor as FileSaveProcessor,
};
/**
@@ -48,14 +49,18 @@ class SaveProcessor
private $relationSaveProcessor;
private $fileSaveProcessor;
public function __construct(
EmailAddressSaveProcessor $emailAddressSaveProcessor,
PhoneNumberSaveProcessor $phoneNumberSaveProcessor,
RelationSaveProcessor $relationSaveProcessor
RelationSaveProcessor $relationSaveProcessor,
FileSaveProcessor $fileSaveProcessor
) {
$this->emailAddressSaveProcessor = $emailAddressSaveProcessor;
$this->phoneNumberSaveProcessor = $phoneNumberSaveProcessor;
$this->relationSaveProcessor = $relationSaveProcessor;
$this->fileSaveProcessor = $fileSaveProcessor;
}
public function process(Entity $entity, array $options): void
@@ -63,5 +68,6 @@ class SaveProcessor
$this->emailAddressSaveProcessor->process($entity, $options);
$this->phoneNumberSaveProcessor->process($entity, $options);
$this->relationSaveProcessor->process($entity, $options);
$this->fileSaveProcessor->process($entity, $options);
}
}
@@ -247,7 +247,6 @@ class Database extends RDBRepository
parent::afterSave($entity, $options);
if (!$this->processFieldsAfterSaveDisabled) {
$this->processFileFieldsSave($entity);
$this->processArrayFieldsSave($entity);
$this->processWysiwygFieldsSave($entity);
}
@@ -315,79 +314,6 @@ class Database extends RDBRepository
}
}
protected function processFileFieldsSave(Entity $entity)
{
$entityDefs = $this->entityManager
->getDefs()
->getEntity($entity->getEntityType());
foreach ($entity->getRelationList() as $name) {
$defs = $entityDefs->getRelation($name);
$type = $defs->getType();
if (!$defs->hasForeignEntityType()) {
continue;
}
$foreignEntityType = $defs->getForeignEntityType();
if (!($type === $entity::BELONGS_TO && $foreignEntityType === 'Attachment')) {
continue;
}
$attribute = $name . 'Id';
if (!$entity->hasAttribute($attribute)) {
continue;
}
if (!$entity->get($attribute)) {
continue;
}
if (!$entity->isAttributeChanged($attribute)) {
continue;
}
$attachment = $this->getEntityManager()->getEntity('Attachment', $entity->get($attribute));
if (!$attachment) {
continue;
}
$attachment->set([
'relatedId' => $entity->id,
'relatedType' => $entity->getEntityType(),
]);
$this->getEntityManager()->saveEntity($attachment);
}
if ($entity->isNew()) {
return;
}
foreach ($this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields']) as $name => $defs) {
if (!empty($defs['type']) && in_array($defs['type'], ['file', 'image'])) {
$attribute = $name . 'Id';
if ($entity->isAttributeChanged($attribute)) {
$previousAttachmentId = $entity->getFetched($attribute);
if ($previousAttachmentId) {
$attachment = $this->getEntityManager()
->getEntity('Attachment', $previousAttachmentId);
if ($attachment) {
$this->getEntityManager()->removeEntity($attachment);
}
}
}
}
}
}
protected function processArrayFieldsSave(Entity $entity)
{
foreach ($entity->getAttributeList() as $attribute) {
@@ -0,0 +1,73 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\Core\FieldProcessing;
use Espo\Core\{
ORM\EntityManager,
};
class FileTest extends \tests\integration\Core\BaseTestCase
{
public function testFile1(): void
{
/* @var $entityManager EntityManager */
$entityManager = $this->getContainer()->get('entityManager');
$attachment1 = $entityManager->createEntity('Attachment', [
'contents' => 'test-1',
'relatedType' => 'Document',
]);
$document = $entityManager->createEntity('Document', [
'fileId' => $attachment1->getId(),
]);
$attachment1 = $entityManager->getEntity('Attachment', $attachment1->getId());
$this->assertEquals($document->getId(), $attachment1->get('relatedId'));
$attachment2 = $entityManager->createEntity('Attachment', [
'contents' => 'test-2',
'relatedType' => 'Document',
]);
$document->set('fileId', $attachment2->getId());
$entityManager->saveEntity($document);
$attachment2 = $entityManager->getEntity('Attachment', $attachment2->getId());
$this->assertEquals($document->getId(), $attachment2->get('relatedId'));
$attachment1 = $entityManager->getEntity('Attachment', $attachment1->getId());
$this->assertNull($attachment1);
}
}