Skip to content

Commit 91731af

Browse files
committed
crmSearchDisplay - enable using default columns in table search displays
1 parent d553f52 commit 91731af

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

ext/search_kit/Civi/Api4/Action/SearchDisplay/Run.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class Run extends AbstractRunAction {
4646
protected function processResult(SearchDisplayRunResult $result) {
4747
$entityName = $this->savedSearch['api_entity'];
4848
$apiParams =& $this->_apiParams;
49+
if (!empty($this->display['settings']['useDefaultSearchColumns'])) {
50+
$defaultDisplay = \Civi\Api4\SearchDisplay::getDefault(FALSE)
51+
->setSavedSearch($this->savedSearch)
52+
->execute()->single();
53+
$this->display['settings']['columns'] = $defaultDisplay['settings']['columns'];
54+
}
4955
$page = $index = NULL;
5056
$key = $this->return;
5157
// Pager can operate in "page" mode for traditional pager, or "scroll" mode for infinite scrolling

ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.component.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,30 @@
3434
// Table can be draggable if the main entity is a SortableEntity.
3535
ctrl.sortableEntity = _.includes(searchMeta.getEntity(ctrl.apiEntity).type, 'SortableEntity');
3636
ctrl.hierarchicalEntity = _.includes(searchMeta.getEntity(ctrl.apiEntity).type, 'HierarchicalEntity');
37-
ctrl.parent.initColumns({label: true, sortable: true});
37+
// if this is a brand new display, start with default columns
38+
// instead of adding columns array
39+
if (!ctrl.display.settings.columns) {
40+
ctrl.display.settings.useDefaultSearchColumns = true;
41+
}
42+
// otherwise run the default columns initialiser
43+
else {
44+
ctrl.parent.initColumns({label: true, sortable: true});
45+
}
3846
};
3947

48+
this.getSetCustomColumns = (value) => {
49+
if (value !== undefined) {
50+
this.display.settings.useDefaultSearchColumns = !value;
51+
// if switching to custom columns and no columns already exist then
52+
// initialise with all the columns to start
53+
if (value && !(this.display.settings.columns && this.display.settings.columns.length)) {
54+
this.parent.initColumns({label: true, sortable: true});
55+
}
56+
return value;
57+
}
58+
return !this.display.settings.useDefaultSearchColumns;
59+
}
60+
4061
this.toggleEditableRowMode = function(name, value) {
4162
ctrl.display.settings.editableRow = ctrl.display.settings.editableRow || {};
4263
if (arguments.length < 2) {

ext/search_kit/ang/crmSearchAdmin/displays/searchAdminDisplayTable.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,19 @@
121121

122122
<fieldset class="crm-search-admin-edit-columns-wrapper">
123123
<legend>
124-
{{:: ts('Columns') }}
124+
{{:: ts('Custom Columns') }}
125125
</legend>
126-
<div ng-include="'~/crmSearchAdmin/displays/common/addColMenu.html'"></div>
127-
<fieldset class="crm-search-admin-edit-columns" ng-model="$ctrl.display.settings.columns" ui-sortable="$ctrl.parent.sortableOptions">
126+
<div class="crm-form-toggle-container">
127+
<input type="checkbox" class="crm-form-toggle" ng-model="$ctrl.getSetCustomColumns" ng-model-options="{getterSetter: true}" />
128+
<span class="crm-form-toggle-text crm-form-toggle-text-on">
129+
{{:: ts('Use custom columns') }}
130+
</span>
131+
<span class="crm-form-toggle-text crm-form-toggle-text-off">
132+
{{:: ts('Use default columns from Search') }}
133+
</span>
134+
</div>
135+
<div ng-if="!$ctrl.display.settings.useDefaultSearchColumns" ng-include="'~/crmSearchAdmin/displays/common/addColMenu.html'"></div>
136+
<fieldset ng-if="!$ctrl.display.settings.useDefaultSearchColumns" class="crm-search-admin-edit-columns" ng-model="$ctrl.display.settings.columns" ui-sortable="$ctrl.parent.sortableOptions">
128137
<fieldset ng-repeat="col in $ctrl.display.settings.columns" class="crm-draggable">
129138
<i class="crm-i fa-arrows crm-search-move-icon" role="img" aria-hidden="true"></i>
130139
<button type="button" class="btn btn-xs pull-right" ng-click="$ctrl.parent.removeCol($index)" title="{{:: ts('Remove') }}">

ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,37 @@
3030
for (let p=0; p < placeholderCount; ++p) {
3131
this.placeholders.push({});
3232
}
33-
this.columns = this.settings.columns.map((column) => {
33+
34+
if (this.settings.useDefaultSearchColumns) {
35+
// start with no columns in case we run before
36+
// we've fetched the right ones
37+
this.columns = [];
38+
// TODO: default permission is access CiviCRM
39+
// need to tweak permissions for frontend forms
40+
crmApi4('SearchDisplay', 'getDefault', {
41+
savedSearch: this.search
42+
})
43+
.then((result) => this.columns = result[0].settings.columns)
44+
.then(() => this.columns.forEach((col) => {
45+
// Used by crmSearchDisplayTable.toggleColumns
46+
col.enabled = true;
47+
col.fetched = true;
48+
}))
49+
.catch((error) => CRM.alert(ts('Error loading search columns')));
50+
}
51+
else {
3452
// Break reference so original settings are preserved
35-
const col = _.cloneDeep(column);
36-
// Used by crmSearchDisplayTable.toggleColumns
37-
col.enabled = true;
38-
col.fetched = true;
39-
return col;
40-
});
53+
this.columns = _.cloneDeep(this.settings.columns);
54+
55+
// Add keys used by crmSearchDisplayTable.toggleColumns
56+
this.columns.forEach((col) => {
57+
col.enabled = true;
58+
col.fetched = true;
59+
});
60+
console.log(this.columns);
61+
console.log(this.settings.columns);
62+
}
63+
4164
_.each(ctrl.onInitialize, function(callback) {
4265
callback.call(ctrl, $scope, $element);
4366
});
@@ -291,13 +314,13 @@
291314
},
292315

293316
getFieldClass: function(colIndex, colData) {
294-
return (colData.cssClass || '') + ' crm-search-col-type-' + this.settings.columns[colIndex].type + (this.settings.columns[colIndex].break ? '' : ' crm-inline-block');
317+
return (colData.cssClass || '') + ' crm-search-col-type-' + this.columns[colIndex].type + (this.columns[colIndex].break ? '' : ' crm-inline-block');
295318
},
296319

297320
getFieldTemplate: function(colIndex, colData) {
298-
let colType = this.settings.columns[colIndex].type;
321+
let colType = this.columns[colIndex].type;
299322
if (colType === 'include') {
300-
return this.settings.columns[colIndex].path;
323+
return this.columns[colIndex].path;
301324
}
302325
if (colType === 'field') {
303326
if (colData.edit) {

0 commit comments

Comments
 (0)