From 590c0e3fc9a241cfe7504b8d16c01cb3aaa6f119 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 27 Oct 2020 16:18:41 -0400 Subject: [PATCH] Search ext: Support for embedding search displays in afforms --- ext/afform/core/ang/afformStandalone.js | 2 +- ext/afform/gui/ang/afGuiEditor/main.html | 2 +- ext/search/Civi/Search/Admin.php | 27 ++++++++++---- .../crmSearchDisplayTable.component.js | 19 +++++++--- .../crmSearchDisplayTable.html | 4 +-- ext/search/search.php | 35 +++++++++++++++++++ 6 files changed, 75 insertions(+), 14 deletions(-) diff --git a/ext/afform/core/ang/afformStandalone.js b/ext/afform/core/ang/afformStandalone.js index 4d65f38090..4cdf39a0d3 100644 --- a/ext/afform/core/ang/afformStandalone.js +++ b/ext/afform/core/ang/afformStandalone.js @@ -7,7 +7,7 @@ $routeProvider.when('/', { controller: 'AfformStandalonePageCtrl', template: function() { - return '
'; + return '
'; } }); }) diff --git a/ext/afform/gui/ang/afGuiEditor/main.html b/ext/afform/gui/ang/afGuiEditor/main.html index f89bbe1283..5bee0da02a 100644 --- a/ext/afform/gui/ang/afGuiEditor/main.html +++ b/ext/afform/gui/ang/afGuiEditor/main.html @@ -2,7 +2,7 @@
-
+
diff --git a/ext/search/Civi/Search/Admin.php b/ext/search/Civi/Search/Admin.php index 1867ac5b49..6d2a2a7d2f 100644 --- a/ext/search/Civi/Search/Admin.php +++ b/ext/search/Civi/Search/Admin.php @@ -24,11 +24,7 @@ class Admin { return [ 'operators' => \CRM_Utils_Array::makeNonAssociative(self::getOperators()), 'functions' => \CRM_Api4_Page_Api4Explorer::getSqlFunctions(), - 'displayTypes' => \Civi\Api4\SearchDisplay::getFields(FALSE) - ->setLoadOptions(['name', 'label', 'description', 'icon']) - ->addWhere('name', '=', 'type') - ->execute() - ->first()['options'], + 'displayTypes' => self::getDisplayTypes(['name', 'label', 'description', 'icon']), ]; } @@ -57,6 +53,7 @@ class Admin { /** * Fetch all entities the current user has permission to `get` + * @return array */ public static function getSchema() { $schema = []; @@ -89,9 +86,10 @@ class Admin { } /** + * @param array $allowedEntities * @return array */ - public static function getLinks($allowedEntities) { + public static function getLinks(array $allowedEntities) { $results = []; $keys = array_flip(['alias', 'entity', 'joinType']); foreach (civicrm_api4('Entity', 'getLinks', ['where' => [['entity', 'IN', $allowedEntities]]], ['entity' => 'links']) as $entity => $links) { @@ -107,4 +105,21 @@ class Admin { return array_filter($results); } + /** + * @param array $props + * @return array + */ + public static function getDisplayTypes(array $props):array { + try { + return \Civi\Api4\SearchDisplay::getFields(FALSE) + ->setLoadOptions($props) + ->addWhere('name', '=', 'type') + ->execute() + ->first()['options']; + } + catch (\Exception $e) { + return []; + } + } + } diff --git a/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.component.js b/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.component.js index fe3de2165a..6cfef82713 100644 --- a/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.component.js +++ b/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.component.js @@ -5,7 +5,8 @@ bindings: { apiEntity: '<', apiParams: '<', - settings: '<' + settings: '<', + filters: '<' }, templateUrl: '~/crmSearchDisplay/crmSearchDisplayTable.html', controller: function($scope, crmApi4) { @@ -15,9 +16,10 @@ this.page = 1; this.$onInit = function() { - this.orderBy = this.apiParams.orderBy || {}; + this.orderBy = _.cloneDeep(this.apiParams.orderBy || {}); this.limit = parseInt(ctrl.settings.limit || 0, 10); - _.each(ctrl.settings.columns, function(col, num) { + this.columns = _.cloneDeep(ctrl.settings.columns); + _.each(ctrl.columns, function(col, num) { var index = ctrl.apiParams.select.indexOf(col.expr); if (_.includes(col.expr, '(') && !_.includes(col.expr, ' AS ')) { col.expr += ' AS column_' + num; @@ -25,11 +27,18 @@ } col.key = _.last(col.expr.split(' AS ')); }); - getResults(); }; function getResults() { var params = _.merge(_.cloneDeep(ctrl.apiParams), {limit: ctrl.limit, offset: (ctrl.page - 1) * ctrl.limit, orderBy: ctrl.orderBy}); + if (_.isEmpty(params.where)) { + params.where = []; + } + _.each(ctrl.filters, function(value, key) { + if (value) { + params.where.push([key, 'CONTAINS', value]); + } + }); if (ctrl.settings.pager) { params.select.push('row_count'); } @@ -43,6 +52,8 @@ getResults(); }; + $scope.$watch('$ctrl.filters', getResults, true); + /** * Returns crm-i icon class for a sortable column * @param col diff --git a/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.html b/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.html index a980eb5e1e..7583ea3f0a 100644 --- a/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.html +++ b/ext/search/ang/crmSearchDisplay/crmSearchDisplayTable.html @@ -4,7 +4,7 @@ - + {{ col.label }} @@ -15,7 +15,7 @@ - + {{ formatResult(row, col) }} diff --git a/ext/search/search.php b/ext/search/search.php index 45b79b6aa2..88448c8c2d 100644 --- a/ext/search/search.php +++ b/ext/search/search.php @@ -141,3 +141,38 @@ function search_civicrm_pre($op, $entity, $id, &$params) { } } } + +/** + * Injects settings data to search displays embedded in afforms + * + * @param \Civi\Angular\Manager $angular + * @see CRM_Utils_Hook::alterAngular() + */ +function search_civicrm_alterAngular($angular) { + $changeSet = \Civi\Angular\ChangeSet::create('searchSettings') + ->alterHtml(';\\.aff\\.html$;', function($doc, $path) { + $displayTypes = array_column(\Civi\Search\Admin::getDisplayTypes(['name']), 'name'); + + if ($displayTypes) { + $componentNames = 'crm-search-display-' . implode(', crm-search-display-', $displayTypes); + foreach (pq($componentNames, $doc) as $component) { + $searchName = pq($component)->attr('search-name'); + $displayName = pq($component)->attr('display-name'); + if ($searchName && $displayName) { + $display = \Civi\Api4\SearchDisplay::get(FALSE) + ->addWhere('name', '=', $displayName) + ->addWhere('saved_search.name', '=', $searchName) + ->addSelect('settings', 'saved_search.api_entity', 'saved_search.api_params') + ->execute()->first(); + if ($display) { + pq($component)->attr('settings', CRM_Utils_JS::encode($display['settings'] ?? [])); + pq($component)->attr('api-entity', CRM_Utils_JS::encode($display['saved_search.api_entity'])); + pq($component)->attr('api-params', CRM_Utils_JS::encode($display['saved_search.api_params'])); + } + } + } + } + }); + $angular->add($changeSet); + +} -- 2.25.1