From 6e460c558424bfc3b94d09af865d53ed0a4d4fd2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 27 Apr 2021 16:50:49 +0300 Subject: [PATCH] field loader usage --- .../Email/StringDataLoader.php | 120 ++++++++++++++++++ .../FieldProcessing/Portal/UrlLoader.php | 54 ++++++++ .../TargetList/EntryCountLoader.php | 70 ++++++++++ .../TargetList/OptedOutCountLoader.php | 76 +++++++++++ .../metadata/recordDefs/TargetList.json | 10 ++ .../Espo/Modules/Crm/Services/TargetList.php | 91 +++++-------- .../Resources/metadata/recordDefs/Email.json | 5 +- .../Resources/metadata/recordDefs/Portal.json | 8 ++ application/Espo/Services/Email.php | 73 ----------- application/Espo/Services/Import.php | 5 +- application/Espo/Services/LastViewed.php | 11 +- application/Espo/Services/Portal.php | 12 -- application/Espo/Services/Record.php | 2 +- 13 files changed, 385 insertions(+), 152 deletions(-) create mode 100644 application/Espo/Classes/FieldProcessing/Email/StringDataLoader.php create mode 100644 application/Espo/Classes/FieldProcessing/Portal/UrlLoader.php create mode 100644 application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/EntryCountLoader.php create mode 100644 application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/OptedOutCountLoader.php create mode 100644 application/Espo/Modules/Crm/Resources/metadata/recordDefs/TargetList.json create mode 100644 application/Espo/Resources/metadata/recordDefs/Portal.json diff --git a/application/Espo/Classes/FieldProcessing/Email/StringDataLoader.php b/application/Espo/Classes/FieldProcessing/Email/StringDataLoader.php new file mode 100644 index 0000000000..ed32e2cf74 --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/Email/StringDataLoader.php @@ -0,0 +1,120 @@ +entityManager = $entityManager; + $this->user = $user; + } + + public function process(Entity $entity, LoaderParams $params): void + { + $userEmailAdddressIdList = []; + + $emailAddressCollection = $this->entityManager + ->getRDBRepository('User') + ->getRelation($this->user, 'emailAddresses') + ->select(['id']) + ->find(); + + foreach ($emailAddressCollection as $emailAddress) { + $userEmailAdddressIdList[] = $emailAddress->getId(); + } + + if ( + in_array($entity->get('fromEmailAddressId'), $userEmailAdddressIdList) || + $entity->get('createdById') === $this->user->getId() + ) { + $entity->loadLinkMultipleField('toEmailAddresses'); + + $idList = $entity->get('toEmailAddressesIds'); + $names = $entity->get('toEmailAddressesNames'); + + if (empty($idList)) { + return; + } + + $list = []; + + foreach ($idList as $emailAddressId) { + $person = $this->entityManager + ->getRepository('EmailAddress') + ->getEntityByAddressId($emailAddressId, null, true); + + $list[] = $person ? $person->get('name') : $names->$emailAddressId; + } + + $entity->set('personStringData', 'To: ' . implode(', ', $list)); + + return; + } + + $fromEmailAddressId = $entity->get('fromEmailAddressId'); + + if (!$fromEmailAddressId) { + return; + } + + if (!array_key_exists($fromEmailAddressId, $this->fromEmailAddressNameCache)) { + $person = $this->entityManager + ->getRepository('EmailAddress') + ->getEntityByAddressId($fromEmailAddressId, null, true); + + $fromName = $person ? $person->get('name') : null; + + $this->fromEmailAddressNameCache[$fromEmailAddressId] = $fromName; + } + + $fromName = + $this->fromEmailAddressNameCache[$fromEmailAddressId] ?? + $entity->get('fromName') ?? + $entity->get('fromEmailAddressName'); + + $entity->set('personStringData', $fromName); + } +} diff --git a/application/Espo/Classes/FieldProcessing/Portal/UrlLoader.php b/application/Espo/Classes/FieldProcessing/Portal/UrlLoader.php new file mode 100644 index 0000000000..5eb24a2cf4 --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/Portal/UrlLoader.php @@ -0,0 +1,54 @@ +entityManager = $entityManager; + } + + public function process(Entity $entity, LoaderParams $params): void + { + $this->entityManager + ->getRepository('Portal') + ->loadUrlField($entity); + } +} diff --git a/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/EntryCountLoader.php b/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/EntryCountLoader.php new file mode 100644 index 0000000000..402deaf1b6 --- /dev/null +++ b/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/EntryCountLoader.php @@ -0,0 +1,70 @@ +entityManager = $entityManager; + } + + public function process(Entity $entity, LoaderParams $params): void + { + if ( + $params->hasSelect() && + !in_array('entryCount', $params->getSelect()) + ) { + return; + } + + $count = 0; + + foreach ($this->targetsLinkList as $link) { + $count += $this->entityManager + ->getRDBRepository('TargetList') + ->getRelation($entity, $link) + ->count(); + } + + $entity->set('entryCount', $count); + } +} diff --git a/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/OptedOutCountLoader.php b/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/OptedOutCountLoader.php new file mode 100644 index 0000000000..d36fc58656 --- /dev/null +++ b/application/Espo/Modules/Crm/Classes/FieldProcessing/TargetList/OptedOutCountLoader.php @@ -0,0 +1,76 @@ +entityManager = $entityManager; + } + + public function process(Entity $entity, LoaderParams $params): void + { + if ( + $params->hasSelect() && + !in_array('optedOutCount', $params->getSelect()) + ) { + return; + } + + $count = 0; + + foreach ($this->targetsLinkList as $link) { + $foreignEntityType = $entity->getRelationParam($link, 'entity'); + + $count += $this->entityManager + ->getRDBRepository($foreignEntityType) + ->join('targetLists') + ->where([ + 'targetListsMiddle.targetListId' => $entity->getId(), + 'targetListsMiddle.optedOut' => true, + ]) + ->count(); + } + + $entity->set('optedOutCount', $count); + } +} diff --git a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/TargetList.json b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/TargetList.json new file mode 100644 index 0000000000..41900150f5 --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/TargetList.json @@ -0,0 +1,10 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Modules\\Crm\\Classes\\FieldProcessing\\TargetList\\EntryCountLoader", + "Espo\\Modules\\Crm\\Classes\\FieldProcessing\\TargetList\\OptedOutCountLoader" + ], + "listLoaderClassNameList": [ + "Espo\\Modules\\Crm\\Classes\\FieldProcessing\\TargetList\\EntryCountLoader", + "Espo\\Modules\\Crm\\Classes\\FieldProcessing\\TargetList\\OptedOutCountLoader" + ] +} \ No newline at end of file diff --git a/application/Espo/Modules/Crm/Services/TargetList.php b/application/Espo/Modules/Crm/Services/TargetList.php index f92e303786..eee080adfe 100644 --- a/application/Espo/Modules/Crm/Services/TargetList.php +++ b/application/Espo/Modules/Crm/Services/TargetList.php @@ -69,60 +69,29 @@ class TargetList extends \Espo\Services\Record implements 'User' => 'users', ]; - public function loadAdditionalFields(Entity $entity) - { - parent::loadAdditionalFields($entity); - $this->loadEntryCountField($entity); - $this->loadOptedOutCountField($entity); - } - - public function loadAdditionalFieldsForList(Entity $entity) - { - parent::loadAdditionalFields($entity); - $this->loadEntryCountField($entity); - } - - protected function loadEntryCountField(Entity $entity) - { - $count = 0; - foreach ($this->targetsLinkList as $link) { - $count += $this->getEntityManager()->getRepository('TargetList')->countRelated($entity, $link); - } - $entity->set('entryCount', $count); - } - - protected function loadOptedOutCountField(Entity $entity) - { - $count = 0; - - foreach ($this->targetsLinkList as $link) { - $foreignEntityType = $entity->getRelationParam($link, 'entity'); - - $count += $this->getEntityManager()->getRepository($foreignEntityType) - ->join('targetLists') - ->where([ - 'targetListsMiddle.targetListId' => $entity->id, - 'targetListsMiddle.optedOut' => 1, - ]) - ->count(); - } - - $entity->set('optedOutCount', $count); - } - protected function afterCreateEntity(Entity $entity, $data) { if (property_exists($data, 'sourceCampaignId') && !empty($data->includingActionList)) { $excludingActionList = []; + if (!empty($data->excludingActionList)) { $excludingActionList = $data->excludingActionList; } - $this->populateFromCampaignLog($entity, $data->sourceCampaignId, $data->includingActionList, $excludingActionList); + + $this->populateFromCampaignLog( + $entity, + $data->sourceCampaignId, + $data->includingActionList, + $excludingActionList + ); } } protected function populateFromCampaignLog( - Entity $entity, string $sourceCampaignId, array $includingActionList, array $excludingActionList + Entity $entity, + string $sourceCampaignId, + array $includingActionList, + array $excludingActionList ) { if (empty($sourceCampaignId)) { throw new BadRequest(); @@ -226,10 +195,6 @@ class TargetList extends \Espo\Services\Record implements throw new Error(); } - $pdo = $this->getEntityManager()->getPDO(); - $query = $this->getEntityManager()->getQueryComposer(); - $sql = null; - $linkEntityType = ucfirst( $entity->getRelationParam($link, 'relationName') ?? '' ); @@ -256,7 +221,7 @@ class TargetList extends \Espo\Services\Record implements return true; } - protected function getOptedOutSelectQueryForLink(string $targetListId, string $link) : Select + protected function getOptedOutSelectQueryForLink(string $targetListId, string $link): Select { $seed = $this->getRepository()->getNew(); @@ -299,7 +264,7 @@ class TargetList extends \Espo\Services\Record implements ->build(); } - protected function findLinkedOptedOut(string $id, array $params) : RecordCollection + protected function findLinkedOptedOut(string $id, array $params): RecordCollection { $offset = $params['offset'] ?? 0; $maxSize = $params['maxSize'] ?? 0; @@ -342,7 +307,7 @@ class TargetList extends \Espo\Services\Record implements $collection = $this->getEntityManager()->createCollection(); - while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { + while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $itemEntity = $this->getEntityManager()->getEntity($row['entityType']); $itemEntity->set($row); @@ -381,9 +346,11 @@ class TargetList extends \Espo\Services\Record implements $link = $map[$targetType]; - $result = $this->getEntityManager()->getRepository('TargetList')->relate($targetList, $link, $targetId, array( - 'optedOut' => true - )); + $result = $this->getEntityManager() + ->getRepository('TargetList') + ->relate($targetList, $link, $targetId, [ + 'optedOut' => true, + ]); if (!$result) { return false; @@ -424,11 +391,14 @@ class TargetList extends \Espo\Services\Record implements if (empty($map[$targetType])) { throw new Error(); } + $link = $map[$targetType]; - $result = $this->getEntityManager()->getRepository('TargetList')->updateRelation($targetList, $link, $targetId, array( - 'optedOut' => false - )); + $result = $this->getEntityManager() + ->getRepository('TargetList') + ->updateRelation($targetList, $link, $targetId, [ + 'optedOut' => false, + ]); if (!$result) { return false; @@ -437,7 +407,7 @@ class TargetList extends \Espo\Services\Record implements $hookData = [ 'link' => $link, 'targetId' => $targetId, - 'targetType' => $targetType + 'targetType' => $targetType, ]; $this->hookManager->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData); @@ -458,10 +428,11 @@ class TargetList extends \Espo\Services\Record implements 'optedOut' => false ) )); + foreach ($linkedList as $linked) { - $repository->relate($entity, $link, $linked, array( - 'optedOut' => false - )); + $repository->relate($entity, $link, $linked, [ + 'optedOut' => false, + ]); } } } diff --git a/application/Espo/Resources/metadata/recordDefs/Email.json b/application/Espo/Resources/metadata/recordDefs/Email.json index 0ad2c496d2..10efacb063 100644 --- a/application/Espo/Resources/metadata/recordDefs/Email.json +++ b/application/Espo/Resources/metadata/recordDefs/Email.json @@ -1,3 +1,6 @@ { - "assignmentNotificatorClassName": "Espo\\Classes\\AssignmentNotificators\\Email" + "assignmentNotificatorClassName": "Espo\\Classes\\AssignmentNotificators\\Email", + "listLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\Email\\StringDataLoader" + ] } diff --git a/application/Espo/Resources/metadata/recordDefs/Portal.json b/application/Espo/Resources/metadata/recordDefs/Portal.json new file mode 100644 index 0000000000..973c8bfc97 --- /dev/null +++ b/application/Espo/Resources/metadata/recordDefs/Portal.json @@ -0,0 +1,8 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\Portal\\UrlLoader" + ], + "listLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\Portal\\UrlLoader" + ] +} diff --git a/application/Espo/Services/Email.php b/application/Espo/Services/Email.php index ebddaa14ea..0e6c769b46 100644 --- a/application/Espo/Services/Email.php +++ b/application/Espo/Services/Email.php @@ -757,79 +757,6 @@ class Email extends Record implements return $fromAddress; } - public function loadAdditionalFieldsForList(Entity $entity) - { - parent::loadAdditionalFieldsForList($entity); - - $userEmailAdddressIdList = []; - - $eaCollection = $this->entityManager - ->getRepository('User') - ->getRelation($this->getUser(), 'emailAddresses') - ->find(); - - foreach ($eaCollection as $ea) { - $userEmailAdddressIdList[] = $ea->id; - } - - if ( - in_array($entity->get('fromEmailAddressId'), $userEmailAdddressIdList) || - $entity->get('createdById') === $this->getUser()->id - ) { - $entity->loadLinkMultipleField('toEmailAddresses'); - $idList = $entity->get('toEmailAddressesIds'); - $names = $entity->get('toEmailAddressesNames'); - - if (!empty($idList)) { - $arr = []; - - foreach ($idList as $emailAddressId) { - $person = $this->getEntityManager()->getRepository('EmailAddress') - ->getEntityByAddressId($emailAddressId, null, true); - if ($person) { - $arr[] = $person->get('name'); - } else { - $arr[] = $names->$emailAddressId; - } - } - - $entity->set('personStringData', 'To: ' . implode(', ', $arr)); - } - - return; - } - - $fromEmailAddressId = $entity->get('fromEmailAddressId'); - - if (empty($fromEmailAddressId)) { - return; - } - - if (!array_key_exists($fromEmailAddressId, $this->fromEmailAddressNameCache)) { - $person = $this->getEntityManager()->getRepository('EmailAddress') - ->getEntityByAddressId($fromEmailAddressId, null, true); - - if ($person) { - $fromName = $person->get('name'); - } else { - $fromName = null; - } - - $this->fromEmailAddressNameCache[$fromEmailAddressId] = $fromName; - } - - $fromName = $this->fromEmailAddressNameCache[$fromEmailAddressId]; - - if (!$fromName) { - $fromName = $entity->get('fromName'); - if (!$fromName) { - $fromName = $entity->get('fromEmailAddressName'); - } - } - - $entity->set('personStringData', $fromName); - } - public function loadUserColumnFields(Entity $entity) { $emailUser = $this->entityManager->getRepository('EmailUser') diff --git a/application/Espo/Services/Import.php b/application/Espo/Services/Import.php index 9759651c30..f6488f71ec 100644 --- a/application/Espo/Services/Import.php +++ b/application/Espo/Services/Import.php @@ -36,6 +36,7 @@ use Espo\Core\{ Record\Collection as RecordCollection, Di, Select\SearchParams, + FieldProcessing\ListLoadProcessor, }; use Espo\{ @@ -99,10 +100,12 @@ class Import extends Record implements $collection = $this->getRepository()->findResultRecords($entity, $link, $query); + $listLoadProcessor = $this->injectableFactory->create(ListLoadProcessor::class); + $recordService = $this->recordServiceContainer->get($foreignEntityType); foreach ($collection as $e) { - $recordService->loadAdditionalFieldsForList($e); + $listLoadProcessor->process($e); $recordService->prepareEntityForOutput($e); } diff --git a/application/Espo/Services/LastViewed.php b/application/Espo/Services/LastViewed.php index 8ddf00c05a..c4a65e6ee9 100644 --- a/application/Espo/Services/LastViewed.php +++ b/application/Espo/Services/LastViewed.php @@ -34,6 +34,7 @@ use Espo\Core\{ Utils\Metadata, Utils\Util, ORM\EntityManager, + FieldProcessing\ListLoadProcessor, }; use Espo\Entities\User; @@ -48,24 +49,26 @@ class LastViewed protected $user; + private $listLoadProcessor; + public function __construct( ServiceFactory $serviceFactory, Metadata $metadata, EntityManager $entityManager, - User $user + User $user, + ListLoadProcessor $listLoadProcessor ) { $this->serviceFactory = $serviceFactory; $this->metadata = $metadata; $this->entityManager = $entityManager; $this->user = $user; + $this->listLoadProcessor = $listLoadProcessor; } public function getList($params): object { $repository = $this->entityManager->getRDBRepository('ActionHistoryRecord'); - $actionHistoryRecordService = $this->serviceFactory->create('ActionHistoryRecord'); - $scopes = $this->metadata->get('scopes'); $targetTypeList = array_filter( @@ -96,7 +99,7 @@ class LastViewed ->find(); foreach ($collection as $entity) { - $actionHistoryRecordService->loadAdditionalFieldsForList($entity); + $this->listLoadProcessor->process($entity); $entity->set('id', Util::generateId()); } diff --git a/application/Espo/Services/Portal.php b/application/Espo/Services/Portal.php index cbdc92e451..644deba986 100644 --- a/application/Espo/Services/Portal.php +++ b/application/Espo/Services/Portal.php @@ -48,18 +48,6 @@ class Portal extends Record implements 'customId', ]; - public function loadAdditionalFields(Entity $entity) - { - parent::loadAdditionalFields($entity); - $this->loadUrlField($entity); - } - - public function loadAdditionalFieldsForList(Entity $entity) - { - parent::loadAdditionalFieldsForList($entity); - $this->loadUrlField($entity); - } - protected function afterUpdateEntity(Entity $entity, $data) { $this->loadUrlField($entity); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 91091277a9..9450bb13f0 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -586,7 +586,7 @@ class Record implements Crud, $loadProcessor->process($entity); } - protected function loadListAdditionalFields(Entity $entity, ?SearchParams $searchParams): void + private function loadListAdditionalFields(Entity $entity, ?SearchParams $searchParams): void { $params = new FieldLoaderParams();