field loading

This commit is contained in:
Yuri Kuznetsov
2021-04-26 12:46:58 +03:00
parent defdd904c8
commit 87a34f2fbb
11 changed files with 644 additions and 93 deletions
@@ -0,0 +1,93 @@
<?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\Core\FieldProcessing;
use Espo\Core\ORM\Entity;
use Espo\Core\{
InjectableFactory,
Utils\Metadata,
};
/**
* Processes loading special fields (before output).
*/
class GeneralLoadProcessor
{
private $injectableFactory;
private $metadata;
public function __construct(InjectableFactory $injectableFactory, Metadata $metadata)
{
$this->injectableFactory = $injectableFactory;
$this->metadata = $metadata;
}
public function process(Entity $entity): void
{
foreach ($this->createProcessorList($entity->getEntityType()) as $processor) {
$processor->process($entity);
}
}
/**
* @return LoadProcessor[]
*/
private function createProcessorList(string $entityType): array
{
$list = [];
foreach ($this->getProcessorClassNameList($entityType) as $className) {
$list[] = $this->createProcessor($className);
}
return $list;
}
/**
* @return string[]
*/
private function getProcessorClassNameList(string $entityType): array
{
$list = $this->metadata
->get(['app', 'fieldProcessing', 'loadProcessorClassNameList']) ?? [];
$additionalList = $this->metadata
->get(['recordDefs', $entityType, 'loadProcessorClassNameList']) ?? [];
return array_merge($list, $additionalList);
}
private function createProcessor(string $className): LoadProcessor
{
return $this->injectableFactory->create($className);
}
}
@@ -0,0 +1,96 @@
<?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\Core\FieldProcessing\Link;
use Espo\Core\{
ORM\Entity,
FieldProcessing\LoadProcessor as LoadProcessorInterface,
};
use Espo\ORM\Defs\Defs as OrmDefs;
class LoadProcessor implements LoadProcessorInterface
{
private $ormDefs;
private $fieldListCacheMap = [];
public function __construct(OrmDefs $ormDefs)
{
$this->ormDefs = $ormDefs;
}
public function process(Entity $entity): void
{
foreach ($this->getFieldList($entity->getEntityType()) as $field) {
$entity->loadLinkField($field);
}
}
/**
* @return string[]
*/
private function getFieldList(string $entityType): array
{
if (array_key_exists($entityType, $this->fieldListCacheMap)) {
return $this->fieldListCacheMap[$entityType];
}
$list = [];
$entityDefs = $this->ormDefs->getEntity($entityType);
foreach ($entityDefs->getFieldList() as $fieldDefs) {
if ($fieldDefs->getType() !== 'link') {
continue;
}
if ($fieldDefs->getParam('noLoad')) {
continue;
}
$name = $fieldDefs->getName();
if (!$entityDefs->hasRelation($name)) {
continue;
}
if ($entityDefs->getRelation($name)->getType() !== Entity::HAS_ONE) {
continue;
}
$list[] = $name;
}
$this->fieldListCacheMap[$entityType] = $list;
return $list;
}
}
@@ -0,0 +1,106 @@
<?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\Core\FieldProcessing\LinkMultiple;
use Espo\Core\{
ORM\Entity,
FieldProcessing\LoadProcessor as LoadProcessorInterface,
};
use Espo\ORM\Defs\Defs as OrmDefs;
class LoadProcessor implements LoadProcessorInterface
{
private $ormDefs;
private $fieldListCacheMap = [];
public function __construct(OrmDefs $ormDefs)
{
$this->ormDefs = $ormDefs;
}
public function process(Entity $entity): void
{
$entityType = $entity->getEntityType();
foreach ($this->getFieldList($entityType) as $field) {
$columns = $this->ormDefs
->getEntity($entityType)
->getField($field)
->getParam('columns');
$entity->loadLinkMultipleField($field, $columns);
}
}
/**
* @return string[]
*/
private function getFieldList(string $entityType): array
{
if (array_key_exists($entityType, $this->fieldListCacheMap)) {
return $this->fieldListCacheMap[$entityType];
}
$list = [];
$entityDefs = $this->ormDefs->getEntity($entityType);
foreach ($entityDefs->getFieldList() as $fieldDefs) {
if (
$fieldDefs->getType() !== 'linkMultiple' &&
$fieldDefs->getType() !== 'attachmentMultiple'
) {
continue;
}
if ($fieldDefs->getParam('noLoad')) {
continue;
}
if ($fieldDefs->isNotStorable()) {
continue;
}
$name = $fieldDefs->getName();
if (!$entityDefs->hasRelation($name)) {
continue;
}
$list[] = $name;
}
$this->fieldListCacheMap[$entityType] = $list;
return $list;
}
}
@@ -0,0 +1,84 @@
<?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\Core\FieldProcessing\LinkParent;
use Espo\Core\{
ORM\Entity,
FieldProcessing\LoadProcessor as LoadProcessorInterface,
};
use Espo\ORM\Defs\Defs as OrmDefs;
class LoadProcessor implements LoadProcessorInterface
{
private $ormDefs;
private $fieldListCacheMap = [];
public function __construct(OrmDefs $ormDefs)
{
$this->ormDefs = $ormDefs;
}
public function process(Entity $entity): void
{
foreach ($this->getFieldList($entity->getEntityType()) as $field) {
$entity->loadParentNameField($field);
}
}
/**
* @return string[]
*/
private function getFieldList(string $entityType): array
{
if (array_key_exists($entityType, $this->fieldListCacheMap)) {
return $this->fieldListCacheMap[$entityType];
}
$list = [];
$entityDefs = $this->ormDefs->getEntity($entityType);
foreach ($entityDefs->getFieldList() as $fieldDefs) {
if ($fieldDefs->getType() !== 'linkParent') {
continue;
}
$name = $fieldDefs->getName();
$list[] = $name;
}
$this->fieldListCacheMap[$entityType] = $list;
return $list;
}
}
@@ -0,0 +1,85 @@
<?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\Core\FieldProcessing\LinkParent;
use Espo\Core\{
ORM\Entity,
ORM\EntityManager,
FieldProcessing\LoadProcessor as LoadProcessorInterface,
};
class TargetLoadProcessor implements LoadProcessorInterface
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function process(Entity $entity): void
{
$targetType = $entity->get('targetType');
$targetId = $entity->get('targetId');
if (!$targetType || !$targetId) {
return;
}
if (!$this->entityManager->hasRepository($targetType)) {
return;
}
$query = $this->entityManager
->getQueryBuilder()
->select()
->from($targetType)
->withDeleted()
->where([
'id' => $targetId,
])
->build();
$target = $this->entityManager
->getRDBRepository($targetType)
->clone($query)
->findOne();
if (!$target) {
return;
}
if (!$target->get('name')) {
return;
}
$entity->set('targetName', $target->get('name'));
}
}
@@ -0,0 +1,40 @@
<?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\Core\FieldProcessing;
use Espo\Core\ORM\Entity;
/**
* Processes loading special fields before output.
*/
interface LoadProcessor
{
public function process(Entity $entity): void;
}
@@ -0,0 +1,114 @@
<?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\Core\FieldProcessing\Misc;
use Espo\Core\{
ORM\Entity,
FieldProcessing\LoadProcessor as LoadProcessorInterface,
Utils\Metadata,
Utils\Config,
Acl,
};
use Espo\Entities\User;
use Espo\Services\Stream as StreamService;
class StreamLoadProcessor implements LoadProcessorInterface
{
private const FOLLOWERS_LIMIT = 5;
private $streamService;
private $metadata;
private $user;
private $acl;
private $config;
public function __construct(
StreamService $streamService,
Metadata $metadata,
User $user,
Acl $acl,
Config $config
) {
$this->streamService = $streamService;
$this->metadata = $metadata;
$this->user = $user;
$this->acl = $acl;
$this->config = $config;
}
public function process(Entity $entity): void
{
$this->processIsFollowed($entity);
$this->processFollowers($entity);
}
public function processIsFollowed(Entity $entity): void
{
if (!$entity->hasAttribute('isFollowed')) {
return;
}
$isFollowed = $this->streamService->checkIsFollowed($entity);
$entity->set('isFollowed', $isFollowed);
}
public function processFollowers(Entity $entity): void
{
if ($this->user->isPortal()) {
return;
}
if (!$this->metadata->get(['scopes', $entity->getEntityType(), 'stream'])) {
return;
}
if (!$this->acl->checkEntityStream($entity)) {
return;
}
$limit = $this->config->get('recordFollowersLoadLimit') ?? self::FOLLOWERS_LIMIT;
$data = $this->streamService->getEntityFollowers($entity, 0, $limit);
if (!$data) {
return;
}
$entity->set('followersIds', $data['idList']);
$entity->set('followersNames', $data['nameMap']);
}
}
@@ -0,0 +1,8 @@
{
"loadProcessorClassNameList": [
"Espo\\Core\\FieldProcessing\\Link\\LoadProcessor",
"Espo\\Core\\FieldProcessing\\LinkMultiple\\LoadProcessor",
"Espo\\Core\\FieldProcessing\\LinkParent\\LoadProcessor",
"Espo\\Core\\FieldProcessing\\Misc\\StreamLoadProcessor"
]
}
@@ -20,6 +20,7 @@
["app", "select"],
["app", "massActions", "__ANY__", "implementationClassName"],
["app", "actions", "__ANY__", "implementationClassName"],
["app", "fieldProcessing"],
["selectDefs"],
["recordDefs"],
["aclDefs"],
@@ -0,0 +1,5 @@
{
"loadProcessorClassNameList": [
"Espo\\Core\\FieldProcessing\\LinkParent\\TargetLoadProcessor"
]
}
+12 -93
View File
@@ -65,6 +65,7 @@ use Espo\Core\{
Action\Data as ActionData,
Action\ActionFactory,
FieldValidation\Params as FieldValidationParams,
FieldProcessing\GeneralLoadProcessor,
};
use Espo\Tools\{
@@ -200,8 +201,6 @@ class Record implements Crud,
const MAX_SELECT_TEXT_ATTRIBUTE_LENGTH = 5000;
const FOLLOWERS_LIMIT = 4;
const FIND_DUPLICATES_LIMIT = 10;
protected $selectBuilderFactory;
@@ -538,61 +537,6 @@ class Record implements Crud,
return $this->streamService;
}
protected function loadIsFollowed(Entity $entity): void
{
if ($this->getStreamService()->checkIsFollowed($entity)) {
$entity->set('isFollowed', true);
}
else {
$entity->set('isFollowed', false);
}
}
protected function loadFollowers(Entity $entity)
{
if ($this->getUser()->isPortal()) {
return;
}
if (!$this->getMetadata()->get(['scopes', $entity->getEntityType(), 'stream'])) {
return;
}
if (!$this->getAcl()->check($entity, 'stream')) {
return;
}
$data = $this->getStreamService()->getEntityFollowers($entity, 0, self::FOLLOWERS_LIMIT);
if ($data) {
$entity->set('followersIds', $data['idList']);
$entity->set('followersNames', $data['nameMap']);
}
}
protected function loadLinkMultipleFields(Entity $entity)
{
$fieldDefs = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields']) ?? [];
foreach ($fieldDefs as $field => $defs) {
if (
isset($defs['type']) &&
in_array($defs['type'], ['linkMultiple', 'attachmentMultiple']) &&
empty($defs['noLoad']) &&
empty($defs['notStorable']) &&
$entity->hasRelation($field)
) {
$columns = null;
if (!empty($defs['columns'])) {
$columns = $defs['columns'];
}
$entity->loadLinkMultipleField($field, $columns);
}
}
}
public function loadLinkMultipleFieldsForList(Entity $entity, $selectAttributeList)
{
foreach ($selectAttributeList as $attribute) {
@@ -612,43 +556,12 @@ class Record implements Crud,
}
}
protected function loadLinkFields(Entity $entity)
{
$fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', []);
$linkDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.links', []);
foreach ($fieldDefs as $field => $defs) {
if (isset($defs['type']) && $defs['type'] === 'link') {
if (!empty($defs['noLoad'])) {
continue;
}
if (empty($linkDefs[$field])) {
continue;
}
if (empty($linkDefs[$field]['type'])) {
continue;
}
if ($linkDefs[$field]['type'] !== 'hasOne') {
continue;
}
$entity->loadLinkField($field);
}
}
}
protected function loadParentNameFields(Entity $entity)
{
$fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', []);
foreach ($fieldDefs as $field => $defs) {
if (isset($defs['type']) && $defs['type'] == 'linkParent') {
$parentId = $entity->get($field . 'Id');
$parentType = $entity->get($field . 'Type');
$entity->loadParentNameField($field);
}
}
@@ -746,13 +659,19 @@ class Record implements Crud,
}
}
private function createLoadProcessor(): GeneralLoadProcessor
{
return $this->injectableFactory->create(GeneralLoadProcessor::class);
}
public function loadAdditionalFields(Entity $entity)
{
$this->loadLinkFields($entity);
$this->loadLinkMultipleFields($entity);
$this->loadParentNameFields($entity);
$this->loadIsFollowed($entity);
$this->loadFollowers($entity);
$loadProcessor = $this->createLoadProcessor();
$loadProcessor->process($entity);
//$this->loadParentNameFields($entity);
$this->loadEmailAddressField($entity);
$this->loadPhoneNumberField($entity);
$this->loadNotJoinedLinkFields($entity);