From 60988e8eafc07c3e78f4ad00dbde7bbf10808308 Mon Sep 17 00:00:00 2001 From: Ruslan Aleev Date: Mon, 25 Mar 2024 22:24:49 +0400 Subject: [PATCH] Add ability to revert lexicon entries for any namespace (#16447) ### What does it do? Add reloading of lexicon entries for any namespace: - Move the button up, it's was almost invisible. - Added a pop-up window with confirmation, where the namespace, topic and language are indicated. - The name of the strings is now returned to the console, and not just their amount. ![lexicons_reload](https://user-images.githubusercontent.com/12523676/208910173-49426e6e-a70a-4c55-9ced-4e1a0afdcb5a.gif) p.s. In a good way, this PR should be improved and merged after this (https://github.com/modxcms/revolution/pull/15942), although the current version is also working. ### Related issue(s)/PR(s) https://github.com/modxcms/revolution/pull/16333 https://github.com/modxcms/revolution/issues/14442 --------- Co-authored-by: Jason Coward --- core/lexicon/en/lexicon.inc.php | 7 +- .../Workspace/Lexicon/ReloadFromBase.php | 47 ++++++---- .../modext/workspace/lexicon/lexicon.grid.js | 90 +++++++++++-------- 3 files changed, 87 insertions(+), 57 deletions(-) diff --git a/core/lexicon/en/lexicon.inc.php b/core/lexicon/en/lexicon.inc.php index bb193c3c0a6..04d4317e42e 100644 --- a/core/lexicon/en/lexicon.inc.php +++ b/core/lexicon/en/lexicon.inc.php @@ -25,7 +25,8 @@ $_lang['lexicon_import_err_upload'] = 'An error occurred while trying to upload the Lexicon Topic file. Check your web server permissions in trying to upload to the tmp directory, and validate that the file you are trying to upload is a valid file.'; $_lang['lexicon_management'] = 'Lexicon Management'; $_lang['lexicon_management_desc'] = 'Here you can override any Lexicon Entries across Namespaces and their respective Topics. Just double-click on any Entry\'s value to override it. To add new Lexicon Entries or Topics, just create their Topic files in their Namespace\'s respective directory.
Each Entry will be available via the [[%name]] placeholder.'; -$_lang['lexicon_rlfb_msg'] = 'Successfully regenerated [[+num]] strings.'; -$_lang['reload_from_base'] = 'Revert All Core Entries'; -$_lang['reload_success'] = 'Successfully reloaded [[+total]] strings.'; +$_lang['lexicon_revert'] = 'Revert Entries'; +$_lang['lexicon_revert_confirm'] = 'Are you sure you want to revert the "[[+namespace]]" namespace > "[[+topic]]" topic > "[[+language]]" lexicon to its default strings?'; +$_lang['lexicon_revert_success'] = 'Reverted [[+total]] total string(s): [[+names]]'; +$_lang['lexicon_revert_error'] = 'No lexicon entries found to revert.'; $_lang['topic'] = 'Topic'; diff --git a/core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php b/core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php index 70ecd4f5867..c3a7c350181 100644 --- a/core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php +++ b/core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php @@ -12,6 +12,7 @@ use MODX\Revolution\modLexiconEntry; use MODX\Revolution\Processors\Processor; +use MODX\Revolution\modNamespace; use MODX\Revolution\modX; /** @@ -41,42 +42,56 @@ public function getLanguageTopics() */ public function process() { - $language = 'en'; - $namespace = 'core'; - $corePath = $this->modx->getOption('core_path', null, MODX_CORE_PATH); + $namespace = $this->getProperty('namespace'); + $topic = $this->getProperty('lexiconTopic'); + $language = $this->getProperty('language'); + + $namespaceObj = $this->modx->getObject(modNamespace::class, $namespace); + + if (empty($namespace) || empty($topic) || empty($language) || empty($namespaceObj)) { + $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('lexicon_revert_error')); + $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED'); + + return $this->success(); + } + + $namespacePath = $namespaceObj->getCorePath(); $this->modx->lexicon->clearCache(); $c = $this->modx->newQuery(modLexiconEntry::class); $c->where([ 'namespace' => $namespace, 'language' => $language, + 'topic' => $topic, ]); $entries = $this->modx->getCollection(modLexiconEntry::class, $c); - $currentTopic = ''; $i = 0; + $names = []; /** @var modLexiconEntry $entry */ foreach ($entries as $entry) { - if ($currentTopic !== $entry->get('topic')) { - $currentTopic = $entry->get('topic'); - - $topicPath = str_replace('//', '/', - $corePath . '/lexicon/' . $language . '/' . $currentTopic . '.inc.php'); - $lex = []; - $_lang = []; - if (file_exists($topicPath)) { - include $topicPath; - $lex = $_lang; - } + $topicPath = str_replace('//', '/', $namespacePath . '/lexicon/' . $language . '/' . $topic . '.inc.php'); + $lex = []; + $_lang = []; + if (file_exists($topicPath)) { + include $topicPath; + $lex = $_lang; } if (!empty($lex[$entry->get('name')])) { $i++; + $names[] = $entry->get('name'); $entry->remove(); } } - $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('reload_success', ['total' => $i])); + if ($i) { + $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('lexicon_revert_success', [ + 'total' => $i, 'names' => implode(', ', $names) + ])); + } else { + $this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('lexicon_revert_error')); + } $this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED'); return $this->success($i); diff --git a/manager/assets/modext/workspace/lexicon/lexicon.grid.js b/manager/assets/modext/workspace/lexicon/lexicon.grid.js index 3202445ec90..7d7d5440d10 100644 --- a/manager/assets/modext/workspace/lexicon/lexicon.grid.js +++ b/manager/assets/modext/workspace/lexicon/lexicon.grid.js @@ -60,6 +60,10 @@ MODx.grid.Lexicon = function(config = {}) { ,cls: 'primary-button' ,handler: this.createEntry ,scope: this + },{ + text: _('lexicon_revert') + ,handler: this.reloadFromBase + ,scope: this }, '->', { @@ -215,13 +219,6 @@ MODx.grid.Lexicon = function(config = {}) { this.getClearFiltersButton(`filter-namespace:core, filter-topic:default, filter-language:${this.currentLanguage}, filter-query`) ] } - ,pagingItems: [ - { - text: _('reload_from_base') - ,handler: this.reloadFromBase - ,scope: this - } - ] }); MODx.grid.Lexicon.superclass.constructor.call(this,config); }; @@ -248,55 +245,72 @@ Ext.extend(MODx.grid.Lexicon,MODx.grid.Grid,{ } ,loadWindow2: function(btn,e,o) { - var tb = this.getTopToolbar(); - this.menu.record = { - 'namespace': tb.getComponent('namespace').getValue() - ,language: tb.getComponent('language').getValue() + this.menu.record = { + namespace: this.getFilterComponent('filter-namespace').getValue(), + language: this.getFilterComponent('filter-language').getValue() }; if (o.xtype != 'modx-window-lexicon-import') { - this.menu.record.topic = tb.getComponent('topic').getValue(); + this.menu.record.topic = this.getFilterComponent('filter-topic').getValue(); } - this.loadWindow(btn, e, o); + this.loadWindow(btn, e, o); } + ,reloadFromBase: function() { - Ext.Ajax.timeout = 0; - var topic = '/workspace/lexicon/reload/'; - this.console = MODx.load({ - xtype: 'modx-console' - ,register: 'mgr' - ,topic: topic - }); + namespace = this.getFilterComponent('filter-namespace').getValue(), + topic = this.getFilterComponent('filter-topic').getValue(), + language = this.getFilterComponent('filter-language').getValue(), + registryTopic = '/workspace/lexicon/reload/'; - this.console.on('complete',function(){ - this.refresh(); - },this); - this.console.show(Ext.getBody()); + MODx.msg.confirm({ + text: _('lexicon_revert_confirm', { + namespace: namespace + ,topic: topic + ,language: language + }) + ,url: this.config.url + ,params: { + action: 'Workspace/Lexicon/ReloadFromBase' + ,register: 'mgr' + ,topic: registryTopic + ,namespace: namespace + ,lexiconTopic: topic + ,language: language + } + ,listeners: { + 'success': { + fn:function() { + this.console = MODx.load({ + xtype: 'modx-console' + ,register: 'mgr' + ,topic: registryTopic + }); - MODx.Ajax.request({ - url: this.config.url - ,params: {action: 'Workspace/Lexicon/ReloadFromBase' ,register: 'mgr' ,topic: topic} - ,listeners: { - 'success': {fn:function(r) { - this.refresh(); - },scope:this} - } - }); + this.console.on('complete',function(){ + this.refresh(); + },this); + this.console.show(Ext.getBody()); + } + ,scope:this + } + } + }); } ,revertEntry: function() { var p = this.menu.record; p.action = 'Workspace/Lexicon/Revert'; - MODx.Ajax.request({ - url: this.config.url - ,params: p - ,listeners: { + MODx.Ajax.request({ + url: this.config.url + ,params: p + ,listeners: { 'success': {fn:function(r) { this.refresh(); },scope:this} } - }); + }); } + ,getMenu: function() { var r = this.getSelectionModel().getSelected(); var m = [];