From 0b6a29a0358e8c2c24caed99392df535d7ad0016 Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 26 Aug 2016 12:19:47 +0300 Subject: [PATCH] field manager: reset to detault --- application/Espo/Controllers/FieldManager.php | 13 +++ application/Espo/Core/Utils/FieldManager.php | 17 +++ .../templates/admin/field-manager/edit.tpl | 2 +- client/src/views/admin/field-manager/edit.js | 100 +++++++++++++----- 4 files changed, 103 insertions(+), 29 deletions(-) diff --git a/application/Espo/Controllers/FieldManager.php b/application/Espo/Controllers/FieldManager.php index 0c6ea7891f..0e0ec66aa4 100644 --- a/application/Espo/Controllers/FieldManager.php +++ b/application/Espo/Controllers/FieldManager.php @@ -107,5 +107,18 @@ class FieldManager extends \Espo\Core\Controllers\Base return $res; } + + public function postActionResetToDefault($params, $data) + { + if (empty($data['scope']) || empty($data['name'])) { + throw new BadRequest(); + } + + $this->getContainer()->get('fieldManager')->resetToDefault($data['name'], $data['scope']); + + $this->getContainer()->get('dataManager')->rebuildMetadata(); + + return true; + } } diff --git a/application/Espo/Core/Utils/FieldManager.php b/application/Espo/Core/Utils/FieldManager.php index 91cb5a2c5d..ce0c46ec55 100644 --- a/application/Espo/Core/Utils/FieldManager.php +++ b/application/Espo/Core/Utils/FieldManager.php @@ -156,6 +156,23 @@ class FieldManager return (bool) $res; } + public function resetToDefault($name, $scope) + { + if (!$this->isCore($name, $scope)) { + throw new Error('Cannot reset to default custom field ['.$name.'] in '.$scope); + } + + if (!$this->getMetadata()->get(['entityDefs', $scope, 'fields', $name])) { + throw new Error('Not found field ['.$name.'] in '.$scope); + } + + $this->getMetadata()->delete('entityDefs', $scope, ['fields.' . $name]); + $this->getMetadata()->save(); + + $this->getLanguage()->delete($scope, 'fields', $name); + $this->getLanguage()->save(); + } + protected function setEntityDefs($name, $fieldDefs, $scope) { $fieldDefs = $this->normalizeDefs($name, $fieldDefs, $scope); diff --git a/client/res/templates/admin/field-manager/edit.tpl b/client/res/templates/admin/field-manager/edit.tpl index 170b159e70..79371008e7 100644 --- a/client/res/templates/admin/field-manager/edit.tpl +++ b/client/res/templates/admin/field-manager/edit.tpl @@ -1,5 +1,5 @@
- + {{#unless isCustom}}{{#unless isNew}}{{/unless}}{{/unless}}
diff --git a/client/src/views/admin/field-manager/edit.js b/client/src/views/admin/field-manager/edit.js index 787c9f9947..3bdd446844 100644 --- a/client/src/views/admin/field-manager/edit.js +++ b/client/src/views/admin/field-manager/edit.js @@ -42,6 +42,8 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, paramList: this.paramList, type: this.type, fieldList: this.fieldList, + isCustom: this.defs.isCustom, + isNew: this.isNew, }; }, @@ -52,21 +54,15 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, 'click button[data-action="save"]': function () { this.save(); }, + 'click button[data-action="resetToDefault"]': function () { + this.resetToDefault(); + }, }, - setup: function () { - this.scope = this.options.scope; - this.field = this.options.field; - this.type = this.options.type; + setupFieldData: function (callback) { this.defs = {}; - this.fieldList = []; - this.isNew = false; - if (!this.field) { - this.isNew = true; - } - this.model = new Model(); this.model.name = 'Admin'; this.model.urlRoot = 'Admin/fieldManager/' + this.scope; @@ -74,8 +70,8 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, this.model.defs = { fields: { name: {required: true}, - label: {required: true}, - }, + label: {required: true} + } }; if (!this.isNew) { @@ -87,10 +83,7 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, this.model.set('type', this.type); } - - this.wait(true); this.getModelFactory().create(this.scope, function (model) { - if (!this.isNew) { this.type = model.getFieldType(this.field); } @@ -144,12 +137,28 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, this.createFieldView(o.type, o.name, null, o); }, this); - this.wait(false); + callback(); }.bind(this)); }.bind(this)); }, + setup: function () { + this.scope = this.options.scope; + this.field = this.options.field; + this.type = this.options.type; + + this.isNew = false; + if (!this.field) { + this.isNew = true; + } + + this.wait(true); + this.setupFieldData(function () { + this.wait(false); + }.bind(this)); + }, + afterRender: function () { this.getView('name').on('change', function (m) { var name = this.model.get('name'); @@ -209,25 +218,60 @@ Espo.define('views/admin/field-manager/edit', ['view', 'model'], function (Dep, this.trigger('after:save'); }.bind(this), true); - var langData = this.getLanguage().data; - if (this.scope in langData) { - if (!('fields' in langData[this.scope])) { - langData[this.scope]['fields'] = {}; - } - langData[this.scope]['fields'][this.model.get('name')] = this.model.get('label'); + this.updateLanguage(); - - if (this.getMetadata().get(['fields', this.model.get('type'), 'translatedOptions']) && this.model.get('translatedOptions')) { - langData[this.scope].options = langData[this.scope].options || {}; - langData[this.scope]['options'][this.model.get('name')] = this.model.get('translatedOptions') || {}; - } - } }.bind(this)); this.notify('Saving...'); this.model.save(); }, + updateLanguage: function () { + var langData = this.getLanguage().data; + if (this.scope in langData) { + if (!('fields' in langData[this.scope])) { + langData[this.scope]['fields'] = {}; + } + langData[this.scope]['fields'][this.model.get('name')] = this.model.get('label'); + + if (this.getMetadata().get(['fields', this.model.get('type'), 'translatedOptions']) && this.model.get('translatedOptions')) { + langData[this.scope].options = langData[this.scope].options || {}; + langData[this.scope]['options'][this.model.get('name')] = this.model.get('translatedOptions') || {}; + } + } + }, + + resetToDefault: function () { + if (!confirm(this.translate('confirmation', 'messages'))) return; + + Espo.Ui.notify(this.translate('pleaseWait', 'messages')); + + this.ajaxPostRequest('FieldManager/action/resetToDefault', { + scope: this.scope, + name: this.field + }).then(function () { + Promise.all([ + new Promise(function (resolve) { + this.getMetadata().load(function () { + this.getMetadata().storeToCache(); + resolve(); + }.bind(this), true); + }.bind(this)), + new Promise(function (resolve) { + this.getLanguage().load(function () { + resolve(); + }.bind(this), true); + }.bind(this)) + ]).then(function () { + this.setupFieldData(function () { + this.notify('Done', 'success'); + this.reRender(); + }.bind(this)); + }.bind(this)); + + }.bind(this)); + } + }); });