diff --git a/application/Espo/Core/ORM/Entity.php b/application/Espo/Core/ORM/Entity.php index 14d74b9bae..069a8e9d32 100644 --- a/application/Espo/Core/ORM/Entity.php +++ b/application/Espo/Core/ORM/Entity.php @@ -74,20 +74,31 @@ class Entity extends \Espo\ORM\Entity $defs['additionalColumns'] = $columns; } + $idsAttribute = $field . 'Ids'; + $foreignEntityType = $this->getRelationParam($field, 'entity'); - if ($foreignEntityType && $this->entityManager) { - $foreignEntityDefs = $this->entityManager->getMetadata()->get($foreignEntityType); - if ($foreignEntityDefs && !empty($foreignEntityDefs['collection'])) { - $collectionDefs = $foreignEntityDefs['collection']; - if (!empty($foreignEntityDefs['collection']['orderBy'])) { - $orderBy = $foreignEntityDefs['collection']['orderBy']; - $order = 'ASC'; - if (array_key_exists('order', $foreignEntityDefs['collection'])) { - $order = $foreignEntityDefs['collection']['order']; - } - if (array_key_exists($orderBy, $foreignEntityDefs['fields'])) { - $defs['orderBy'] = $orderBy; - $defs['order'] = $order; + + if ($this->getAttributeParam($idsAttribute, 'orderBy')) { + $defs['orderBy'] = $this->getAttributeParam($idsAttribute, 'orderBy'); + $defs['order'] = 'ASC'; + if ($this->getAttributeParam($idsAttribute, 'orderDirection')) { + $defs['order'] = $this->getAttributeParam($idsAttribute, 'orderDirection'); + } + } else { + if ($foreignEntityType && $this->entityManager) { + $foreignEntityDefs = $this->entityManager->getMetadata()->get($foreignEntityType); + if ($foreignEntityDefs && !empty($foreignEntityDefs['collection'])) { + $collectionDefs = $foreignEntityDefs['collection']; + if (!empty($foreignEntityDefs['collection']['orderBy'])) { + $orderBy = $foreignEntityDefs['collection']['orderBy']; + $order = 'ASC'; + if (array_key_exists('order', $foreignEntityDefs['collection'])) { + $order = $foreignEntityDefs['collection']['order']; + } + if (array_key_exists($orderBy, $foreignEntityDefs['fields'])) { + $defs['orderBy'] = $orderBy; + $defs['order'] = $order; + } } } } @@ -131,7 +142,7 @@ class Entity extends \Espo\ORM\Entity } } - $this->set($field . 'Ids', $ids); + $this->set($idsAttribute, $ids); $this->set($field . 'Names', $names); if ($hasType) { $this->set($field . 'Types', $types); diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/LinkMultiple.php b/application/Espo/Core/Utils/Database/Orm/Fields/LinkMultiple.php index 84640dc5c3..1001be8c72 100644 --- a/application/Espo/Core/Utils/Database/Orm/Fields/LinkMultiple.php +++ b/application/Espo/Core/Utils/Database/Orm/Fields/LinkMultiple.php @@ -44,17 +44,26 @@ class LinkMultiple extends Base ), $fieldName.'Names' => array( 'type' => 'varchar', - 'notStorable' => true, - ), - ), + 'notStorable' => true + ) + ) ), 'unset' => array( $entityName => array( - 'fields.'.$fieldName, - ), - ), + 'fields.'.$fieldName + ) + ) ); + $fieldParams = $this->getFieldParams(); + + if (array_key_exists('orderBy', $fieldParams)) { + $data[$entityName]['fields'][$fieldName . 'Ids']['orderBy'] = $fieldParams['orderBy']; + if (array_key_exists('orderDirection', $fieldParams)) { + $data[$entityName]['fields'][$fieldName . 'Ids']['orderDirection'] = $fieldParams['orderDirection']; + } + } + $columns = $this->getMetadata()->get("entityDefs.{$entityName}.fields.{$fieldName}.columns"); if (!empty($columns)) { $data[$entityName]['fields'][$fieldName . 'Columns'] = array( diff --git a/application/Espo/Modules/Crm/Resources/metadata/clientDefs/Account.json b/application/Espo/Modules/Crm/Resources/metadata/clientDefs/Account.json index a812be0ba6..07a60d624d 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/clientDefs/Account.json +++ b/application/Espo/Modules/Crm/Resources/metadata/clientDefs/Account.json @@ -49,7 +49,8 @@ "relationshipPanels": { "contacts": { "filterList": ["all", "accountActive"], - "layout":"listForAccount" + "layout":"listForAccount", + "orderBy": "name" }, "opportunities":{ "layout":"listForAccount" diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Account.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Account.json index 1da5af6d88..c6ca3c8201 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Account.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Account.json @@ -325,11 +325,14 @@ } }, "collection": { - "sortBy": "name", - "asc": true, + "sortBy": "createdAt", + "asc": false, "textFilterFields": ["name", "emailAddress"] }, "indexes": { + "createdAt": { + "columns": ["createdAt", "deleted"] + }, "name": { "columns": ["name", "deleted"] }, diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json index e74d27351c..79137f25bf 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Call.json @@ -93,7 +93,8 @@ "view": "crm:views/meeting/fields/users", "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "contacts": { "type": "linkMultiple", @@ -102,7 +103,8 @@ "view": "crm:views/meeting/fields/contacts", "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "leads": { "type": "linkMultiple", @@ -111,7 +113,8 @@ "view": "crm:views/meeting/fields/attendees", "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "createdAt": { "type": "datetime", diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Case.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Case.json index 26b1246a32..cd2845ecf1 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Case.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Case.json @@ -48,7 +48,8 @@ }, "contacts": { "type": "linkMultiple", - "view": "crm:views/case/fields/contacts" + "view": "crm:views/case/fields/contacts", + "orderBy": "name" }, "inboundEmail": { "type": "link", diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json index e26e4ac7e2..0bf3ebf97b 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Contact.json @@ -115,7 +115,8 @@ "columns": { "role": "contactRole", "isInactive": "contactIsInactive" - } + }, + "orderBy": "name" }, "accountRole": { "type": "varchar", @@ -404,11 +405,14 @@ } }, "collection": { - "sortBy": "name", - "asc": true, + "sortBy": "createdAt", + "asc": false, "textFilterFields": ["name", "emailAddress"] }, "indexes": { + "createdAt": { + "columns": ["createdAt", "deleted"] + }, "firstName": { "columns": ["firstName", "deleted"] }, diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Meeting.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Meeting.json index 576462b073..7ae2aa74a9 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Meeting.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Meeting.json @@ -88,7 +88,8 @@ "layoutListDisabled": true, "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "contacts": { "type": "linkMultiple", @@ -97,7 +98,8 @@ "view": "crm:views/meeting/fields/contacts", "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "leads": { "type": "linkMultiple", @@ -106,7 +108,8 @@ "layoutListDisabled": true, "columns": { "status": "acceptanceStatus" - } + }, + "orderBy": "name" }, "createdAt": { "type": "datetime", diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json index 7c9c49701f..fb08334269 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json @@ -38,7 +38,8 @@ "view": "crm:views/opportunity/fields/contacts", "columns": { "role": "opportunityRole" - } + }, + "orderBy": "name" }, "stage": { "type": "enum", diff --git a/client/src/views/record/panels/relationship.js b/client/src/views/record/panels/relationship.js index 050a163ce2..390e963c03 100644 --- a/client/src/views/record/panels/relationship.js +++ b/client/src/views/record/panels/relationship.js @@ -131,6 +131,16 @@ Espo.define('views/record/panels/relationship', ['views/record/panels/bottom', ' var sortBy = this.defs.sortBy || null; var asc = this.defs.asc || null; + if (this.defs.orderBy) { + sortBy = this.defs.orderBy; + asc = true; + if (this.defs.orderDirection) { + if (this.defs.orderDirection && (this.defs.orderDirection === true || this.defs.orderDirection.toLowerCase() === 'DESC')) { + asc = false; + } + } + } + this.wait(true); this.getCollectionFactory().create(this.scope, function (collection) { collection.maxSize = this.getConfig().get('recordsPerPageSmall') || 5;