SearchKit - Expose default display to the UI
[civicrm-core.git] / ext / afform / admin / ang / afAdmin / afAdminList.controller.js
CommitLineData
9e58ceed
CW
1(function(angular, $, _) {
2 "use strict";
3
4 angular.module('afAdmin').controller('afAdminList', function($scope, afforms, crmApi4, crmStatus) {
67d666c6 5 var ts = $scope.ts = CRM.ts('org.civicrm.afform_admin'),
9e58ceed 6 ctrl = $scope.$ctrl = this;
19a6399f
CW
7 this.sortField = 'title';
8 this.sortDir = false;
9e58ceed
CW
9
10 $scope.crmUrl = CRM.url;
11
a191be25
CW
12 $scope.searchCreateLinks = {};
13
9e58ceed 14 this.tabs = CRM.afAdmin.afform_type;
3d371a03 15 $scope.types = _.indexBy(ctrl.tabs, 'name');
fbcd8c17 16 _.each(['form', 'block', 'search'], function(type) {
3d371a03 17 if ($scope.types[type]) {
fbcd8c17 18 if (type === 'form') {
3d371a03 19 $scope.types.form.default = '#create/form/Individual';
fbcd8c17
CW
20 }
21 }
22 });
adf62101 23 $scope.types.system.options = false;
9e58ceed
CW
24
25 this.afforms = _.transform(afforms, function(afforms, afform) {
fbcd8c17 26 afform.type = afform.type || 'system';
eee24dde
CW
27 // Aggregate a couple fields for the "Placement" column
28 afform.placement = [];
29 if (afform.is_dashlet) {
30 afform.placement.push(ts('Dashboard'));
31 }
32 if (afform['contact_summary:label']) {
33 afform.placement.push(afform['contact_summary:label']);
34 }
fbcd8c17
CW
35 afforms[afform.type] = afforms[afform.type] || [];
36 afforms[afform.type].push(afform);
9e58ceed
CW
37 }, {});
38
19a6399f
CW
39 // Change sort field/direction when clicking a column header
40 this.sortBy = function(col) {
41 ctrl.sortDir = ctrl.sortField === col ? !ctrl.sortDir : false;
42 ctrl.sortField = col;
43 };
44
9e58ceed
CW
45 $scope.$bindToRoute({
46 expr: '$ctrl.tab',
47 param: 'tab',
48 format: 'raw',
49 default: ctrl.tabs[0].name
50 });
51
fbcd8c17
CW
52 this.createLinks = function() {
53 ctrl.searchCreateLinks = '';
adf62101 54 if ($scope.types[ctrl.tab].options) {
fbcd8c17
CW
55 return;
56 }
57 var links = [];
58
59 if (ctrl.tab === 'form') {
60 _.each(CRM.afGuiEditor.entities, function(entity, name) {
41597179 61 if (entity.type === 'primary') {
fbcd8c17
CW
62 links.push({
63 url: '#create/form/' + name,
64 label: entity.label,
65 icon: entity.icon
66 });
67 }
68 });
3d371a03 69 $scope.types.form.options = _.sortBy(links, 'Label');
fbcd8c17
CW
70 }
71
72 if (ctrl.tab === 'block') {
73 _.each(CRM.afGuiEditor.entities, function(entity, name) {
a191be25 74 if (true) { // FIXME: What conditions do we use for block entities?
fbcd8c17
CW
75 links.push({
76 url: '#create/block/' + name,
77 label: entity.label,
a191be25 78 icon: entity.icon || 'fa-cog'
fbcd8c17
CW
79 });
80 }
81 });
a191be25
CW
82 $scope.types.block.options = _.sortBy(links, function(item) {
83 return item.url === '#create/block/*' ? '0' : item.label;
84 });
adf62101 85 // Add divider after the * entity (content block)
a191be25 86 $scope.types.block.options.splice(1, 0, {'class': 'divider', label: ''});
fbcd8c17 87 }
2ef64700
CW
88
89 if (ctrl.tab === 'search') {
5c952e51
CW
90 var searchNames = [];
91 // Non-aggregated query will return the same search multiple times - once per display
92 crmApi4('SavedSearch', 'get', {
93 select: ['name', 'label', 'display.name', 'display.label', 'display.type:icon'],
94 where: [['api_entity', 'IS NOT NULL'], ['api_params', 'IS NOT NULL']],
95 join: [['SearchDisplay AS display', 'LEFT', ['id', '=', 'display.saved_search_id']]],
96 orderBy: {'label':'ASC'}
97 }).then(function(searches) {
98 _.each(searches, function(search) {
99 // Add default display for each search (track searchNames in a var to just add once per search)
100 if (!_.includes(searchNames, search.name)) {
101 searchNames.push(search.name);
102 links.push({
103 url: '#create/search/' + search.name,
104 label: search.label + ': ' + ts('Search results table'),
105 icon: 'fa-table'
106 });
107 }
108 // If the search has no displays (other than the default) this will be empty
109 if (search['display.name']) {
110 links.push({
111 url: '#create/search/' + search.name + '.' + search['display.name'],
112 label: search.label + ': ' + search['display.label'],
113 icon: search['display.type:icon']
114 });
115 }
2ef64700 116 });
5c952e51 117 $scope.types.search.options = links;
2ef64700
CW
118 });
119 }
fbcd8c17
CW
120 };
121
9e58ceed
CW
122 this.revert = function(afform) {
123 var index = _.findIndex(ctrl.afforms[ctrl.tab], {name: afform.name});
124 if (index > -1) {
125 var apiOps = [['Afform', 'revert', {where: [['name', '=', afform.name]]}]];
126 if (afform.has_base) {
127 apiOps.push(['Afform', 'get', {
128 where: [['name', '=', afform.name]],
129 select: ['name', 'title', 'type', 'is_public', 'server_route', 'has_local', 'has_base']
130 }, 0]);
131 }
132 var apiCall = crmStatus(
133 afform.has_base ? {start: ts('Reverting...')} : {start: ts('Deleting...'), success: ts('Deleted')},
134 crmApi4(apiOps)
135 );
136 if (afform.has_base) {
137 afform.has_local = false;
138 apiCall.then(function(result) {
139 ctrl.afforms[ctrl.tab][index] = result[1];
140 });
141 } else {
142 ctrl.afforms[ctrl.tab].splice(index, 1);
143 }
144 }
145 };
146 });
147
148})(angular, CRM.$, CRM._);