diff --git a/application/Espo/Controllers/Preferences.php b/application/Espo/Controllers/Preferences.php index 4a95b0daba..0b7ceaf955 100644 --- a/application/Espo/Controllers/Preferences.php +++ b/application/Espo/Controllers/Preferences.php @@ -24,6 +24,7 @@ namespace Espo\Controllers; use \Espo\Core\Exceptions\Error; use \Espo\Core\Exceptions\Forbidden; +use \Espo\Core\Exceptions\BadRequest; use \Espo\Core\Exceptions\NotFound; class Preferences extends \Espo\Core\Controllers\Base @@ -52,6 +53,17 @@ class Preferences extends \Espo\Core\Controllers\Base } } + public function actionDelete($params, $data) + { + $userId = $params['id']; + if (empty($userId)) { + throw new BadRequest(); + } + $this->handleUserAccess($userId); + + return $this->getEntityManager()->getRepository('Preferences')->resetToDefaults($userId); + } + public function actionPatch($params, $data) { return $this->actionUpdate($params, $data); diff --git a/application/Espo/Repositories/Preferences.php b/application/Espo/Repositories/Preferences.php index 086c8f7be7..20386da253 100644 --- a/application/Espo/Repositories/Preferences.php +++ b/application/Espo/Repositories/Preferences.php @@ -120,6 +120,15 @@ class Preferences extends \Espo\Core\ORM\Repository return true; } } + + public function resetToDefaults($userId) + { + $fileName = $this->getFilePath($userId); + $this->getFileManager()->unlink($fileName); + if ($entity = $this->get($userId)) { + return $entity->toArray(); + } + } public function find(array $params) { diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index 9e139a02da..1bd60423a7 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -143,9 +143,11 @@ "fieldShouldBeLess": "{field} should be less then {value}", "fieldShouldBeGreater": "{field} should be greater then {value}", "fieldBadPasswordConfirm": "{field} confirmed improperly", + "resetPreferencesDone": "Preferences has been reset to defaults", "assignmentEmailNotificationSubject": "EspoCRM {entityType}: {Entity.name}", "assignmentEmailNotificationBody": "{assignerUserName} has assigned {entityType} '{Entity.name}' to you\n\n{recordUrl}", "confirmation": "Are you sure?", + "resetPreferencesConfirmation": "Are you sure you want to reset preferences to defaults?", "removeRecordConfirmation": "Are you sure you want to remove the record?", "unlinkRecordConfirmation": "Are you sure you want to unlink relationship?", "removeSelectedRecordsConfirmation": "Are you sure you want to remove selected records?" diff --git a/frontend/client/src/views/preferences/record/edit.js b/frontend/client/src/views/preferences/record/edit.js index 83d7f8abaf..04f22dc3ef 100644 --- a/frontend/client/src/views/preferences/record/edit.js +++ b/frontend/client/src/views/preferences/record/edit.js @@ -34,7 +34,12 @@ Espo.define('Views.Preferences.Record.Edit', 'Views.Record.Edit', function (Dep) { name: 'cancel', label: 'Cancel', - } + }, + { + name: 'reset', + label: 'Reset', + style: 'danger' + } ], dependencyDefs: { @@ -68,6 +73,21 @@ Espo.define('Views.Preferences.Record.Edit', 'Views.Record.Edit', function (Dep) }, + actionReset: function () { + if (confirm(this.translate('resetPreferencesConfirmation', 'messages'))) { + $.ajax({ + url: 'Preferences/' + this.model.id, + type: 'DELETE', + + }).done(function (data) { + Espo.Ui.success(this.translate('resetPreferencesDone', 'messages')); + this.model.set(data); + this.getPreferences().set(this.model.toJSON()); + this.getPreferences().trigger('update'); + }.bind(this)); + } + }, + afterRender: function () { Dep.prototype.afterRender.call(this);