This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
espocrm-base/application/Espo/Hooks/Common/Notifications.php
T
Yuri Kuznetsov 810677014c options usage
2021-05-20 18:22:57 +03:00

212 lines
6.4 KiB
PHP

<?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\Hooks\Common;
use Espo\{
ORM\Entity,
Services\Stream as StreamService,
};
use Espo\Core\{
Utils\Metadata,
Utils\Config,
ORM\EntityManager,
ServiceFactory,
Notification\AssignmentNotificator,
Notification\AssignmentNotificatorFactory,
Notification\NotificatorParams,
};
use Espo\Entities\User;
class Notifications
{
public static $order = 10;
private $notifatorsHash = [];
private $streamService;
private $hasStreamCache = [];
private $metadata;
private $config;
private $entityManager;
private $serviceFactory;
private $notificatorFactory;
private $user;
public function __construct(
Metadata $metadata,
Config $config,
EntityManager $entityManager,
ServiceFactory $serviceFactory,
AssignmentNotificatorFactory $notificatorFactory,
User $user
) {
$this->metadata = $metadata;
$this->config = $config;
$this->entityManager = $entityManager;
$this->serviceFactory = $serviceFactory;
$this->notificatorFactory = $notificatorFactory;
$this->user = $user;
}
public function afterSave(Entity $entity, array $options = []): void
{
if (!empty($options['silent']) || !empty($options['noNotifications'])) {
return;
}
$entityType = $entity->getEntityType();
/**
* No need to process assignment notifications for entity types that have Stream enabled.
* Users are notified via Stream notifications.
*/
if ($this->checkHasStream($entityType) && !$entity->hasLinkMultipleField('assignedUsers')) {
return;
}
$assignmentNotificationsEntityList = $this->config->get('assignmentNotificationsEntityList', []);
if (!in_array($entityType, $assignmentNotificationsEntityList)) {
return;
}
$notificator = $this->getNotificator($entityType);
if (!$notificator instanceof AssignmentNotificator) {
// For backward compatiblity.
$notificator->process($entity, $options);
return;
}
$params = NotificatorParams::create()->withRawOptions($options);
$notificator->process($entity, $params);
}
public function beforeRemove(Entity $entity, array $options = []): void
{
if (!empty($options['silent']) || !empty($options['noNotifications'])) {
return;
}
$entityType = $entity->getEntityType();
if ($this->checkHasStream($entityType)) {
$followersData = $this->getStreamService()->getEntityFollowers($entity);
foreach ($followersData['idList'] as $userId) {
if ($userId === $this->user->id) {
continue;
}
$notification = $this->entityManager->getEntity('Notification');
$notification->set([
'userId' => $userId,
'type' => 'EntityRemoved',
'data' => [
'entityType' => $entity->getEntityType(),
'entityId' => $entity->id,
'entityName' => $entity->get('name'),
'userId' => $this->user->id,
'userName' => $this->user->get('name'),
],
]);
$this->entityManager->saveEntity($notification);
}
}
}
public function afterRemove(Entity $entity): void
{
$query = $this->entityManager->getQueryBuilder()
->delete()
->from('Notification')
->where([
'OR' => [
[
'relatedId' => $entity->id,
'relatedType' => $entity->getEntityType(),
],
[
'relatedParentId' => $entity->id,
'relatedParentType' => $entity->getEntityType(),
],
],
])
->build();
$this->entityManager->getQueryExecutor()->execute($query);
}
private function checkHasStream($entityType): bool
{
if (!array_key_exists($entityType, $this->hasStreamCache)) {
$this->hasStreamCache[$entityType] = (bool) $this->metadata->get("scopes.{$entityType}.stream");
}
return $this->hasStreamCache[$entityType];
}
/**
* @return AssignmentNotificator
*/
private function getNotificator(string $entityType): object
{
if (empty($this->notifatorsHash[$entityType])) {
$notificator = $this->notificatorFactory->create($entityType);
$this->notifatorsHash[$entityType] = $notificator;
}
return $this->notifatorsHash[$entityType];
}
private function getStreamService(): StreamService
{
if (empty($this->streamService)) {
$this->streamService = $this->serviceFactory->create('Stream');
}
return $this->streamService;
}
}