From ece49bc4dd4e2945768a911886d008bb8bb5f544 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 7 Sep 2020 12:50:02 +0300 Subject: [PATCH] label manager in tool --- application/Espo/Controllers/LabelManager.php | 56 ++-- .../Espo/Tools/LabelManager/LabelManager.php | 239 ++++++++++++++++++ 2 files changed, 277 insertions(+), 18 deletions(-) create mode 100644 application/Espo/Tools/LabelManager/LabelManager.php diff --git a/application/Espo/Controllers/LabelManager.php b/application/Espo/Controllers/LabelManager.php index d69a3cab35..86eb891fa9 100644 --- a/application/Espo/Controllers/LabelManager.php +++ b/application/Espo/Controllers/LabelManager.php @@ -29,49 +29,69 @@ namespace Espo\Controllers; -use Espo\Core\Utils as Utils; -use Espo\Core\Exceptions\NotFound; -use Espo\Core\Exceptions\Error; -use Espo\Core\Exceptions\Forbidden; -use Espo\Core\Exceptions\BadRequest; +use Espo\Core\{ + Exceptions\Forbidden, + Exceptions\BadRequest, + Api\Request, + DataManager, +}; -class LabelManager extends \Espo\Core\Controllers\Base +use Espo\{ + Tools\LabelManager\LabelManager as LabelManagerTool, + Entities\User, +}; + +class LabelManager { + protected $user; + protected $dataManager; + protected $labelManagerTool; + + public function __construct(User $user, DataManager $dataManager, LabelManagerTool $labelManagerTool) + { + $this->user = $user; + $this->dataManager = $dataManager; + $this->labelManagerTool = $labelManagerTool; + + $this->checkControllerAccess(); + } + protected function checkControllerAccess() { - if (!$this->getUser()->isAdmin()) { + if (!$this->user->isAdmin()) { throw new Forbidden(); } } - public function postActionGetScopeList($params) + public function postActionGetScopeList() { - $labelManager = $this->getContainer()->get('injectableFactory')->create('\\Espo\\Core\\Utils\\LabelManager'); - - return $labelManager->getScopeList(); + return $this->labelManagerTool->getScopeList(); } - public function postActionGetScopeData($params, $data, $request) + public function postActionGetScopeData(Request $request) { + $data = $request->getParsedBody(); + if (empty($data->scope) || empty($data->language)) { throw new BadRequest(); } - $labelManager = $this->getContainer()->get('injectableFactory')->create('\\Espo\\Core\\Utils\\LabelManager'); - return $labelManager->getScopeData($data->language, $data->scope); + + return $this->labelManagerTool->getScopeData($data->language, $data->scope); } - public function postActionSaveLabels($params, $data) + public function postActionSaveLabels(Request $request) { + $data = $request->getParsedBody(); + if (empty($data->scope) || empty($data->language) || !isset($data->labels)) { throw new BadRequest(); } $labels = get_object_vars($data->labels); - $labelManager = $this->getContainer()->get('injectableFactory')->create('\\Espo\\Core\\Utils\\LabelManager'); - $returnData = $labelManager->saveLabels($data->language, $data->scope, $labels); + $returnData = $this->labelManagerTool->saveLabels($data->language, $data->scope, $labels); - $this->getContainer()->get('dataManager')->clearCache(); + $this->dataManager->clearCache(); return $returnData; } diff --git a/application/Espo/Tools/LabelManager/LabelManager.php b/application/Espo/Tools/LabelManager/LabelManager.php new file mode 100644 index 0000000000..834bb25451 --- /dev/null +++ b/application/Espo/Tools/LabelManager/LabelManager.php @@ -0,0 +1,239 @@ +defaultLanguage; + + $data = $languageObj->getAll(); + foreach (array_keys($data) as $scope) { + if (!in_array($scope, $scopeList)) { + $scopeList[] = $scope; + } + } + + foreach ($this->metadata->get('scopes') as $scope => $data) { + if (!in_array($scope, $scopeList)) { + $scopeList[] = $scope; + } + } + + return $scopeList; + } + + public function getScopeData($language, $scope) + { + $languageObj = new Language($language, $this->fileManager, $this->metadata); + + $data = $languageObj->get($scope); + + if (empty($data)) { + return (object) []; + } + + if ($this->metadata->get(['scopes', $scope, 'entity'])) { + + if (empty($data['fields'])) { + $data['fields'] = []; + } + foreach ($this->metadata->get(['entityDefs', $scope, 'fields']) as $field => $item) { + if (!array_key_exists($field, $data['fields'])) { + $data['fields'][$field] = $languageObj->get('Global.fields.' . $field); + if (is_null($data['fields'][$field])) { + $data['fields'][$field] = ''; + } + } + } + if (empty($data['links'])) { + $data['links'] = []; + } + foreach ($this->metadata->get(['entityDefs', $scope, 'links']) as $link => $item) { + if (!array_key_exists($link, $data['links'])) { + $data['links'][$link] = $languageObj->get('Global.links.' . $link); + if (is_null($data['links'][$link])) { + $data['links'][$link] = ''; + } + } + } + + if (empty($data['labels'])) { + $data['labels'] = []; + } + if (!array_key_exists('Create ' . $scope, $data['labels'])) { + $data['labels']['Create ' . $scope] = ''; + } + } + + foreach ($this->metadata->get(['entityDefs', $scope, 'fields'], []) as $field => $item) { + if (!$this->metadata->get(['entityDefs', $scope, 'fields', $field, 'options'])) continue; + $optionsData = []; + $optionList = $this->metadata->get(['entityDefs', $scope, 'fields', $field, 'options'], []); + if (!array_key_exists('options', $data)) { + $data['options'] = []; + } + if (!array_key_exists($field, $data['options'])) { + $data['options'][$field] = []; + } + foreach ($optionList as $option) { + if (empty($option)) continue; + $optionsData[$option] = $option; + if (array_key_exists($option, $data['options'][$field])) { + if (!empty($data['options'][$field][$option])) { + $optionsData[$option] = $data['options'][$field][$option]; + } + } + } + $data['options'][$field] = $optionsData; + } + + if ($scope === 'Global') { + if (empty($data['scopeNames'])) { + $data['scopeNames'] = []; + } + if (empty($data['scopeNamesPlural'])) { + $data['scopeNamesPlural'] = []; + } + foreach ($this->metadata->get(['scopes']) as $scopeKey => $item) { + if (!empty($item['entity'])) { + if (empty($data['scopeNamesPlural'][$scopeKey])) { + $data['scopeNamesPlural'][$scopeKey] = ''; + } + } + if (empty($data['scopeNames'][$scopeKey])) { + $data['scopeNames'][$scopeKey] = ''; + } + } + } + + foreach ($data as $key => $value) { + if (empty($value)) { + unset($data[$key]); + } + } + + $finalData = []; + + foreach ($data as $category => $item) { + if (in_array($scope . '.' . $category, $this->ignoreList)) continue; + foreach ($item as $key => $categoryItem) { + if (is_array($categoryItem)) { + foreach ($categoryItem as $subKey => $subItem) { + $finalData[$category][$category .'[.]' . $key .'[.]' . $subKey] = $subItem; + } + } else { + $finalData[$category][$category .'[.]' . $key] = $categoryItem; + } + } + } + + return $finalData; + } + + public function saveLabels($language, $scope, $labels) + { + $languageObj = new Language($language, $this->fileManager, $this->metadata); + $languageOriginalObj = new Language($language, $this->fileManager, $this->metadata, false, true); + + $returnDataHash = []; + + foreach ($labels as $key => $value) { + $arr = explode('[.]', $key); + $category = $arr[0]; + $name = $arr[1]; + + $setPath = [$scope, $category, $name]; + + $setValue = null; + + if (count($arr) == 2) { + if ($value !== '') { + $languageObj->set($scope, $category, $name, $value); + $setValue = $value; + } else { + $setValue = $languageOriginalObj->get(implode('.', [$scope, $category, $name])); + if (is_null($setValue) && $scope !== 'Global') { + $setValue = $languageOriginalObj->get(implode('.', ['Global', $category, $name])); + } + $languageObj->delete($scope, $category, $name); + } + } else if (count($arr) == 3) { + $name = $arr[1]; + $attribute = $arr[2]; + $data = $languageObj->get($scope . '.' . $category . '.' . $name); + + $setPath[] = $attribute; + + if (is_array($data)) { + if ($value !== '') { + $data[$attribute] = $value; + $setValue = $value; + } else { + $dataOriginal = $languageOriginalObj->get($scope . '.' . $category . '.' . $name); + if (is_array($dataOriginal) && isset($dataOriginal[$attribute])) { + $data[$attribute] = $dataOriginal[$attribute]; + $setValue = $dataOriginal[$attribute]; + } + } + $languageObj->set($scope, $category, $name, $data); + } + } + + if (!is_null($setValue)) { + $frontKey = implode('[.]', $setPath); + $returnDataHash[$frontKey] = $setValue; + } + } + + $languageObj->save(); + + return $returnDataHash; + } +}