field loading usage

This commit is contained in:
Yuri Kuznetsov
2021-04-28 09:46:22 +03:00
parent b22bb295af
commit 5968e3d065
4 changed files with 149 additions and 72 deletions
@@ -0,0 +1,62 @@
<?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\Repositories\Email as EmailRepository;
class AddressDataLoader implements Loader
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->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);
}
}
@@ -0,0 +1,79 @@
<?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 UserColumnsLoader implements Loader
{
private $entityManager;
private $user;
public function __construct(EntityManager $entityManager, User $user)
{
$this->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'),
]);
}
}
@@ -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"
]
+4 -72
View File
@@ -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);
}
}