From bab7f3e9fc5ef5bb1f51bcc3ceebee16ae84855d Mon Sep 17 00:00:00 2001 From: yuri Date: Thu, 9 Apr 2015 11:56:47 +0300 Subject: [PATCH] email notifications --- application/Espo/Core/Hooks/Base.php | 5 ++ application/Espo/Core/Notificators/Base.php | 5 ++ application/Espo/Core/Services/Base.php | 5 ++ application/Espo/Notificators/Email.php | 63 +++++++++++++++------ 4 files changed, 62 insertions(+), 16 deletions(-) diff --git a/application/Espo/Core/Hooks/Base.php b/application/Espo/Core/Hooks/Base.php index 734fb4e2f6..c06d8bee1d 100644 --- a/application/Espo/Core/Hooks/Base.php +++ b/application/Espo/Core/Hooks/Base.php @@ -52,6 +52,11 @@ abstract class Base implements Injectable return $this->dependencies; } + protected function addDependency($name) + { + $this->dependencies[] = $name; + } + protected function getInjection($name) { return $this->injections[$name]; diff --git a/application/Espo/Core/Notificators/Base.php b/application/Espo/Core/Notificators/Base.php index c94a631ec4..bb094dbf8c 100644 --- a/application/Espo/Core/Notificators/Base.php +++ b/application/Espo/Core/Notificators/Base.php @@ -46,6 +46,11 @@ class Base implements Injectable { } + protected function addDependency($name) + { + $this->dependencies[] = $name; + } + public function getDependencyList() { return $this->dependencies; diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php index b6e8b60ac7..7691237461 100644 --- a/application/Espo/Core/Services/Base.php +++ b/application/Espo/Core/Services/Base.php @@ -53,6 +53,11 @@ abstract class Base implements Injectable return $this->injections[$name]; } + protected function addDependency($name) + { + $this->dependencies[] = $name; + } + public function getDependencyList() { return $this->dependencies; diff --git a/application/Espo/Notificators/Email.php b/application/Espo/Notificators/Email.php index b4e9d02fab..eb6195f90c 100644 --- a/application/Espo/Notificators/Email.php +++ b/application/Espo/Notificators/Email.php @@ -26,29 +26,41 @@ use \Espo\ORM\Entity; class Email extends \Espo\Core\Notificators\Base { + protected function init() + { + $this->addDependency('serviceFactory'); + } + + private $streamService = null; + + protected function getStreamService() + { + if (empty($this->streamService)) { + $this->streamService = $this->getInjection('serviceFactory')->create('Stream'); + } + return $this->streamService; + } + public function process(Entity $entity) { - if (!$entity->isNew()) { + if ($entity->get('status') != 'Archived') { + return; + } + + $previousUserIdList = $entity->getFetched('usersIds'); + if (!is_array($previousUserIdList)) { + $previousUserIdList = []; + } + + $emailUserIdList = $entity->get('usersIds'); + + if (is_null($emailUserIdList) || !is_array($emailUserIdList)) { return; } $userIdList = []; - if ($entity->has('assignedUserId') && $entity->get('assignedUserId')) { - $assignedUserId = $entity->get('assignedUserId'); - if ($assignedUserId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) { - $userIdList[] = $assignedUserId; - } - } - $emailUserIdList = $entity->get('usersIds'); - if (is_null($emailUserIdList)) { - $entity->loadLinkMultipleField('from'); - $emailUserIdList = $entity->get('usersIds'); - } - if (!is_array($emailUserIdList)) { - $emailUserIdList = []; - } foreach ($emailUserIdList as $userId) { - if (!in_array($userId, $userIdList)) { + if (!in_array($userId, $userIdList) && !in_array() && $userId != $this->getUser()->id) { $userIdList[] = $userId; } } @@ -68,7 +80,26 @@ class Email extends \Espo\Core\Notificators\Base } } + $parent = null; + if ($entity->get('parentId') && $entity->get('parentType')) { + $parent = $this->getEntityManager()->getEntity($entity->get('parentType'), $entity->get('parentId')); + } + $account = null; + if ($entity->get('accountId')) { + $account = $this->getEntityManager()->getEntity('Account', $entity->get('accountId')); + } + foreach ($userIdList as $userId) { + if ($parent) { + if ($this->getStreamService()->checkIsFollowed($parent, $userId)) { + continue; + } + } + if ($account) { + if ($this->getStreamService()->checkIsFollowed($account, $userId)) { + continue; + } + } $notification = $this->getEntityManager()->getEntity('Notification'); $notification->set(array( 'type' => 'EmailReceived',