Skip to content

Commit

Permalink
dev/core#3880 - Clone searchKit displays along with the saved search
Browse files Browse the repository at this point in the history
Moves the clickable link to the export dialog
  • Loading branch information
colemanw committed Sep 28, 2024
1 parent 642a2be commit 7069395
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 17 deletions.
39 changes: 39 additions & 0 deletions ext/search_kit/ang/crmSearchAdmin.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
angular.module('crmSearchAdmin', CRM.angRequires('crmSearchAdmin'))

.config(function($routeProvider) {
const ts = CRM.ts('org.civicrm.search_kit');

$routeProvider.when('/list', {
controller: 'searchList',
reloadOnSearch: false,
Expand Down Expand Up @@ -41,6 +43,30 @@
}
}
});
$routeProvider.when('/clone/:id', {
controller: 'searchClone',
template: '<crm-search-admin saved-search="$ctrl.savedSearch"></crm-search-admin>',
resolve: {
// Load saved search
savedSearch: function($route, crmApi4) {
var params = $route.current.params;
return crmApi4('SavedSearch', 'get', {
select: ['label', 'description', 'api_entity', 'api_params', 'expires_date', 'GROUP_CONCAT(DISTINCT entity_tag.tag_id) AS tag_id'],
where: [['id', '=', params.id]],
join: [
['EntityTag AS entity_tag', 'LEFT', ['entity_tag.entity_table', '=', '"civicrm_saved_search"'], ['id', '=', 'entity_tag.entity_id']],
],
groupBy: ['id'],
chain: {
displays: ['SearchDisplay', 'get', {
select: ['label', 'type', 'settings'],
where: [['saved_search_id', '=', '$id']],
}]
}
}, 0);
}
}
});
})

// Controller for tabbed view of SavedSearches
Expand Down Expand Up @@ -100,6 +126,19 @@
$scope.$ctrl = this;
})

// Controller for cloning a SavedSearch
.controller('searchClone', function($scope, savedSearch) {
searchEntity = savedSearch.api_entity;
savedSearch.label += ' (' + ts('copy') + ')';
delete savedSearch.id;
savedSearch.displays.forEach(display => {
delete display.id;
display.label += ' (' + ts('copy') + ')';
});
this.savedSearch = savedSearch;
$scope.$ctrl = this;
})

.factory('searchMeta', function($q, crmApi4, formatForSelect2) {
function getEntity(entityName) {
if (entityName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
});

// Set default label
ctrl.savedSearch.label = ts('%1 Search by %2', {
ctrl.savedSearch.label = ctrl.savedSearch.label || ts('%1 Search by %2', {
1: searchMeta.getEntity(ctrl.savedSearch.api_entity).title,
2: CRM.crmSearchAdmin.myName
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

angular.module('crmSearchAdmin').component('crmSearchAdminExport', {
bindings: {
savedSearchId: '<',
savedSearchName: '<',
displayNames: '<'
savedSearch: '<'
},
templateUrl: '~/crmSearchAdmin/crmSearchAdminExport.html',
controller: function ($scope, $element, crmApi4) {
Expand All @@ -20,15 +18,17 @@
];

this.$onInit = function() {
this.apiExplorerLink = CRM.url('civicrm/api4#/explorer/SavedSearch/export?_format=php&cleanup=always&id=' + ctrl.savedSearchId);
this.apiExplorerLink = CRM.url('civicrm/api4#/explorer/SavedSearch/export?_format=php&cleanup=always&id=' + ctrl.savedSearch.id);
this.simpleLink = CRM.url('civicrm/admin/search#/create/' + ctrl.savedSearch.api_entity + '?params=' + encodeURI(angular.toJson(ctrl.savedSearch.api_params)));

var findDisplays = _.transform(ctrl.displayNames, function(findDisplays, displayName) {
findDisplays.push(['search_displays', 'CONTAINS', ctrl.savedSearchName + '.' + displayName]);
}, [['search_displays', 'CONTAINS', ctrl.savedSearchName]]);
var apiCalls = [
['SavedSearch', 'export', {id: ctrl.savedSearchId}],
let apiCalls = [
['SavedSearch', 'export', {id: ctrl.savedSearch.id}],
];
if (ctrl.afformEnabled) {
let findDisplays = [['search_displays', 'CONTAINS', ctrl.savedSearch.name]];
ctrl.savedSearch.display_name.forEach(displayName => {
findDisplays.push(['search_displays', 'CONTAINS', `${ctrl.savedSearch.name}.${displayName}`]);
});
apiCalls.push(['Afform', 'get', {layoutFormat: 'html', where: [['type', '=', 'search'], ['OR', findDisplays]]}]);
}
crmApi4(apiCalls)
Expand Down
13 changes: 12 additions & 1 deletion ext/search_kit/ang/crmSearchAdmin/crmSearchAdminExport.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
<i class="crm-i fa-info-circle"></i>
{{:: ts('Search configuration can be copied from here, then pasted into "Import" to e.g. transfer between sites.') }}
</p>
<p>
<i class="crm-i fa-link"></i>
{{:: ts('Copyable link:') }}
<a ng-href="{{:: $ctrl.simpleLink }}" target="_blank">
<u>{{:: ts('Saved Search Criteria') }}</u>
</a>
<p>
<i class="crm-i fa-suitcase"></i>
{{:: ts('To package for distribution in an extension, use') }}
<a ng-href="https://docs.civicrm.org/dev/en/latest/extensions/civix/#export" target="_blank">
<u>{{:: ts('Civix Export') }}</u>
<i class="crm-i fa-file-circle-question"></i>
</a>
{{:: ts('or') }}
<a ng-href="{{:: $ctrl.apiExplorerLink }}" target="_blank">
<u>{{:: ts('API Explorer Export') }}</u>
<u>{{:: ts('API Explorer') }}</u>
<i class="crm-i fa-external-link"></i>
</a>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</a>
</li>
<li title="{{:: ts('Create a new search based on this one') }}">
<a ng-href="#/create/{{:: row.data.api_entity + '?params=' + $ctrl.encode(row.data.api_params) }}">
<a ng-href="#/clone/{{:: row.data.id }}">
<i class="crm-i fa-copy"></i>
{{:: ts('Clone...') }}
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@
updateAfformCounts();
});

this.encode = function(params) {
return encodeURI(angular.toJson(params));
};

this.deleteOrRevert = function(row) {
var search = row.data,
revert = !!search['base_module:label'];
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<crm-search-admin-export saved-search-id="model.data.id" saved-search-name="model.data.name" display-names="model.data.display_name"></crm-search-admin-export>
<crm-search-admin-export saved-search="model.data"></crm-search-admin-export>

0 comments on commit 7069395

Please sign in to comment.