diff --git a/application/Espo/Core/Mail/Importer.php b/application/Espo/Core/Mail/Importer.php index 6432de3e87..ba7ee65be3 100644 --- a/application/Espo/Core/Mail/Importer.php +++ b/application/Espo/Core/Mail/Importer.php @@ -154,6 +154,7 @@ class Importer } if ($duplicate = $this->findDuplicate($email)) { + $duplicate = $this->getEntityManager()->getEntity('Email', $duplicate->id); $this->processDuplicate($duplicate, $assignedUserId, $userIdList, $folderData, $teamsIdList); return $duplicate; } @@ -283,6 +284,7 @@ class Importer if ($duplicate = $this->findDuplicate($email)) { $this->getEntityManager()->getPdo()->query('UNLOCK TABLES'); + $duplicate = $this->getEntityManager()->getEntity('Email', $duplicate->id); $this->processDuplicate($duplicate, $assignedUserId, $userIdList, $folderData, $teamsIdList); return $duplicate; } diff --git a/application/Espo/Core/Notificators/Base.php b/application/Espo/Core/Notificators/Base.php index 9463502c72..c15f635d64 100644 --- a/application/Espo/Core/Notificators/Base.php +++ b/application/Espo/Core/Notificators/Base.php @@ -90,7 +90,7 @@ class Base implements Injectable return $this->injections['user']; } - public function process(Entity $entity) + public function process(Entity $entity, array $options = []) { if ($entity->hasLinkMultipleField('assignedUsers')) { $userIdList = $entity->getLinkMultipleIdList('assignedUsers'); diff --git a/application/Espo/Hooks/Common/Notifications.php b/application/Espo/Hooks/Common/Notifications.php index 803b583d2b..b219320202 100644 --- a/application/Espo/Hooks/Common/Notifications.php +++ b/application/Espo/Hooks/Common/Notifications.php @@ -84,7 +84,7 @@ class Notifications extends \Espo\Core\Hooks\Base return $this->notifatorsHash[$entityType]; } - public function afterSave(Entity $entity, array $options = array()) + public function afterSave(Entity $entity, array $options = []) { if (!empty($options['silent']) || !empty($options['noNotifications'])) { return; @@ -95,12 +95,12 @@ class Notifications extends \Espo\Core\Hooks\Base if (!$this->checkHasStream($entityType) || $entity->hasLinkMultipleField('assignedUsers')) { if (in_array($entityType, $this->getConfig()->get('assignmentNotificationsEntityList', []))) { $notificator = $this->getNotificator($entityType); - $notificator->process($entity); + $notificator->process($entity, $options); } } } - public function beforeRemove(Entity $entity, array $options = array()) + public function beforeRemove(Entity $entity, array $options = []) { if (!empty($options['silent']) || !empty($options['noNotifications'])) { return; diff --git a/application/Espo/Notificators/Email.php b/application/Espo/Notificators/Email.php index c071a1c497..ea74452e90 100644 --- a/application/Espo/Notificators/Email.php +++ b/application/Espo/Notificators/Email.php @@ -56,7 +56,7 @@ class Email extends \Espo\Core\Notificators\Base return $this->streamService; } - public function process(Entity $entity) + public function process(Entity $entity, array $options = []) { if ($entity->get('status') !== 'Archived' && $entity->get('status') !== 'Sent') { return; @@ -95,10 +95,10 @@ class Email extends \Espo\Core\Notificators\Base } } - $data = array( + $data = [ 'emailId' => $entity->id, 'emailName' => $entity->get('name'), - ); + ]; if (!$entity->has('from')) { $this->getEntityManager()->getRepository('Email')->loadFromField($entity); @@ -149,10 +149,10 @@ class Email extends \Espo\Core\Notificators\Base $folderId = $entity->getLinkMultipleColumn('users', 'folderId', $userId); if ($folderId) { if ( - $this->getEntityManager()->getRepository('EmailFolder')->where(array( + $this->getEntityManager()->getRepository('EmailFolder')->where([ 'id' => $folderId, 'skipNotifications' => true - ))->count() + ])->count() ) { continue; } @@ -165,7 +165,7 @@ class Email extends \Espo\Core\Notificators\Base if (!$this->getAclManager()->checkScope($user, 'Email')) { continue; } - if ($entity->get('status') == 'Archived') { + if ($entity->get('status') == 'Archived' || !empty($options['isBeingImported'])) { if ($parent) { if ($this->getStreamService()->checkIsFollowed($parent, $userId)) { continue; @@ -177,16 +177,24 @@ class Email extends \Espo\Core\Notificators\Base } } } + if ( + $this->getEntityManager()->getRepository('Notification')->where([ + 'type' => 'EmailReceived', + 'userId' => $userId, + 'relatedId' => $entity->id, + 'relatedType' => 'Email' + ])->select(['id'])->findOne() + ) continue; + $notification = $this->getEntityManager()->getEntity('Notification'); - $notification->set(array( + $notification->set([ 'type' => 'EmailReceived', 'userId' => $userId, 'data' => $data, 'relatedId' => $entity->id, 'relatedType' => 'Email' - )); + ]); $this->getEntityManager()->saveEntity($notification); } } - } diff --git a/application/Espo/Repositories/Email.php b/application/Espo/Repositories/Email.php index ffde900050..edc7a94c8a 100644 --- a/application/Espo/Repositories/Email.php +++ b/application/Espo/Repositories/Email.php @@ -214,16 +214,14 @@ class Email extends \Espo\Core\ORM\Repositories\RDB $entity->setDummyMessageId(); } - $eaRepository = $this->getEntityManager()->getRepository('EmailAddress'); - - if ($entity->has('attachmentsIds')) { - $attachmentsIds = $entity->get('attachmentsIds'); - if (!empty($attachmentsIds)) { - $entity->set('hasAttachment', true); - } - } - if (empty($options['isDuplicate'])) { + if ($entity->has('attachmentsIds')) { + $attachmentsIds = $entity->get('attachmentsIds'); + if (!empty($attachmentsIds)) { + $entity->set('hasAttachment', true); + } + } + if ($entity->has('from') || $entity->has('to') || $entity->has('cc') || $entity->has('bcc') || $entity->has('replyTo')) { if (!$entity->has('usersIds')) { $entity->loadLinkMultipleField('users'); @@ -232,7 +230,7 @@ class Email extends \Espo\Core\ORM\Repositories\RDB if ($entity->has('from')) { $from = trim($entity->get('from')); if (!empty($from)) { - $ids = $eaRepository->getIds([$from]); + $ids = $this->getEntityManager()->getRepository('EmailAddress')->getIds([$from]); if (!empty($ids)) { $entity->set('fromEmailAddressId', $ids[0]); $this->addUserByEmailAddressId($entity, $ids[0], true); @@ -271,32 +269,34 @@ class Email extends \Espo\Core\ORM\Repositories\RDB parent::beforeSave($entity, $options); - if ($entity->get('status') === 'Sending' && $entity->get('createdById')) { - $entity->addLinkMultipleId('users', $entity->get('createdById')); - $entity->setLinkMultipleColumn('users', 'isRead', $entity->get('createdById'), true); - } + if (empty($options['isDuplicate'])) { + if ($entity->get('status') === 'Sending' && $entity->get('createdById')) { + $entity->addLinkMultipleId('users', $entity->get('createdById')); + $entity->setLinkMultipleColumn('users', 'isRead', $entity->get('createdById'), true); + } - if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { - $entity->set('accountId', null); - } + if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) { + $entity->set('accountId', null); + } - $parentId = $entity->get('parentId'); - $parentType = $entity->get('parentType'); - if ($parentId && $parentType) { - $parent = $this->getEntityManager()->getEntity($parentType, $parentId); - if ($parent) { - $accountId = null; - if ($parent->getEntityType() == 'Account') { - $accountId = $parent->id; - } - if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { - $accountId = $parent->get('accountId'); - } - if ($accountId) { - $account = $this->getEntityManager()->getEntity('Account', $accountId); - if ($account) { - $entity->set('accountId', $accountId); - $entity->set('accountName', $account->get('name')); + $parentId = $entity->get('parentId'); + $parentType = $entity->get('parentType'); + if ($parentId && $parentType) { + $parent = $this->getEntityManager()->getEntity($parentType, $parentId); + if ($parent) { + $accountId = null; + if ($parent->getEntityType() == 'Account') { + $accountId = $parent->id; + } + if (!$accountId && $parent->get('accountId') && $parent->getRelationParam('account', 'entity') == 'Account') { + $accountId = $parent->get('accountId'); + } + if ($accountId) { + $account = $this->getEntityManager()->getEntity('Account', $accountId); + if ($account) { + $entity->set('accountId', $accountId); + $entity->set('accountName', $account->get('name')); + } } } } @@ -331,7 +331,8 @@ class Email extends \Espo\Core\ORM\Repositories\RDB protected function afterSave(Entity $entity, array $options = []) { parent::afterSave($entity, $options); - if (!$entity->isNew()) { + + if (!$entity->isNew() && empty($options['isDuplicate'])) { if ($entity->get('parentType') && $entity->get('parentId') && $entity->isAttributeChanged('parentId')) { $replyList = $this->findRelated($entity, 'replies'); foreach ($replyList as $reply) { @@ -348,6 +349,8 @@ class Email extends \Espo\Core\ORM\Repositories\RDB } if ( + empty($options['isDuplicate']) + && ($entity->get('status') === 'Archived' || $entity->get('status') === 'Sent') && ($entity->isAttributeChanged('status') || $entity->isNew())