diff --git a/application/Espo/Controllers/EntityManager.php b/application/Espo/Controllers/EntityManager.php
index 6e89ce0ab9..359e62d46d 100644
--- a/application/Espo/Controllers/EntityManager.php
+++ b/application/Espo/Controllers/EntityManager.php
@@ -62,6 +62,12 @@ class EntityManager extends \Espo\Core\Controllers\Base
if (!empty($data['stream'])) {
$params['stream'] = $data['stream'];
}
+ if (!empty($data['sortBy'])) {
+ $params['sortBy'] = $data['sortBy'];
+ }
+ if (!empty($data['sortDirection'])) {
+ $params['asc'] = $data['sortDirection'] === 'asc';
+ }
$result = $this->getContainer()->get('entityManagerUtil')->create($name, $type, $params);
@@ -90,6 +96,10 @@ class EntityManager extends \Espo\Core\Controllers\Base
$name = $data['name'];
$name = filter_var($name, \FILTER_SANITIZE_STRING);
+ if (!empty($data['sortDirection'])) {
+ $data['asc'] = $data['sortDirection'] === 'asc';
+ }
+
$result = $this->getContainer()->get('entityManagerUtil')->update($name, $data);
if ($result) {
diff --git a/application/Espo/Core/Utils/EntityManager.php b/application/Espo/Core/Utils/EntityManager.php
index 7dcbcd00eb..9bd34a42bf 100644
--- a/application/Espo/Core/Utils/EntityManager.php
+++ b/application/Espo/Core/Utils/EntityManager.php
@@ -193,6 +193,16 @@ class EntityManager
$this->getLanguage()->set('Global', 'scopeNamesPlural', $name, $labelPlural);
}
+ if (isset($data['sortBy'])) {
+ $entityDefsData = array(
+ 'collection' => array(
+ 'sortBy' => $data['sortBy'],
+ 'asc' => !empty($data['asc'])
+ )
+ );
+ $this->getMetadata()->set('entityDefs', $name, $entityDefsData);
+ }
+
$this->getMetadata()->save();
$this->getLanguage()->save();
diff --git a/application/Espo/Resources/i18n/en_US/EntityManager.json b/application/Espo/Resources/i18n/en_US/EntityManager.json
index d5fbf524fc..8219e246da 100644
--- a/application/Espo/Resources/i18n/en_US/EntityManager.json
+++ b/application/Espo/Resources/i18n/en_US/EntityManager.json
@@ -14,7 +14,9 @@
"entityForeign": "Foreign Entity",
"linkForeign": "Foreign Link",
"link": "Link",
- "labelForeign": "Foreign Label"
+ "labelForeign": "Foreign Label",
+ "sortBy": "Default Order (field)",
+ "sortDirection": "Default Order (direction)"
},
"options": {
"type": {
@@ -29,6 +31,10 @@
"manyToOne": "Many-to-One",
"parentToChildren": "Parent-to-Children",
"childrenToParent": "Children-to-Parent"
+ },
+ "sortDirection": {
+ "asc": "Ascending",
+ "desc": "Descending"
}
},
"messages": {
diff --git a/frontend/client/res/templates/admin/entity-manager/modals/edit-entity.tpl b/frontend/client/res/templates/admin/entity-manager/modals/edit-entity.tpl
index f8b9d5a3b0..d902b4d3b7 100644
--- a/frontend/client/res/templates/admin/entity-manager/modals/edit-entity.tpl
+++ b/frontend/client/res/templates/admin/entity-manager/modals/edit-entity.tpl
@@ -26,13 +26,29 @@
+{{#if stream}}
- {{#if stream}}
- {{/if}}
+{{/if}}
+{{#if sortBy}}
+
+{{/if}}
diff --git a/frontend/client/src/views/admin/entity-manager/modals/edit-entity.js b/frontend/client/src/views/admin/entity-manager/modals/edit-entity.js
index 4e4b1474a6..03dfcb70bc 100644
--- a/frontend/client/src/views/admin/entity-manager/modals/edit-entity.js
+++ b/frontend/client/src/views/admin/entity-manager/modals/edit-entity.js
@@ -70,6 +70,9 @@ Espo.define('Views.Admin.EntityManager.Modals.EditEntity', 'Views.Modal', functi
this.model.set('labelPlural', this.translate(scope, 'scopeNamesPlural'));
this.model.set('type', this.getMetadata().get('scopes.' + scope + '.type') || '');
this.model.set('stream', this.getMetadata().get('scopes.' + scope + '.stream') || false);
+
+ this.model.set('sortBy', this.getMetadata().get('entityDefs.' + scope + '.collection.sortBy'));
+ this.model.set('sortDirection', this.getMetadata().get('entityDefs.' + scope + '.collection.asc') ? 'asc' : 'desc');
}
this.createView('type', 'Fields.Enum', {
@@ -131,6 +134,46 @@ Espo.define('Views.Admin.EntityManager.Modals.EditEntity', 'Views.Modal', functi
}
}
});
+
+ if (scope) {
+ var fieldDefs = this.getMetadata().get('entityDefs.' + scope + '.fields') || {}
+ var orderableFieldList = Object.keys(fieldDefs).filter(function (item) {
+ if (fieldDefs[item].notStorable) {
+ return false;
+ }
+ return true;
+ }, this);
+
+ var translatedOptions = {};
+ orderableFieldList.forEach(function (item) {
+ translatedOptions[item] = this.translate(item, 'fields', scope);
+ }, this);
+
+ this.createView('sortBy', 'Fields.Enum', {
+ model: model,
+ mode: 'edit',
+ el: this.options.el + ' .field-sortBy',
+ defs: {
+ name: 'sortBy',
+ params: {
+ options: orderableFieldList,
+ translatedOptions: translatedOptions
+ }
+ }
+ });
+
+ this.createView('sortDirection', 'Fields.Enum', {
+ model: model,
+ mode: 'edit',
+ el: this.options.el + ' .field-sortDirection',
+ defs: {
+ name: 'sortDirection',
+ params: {
+ options: ['asc', 'desc']
+ }
+ }
+ });
+ }
},
afterRender: function () {
@@ -162,6 +205,11 @@ Espo.define('Views.Admin.EntityManager.Modals.EditEntity', 'Views.Modal', functi
'stream'
];
+ if (this.scope) {
+ arr.push('sortBy');
+ arr.push('sortDirection');
+ }
+
var notValid = false;
arr.forEach(function (item) {
@@ -189,16 +237,23 @@ Espo.define('Views.Admin.EntityManager.Modals.EditEntity', 'Views.Modal', functi
var name = this.model.get('name');
+ var data = {
+ name: name,
+ labelSingular: this.model.get('labelSingular'),
+ labelPlural: this.model.get('labelPlural'),
+ type: this.model.get('type'),
+ stream: this.model.get('stream')
+ };
+
+ if (this.scope) {
+ data.sortBy = this.model.get('sortBy');
+ data.sortDirection = this.model.get('sortDirection');
+ }
+
$.ajax({
url: url,
type: 'POST',
- data: JSON.stringify({
- name: name,
- labelSingular: this.model.get('labelSingular'),
- labelPlural: this.model.get('labelPlural'),
- type: this.model.get('type'),
- stream: this.model.get('stream')
- }),
+ data: JSON.stringify(data),
error: function () {
this.$el.find('button[data-name="save"]').removeClass('disabled');
}.bind(this)
diff --git a/frontend/client/src/views/fields/enum.js b/frontend/client/src/views/fields/enum.js
index f5a8c0509c..7de656b7bd 100644
--- a/frontend/client/src/views/fields/enum.js
+++ b/frontend/client/src/views/fields/enum.js
@@ -49,6 +49,10 @@ Espo.define('Views.Fields.Enum', ['Views.Fields.Base'], function (Dep) {
}
}
+ if ('translatedOptions' in this.params) {
+ this.translatedOptions = this.params.translatedOptions;
+ }
+
if (this.params.translation) {
var data = this.getLanguage().data;
var arr = this.params.translation.split('.');