field loading usage

This commit is contained in:
Yuri Kuznetsov
2021-04-27 18:56:22 +03:00
parent 6e460c5584
commit c2141dc034
12 changed files with 201 additions and 104 deletions
@@ -38,6 +38,11 @@ class LoaderParams
}
public function hasInSelect(string $field): bool
{
return $this->hasSelect() && in_array($field, $this->select);
}
public function hasSelect(): bool
{
return $this->select !== null;
@@ -0,0 +1,90 @@
<?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\Reminders;
use Espo\Core\{
ORM\Entity,
ORM\EntityManager,
FieldProcessing\Loader as LoaderInterface,
FieldProcessing\LoaderParams,
};
class Loader implements LoaderInterface
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function process(Entity $entity, LoaderParams $params): void
{
$hasReminder = $this->entityManager
->getDefs()
->getEntity($entity->getEntityType())
->hasField('reminders');
if (!$hasReminder) {
return;
}
if ($params->hasSelect() && !$params->hasInSelect('reminders')) {
return;
}
$entity->set('reminders', $this->fetchReminderDataList($entity));
}
private function fetchReminderDataList(Entity $entity): array
{
$list = [];
$collection = $this->entityManager
->getRDBRepository('Reminder')
->select(['seconds', 'type'])
->where([
'entityType' => $entity->getEntityType(),
'entityId' => $entity->getId(),
])
->distinct()
->order('seconds')
->find();
foreach ($collection as $reminder) {
$list[] = (object) [
'seconds' => $reminder->get('seconds'),
'type' => $reminder->get('type'),
];
}
return $list;
}
}
@@ -0,0 +1,5 @@
{
"readLoaderClassNameList": [
"Espo\\Core\\FieldProcessing\\Reminders\\Loader"
]
}
@@ -29,24 +29,9 @@
namespace Espo\Core\Templates\Services;
use \Espo\ORM\Entity;
class Event extends \Espo\Services\Record
{
protected $validateRequiredSkipFieldList = [
'dateEnd'
];
public function loadAdditionalFields(Entity $entity)
{
parent::loadAdditionalFields($entity);
$this->loadRemindersField($entity);
}
protected function loadRemindersField(Entity $entity)
{
$reminders = $this->getRepository()->getEntityReminderList($entity);
$entity->set('reminders', $reminders);
}
}
@@ -0,0 +1,5 @@
{
"readLoaderClassNameList": [
"Espo\\Core\\FieldProcessing\\Reminders\\Loader"
]
}
@@ -0,0 +1,5 @@
{
"readLoaderClassNameList": [
"Espo\\Core\\FieldProcessing\\Reminders\\Loader"
]
}
@@ -0,0 +1,5 @@
{
"readLoaderClassNameList": [
"Espo\\Core\\FieldProcessing\\Reminders\\Loader"
]
}
@@ -32,8 +32,6 @@ namespace Espo\Modules\Crm\Services;
use Espo\ORM\Entity;
use Espo\Modules\Crm\Business\Event\Invitations;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Di;
@@ -44,14 +42,14 @@ class Meeting extends \Espo\Services\Record implements
use Di\HookManagerSetter;
protected $validateRequiredSkipFieldList = [
'dateEnd'
'dateEnd',
];
protected $exportSkipFieldList = ['duration'];
protected $duplicateIgnoreAttributeList = ['usersColumns', 'contactsColumns', 'leadsColumns'];
public function checkAssignment(Entity $entity) : bool
public function checkAssignment(Entity $entity): bool
{
$result = parent::checkAssignment($entity);
@@ -59,8 +57,8 @@ class Meeting extends \Espo\Services\Record implements
return false;
}
$userIdList = $entity->get('usersIds')
;
$userIdList = $entity->get('usersIds');
if (!is_array($userIdList)) {
$userIdList = [];
}
@@ -85,7 +83,8 @@ class Meeting extends \Espo\Services\Record implements
$newIdList[] = $id;
}
}
} else {
}
else {
$newIdList = $userIdList;
}
@@ -101,8 +100,11 @@ class Meeting extends \Espo\Services\Record implements
protected function getInvitationManager(bool $useUserSmtp = true)
{
$smtpParams = null;
if ($useUserSmtp) {
$smtpParams = $this->getServiceFactory()->create('Email')->getUserSmtpParams($this->getUser()->id);
$smtpParams = $this->getServiceFactory()
->create('Email')
->getUserSmtpParams($this->getUser()->id);
}
return $this->injectableFactory->createWith(Invitations::class, [
@@ -124,14 +126,18 @@ class Meeting extends \Espo\Services\Record implements
->find();
foreach ($users as $user) {
if ($user->id === $this->getUser()->id) {
if ($entity->getLinkMultipleColumn('users', 'status', $user->id) === 'Accepted') {
continue;
}
if (
$user->getId() === $this->getUser()->getId() &&
$entity->getLinkMultipleColumn('users', 'status', $user->getId()) === 'Accepted'
) {
continue;
}
if ($user->get('emailAddress') && !array_key_exists($user->get('emailAddress'), $emailHash)) {
$invitationManager->sendInvitation($entity, $user, 'users');
$emailHash[$user->get('emailAddress')] = true;
$sentCount ++;
}
}
@@ -142,9 +148,14 @@ class Meeting extends \Espo\Services\Record implements
->find();
foreach ($contacts as $contact) {
if ($contact->get('emailAddress') && !array_key_exists($contact->get('emailAddress'), $emailHash)) {
if (
$contact->get('emailAddress') &&
!array_key_exists($contact->get('emailAddress'), $emailHash)
) {
$invitationManager->sendInvitation($entity, $contact, 'contacts');
$emailHash[$user->get('emailAddress')] = true;
$sentCount ++;
}
}
@@ -155,39 +166,36 @@ class Meeting extends \Espo\Services\Record implements
->find();
foreach ($leads as $lead) {
if ($lead->get('emailAddress') && !array_key_exists($lead->get('emailAddress'), $emailHash)) {
if (
$lead->get('emailAddress') &&
!array_key_exists($lead->get('emailAddress'), $emailHash)
) {
$invitationManager->sendInvitation($entity, $lead, 'leads');
$emailHash[$user->get('emailAddress')] = true;
$sentCount ++;
}
}
if (!$sentCount) return false;
if (!$sentCount) {
return false;
}
return true;
}
public function loadAdditionalFields(Entity $entity)
{
parent::loadAdditionalFields($entity);
$this->loadRemindersField($entity);
}
protected function loadRemindersField(Entity $entity)
{
$reminders = $this->getRepository()->getEntityReminderList($entity);
$entity->set('reminders', $reminders);
}
public function massSetHeld(array $ids)
{
foreach ($ids as $id) {
$entity = $this->getEntityManager()->getEntity($this->entityType, $id);
if ($entity && $this->getAcl()->check($entity, 'edit')) {
$entity->set('status', 'Held');
$this->getEntityManager()->saveEntity($entity);
}
}
return true;
}
@@ -195,11 +203,14 @@ class Meeting extends \Espo\Services\Record implements
{
foreach ($ids as $id) {
$entity = $this->getEntityManager()->getEntity($this->entityType, $id);
if ($entity && $this->getAcl()->check($entity, 'edit')) {
$entity->set('status', 'Not Held');
$this->getEntityManager()->saveEntity($entity);
}
}
return true;
}
@@ -207,17 +218,31 @@ class Meeting extends \Espo\Services\Record implements
{
$userId = $userId ?? $this->getUser()->id;
$statusList = $this->getMetadata()->get(['entityDefs', $this->entityType, 'fields', 'acceptanceStatus', 'options'], []);
if (!in_array($status, $statusList)) throw new BadRequest();
$statusList = $this->getMetadata()
->get(['entityDefs', $this->entityType, 'fields', 'acceptanceStatus', 'options'], []);
if (!in_array($status, $statusList)) {
throw new BadRequest();
}
$entity = $this->getEntityManager()->getEntity($this->entityType, $id);
if (!$entity) throw new NotFound();
if (!$entity->hasLinkMultipleId('users', $userId));
if (!$entity) {
throw new NotFound();
}
$this->getEntityManager()->getRepository($this->entityType)->updateRelation(
$entity, 'users', $userId, (object) ['status' => $status]
);
if (!$entity->hasLinkMultipleId('users', $userId)) {
return;
}
$this->getEntityManager()
->getRepository($this->entityType)
->updateRelation(
$entity,
'users',
$userId,
(object) ['status' => $status]
);
$actionData = [
'eventName' => $entity->get('name'),
@@ -1,50 +0,0 @@
<?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\Services;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\ORM\Entity;
class Task extends \Espo\Services\Record
{
public function loadAdditionalFields(Entity $entity)
{
parent::loadAdditionalFields($entity);
$this->loadRemindersField($entity);
}
protected function loadRemindersField(Entity $entity)
{
$reminders = $this->getRepository()->getEntityReminderList($entity);
$entity->set('reminders', $reminders);
}
}
+3
View File
@@ -187,6 +187,9 @@ class Record implements Crud,
protected $validateSkipFieldList = [];
/**
* @todo Move to metadata.
*/
protected $validateRequiredSkipFieldList = [];
protected $findDuplicatesSelectAttributeList = ['id', 'name'];
@@ -478,6 +478,7 @@ class EntityManager
$this->getMetadata()->set('clientDefs', $name, $clientDefsData);
$this->processMetadataCreateSelectDefs($templatePath, $name, $type);
$this->processMetadataCreateRecordDefs($templatePath, $name, $type);
$this->getBaseLanguage()->set('Global', 'scopeNames', $name, $labelSingular);
$this->getBaseLanguage()->set('Global', 'scopeNamesPlural', $name, $labelPlural);
@@ -511,6 +512,21 @@ class EntityManager
$this->getMetadata()->set('selectDefs', $name, $data);
}
private function processMetadataCreateRecordDefs(string $templatePath, string $name, string $type): void
{
$path = $templatePath . "/Metadata/{$type}/recordDefs.json";
if (!$this->getFileManager()->isFile($path)) {
return;
}
$contents = $this->getFileManager()->getContents($path);
$data = Json::decode($contents, true);
$this->getMetadata()->set('recordDefs', $name, $data);
}
public function update($name, $data)
{
if (!$this->getMetadata()->get('scopes.' . $name)) {
+8 -5
View File
@@ -70,11 +70,13 @@ class AfterUpgrade
$toSave = false;
$path = "application/Espo/Core/Templates/Metadata/Event/selectDefs.json";
$path1 = "application/Espo/Core/Templates/Metadata/Event/selectDefs.json";
$contents1 = $fileManager->getContents($path1);
$data1 = Json::decode($contents1, true);
$contents = $fileManager->getContents($path);
$data = Json::decode($contents, true);
$path2 = "application/Espo/Core/Templates/Metadata/Event/recordDefs.json";
$contents2 = $fileManager->getContents($path2);
$data2 = Json::decode($contents2, true);
foreach ($defs as $entityType => $item) {
$isCustom = $item['isCustom'] ?? false;
@@ -86,7 +88,8 @@ class AfterUpgrade
$toSave = true;
$metadata->set('selectDefs', $entityType, $data);
$metadata->set('selectDefs', $entityType, $data1);
$metadata->set('recordDefs', $entityType, $data2);
}
if ($toSave) {