ref
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2022 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\Modules\Crm\Services;
|
||||
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
|
||||
use Espo\ORM\Collection;
|
||||
use Espo\ORM\EntityCollection;
|
||||
use Espo\Repositories\Attachment as AttachmentRepository;
|
||||
use Espo\Entities\Attachment;
|
||||
|
||||
use Espo\Services\Record;
|
||||
|
||||
/**
|
||||
* @extends Record<\Espo\Modules\Crm\Entities\Document>
|
||||
*/
|
||||
class Document extends Record
|
||||
{
|
||||
/**
|
||||
* @return Collection<Attachment>
|
||||
* @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<Attachment> $attachmentList */
|
||||
$attachmentList = $this->entityManager
|
||||
->getCollectionFactory()
|
||||
->create('Attachment');
|
||||
|
||||
$attachmentList[] = $attachment;
|
||||
|
||||
return $attachmentList;
|
||||
}
|
||||
|
||||
private function getAttachmentRepository(): AttachmentRepository
|
||||
{
|
||||
/** @var AttachmentRepository */
|
||||
return $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2022 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\Modules\Crm\Tools\Document;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\Record\ServiceContainer;
|
||||
use Espo\Entities\Attachment;
|
||||
use Espo\Modules\Crm\Entities\Document;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Repositories\Attachment as AttachmentRepository;
|
||||
use Espo\Tools\Attachment\AccessChecker as AttachmentAccessChecker;
|
||||
use Espo\Tools\Attachment\FieldData;
|
||||
|
||||
class Service
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
private AttachmentAccessChecker $attachmentAccessChecker;
|
||||
private ServiceContainer $serviceContainer;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
AttachmentAccessChecker $attachmentAccessChecker,
|
||||
ServiceContainer $serviceContainer
|
||||
) {
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user