field loader usage

This commit is contained in:
Yuri Kuznetsov
2021-04-27 16:50:49 +03:00
parent 3a523a514c
commit 6e460c5584
13 changed files with 385 additions and 152 deletions
@@ -0,0 +1,120 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldProcessing\Email;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
ORM\EntityManager,
};
use Espo\Entities\User;
class StringDataLoader implements Loader
{
private $entityManager;
private $user;
private $fromEmailAddressNameCache = [];
public function __construct(EntityManager $entityManager, User $user)
{
$this->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);
}
}
@@ -0,0 +1,54 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldProcessing\Portal;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
ORM\EntityManager,
};
class UrlLoader implements Loader
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function process(Entity $entity, LoaderParams $params): void
{
$this->entityManager
->getRepository('Portal')
->loadUrlField($entity);
}
}
@@ -0,0 +1,70 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Classes\FieldProcessing\TargetList;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
ORM\EntityManager,
};
class EntryCountLoader implements Loader
{
private $targetsLinkList = ['contacts', 'leads', 'users', 'accounts'];
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->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);
}
}
@@ -0,0 +1,76 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Classes\FieldProcessing\TargetList;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
ORM\EntityManager,
};
class OptedOutCountLoader implements Loader
{
private $targetsLinkList = ['contacts', 'leads', 'users', 'accounts'];
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->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);
}
}
@@ -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"
]
}
@@ -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,
]);
}
}
}
@@ -1,3 +1,6 @@
{
"assignmentNotificatorClassName": "Espo\\Classes\\AssignmentNotificators\\Email"
"assignmentNotificatorClassName": "Espo\\Classes\\AssignmentNotificators\\Email",
"listLoaderClassNameList": [
"Espo\\Classes\\FieldProcessing\\Email\\StringDataLoader"
]
}
@@ -0,0 +1,8 @@
{
"readLoaderClassNameList": [
"Espo\\Classes\\FieldProcessing\\Portal\\UrlLoader"
],
"listLoaderClassNameList": [
"Espo\\Classes\\FieldProcessing\\Portal\\UrlLoader"
]
}
-73
View File
@@ -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')
+4 -1
View File
@@ -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);
}
+7 -4
View File
@@ -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());
}
-12
View File
@@ -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);
+1 -1
View File
@@ -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();