Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#3880 - Clone searchKit displays along with the saved search #31200

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 additions & 2 deletions ext/search_kit/ang/crmSearchAdmin/crmSearchAdmin.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
this.savedSearch.tag_id = this.savedSearch.tag_id || [];
this.groupExists = !!this.savedSearch.groups.length;

if (!this.savedSearch.id) {
const path = $location.path();
// In create mode, set defaults and bind params to route for easy copy/paste
if (path.includes('create/')) {
var defaults = {
version: 4,
select: searchMeta.getEntity(ctrl.savedSearch.api_entity).default_columns,
Expand All @@ -115,7 +117,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,19 @@
];

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]];
if (ctrl.savedSearch.display_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>