From 089402ed5c15e59476d8a259ec4fa16813aa4c9f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 25 Nov 2021 20:12:32 -0500 Subject: [PATCH] SearchKit - Use order_by from metadata to supply default sort for searchDisplays --- .../Civi/Api4/Action/SearchDisplay/GetDefault.php | 8 ++++++++ ext/search_kit/Civi/Search/Admin.php | 2 +- .../displays/searchAdminDisplayGrid.component.js | 6 +++++- .../displays/searchAdminDisplayList.component.js | 6 +++++- .../displays/searchAdminDisplayTable.component.js | 7 +++++-- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php index 72bc09b69d..cfdc81a2a0 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetDefault.php @@ -69,9 +69,17 @@ class GetDefault extends \Civi\Api4\Generic\AbstractAction { 'show_count' => TRUE, 'expose_limit' => TRUE, ], + 'sort' => [], 'columns' => [], ], ]; + // Supply default sort if no orderBy given in api params + if (!empty($this->savedSearch['api_entity']) && empty($this->savedSearch['api_params']['orderBy'])) { + $defaultSort = CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'order_by'); + if ($defaultSort) { + $display['settings']['sort'][] = [$defaultSort, 'ASC']; + } + } foreach ($this->getSelectClause() as $key => $clause) { $display['settings']['columns'][] = $this->configureColumn($clause, $key); } diff --git a/ext/search_kit/Civi/Search/Admin.php b/ext/search_kit/Civi/Search/Admin.php index 01de7d95ae..345c6ce1a9 100644 --- a/ext/search_kit/Civi/Search/Admin.php +++ b/ext/search_kit/Civi/Search/Admin.php @@ -98,7 +98,7 @@ class Admin { public static function getSchema() { $schema = []; $entities = \Civi\Api4\Entity::get() - ->addSelect('name', 'title', 'title_plural', 'bridge_title', 'type', 'primary_key', 'description', 'label_field', 'icon', 'dao', 'bridge', 'ui_join_filters', 'searchable') + ->addSelect('name', 'title', 'title_plural', 'bridge_title', 'type', 'primary_key', 'description', 'label_field', 'icon', 'dao', 'bridge', 'ui_join_filters', 'searchable', 'order_by') ->addWhere('searchable', '!=', 'none') ->addOrderBy('title_plural') ->setChain([ diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayGrid.component.js b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayGrid.component.js index 3a55a7aab1..3273fafa1c 100644 --- a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayGrid.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayGrid.component.js @@ -11,7 +11,7 @@ parent: '^crmSearchAdminDisplay' }, templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayGrid.html', - controller: function($scope) { + controller: function($scope, searchMeta) { var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), ctrl = this; @@ -20,8 +20,12 @@ ctrl.display.settings = { colno: '3', limit: CRM.crmSearchAdmin.defaultPagerSize, + sort: [], pager: {} }; + if (searchMeta.getEntity(ctrl.apiEntity).order_by) { + ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']); + } } ctrl.parent.initColumns({}); }; diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js index fe35e0a024..de64dfb92c 100644 --- a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayList.component.js @@ -11,7 +11,7 @@ parent: '^crmSearchAdminDisplay' }, templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayList.html', - controller: function($scope) { + controller: function($scope, searchMeta) { var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), ctrl = this; @@ -35,8 +35,12 @@ ctrl.display.settings = { style: 'ul', limit: CRM.crmSearchAdmin.defaultPagerSize, + sort: [], pager: {} }; + if (searchMeta.getEntity(ctrl.apiEntity).order_by) { + ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']); + } } ctrl.parent.initColumns({}); }; diff --git a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js index 78119bbad1..55b4fbbd5f 100644 --- a/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js @@ -11,7 +11,7 @@ parent: '^crmSearchAdminDisplay' }, templateUrl: '~/crmSearchAdmin/displays/searchAdminDisplayTable.html', - controller: function($scope) { + controller: function($scope, searchMeta) { var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'), ctrl = this; @@ -35,7 +35,10 @@ this.$onInit = function () { if (!ctrl.display.settings) { - ctrl.display.settings = _.extend({}, CRM.crmSearchAdmin.defaultDisplay.settings, {columns: null}); + ctrl.display.settings = _.extend({}, _.cloneDeep(CRM.crmSearchAdmin.defaultDisplay.settings), {columns: null}); + if (searchMeta.getEntity(ctrl.apiEntity).order_by) { + ctrl.display.settings.sort.push([searchMeta.getEntity(ctrl.apiEntity).order_by, 'ASC']); + } } // Displays created prior to 5.43 may not have this property ctrl.display.settings.classes = ctrl.display.settings.classes || []; -- 2.25.1