From 4917f0d5f614c3722e92faa3f0d2e1aa96f75ffd Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 20 Oct 2022 20:55:42 +0300 Subject: [PATCH] ref --- .../Espo/Modules/Crm/Controllers/Document.php | 48 ++++--- .../Espo/Modules/Crm/Entities/Document.php | 13 +- .../Espo/Modules/Crm/Services/Document.php | 90 ------------- .../Modules/Crm/Tools/Document/Service.php | 122 ++++++++++++++++++ application/Espo/Tools/Email/Service.php | 2 +- .../src/views/fields/attachment-multiple.js | 6 +- client/src/views/fields/file.js | 8 +- 7 files changed, 178 insertions(+), 111 deletions(-) delete mode 100644 application/Espo/Modules/Crm/Services/Document.php create mode 100644 application/Espo/Modules/Crm/Tools/Document/Service.php diff --git a/application/Espo/Modules/Crm/Controllers/Document.php b/application/Espo/Modules/Crm/Controllers/Document.php index 94de26bc55..77de0639cc 100644 --- a/application/Espo/Modules/Crm/Controllers/Document.php +++ b/application/Espo/Modules/Crm/Controllers/Document.php @@ -29,42 +29,58 @@ namespace Espo\Modules\Crm\Controllers; -use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Controllers\Record; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Api\Request; -use Espo\Modules\Crm\Services\Document as Service; +use Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\NotFound; +use Espo\Modules\Crm\Tools\Document\Service; +use Espo\Tools\Attachment\FieldData; +use stdClass; -class Document extends \Espo\Core\Controllers\Record +class Document extends Record { + /** - * @return \stdClass[] + * @return stdClass[] * @throws BadRequest * @throws Forbidden - * @throws \Espo\Core\Exceptions\NotFound + * @throws Error + * @throws NotFound */ public function postActionGetAttachmentList(Request $request): array { $data = $request->getParsedBody(); - if (empty($data->id)) { - throw new BadRequest(); + $id = $data->id ?? null; + $field = $data->field ?? null; + $parentType = $data->parentType ?? null; + $relatedType = $data->relatedType ?? null; + + if (!$id || !$field) { + throw new BadRequest("No `id` or `field`."); } - $id = $data->id; - - if (!$this->getAcl()->checkScope('Attachment', 'create')) { - throw new Forbidden(); + try { + $fieldData = new FieldData( + $field, + $parentType, + $relatedType + ); + } + catch (Error $e) { + throw new BadRequest($e->getMessage()); } - return $this->getDocumentService() - ->getAttachmentList($id) - ->getValueMapList(); + $attachment = $this->getDocumentService()->copyAttachment($id, $fieldData); + + return [$attachment->getValueMap()]; } private function getDocumentService(): Service { - /** @var Service */ - return $this->getRecordService(); + return $this->injectableFactory->create(Service::class); } } diff --git a/application/Espo/Modules/Crm/Entities/Document.php b/application/Espo/Modules/Crm/Entities/Document.php index 376a10121f..d38f5b3d10 100644 --- a/application/Espo/Modules/Crm/Entities/Document.php +++ b/application/Espo/Modules/Crm/Entities/Document.php @@ -31,11 +31,22 @@ namespace Espo\Modules\Crm\Entities; use Espo\Core\Field\Link; use Espo\Core\Field\LinkMultiple; +use Espo\Core\ORM\Entity; -class Document extends \Espo\Core\ORM\Entity +class Document extends Entity { public const ENTITY_TYPE = 'Document'; + public function getName(): ?string + { + return $this->get('name'); + } + + public function getFileId(): ?string + { + return $this->get('fileId'); + } + public function getAssignedUser(): ?Link { /** @var ?Link */ diff --git a/application/Espo/Modules/Crm/Services/Document.php b/application/Espo/Modules/Crm/Services/Document.php deleted file mode 100644 index c9b8eb6320..0000000000 --- a/application/Espo/Modules/Crm/Services/Document.php +++ /dev/null @@ -1,90 +0,0 @@ - - */ -class Document extends Record -{ - /** - * @return Collection - * @throws NotFound - * @throws Forbidden - */ - public function getAttachmentList(string $id) - { - $entity = $this->getEntity($id); - - if (!$entity) { - throw new NotFound(); - } - - $fileId = $entity->get('fileId'); - - if (!$fileId) { - throw new NotFound(); - } - - $file = $this->getEntityManager()->getEntity('Attachment', $fileId); - - if (!$file) { - throw new NotFound(); - } - - - $attachment = $this->getAttachmentRepository()->getCopiedAttachment($file, 'Attachment'); - - /** @var EntityCollection $attachmentList */ - $attachmentList = $this->entityManager - ->getCollectionFactory() - ->create('Attachment'); - - $attachmentList[] = $attachment; - - return $attachmentList; - } - - private function getAttachmentRepository(): AttachmentRepository - { - /** @var AttachmentRepository */ - return $this->entityManager->getRepository(Attachment::ENTITY_TYPE); - } -} diff --git a/application/Espo/Modules/Crm/Tools/Document/Service.php b/application/Espo/Modules/Crm/Tools/Document/Service.php new file mode 100644 index 0000000000..69b97ca304 --- /dev/null +++ b/application/Espo/Modules/Crm/Tools/Document/Service.php @@ -0,0 +1,122 @@ +entityManager = $entityManager; + $this->attachmentAccessChecker = $attachmentAccessChecker; + $this->serviceContainer = $serviceContainer; + } + + /** + * Copy an attachment for re-using (e.g. in an email). + * + * @throws NotFound + * @throws Forbidden + * @throws Error + */ + public function copyAttachment(string $id, FieldData $fieldData): Attachment + { + /** @var ?Document $entity */ + $entity = $this->serviceContainer + ->get(Document::ENTITY_TYPE) + ->getEntity($id); + + if (!$entity) { + throw new NotFound(); + } + + $this->attachmentAccessChecker->check($fieldData); + + $attachmentId = $entity->getFileId(); + + if (!$attachmentId) { + throw new Error("No file."); + } + + $attachment = $this->copyAttachmentById($attachmentId, $fieldData); + + if (!$attachment) { + throw new Error("No file."); + } + + return $attachment; + } + + private function copyAttachmentById(string $attachmentId, FieldData $fieldData): ?Attachment + { + /** @var ?Attachment $attachment */ + $attachment = $this->entityManager + ->getRDBRepositoryByClass(Attachment::class) + ->getById($attachmentId); + + if (!$attachment) { + return null; + } + + $copied = $this->getAttachmentRepository()->getCopiedAttachment($attachment); + + $copied->set('parentType', $fieldData->getParentType()); + $copied->set('relatedType', $fieldData->getRelatedType()); + $copied->setTargetField($fieldData->getField()); + $copied->setRole(Attachment::ROLE_ATTACHMENT); + + $this->getAttachmentRepository()->save($copied); + + return $copied; + } + + private function getAttachmentRepository(): AttachmentRepository + { + /** @var AttachmentRepository */ + return $this->entityManager->getRepositoryByClass(Attachment::class); + } +} diff --git a/application/Espo/Tools/Email/Service.php b/application/Espo/Tools/Email/Service.php index c22101fe15..dd87fa6386 100644 --- a/application/Espo/Tools/Email/Service.php +++ b/application/Espo/Tools/Email/Service.php @@ -56,7 +56,7 @@ class Service } /** - * Copy article attachments for re-using (e.g. in an email). + * Copy email attachments for re-using (e.g. in an forward email). * * @return Attachment[] * @throws NotFound diff --git a/client/src/views/fields/attachment-multiple.js b/client/src/views/fields/attachment-multiple.js index 7579742363..0ee34a3c85 100644 --- a/client/src/views/fields/attachment-multiple.js +++ b/client/src/views/fields/attachment-multiple.js @@ -836,7 +836,11 @@ function (Dep, FileUpload) { } Espo.Ajax - .postRequest(source + '/action/getAttachmentList', {id: model.id}) + .postRequest(source + '/action/getAttachmentList', { + id: model.id, + field: this.name, + parentType: this.entityType, + }) .then(attachmentList => { attachmentList.forEach(item => { this.getModelFactory().create('Attachment', attachment => { diff --git a/client/src/views/fields/file.js b/client/src/views/fields/file.js index 7660404220..2e0f815efc 100644 --- a/client/src/views/fields/file.js +++ b/client/src/views/fields/file.js @@ -759,7 +759,7 @@ define('views/fields/file', ['views/fields/link', 'helpers/file-upload'], functi modelList = [modelList]; } - modelList.forEach((model) => { + modelList.forEach(model => { if (model.name === 'Attachment') { this.setAttachment(model); @@ -767,7 +767,11 @@ define('views/fields/file', ['views/fields/link', 'helpers/file-upload'], functi } Espo.Ajax - .postRequest(source + '/action/getAttachmentList', {id: model.id}) + .postRequest(source + '/action/getAttachmentList', { + id: model.id, + field: this.name, + relatedType: this.entityType, + }) .then(attachmentList => { attachmentList.forEach(item => { this.getModelFactory().create('Attachment', (attachment) => {