diff --git a/application/Espo/Core/Notificators/Base.php b/application/Espo/Core/Notificators/Base.php index 5a62845020..9463502c72 100644 --- a/application/Espo/Core/Notificators/Base.php +++ b/application/Espo/Core/Notificators/Base.php @@ -92,11 +92,27 @@ class Base implements Injectable public function process(Entity $entity) { - if (!$entity->get('assignedUserId')) return; - if (!$entity->isAttributeChanged('assignedUserId')) return; + if ($entity->hasLinkMultipleField('assignedUsers')) { + $userIdList = $entity->getLinkMultipleIdList('assignedUsers'); + $fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds'); + if (!is_array($fetchedAssignedUserIdList)) { + $fetchedAssignedUserIdList = []; + } - $assignedUserId = $entity->get('assignedUserId'); + foreach ($userIdList as $userId) { + if (in_array($userId, $fetchedAssignedUserIdList)) continue; + $this->processForUser($entity, $userId); + } + } else { + if (!$entity->get('assignedUserId')) return; + if (!$entity->isAttributeChanged('assignedUserId')) return; + $assignedUserId = $entity->get('assignedUserId'); + $this->processForUser($entity, $assignedUserId); + } + } + protected function processForUser(Entity $entity, $assignedUserId) + { if ($entity->hasAttribute('createdById') && $entity->hasAttribute('modifiedById')) { if ($entity->isNew()) { $isNotSelfAssignment = $assignedUserId !== $entity->get('createdById'); diff --git a/application/Espo/Hooks/Common/AssignmentEmailNotification.php b/application/Espo/Hooks/Common/AssignmentEmailNotification.php index 8d9a028216..87ea099319 100644 --- a/application/Espo/Hooks/Common/AssignmentEmailNotification.php +++ b/application/Espo/Hooks/Common/AssignmentEmailNotification.php @@ -41,27 +41,49 @@ class AssignmentEmailNotification extends \Espo\Core\Hooks\Base if ( $this->getConfig()->get('assignmentEmailNotifications') && - $entity->has('assignedUserId') + ( + $entity->has('assignedUserId') + || + $entity->hasLinkMultipleField('assignedUsers') && $entity->has('assignedUsersIds') + ) && in_array($entity->getEntityType(), $this->getConfig()->get('assignmentEmailNotificationsEntityList', [])) ) { + if ($entity->has('assignedUsersIds')) { + $userIdList = $entity->getLinkMultipleIdList('assignedUsers'); + $fetchedAssignedUserIdList = $entity->getFetched('assignedUsersIds'); + if (!is_array($fetchedAssignedUserIdList)) { + $fetchedAssignedUserIdList = []; + } - $userId = $entity->get('assignedUserId'); - if (!empty($userId) && $userId != $this->getUser()->id && $entity->isAttributeChanged('assignedUserId')) { - $job = $this->getEntityManager()->getEntity('Job'); - $job->set(array( - 'serviceName' => 'EmailNotification', - 'methodName' => 'notifyAboutAssignmentJob', - 'data' => json_encode(array( - 'userId' => $userId, - 'assignerUserId' => $this->getUser()->id, - 'entityId' => $entity->id, - 'entityType' => $entity->getEntityType() - )), - 'executeTime' => date('Y-m-d H:i:s'), - )); - $this->getEntityManager()->saveEntity($job); + foreach ($userIdList as $userId) { + if (in_array($userId, $fetchedAssignedUserIdList)) continue; + if ($this->getUser()->id === $userId) continue; + $this->createJob($entity, $userId); + } + } else { + $userId = $entity->get('assignedUserId'); + if (!empty($userId) && $userId != $this->getUser()->id && $entity->isAttributeChanged('assignedUserId')) { + $this->createJob($entity, $userId); + } } } } + + protected function createJob(Entity $entity, $userId) + { + $job = $this->getEntityManager()->getEntity('Job'); + $job->set(array( + 'serviceName' => 'EmailNotification', + 'methodName' => 'notifyAboutAssignmentJob', + 'data' => json_encode(array( + 'userId' => $userId, + 'assignerUserId' => $this->getUser()->id, + 'entityId' => $entity->id, + 'entityType' => $entity->getEntityType() + )), + 'executeTime' => date('Y-m-d H:i:s'), + )); + $this->getEntityManager()->saveEntity($job); + } } diff --git a/application/Espo/Hooks/Common/Notifications.php b/application/Espo/Hooks/Common/Notifications.php index 8c659aaa3c..803b583d2b 100644 --- a/application/Espo/Hooks/Common/Notifications.php +++ b/application/Espo/Hooks/Common/Notifications.php @@ -92,7 +92,7 @@ class Notifications extends \Espo\Core\Hooks\Base $entityType = $entity->getEntityType(); - if (!$this->checkHasStream($entityType)) { + if (!$this->checkHasStream($entityType) || $entity->hasLinkMultipleField('assignedUsers')) { if (in_array($entityType, $this->getConfig()->get('assignmentNotificationsEntityList', []))) { $notificator = $this->getNotificator($entityType); $notificator->process($entity); diff --git a/application/Espo/Services/EmailNotification.php b/application/Espo/Services/EmailNotification.php index 93bec04a21..73e2b1e6c2 100644 --- a/application/Espo/Services/EmailNotification.php +++ b/application/Espo/Services/EmailNotification.php @@ -121,43 +121,48 @@ class EmailNotification extends \Espo\Core\Services\Base $assignerUser = $this->getEntityManager()->getEntity('User', $assignerUserId); $entity = $this->getEntityManager()->getEntity($entityType, $entityId); - if ($entity && $assignerUser && $entity->get('assignedUserId') == $userId) { - $emailAddress = $user->get('emailAddress'); - if (!empty($emailAddress)) { - $email = $this->getEntityManager()->getEntity('Email'); + if (!$entity) return true; + if (!$assignerUser) return true; - $subjectTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'subject', $entity->getEntityType()); - $bodyTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'body', $entity->getEntityType()); + if (!$entity->hasLinkMultipleField('assignedUsers')) { + if ($entity->get('assignedUserId') !== $userId) return true; + } - $subjectTpl = str_replace(array("\n", "\r"), '', $subjectTpl); + $emailAddress = $user->get('emailAddress'); + if (!empty($emailAddress)) { + $email = $this->getEntityManager()->getEntity('Email'); - $recordUrl = rtrim($this->getConfig()->get('siteUrl'), '/') . '/#' . $entity->getEntityType() . '/view/' . $entity->id; + $subjectTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'subject', $entity->getEntityType()); + $bodyTpl = $this->getTemplateFileManager()->getTemplate('assignment', 'body', $entity->getEntityType()); - $data = array( - 'userName' => $user->get('name'), - 'assignerUserName' => $assignerUser->get('name'), - 'recordUrl' => $recordUrl, - 'entityType' => $this->getLanguage()->translate($entity->getEntityType(), 'scopeNames') - ); - $data['entityTypeLowerFirst'] = lcfirst($data['entityType']); + $subjectTpl = str_replace(array("\n", "\r"), '', $subjectTpl); - $subject = $this->getHtmlizer()->render($entity, $subjectTpl, 'assignment-email-subject-' . $entity->getEntityType(), $data, true); - $body = $this->getHtmlizer()->render($entity, $bodyTpl, 'assignment-email-body-' . $entity->getEntityType(), $data, true); + $recordUrl = rtrim($this->getConfig()->get('siteUrl'), '/') . '/#' . $entity->getEntityType() . '/view/' . $entity->id; - $email->set(array( - 'subject' => $subject, - 'body' => $body, - 'isHtml' => true, - 'to' => $emailAddress, - 'isSystem' => true, - 'parentId' => $entity->id, - 'parentType' => $entity->getEntityType() - )); - try { - $this->getMailSender()->send($email); - } catch (\Exception $e) { - $GLOBALS['log']->error('EmailNotification: [' . $e->getCode() . '] ' .$e->getMessage()); - } + $data = array( + 'userName' => $user->get('name'), + 'assignerUserName' => $assignerUser->get('name'), + 'recordUrl' => $recordUrl, + 'entityType' => $this->getLanguage()->translate($entity->getEntityType(), 'scopeNames') + ); + $data['entityTypeLowerFirst'] = lcfirst($data['entityType']); + + $subject = $this->getHtmlizer()->render($entity, $subjectTpl, 'assignment-email-subject-' . $entity->getEntityType(), $data, true); + $body = $this->getHtmlizer()->render($entity, $bodyTpl, 'assignment-email-body-' . $entity->getEntityType(), $data, true); + + $email->set(array( + 'subject' => $subject, + 'body' => $body, + 'isHtml' => true, + 'to' => $emailAddress, + 'isSystem' => true, + 'parentId' => $entity->id, + 'parentType' => $entity->getEntityType() + )); + try { + $this->getMailSender()->send($email); + } catch (\Exception $e) { + $GLOBALS['log']->error('EmailNotification: [' . $e->getCode() . '] ' .$e->getMessage()); } } diff --git a/client/src/views/settings/fields/assignment-notifications-entity-list.js b/client/src/views/settings/fields/assignment-notifications-entity-list.js index 859a20f4fd..3cd2e86d57 100644 --- a/client/src/views/settings/fields/assignment-notifications-entity-list.js +++ b/client/src/views/settings/fields/assignment-notifications-entity-list.js @@ -33,8 +33,14 @@ Espo.define('views/settings/fields/assignment-notifications-entity-list', 'views this.params.options = Object.keys(this.getMetadata().get('scopes')).filter(function (scope) { if (this.getMetadata().get('scopes.' + scope + '.disabled')) return; + + if ( + this.getMetadata().get(['scopes', scope, 'stream']) + && + !this.getMetadata().get(['entityDefs', scope, 'fields', 'assignedUsers']) + ) return; + return this.getMetadata().get('scopes.' + scope + '.notifications') && - !this.getMetadata().get('scopes.' + scope + '.stream') && this.getMetadata().get('scopes.' + scope + '.entity'); }, this).sort(function (v1, v2) { return this.translate(v1, 'scopeNamesPlural').localeCompare(this.translate(v2, 'scopeNamesPlural'));