Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix problems in lexicon creation form #15892

Merged
merged 3 commits into from Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions core/src/Revolution/Processors/Workspace/Lexicon/Create.php
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -43,12 +44,17 @@ public function getLanguageTopics()
*/
public function process()
{
if ($this->alreadyExists()) {
$data = [];
foreach ($this->getProperties() as $k => $v) {
$data[$k] = $k === 'value' ? $v : trim($v);
}

if ($this->alreadyExists($data)) {
return $this->failure($this->modx->lexicon('entry_err_ae'));
}

$this->entry = $this->modx->newObject(modLexiconEntry::class);
$this->entry->fromArray($this->getProperties());
$this->entry->fromArray($data);
$this->entry->set('editedon', date('Y-m-d h:i:s'));

if ($this->entry->save() === false) {
Expand All @@ -61,13 +67,13 @@ public function process()
/**
* @return bool
*/
public function alreadyExists()
public function alreadyExists($data)
{
return $this->modx->getCount(modLexiconEntry::class, [
'name' => $this->getProperty('name'),
'namespace' => $this->getProperty('namespace'),
'language' => $this->getProperty('language'),
'topic' => $this->getProperty('topic'),
'name' => $data['name'],
'namespace' => $data['namespace'],
'language' => $data['language'],
'topic' => $data['topic']
]) > 0;
}

Expand Down
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -52,6 +53,9 @@ public function initialize()
if (empty($data)) {
return $this->modx->lexicon('invalid_data');
}
foreach ($data as $k => $v) {
$data[$k] = $k === 'value' ? $v : trim($v);
}
$this->setProperties($data);
$this->unsetProperty('data');

Expand Down
29 changes: 28 additions & 1 deletion core/src/Revolution/modLexiconEntry.php
Expand Up @@ -38,19 +38,46 @@ public function clearCache()
return false;
}

/**
* Ensures required values are present or set to their defaults, when applicable
*
* @return boolean True when valid
*/
private function validateEntry()
{
if (empty($this->get('name'))) {
return false;
}
if (empty($this->get('namespace'))) {
$this->set('namespace', 'core');
}
if (empty($this->get('topic'))) {
$this->set('topic', 'default');
}
if (empty($this->get('language'))) {
$defaultLanguage = $this->xpdo->getOption('cultureKey');
$language = !empty($defaultLanguage) ? $defaultLanguage : 'en' ;
$this->set('language', $language);
}
return true;
}

/**
* Overrides xPDOObject::save to clear lexicon cache on saving.
*
* {@inheritdoc}
*/
public function save($cacheFlag = null)
{
if (!$this->validateEntry()) {
return false;
}
if ($this->_new) {
if (!$this->get('createdon')) {
$this->set('createdon', strftime('%Y-%m-%d %H:%M:%S'));
}
}
$saved = parent:: save($cacheFlag);
$saved = parent::save($cacheFlag);
if ($saved && empty($this->xpdo->config[xPDO::OPT_SETUP])) {
$this->clearCache();
}
Expand Down
9 changes: 9 additions & 0 deletions manager/assets/modext/workspace/lexicon/lexicon.grid.js
Expand Up @@ -405,13 +405,17 @@ MODx.window.LexiconEntryCreate = function(config) {
,itemId: 'name'
,name: 'name'
,anchor: '100%'
,msgTarget: 'under'
,allowBlank: false
},{
xtype: 'modx-combo-namespace'
,fieldLabel: _('namespace')
,name: 'namespace'
,id: 'modx-'+this.ident+'-namespace'
,itemId: 'namespace'
,anchor: '100%'
,msgTarget: 'under'
,allowBlank: false
,listeners: {
'select': {fn: function(cb,r,i) {
cle = this.fp.getComponent('topic');
Expand All @@ -429,20 +433,25 @@ MODx.window.LexiconEntryCreate = function(config) {
,id: 'modx-'+this.ident+'-topic'
,itemId: 'topic'
,anchor: '100%'
,msgTarget: 'under'
,allowBlank: false
},{
xtype: 'modx-combo-language'
,fieldLabel: _('language')
,name: 'language'
,id: 'modx-'+this.ident+'-language'
,itemId: 'language'
,anchor: '100%'
,msgTarget: 'under'
,allowBlank: false
},{
xtype: 'textarea'
,fieldLabel: _('value')
,id: 'modx-'+this.ident+'-value'
,itemId: 'value'
,name: 'value'
,anchor: '100%'
,msgTarget: 'under'
}]
});
MODx.window.LexiconEntryCreate.superclass.constructor.call(this,config);
Expand Down