From c2141dc0349b430fef68af7b951eb56df2ba4f3c Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 27 Apr 2021 18:56:22 +0300 Subject: [PATCH] field loading usage --- .../Core/FieldProcessing/LoaderParams.php | 5 + .../Core/FieldProcessing/Reminders/Loader.php | 90 ++++++++++++++++++ .../Templates/Metadata/Event/recordDefs.json | 5 + .../Espo/Core/Templates/Services/Event.php | 15 --- .../Resources/metadata/recordDefs/Call.json | 5 + .../metadata/recordDefs/Meeting.json | 5 + .../Resources/metadata/recordDefs/Task.json | 5 + .../Espo/Modules/Crm/Services/Meeting.php | 93 ++++++++++++------- .../Espo/Modules/Crm/Services/Task.php | 50 ---------- application/Espo/Services/Record.php | 3 + .../Tools/EntityManager/EntityManager.php | 16 ++++ upgrades/6.2/scripts/AfterUpgrade.php | 13 ++- 12 files changed, 201 insertions(+), 104 deletions(-) create mode 100644 application/Espo/Core/FieldProcessing/Reminders/Loader.php create mode 100644 application/Espo/Core/Templates/Metadata/Event/recordDefs.json create mode 100644 application/Espo/Modules/Crm/Resources/metadata/recordDefs/Call.json create mode 100644 application/Espo/Modules/Crm/Resources/metadata/recordDefs/Meeting.json create mode 100644 application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json delete mode 100644 application/Espo/Modules/Crm/Services/Task.php diff --git a/application/Espo/Core/FieldProcessing/LoaderParams.php b/application/Espo/Core/FieldProcessing/LoaderParams.php index cc2cc78cef..7375882d97 100644 --- a/application/Espo/Core/FieldProcessing/LoaderParams.php +++ b/application/Espo/Core/FieldProcessing/LoaderParams.php @@ -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; diff --git a/application/Espo/Core/FieldProcessing/Reminders/Loader.php b/application/Espo/Core/FieldProcessing/Reminders/Loader.php new file mode 100644 index 0000000000..86c8c856bf --- /dev/null +++ b/application/Espo/Core/FieldProcessing/Reminders/Loader.php @@ -0,0 +1,90 @@ +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; + } +} diff --git a/application/Espo/Core/Templates/Metadata/Event/recordDefs.json b/application/Espo/Core/Templates/Metadata/Event/recordDefs.json new file mode 100644 index 0000000000..8db836c14e --- /dev/null +++ b/application/Espo/Core/Templates/Metadata/Event/recordDefs.json @@ -0,0 +1,5 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Core\\FieldProcessing\\Reminders\\Loader" + ] +} diff --git a/application/Espo/Core/Templates/Services/Event.php b/application/Espo/Core/Templates/Services/Event.php index f1dae6c91e..fe0967412a 100644 --- a/application/Espo/Core/Templates/Services/Event.php +++ b/application/Espo/Core/Templates/Services/Event.php @@ -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); - } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Call.json b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Call.json new file mode 100644 index 0000000000..8db836c14e --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Call.json @@ -0,0 +1,5 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Core\\FieldProcessing\\Reminders\\Loader" + ] +} diff --git a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Meeting.json b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Meeting.json new file mode 100644 index 0000000000..8db836c14e --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Meeting.json @@ -0,0 +1,5 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Core\\FieldProcessing\\Reminders\\Loader" + ] +} diff --git a/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json new file mode 100644 index 0000000000..8db836c14e --- /dev/null +++ b/application/Espo/Modules/Crm/Resources/metadata/recordDefs/Task.json @@ -0,0 +1,5 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Core\\FieldProcessing\\Reminders\\Loader" + ] +} diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index bbfa43a9c0..588b9856ec 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -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'), diff --git a/application/Espo/Modules/Crm/Services/Task.php b/application/Espo/Modules/Crm/Services/Task.php deleted file mode 100644 index 7a9e38e415..0000000000 --- a/application/Espo/Modules/Crm/Services/Task.php +++ /dev/null @@ -1,50 +0,0 @@ -loadRemindersField($entity); - } - - protected function loadRemindersField(Entity $entity) - { - $reminders = $this->getRepository()->getEntityReminderList($entity); - $entity->set('reminders', $reminders); - } -} diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 9450bb13f0..695d10ea16 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -187,6 +187,9 @@ class Record implements Crud, protected $validateSkipFieldList = []; + /** + * @todo Move to metadata. + */ protected $validateRequiredSkipFieldList = []; protected $findDuplicatesSelectAttributeList = ['id', 'name']; diff --git a/application/Espo/Tools/EntityManager/EntityManager.php b/application/Espo/Tools/EntityManager/EntityManager.php index 9d19b11686..df37848333 100644 --- a/application/Espo/Tools/EntityManager/EntityManager.php +++ b/application/Espo/Tools/EntityManager/EntityManager.php @@ -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)) { diff --git a/upgrades/6.2/scripts/AfterUpgrade.php b/upgrades/6.2/scripts/AfterUpgrade.php index 9c269226e0..d820126941 100644 --- a/upgrades/6.2/scripts/AfterUpgrade.php +++ b/upgrades/6.2/scripts/AfterUpgrade.php @@ -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) {