From 57e733754a87acef4980cac904a80ee8eed0a96e Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 13 Mar 2022 15:20:47 +0200 Subject: [PATCH] type fixes --- application/Espo/Core/Utils/Language.php | 25 ++++++++++++++++--- .../Espo/Modules/Crm/Services/Campaign.php | 7 +++--- .../Tools/EmailNotification/Processor.php | 21 ++++++++++------ application/Espo/Tools/Kanban/Kanban.php | 11 +++++++- .../Espo/Tools/LabelManager/LabelManager.php | 6 +++-- .../Espo/Tools/LeadCapture/LeadCapture.php | 5 ++-- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php index 59062a0673..c9343f78fe 100644 --- a/application/Espo/Core/Utils/Language.php +++ b/application/Espo/Core/Utils/Language.php @@ -131,12 +131,31 @@ class Language } /** - * Translate label/labels. + * Translate a label. * - * @param string|string[] $label name of label + * @param string $label * @param string $category * @param string $scope - * @param string[]|null $requiredOptions List of required options. + * @return string + */ + public function translateLabel(string $label, string $category = 'labels', string $scope = 'Global'): string + { + $translated = $this->translate($label, $category, $scope); + + if (is_array($translated)) { + return implode(', ', $translated); + } + + return $translated; + } + + /** + * Translate label or labels. + * + * @param string|string[] $label A name of label. + * @param string $category A category. + * @param string $scope A scope. + * @param string[]|null $requiredOptions A list of required options. * Ex., $requiredOptions = ['en_US', 'de_DE'] * "language" option has only ['en_US' => 'English (United States)'] * Result will be ['en_US' => 'English (United States)', 'de_DE' => 'de_DE']. diff --git a/application/Espo/Modules/Crm/Services/Campaign.php b/application/Espo/Modules/Crm/Services/Campaign.php index ec78cad239..b3a16b5f22 100644 --- a/application/Espo/Modules/Crm/Services/Campaign.php +++ b/application/Espo/Modules/Crm/Services/Campaign.php @@ -383,11 +383,10 @@ class Campaign extends Record implements throw new Forbidden(); } - $targetEntityType = null; + /** @var string $targetEntityType */ + $targetEntityType = $campaign->getRelationParam($link, 'entity'); if ($checkAcl) { - $targetEntityType = $campaign->getRelationParam($link, 'entity'); - if (!$this->acl->check($targetEntityType, 'read')) { throw new Forbidden("Could not mail merge campaign because access to target entity type is forbidden."); } @@ -410,7 +409,7 @@ class Campaign extends Record implements $template = $this->entityManager->getEntity('Template', $campaign->get($link . 'TemplateId')); if (!$template) { - throw new Error("Template not found"); + throw new Error("Template not found."); } if ($template->get('entityType') !== $targetEntityType) { diff --git a/application/Espo/Tools/EmailNotification/Processor.php b/application/Espo/Tools/EmailNotification/Processor.php index b299f99e2d..b552753c67 100644 --- a/application/Espo/Tools/EmailNotification/Processor.php +++ b/application/Espo/Tools/EmailNotification/Processor.php @@ -480,7 +480,7 @@ class Processor protected function processNotificationNotePost(NoteEntity $note, UserEntity $user): void { $parentId = $note->get('parentId'); - $parentType = $note->get('parentType'); + $parentType = $note->getParentType(); $emailAddress = $user->get('emailAddress'); @@ -514,7 +514,7 @@ class Processor $data['name'] = $data['parentName']; - $data['entityType'] = $this->language->translate($data['parentType'], 'scopeNames'); + $data['entityType'] = $this->language->translateLabel($parentType, 'scopeNames'); $data['entityTypeLowerFirst'] = Util::mbLowerCaseFirst($data['entityType']); $subjectTpl = $this->templateFileManager->getTemplate('notePost', 'subject', $parentType); @@ -637,7 +637,7 @@ class Processor $this->noteAccessControl->apply($note, $user); $parentId = $note->get('parentId'); - $parentType = $note->get('parentType'); + $parentType = $note->getParentType(); $emailAddress = $user->get('emailAddress'); @@ -664,7 +664,7 @@ class Processor $data['name'] = $data['parentName']; - $data['entityType'] = $this->language->translate($data['parentType'], 'scopeNames'); + $data['entityType'] = $this->language->translateLabel($parentType, 'scopeNames'); $data['entityTypeLowerFirst'] = Util::mbLowerCaseFirst($data['entityType']); $noteData = $note->get('data'); @@ -678,9 +678,14 @@ class Processor } $data['value'] = $noteData->value; - $data['field'] = $noteData->field; + $data['field'] = $field = $noteData->field; + + if (!is_string($field)) { + return; + } + $data['valueTranslated'] = $this->language->translateOption($data['value'], $data['field'], $parentType); - $data['fieldTranslated'] = $this->language->translate($data['field'], 'fields', $parentType); + $data['fieldTranslated'] = $this->language->translateLabel($field, 'fields', $parentType); $data['fieldTranslatedLowerCase'] = Util::mbLowerCaseFirst($data['fieldTranslated']); $data['userName'] = $note->get('createdByName'); @@ -741,7 +746,7 @@ class Processor protected function processNotificationNoteEmailReceived(NoteEntity $note, UserEntity $user): void { $parentId = $note->get('parentId'); - $parentType = $note->get('parentType'); + $parentType = $note->getParentType(); $allowedEntityTypeList = $this->config->get('streamEmailNotificationsEmailReceivedEntityTypeList'); @@ -822,7 +827,7 @@ class Processor $data['name'] = $data['parentName']; - $data['entityType'] = $this->language->translate($data['parentType'], 'scopeNames'); + $data['entityType'] = $this->language->translateLabel($parentType, 'scopeNames'); $data['entityTypeLowerFirst'] = Util::mbLowerCaseFirst($data['entityType']); $subjectTpl = $this->templateFileManager->getTemplate('noteEmailReceived', 'subject', $parentType); diff --git a/application/Espo/Tools/Kanban/Kanban.php b/application/Espo/Tools/Kanban/Kanban.php index ba6fc44dc3..57af8dfe83 100644 --- a/application/Espo/Tools/Kanban/Kanban.php +++ b/application/Espo/Tools/Kanban/Kanban.php @@ -43,6 +43,8 @@ use Espo\{ ORM\EntityManager, }; +use ArrayAccess; + class Kanban { private const DEFAULT_MAX_ORDER_NUMBER = 50; @@ -240,7 +242,8 @@ class Kanban if ( $maxSize && is_countable($collectionSub) && - count($collectionSub) > $maxSize + count($collectionSub) > $maxSize && + $collectionSub instanceof ArrayAccess ) { $totalSub = -1; @@ -289,6 +292,8 @@ class Kanban protected function getStatusField(): string { + assert(is_string($this->entityType)); + $statusField = $this->metadata->get(['scopes', $this->entityType, 'statusField']); if (!$statusField) { @@ -304,6 +309,8 @@ class Kanban */ protected function getStatusList(): array { + assert(is_string($this->entityType)); + $statusField = $this->getStatusField(); $statusList = $this->metadata->get(['entityDefs', $this->entityType, 'fields', $statusField, 'options']); @@ -320,6 +327,8 @@ class Kanban */ protected function getStatusIgnoreList(): array { + assert(is_string($this->entityType)); + return $this->metadata->get(['scopes', $this->entityType, 'kanbanStatusIgnoreList'], []); } } diff --git a/application/Espo/Tools/LabelManager/LabelManager.php b/application/Espo/Tools/LabelManager/LabelManager.php index b95709fb73..4f4129d19f 100644 --- a/application/Espo/Tools/LabelManager/LabelManager.php +++ b/application/Espo/Tools/LabelManager/LabelManager.php @@ -29,6 +29,8 @@ namespace Espo\Tools\LabelManager; +use Espo\Core\Utils\Json; + use Espo\Core\{ Di, Utils\Language, @@ -214,7 +216,7 @@ class LabelManager implements } } - return json_decode(json_encode($finalData)); + return json_decode(Json::encode($finalData)); } /** @@ -291,6 +293,6 @@ class LabelManager implements $languageObj->save(); - return json_decode(json_encode($returnDataHash)); + return json_decode(Json::encode($returnDataHash)); } } diff --git a/application/Espo/Tools/LeadCapture/LeadCapture.php b/application/Espo/Tools/LeadCapture/LeadCapture.php index 794add0cfc..9dbd38ee85 100644 --- a/application/Espo/Tools/LeadCapture/LeadCapture.php +++ b/application/Espo/Tools/LeadCapture/LeadCapture.php @@ -425,7 +425,8 @@ class LeadCapture if (is_string($terminateAt) && time() > strtotime($terminateAt)) { return (object) [ 'status' => 'expired', - 'message' => $this->defaultLanguage->translate('optInConfirmationExpired', 'messages', 'LeadCapture'), + 'message' => $this->defaultLanguage + ->translateLabel('optInConfirmationExpired', 'messages', 'LeadCapture'), ]; } @@ -545,7 +546,7 @@ class LeadCapture $linkHtml = '' . - $this->defaultLanguage->translate('Confirm Opt-In', 'labels', 'LeadCapture') . + $this->defaultLanguage->translateLabel('Confirm Opt-In', 'labels', 'LeadCapture') . ''; $body = str_replace('{optInUrl}', $url, $body);