Initial commit
This commit is contained in:
46
application/Espo/Core/Notification/AssignmentNotificator.php
Normal file
46
application/Espo/Core/Notification/AssignmentNotificator.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Notification\AssignmentNotificator\Params;
|
||||
|
||||
/**
|
||||
* Processes assignment notifications. Called after entity is saved.
|
||||
*
|
||||
* @template TEntity of Entity
|
||||
*/
|
||||
interface AssignmentNotificator
|
||||
{
|
||||
/**
|
||||
* @param TEntity $entity
|
||||
*/
|
||||
public function process(Entity $entity, Params $params): void;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification\AssignmentNotificator;
|
||||
|
||||
/**
|
||||
* Immutable.
|
||||
*/
|
||||
class Params
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private $options = [];
|
||||
|
||||
private ?string $actionId = null;
|
||||
|
||||
/**
|
||||
* Whether an option is set.
|
||||
*/
|
||||
public function hasOption(string $option): bool
|
||||
{
|
||||
return array_key_exists($option, $this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an option.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOption(string $option)
|
||||
{
|
||||
return $this->options[$option] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getRawOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function withRawOptions(array $options): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
|
||||
$obj->options = $options;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with an option.
|
||||
*
|
||||
* @since 9.0.0
|
||||
*/
|
||||
public function withOption(string $option, mixed $value): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->options[$option] = $value;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance.
|
||||
*/
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 9.2.0
|
||||
*/
|
||||
public function withActionId(?string $actionId): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->actionId = $actionId;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 9.2.0
|
||||
*/
|
||||
public function getActionId(): ?string
|
||||
{
|
||||
return $this->actionId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification;
|
||||
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Core\Utils\ClassFinder;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\Repository\Util as RepositoryUtil;
|
||||
|
||||
class AssignmentNotificatorFactory
|
||||
{
|
||||
/** @var class-string<AssignmentNotificator<Entity>> */
|
||||
protected string $defaultClassName = DefaultAssignmentNotificator::class;
|
||||
|
||||
public function __construct(
|
||||
private InjectableFactory $injectableFactory,
|
||||
private ClassFinder $classFinder,
|
||||
private Metadata $metadata
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @template T of Entity
|
||||
* @param class-string<T> $className An entity class name.
|
||||
* @return AssignmentNotificator<T>
|
||||
*/
|
||||
public function createByClass(string $className): AssignmentNotificator
|
||||
{
|
||||
$entityType = RepositoryUtil::getEntityTypeByClass($className);
|
||||
|
||||
/** @var AssignmentNotificator<T> */
|
||||
return $this->create($entityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Change return type to AssignmentNotificator.
|
||||
*
|
||||
* @return AssignmentNotificator<Entity>
|
||||
*/
|
||||
public function create(string $entityType): object // AssignmentNotificator
|
||||
{
|
||||
$className = $this->getClassName($entityType);
|
||||
|
||||
return $this->injectableFactory->create($className);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string<AssignmentNotificator<Entity>>
|
||||
*/
|
||||
private function getClassName(string $entityType): string
|
||||
{
|
||||
/** @var ?class-string<AssignmentNotificator<Entity>> $className1 */
|
||||
$className1 = $this->metadata->get(['notificationDefs', $entityType, 'assignmentNotificatorClassName']);
|
||||
|
||||
if ($className1) {
|
||||
return $className1;
|
||||
}
|
||||
|
||||
/* For backward compatibility. */
|
||||
/** @var ?class-string<AssignmentNotificator<Entity>> $className2 */
|
||||
$className2 = $this->classFinder->find('Notificators', $entityType);
|
||||
|
||||
if ($className2) {
|
||||
return $className2;
|
||||
}
|
||||
|
||||
return $this->defaultClassName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification;
|
||||
|
||||
use Espo\Core\Field\LinkParent;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Core\ORM\Entity as CoreEntity;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\Notification;
|
||||
use Espo\Core\Notification\AssignmentNotificator\Params;
|
||||
|
||||
/**
|
||||
* @implements AssignmentNotificator<Entity>
|
||||
*/
|
||||
class DefaultAssignmentNotificator implements AssignmentNotificator
|
||||
{
|
||||
public const OPTION_FORCE_ASSIGNED_USER = 'forceAssignedUser';
|
||||
|
||||
private const FIELD_ASSIGNED_USERS = Field::ASSIGNED_USERS;
|
||||
private const ATTR_ASSIGNED_USER_ID = 'assignedUserId';
|
||||
|
||||
public function __construct(
|
||||
protected User $user,
|
||||
protected EntityManager $entityManager,
|
||||
protected UserEnabledChecker $userChecker
|
||||
) {}
|
||||
|
||||
public function process(Entity $entity, Params $params): void
|
||||
{
|
||||
if (!$entity instanceof CoreEntity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
$entity->hasLinkMultipleField(self::FIELD_ASSIGNED_USERS) &&
|
||||
!$params->getOption(self::OPTION_FORCE_ASSIGNED_USER)
|
||||
) {
|
||||
$userIds = $entity->getLinkMultipleIdList(self::FIELD_ASSIGNED_USERS);
|
||||
/** @var string[] $fetchedIds */
|
||||
$fetchedIds = $entity->getFetched(self::FIELD_ASSIGNED_USERS . 'Ids') ?? [];
|
||||
|
||||
foreach ($userIds as $userId) {
|
||||
if (in_array($userId, $fetchedIds)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->processForUser($entity, $userId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$entity->get(self::ATTR_ASSIGNED_USER_ID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$entity->isAttributeChanged(self::ATTR_ASSIGNED_USER_ID)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$assignedUserId = $entity->get(self::ATTR_ASSIGNED_USER_ID);
|
||||
|
||||
$this->processForUser($entity, $assignedUserId);
|
||||
}
|
||||
|
||||
protected function processForUser(Entity $entity, string $assignedUserId): void
|
||||
{
|
||||
if (!$this->userChecker->checkAssignment($entity->getEntityType(), $assignedUserId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->hasAttribute('createdById') && $entity->hasAttribute('modifiedById')) {
|
||||
$isSelfAssignment = $entity->isNew() ?
|
||||
$assignedUserId === $entity->get('createdById') :
|
||||
$assignedUserId === $entity->get('modifiedById');
|
||||
|
||||
if ($isSelfAssignment) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$isSelfAssignment = $assignedUserId === $this->user->getId();
|
||||
|
||||
if ($isSelfAssignment) {
|
||||
return;
|
||||
}
|
||||
|
||||
$notification = $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew();
|
||||
|
||||
$notification
|
||||
->setType(Notification::TYPE_ASSIGN)
|
||||
->setUserId($assignedUserId)
|
||||
->setData([
|
||||
'entityType' => $entity->getEntityType(),
|
||||
'entityId' => $entity->getId(),
|
||||
'entityName' => $entity->get(Field::NAME),
|
||||
'isNew' => $entity->isNew(),
|
||||
'userId' => $this->user->getId(),
|
||||
'userName' => $this->user->getName(),
|
||||
])
|
||||
->setRelated(LinkParent::createFromEntity($entity));
|
||||
|
||||
$this->entityManager->saveEntity($notification);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification;
|
||||
|
||||
use Espo\Core\Mail\SenderParams;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\Email;
|
||||
|
||||
/**
|
||||
* Handles a notification emails (supposed for adding CC, BCC addresses).
|
||||
* Provides sender parameters for notification emails (e.g. setting Reply-To address).
|
||||
*/
|
||||
interface EmailNotificationHandler
|
||||
{
|
||||
public function prepareEmail(Email $email, Entity $entity, User $user): void;
|
||||
|
||||
public function getSenderParams(Entity $entity, User $user): ?SenderParams;
|
||||
}
|
||||
76
application/Espo/Core/Notification/UserEnabledChecker.php
Normal file
76
application/Espo/Core/Notification/UserEnabledChecker.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* 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\Core\Notification;
|
||||
|
||||
use Espo\Core\ORM\EntityManager;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Entities\Preferences;
|
||||
|
||||
class UserEnabledChecker
|
||||
{
|
||||
/** @var array<string, bool> */
|
||||
private $assignmentCache = [];
|
||||
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private Config $config,
|
||||
) {}
|
||||
|
||||
public function checkAssignment(string $entityType, string $userId): bool
|
||||
{
|
||||
if (!in_array($entityType, $this->config->get('assignmentNotificationsEntityList', []))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = $entityType . '_' . $userId;
|
||||
|
||||
if (!array_key_exists($key, $this->assignmentCache)) {
|
||||
$preferences = $this->entityManager->getEntityById(Preferences::ENTITY_TYPE, $userId);
|
||||
|
||||
$isEnabled = false;
|
||||
|
||||
$ignoreList = [];
|
||||
|
||||
if ($preferences) {
|
||||
$isEnabled = true;
|
||||
|
||||
$ignoreList = $preferences->get('assignmentNotificationsIgnoreEntityTypeList') ?? [];
|
||||
}
|
||||
|
||||
if ($preferences && in_array($entityType, $ignoreList)) {
|
||||
$isEnabled = false;
|
||||
}
|
||||
|
||||
$this->assignmentCache[$key] = $isEnabled;
|
||||
}
|
||||
|
||||
return $this->assignmentCache[$key];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user