Skip to content

Commit

Permalink
Add reloading of lexicon entries for any namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan-Aleev committed Jun 18, 2023
1 parent 1caf318 commit fc0a10b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
7 changes: 4 additions & 3 deletions core/lexicon/en/lexicon.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br /><b>Each Entry will be available via the [[%name]] placeholder.</b>';
$_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';
45 changes: 29 additions & 16 deletions core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use MODX\Revolution\modLexiconEntry;
use MODX\Revolution\Processors\Processor;
use MODX\Revolution\modNamespace;
use MODX\Revolution\modX;

/**
Expand Down Expand Up @@ -41,42 +42,54 @@ 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');

Check warning on line 47 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L45-L47

Added lines #L45 - L47 were not covered by tests

$namespaceObj = $this->modx->getObject(modNamespace::class, $namespace);

Check warning on line 49 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L49

Added line #L49 was not covered by tests

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');

Check warning on line 53 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L51-L53

Added lines #L51 - L53 were not covered by tests

return $this->success();

Check warning on line 55 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L55

Added line #L55 was not covered by tests
}

$namespacePath = $namespaceObj->getCorePath();

Check warning on line 58 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L58

Added line #L58 was not covered by tests

$this->modx->lexicon->clearCache();
$c = $this->modx->newQuery(modLexiconEntry::class);
$c->where([
'namespace' => $namespace,
'language' => $language,
'topic' => $topic,

Check warning on line 65 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L65

Added line #L65 was not covered by tests
]);
$entries = $this->modx->getCollection(modLexiconEntry::class, $c);

$currentTopic = '';
$i = 0;
$names = [];

Check warning on line 70 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L70

Added line #L70 was not covered by tests
/** @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;

Check warning on line 78 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L73-L78

Added lines #L73 - L78 were not covered by tests
}

if (!empty($lex[$entry->get('name')])) {
$i++;
$names[] = $entry->get('name');

Check warning on line 83 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L83

Added line #L83 was not covered by tests
$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)]));

Check warning on line 89 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L88-L89

Added lines #L88 - L89 were not covered by tests
} else {
$this->modx->log(modX::LOG_LEVEL_WARN, $this->modx->lexicon('lexicon_revert_error'));

Check warning on line 91 in core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Workspace/Lexicon/ReloadFromBase.php#L91

Added line #L91 was not covered by tests
}
$this->modx->log(modX::LOG_LEVEL_INFO, 'COMPLETED');

return $this->success($i);
Expand Down
60 changes: 38 additions & 22 deletions manager/assets/modext/workspace/lexicon/lexicon.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ MODx.grid.Lexicon = function(config) {
,handler: this.createEntry
,scope: this
},{
text: _('reload_from_base')
text: _('lexicon_revert')
,handler: this.reloadFromBase
,scope: this
},
Expand Down Expand Up @@ -247,30 +247,46 @@ Ext.extend(MODx.grid.Lexicon,MODx.grid.Grid,{
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
});
const tb = this.getTopToolbar(),
namespace = tb.getComponent('namespace').getValue(),
topic = tb.getComponent('topic').getValue(),
language = tb.getComponent('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';
Expand Down

0 comments on commit fc0a10b

Please sign in to comment.