target list ref
This commit is contained in:
@@ -31,79 +31,92 @@ namespace Espo\Modules\Crm\Controllers;
|
||||
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Modules\Crm\Services\TargetList as Service;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\Controllers\Record;
|
||||
use Espo\Modules\Crm\Tools\TargetList\OptOutService;
|
||||
use Espo\Modules\Crm\Tools\TargetList\RecordService;
|
||||
|
||||
class TargetList extends Record
|
||||
{
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws Forbidden
|
||||
* @throws NotFound
|
||||
*/
|
||||
public function postActionUnlinkAll(Request $request): bool
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
if (empty($data->id)) {
|
||||
$id = $data->id ?? null;
|
||||
$link = $data->link ?? null;
|
||||
|
||||
if (
|
||||
!is_string($id) ||
|
||||
!is_string($link)
|
||||
) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data->link)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
$this->getTargetListService()->unlinkAll($data->id, $data->link);
|
||||
$this->injectableFactory->create(RecordService::class)->unlinkAll($id, $link);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws NotFound
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function postActionOptOut(Request $request): bool
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
if (empty($data->id)) {
|
||||
$id = $data->id ?? null;
|
||||
$targetType = $data->targetType ?? null;
|
||||
$targetId = $data->targetId ?? null;
|
||||
|
||||
if (
|
||||
!is_string($id) ||
|
||||
!is_string($targetType) ||
|
||||
!is_string($targetId)
|
||||
) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data->targetType)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data->targetId)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
$data->id = strval($data->id);
|
||||
$data->targetId = strval($data->targetId);
|
||||
|
||||
$this->getTargetListService()->optOut($data->id, $data->targetType, $data->targetId);
|
||||
$this->getOptOutService()->optOut($id, $targetType, $targetId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws NotFound
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function postActionCancelOptOut(Request $request): bool
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
if (empty($data->id)) {
|
||||
$id = $data->id ?? null;
|
||||
$targetType = $data->targetType ?? null;
|
||||
$targetId = $data->targetId ?? null;
|
||||
|
||||
if (
|
||||
!is_string($id) ||
|
||||
!is_string($targetType) ||
|
||||
!is_string($targetId)
|
||||
) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data->targetType)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (empty($data->targetId)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
$data->id = strval($data->id);
|
||||
$data->targetId = strval($data->targetId);
|
||||
|
||||
$this->getTargetListService()->cancelOptOut($data->id, $data->targetType, $data->targetId);
|
||||
$this->getOptOutService()->cancelOptOut($id, $targetType, $targetId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function getTargetListService(): Service
|
||||
private function getOptOutService(): OptOutService
|
||||
{
|
||||
/** @var Service */
|
||||
return $this->getRecordService();
|
||||
return $this->injectableFactory->create(OptOutService::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,180 +29,31 @@
|
||||
|
||||
namespace Espo\Modules\Crm\Services;
|
||||
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Modules\Crm\Entities\TargetList as TargetListEntity;
|
||||
use Espo\Services\Record;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Core\Di;
|
||||
|
||||
/**
|
||||
* @extends Record<TargetListEntity>
|
||||
*/
|
||||
class TargetList extends Record implements
|
||||
|
||||
Di\HookManagerAware
|
||||
class TargetList extends Record
|
||||
{
|
||||
use Di\HookManagerSetter;
|
||||
|
||||
/** @var string[] */
|
||||
protected array $targetLinkList = [];
|
||||
/** @var array<string, string> */
|
||||
protected array $entityTypeLinkMap = [];
|
||||
|
||||
public function setMetadata(Metadata $metadata): void
|
||||
{
|
||||
parent::setMetadata($metadata);
|
||||
|
||||
$this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
|
||||
$targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
|
||||
|
||||
$this->duplicatingLinkList = $this->targetLinkList;
|
||||
$this->noEditAccessRequiredLinkList = $this->targetLinkList;
|
||||
$this->duplicatingLinkList = $targetLinkList;
|
||||
$this->noEditAccessRequiredLinkList = $targetLinkList;
|
||||
|
||||
foreach ($this->targetLinkList as $link) {
|
||||
foreach ($targetLinkList as $link) {
|
||||
/** @var string $link */
|
||||
$this->linkMandatorySelectAttributeList[$link] = ['targetListIsOptedOut'];
|
||||
|
||||
$entityType = $this->entityManager
|
||||
->getDefs()
|
||||
->getEntity(TargetListEntity::ENTITY_TYPE)
|
||||
->getRelation($link)
|
||||
->getForeignEntityType();
|
||||
|
||||
$this->entityTypeLinkMap[$entityType] = $link;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Forbidden
|
||||
* @throws Error
|
||||
* @throws NotFound
|
||||
* @todo Move.
|
||||
*/
|
||||
public function unlinkAll(string $id, string $link): void
|
||||
{
|
||||
/** @var ?TargetListEntity $entity */
|
||||
$entity = $this->getRepository()->getById($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
if (!$this->acl->check($entity, Table::ACTION_EDIT)) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$foreignEntityType = $entity->getRelationParam($link, 'entity');
|
||||
|
||||
if (!$foreignEntityType) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$linkEntityType = ucfirst(
|
||||
$entity->getRelationParam($link, 'relationName') ?? ''
|
||||
);
|
||||
|
||||
if ($linkEntityType === '') {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$updateQuery = $this->entityManager->getQueryBuilder()
|
||||
->update()
|
||||
->in($linkEntityType)
|
||||
->set(['deleted' => true])
|
||||
->where(['targetListId' => $entity->getId()])
|
||||
->build();
|
||||
|
||||
$this->entityManager->getQueryExecutor()->execute($updateQuery);
|
||||
|
||||
$this->hookManager->process('TargetList', 'afterUnlinkAll', $entity, [], ['link' => $link]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFound
|
||||
* @throws Error
|
||||
* @todo Move. Use Tools\TargetList\MetadataProvider.
|
||||
*/
|
||||
public function optOut(string $id, string $targetType, string $targetId): void
|
||||
{
|
||||
$targetList = $this->entityManager->getEntityById(TargetListEntity::ENTITY_TYPE, $id);
|
||||
|
||||
if (!$targetList) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$target = $this->entityManager->getEntity($targetType, $targetId);
|
||||
|
||||
if (!$target) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$map = $this->entityTypeLinkMap;
|
||||
|
||||
if (empty($map[$targetType])) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$link = $map[$targetType];
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepository(TargetListEntity::ENTITY_TYPE)
|
||||
->getRelation($targetList, $link)
|
||||
->relateById($targetId, ['optedOut' => true]);
|
||||
|
||||
$hookData = [
|
||||
'link' => $link,
|
||||
'targetId' => $targetId,
|
||||
'targetType' => $targetType,
|
||||
];
|
||||
|
||||
$this->hookManager->process('TargetList', 'afterOptOut', $targetList, [], $hookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFound
|
||||
* @throws Error
|
||||
* @todo Move. Use Tools\TargetList\MetadataProvider.
|
||||
*/
|
||||
public function cancelOptOut(string $id, string $targetType, string $targetId): void
|
||||
{
|
||||
$targetList = $this->entityManager->getEntityById(TargetListEntity::ENTITY_TYPE, $id);
|
||||
|
||||
if (!$targetList) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$target = $this->entityManager->getEntityById($targetType, $targetId);
|
||||
|
||||
if (!$target) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$map = $this->entityTypeLinkMap;
|
||||
|
||||
if (empty($map[$targetType])) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
$link = $map[$targetType];
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepository(TargetListEntity::ENTITY_TYPE)
|
||||
->getRelation($targetList, $link)
|
||||
->updateColumnsById($targetId, ['optedOut' => false]);
|
||||
|
||||
$hookData = [
|
||||
'link' => $link,
|
||||
'targetId' => $targetId,
|
||||
'targetType' => $targetType,
|
||||
];
|
||||
|
||||
$this->hookManager->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Don't use additionalColumnsConditions.
|
||||
*/
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace Espo\Modules\Crm\Tools\TargetList;
|
||||
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\HookManager;
|
||||
use Espo\Core\Record\Collection;
|
||||
use Espo\Core\Record\Collection as RecordCollection;
|
||||
use Espo\Core\Record\EntityProvider;
|
||||
@@ -48,9 +49,86 @@ class OptOutService
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private MetadataProvider $metadataProvider,
|
||||
private EntityProvider $entityProvider
|
||||
private EntityProvider $entityProvider,
|
||||
private HookManager $hookManager
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Opt out a target.
|
||||
*
|
||||
* @throws Forbidden
|
||||
* @throws NotFound
|
||||
*/
|
||||
public function optOut(string $id, string $targetType, string $targetId): void
|
||||
{
|
||||
$targetList = $this->entityProvider->get(TargetList::class, $id);
|
||||
|
||||
$target = $this->entityManager->getEntityById($targetType, $targetId);
|
||||
|
||||
if (!$target) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$map = $this->metadataProvider->getEntityTypeLinkMap();
|
||||
|
||||
if (empty($map[$targetType])) {
|
||||
throw new Forbidden("Not supported target type.");
|
||||
}
|
||||
|
||||
$link = $map[$targetType];
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepository(TargetList::ENTITY_TYPE)
|
||||
->getRelation($targetList, $link)
|
||||
->relateById($targetId, ['optedOut' => true]);
|
||||
|
||||
$hookData = [
|
||||
'link' => $link,
|
||||
'targetId' => $targetId,
|
||||
'targetType' => $targetType,
|
||||
];
|
||||
|
||||
$this->hookManager->process(TargetList::ENTITY_TYPE, 'afterOptOut', $targetList, [], $hookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel opt-out for a target.
|
||||
*
|
||||
* @throws Forbidden
|
||||
* @throws NotFound
|
||||
*/
|
||||
public function cancelOptOut(string $id, string $targetType, string $targetId): void
|
||||
{
|
||||
$targetList = $this->entityProvider->get(TargetList::class, $id);
|
||||
|
||||
$target = $this->entityManager->getEntityById($targetType, $targetId);
|
||||
|
||||
if (!$target) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$map = $this->metadataProvider->getEntityTypeLinkMap();
|
||||
|
||||
if (empty($map[$targetType])) {
|
||||
throw new Forbidden("Not supported target type.");
|
||||
}
|
||||
|
||||
$link = $map[$targetType];
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepository(TargetList::ENTITY_TYPE)
|
||||
->getRelation($targetList, $link)
|
||||
->updateColumnsById($targetId, ['optedOut' => false]);
|
||||
|
||||
$hookData = [
|
||||
'link' => $link,
|
||||
'targetId' => $targetId,
|
||||
'targetType' => $targetType,
|
||||
];
|
||||
|
||||
$this->hookManager->process('TargetList', TargetList::ENTITY_TYPE, $targetList, [], $hookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find opted out targets in a target list.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Crm\Tools\TargetList;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\HookManager;
|
||||
use Espo\Modules\Crm\Entities\TargetList;
|
||||
use Espo\ORM\EntityManager;
|
||||
use RuntimeException;
|
||||
|
||||
class RecordService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private Acl $acl,
|
||||
private HookManager $hookManager,
|
||||
private MetadataProvider $metadataProvider
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Unlink all targets.
|
||||
*
|
||||
* @throws Forbidden
|
||||
* @throws NotFound
|
||||
* @throws BadRequest
|
||||
*/
|
||||
public function unlinkAll(string $id, string $link): void
|
||||
{
|
||||
$entity = $this->getEntity($id);
|
||||
|
||||
$linkEntityType = $this->getLinkEntityType($entity, $link);
|
||||
|
||||
$updateQuery = $this->entityManager->getQueryBuilder()
|
||||
->update()
|
||||
->in($linkEntityType)
|
||||
->set(['deleted' => true])
|
||||
->where(['targetListId' => $entity->getId()])
|
||||
->build();
|
||||
|
||||
$this->entityManager->getQueryExecutor()->execute($updateQuery);
|
||||
|
||||
$this->hookManager->process(TargetList::ENTITY_TYPE, 'afterUnlinkAll', $entity, [], ['link' => $link]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Forbidden
|
||||
* @throws NotFound
|
||||
*/
|
||||
private function getEntity(string $id): TargetList
|
||||
{
|
||||
$entity = $this->entityManager->getRDBRepositoryByClass(TargetList::class)->getById($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
if (!$this->acl->check($entity, Table::ACTION_EDIT)) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
*/
|
||||
private function getLinkEntityType(TargetList $entity, string $link): string
|
||||
{
|
||||
if (!in_array($link, $this->metadataProvider->getTargetLinkList())) {
|
||||
throw new BadRequest("Not supported link.");
|
||||
}
|
||||
|
||||
$foreignEntityType = $entity->getRelationParam($link, 'entity');
|
||||
|
||||
if (!$foreignEntityType) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
$linkEntityType = ucfirst($entity->getRelationParam($link, 'relationName') ?? '');
|
||||
|
||||
if ($linkEntityType === '') {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
return $linkEntityType;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user