From 5968e3d065a71e860edd832ac2b0ac2f0279ac8a Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 28 Apr 2021 09:46:22 +0300 Subject: [PATCH] field loading usage --- .../Email/AddressDataLoader.php | 62 +++++++++++++++ .../Email/UserColumnsLoader.php | 79 +++++++++++++++++++ .../Resources/metadata/recordDefs/Email.json | 4 + application/Espo/Services/Email.php | 76 +----------------- 4 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 application/Espo/Classes/FieldProcessing/Email/AddressDataLoader.php create mode 100644 application/Espo/Classes/FieldProcessing/Email/UserColumnsLoader.php diff --git a/application/Espo/Classes/FieldProcessing/Email/AddressDataLoader.php b/application/Espo/Classes/FieldProcessing/Email/AddressDataLoader.php new file mode 100644 index 0000000000..2531b9d55d --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/Email/AddressDataLoader.php @@ -0,0 +1,62 @@ +entityManager = $entityManager; + } + + public function process(Entity $entity, LoaderParams $params): void + { + /* @var $repository EmailRepository */ + $repository = $this->entityManager->getRepository('Email'); + + $repository->loadFromField($entity); + $repository->loadToField($entity); + $repository->loadCcField($entity); + $repository->loadBccField($entity); + $repository->loadReplyToField($entity); + $repository->loadNameHash($entity); + } +} diff --git a/application/Espo/Classes/FieldProcessing/Email/UserColumnsLoader.php b/application/Espo/Classes/FieldProcessing/Email/UserColumnsLoader.php new file mode 100644 index 0000000000..22d612165d --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/Email/UserColumnsLoader.php @@ -0,0 +1,79 @@ +entityManager = $entityManager; + $this->user = $user; + } + + public function process(Entity $entity, LoaderParams $params): void + { + $emailUser = $this->entityManager + ->getRepository('EmailUser') + ->select(['isRead', 'isImportant', 'inTrash']) + ->where([ + 'deleted' => false, + 'userId' => $this->user->getId(), + 'emailId' => $entity->getId(), + ]) + ->findOne(); + + if (!$emailUser) { + $entity->set('isRead', null); + $entity->clear('isImportant'); + $entity->clear('inTrash'); + + return; + } + + $entity->set([ + 'isRead' => $emailUser->get('isRead'), + 'isImportant' => $emailUser->get('isImportant'), + 'inTrash' => $emailUser->get('inTrash'), + ]); + } +} diff --git a/application/Espo/Resources/metadata/recordDefs/Email.json b/application/Espo/Resources/metadata/recordDefs/Email.json index 10efacb063..9c60f320d3 100644 --- a/application/Espo/Resources/metadata/recordDefs/Email.json +++ b/application/Espo/Resources/metadata/recordDefs/Email.json @@ -1,5 +1,9 @@ { "assignmentNotificatorClassName": "Espo\\Classes\\AssignmentNotificators\\Email", + "readLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\Email\\AddressDataLoader", + "Espo\\Classes\\FieldProcessing\\Email\\UserColumnsLoader" + ], "listLoaderClassNameList": [ "Espo\\Classes\\FieldProcessing\\Email\\StringDataLoader" ] diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index 2cd6748a82..a50e49f517 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -446,31 +446,6 @@ class Email extends Record implements } } - public function loadFromField(Entity $entity) - { - $this->getEntityManager()->getRepository('Email')->loadFromField($entity); - } - - public function loadToField(Entity $entity) - { - $this->getEntityManager()->getRepository('Email')->loadToField($entity); - } - - public function loadCcField(Entity $entity) - { - $this->getEntityManager()->getRepository('Email')->loadCcField($entity); - } - - public function loadBccField(Entity $entity) - { - $this->getEntityManager()->getRepository('Email')->loadBccField($entity); - } - - public function loadReplyToField(Entity $entity) - { - $this->getEntityManager()->getRepository('Email')->loadReplyToField($entity); - } - public function getEntity(?string $id = null): ?Entity { $entity = parent::getEntity($id); @@ -482,21 +457,6 @@ class Email extends Record implements return $entity; } - public function loadAdditionalFields(Entity $entity) - { - parent::loadAdditionalFields($entity); - - $this->loadFromField($entity); - $this->loadToField($entity); - $this->loadCcField($entity); - $this->loadBccField($entity); - $this->loadReplyToField($entity); - - $this->loadNameHash($entity); - - $this->loadUserColumnFields($entity); - } - public function markAsReadByIdList(array $idList, $userId = null) { foreach ($idList as $id) { @@ -785,37 +745,6 @@ class Email extends Record implements return $fromAddress; } - public function loadUserColumnFields(Entity $entity) - { - $emailUser = $this->entityManager->getRepository('EmailUser') - ->select(['isRead', 'isImportant', 'inTrash']) - ->where([ - 'deleted' => false, - 'userId' => $this->getUser()->id, - 'emailId' => $entity->id, - ]) - ->findOne(); - - if (!$emailUser) { - $entity->set('isRead', null); - $entity->clear('isImportant'); - $entity->clear('inTrash'); - - return; - } - - $entity->set([ - 'isRead' => $emailUser->get('isRead'), - 'isImportant' => $emailUser->get('isImportant'), - 'inTrash' => $emailUser->get('inTrash'), - ]); - } - - public function loadNameHash(Entity $entity, array $fieldList = ['from', 'to', 'cc', 'bcc', 'replyTo']) - { - $this->getEntityManager()->getRepository('Email')->loadNameHash($entity, $fieldList); - } - public function copyAttachments(string $emailId, ?string $parentType, ?string $parentId) { return $this->getCopiedAttachments($emailId, $parentType, $parentId); @@ -983,7 +912,10 @@ class Email extends Record implements if (!$skipFilter) { foreach ($entity->getAttributeList() as $attribute) { - if (in_array($attribute, $this->allowedForUpdateAttributeList)) continue; + if (in_array($attribute, $this->allowedForUpdateAttributeList)) { + continue; + } + $entity->clear($attribute); } }