Expose field label to APIv4 and Search creaor
[civicrm-core.git] / ext / search / ang / search / crmSearch.component.js
CommitLineData
25523059
CW
1(function(angular, $, _) {
2 "use strict";
3
b0422f12 4 angular.module('search').component('crmSearch', {
25523059
CW
5 bindings: {
6 entity: '='
7 },
b0422f12 8 templateUrl: '~/search/crmSearch.html',
25523059
CW
9 controller: function($scope, $element, $timeout, crmApi4, dialogService, searchMeta, formatForSelect2) {
10 var ts = $scope.ts = CRM.ts(),
11 ctrl = this;
12 this.selectedRows = [];
13 this.limit = CRM.cache.get('searchPageSize', 30);
14 this.page = 1;
15 this.params = {};
16 // After a search this.results is an object of result arrays keyed by page,
17 // Prior to searching it's an empty string because 1: falsey and 2: doesn't throw an error if you try to access undefined properties
18 this.results = '';
19 this.rowCount = false;
20 // Have the filters (WHERE, HAVING, GROUP BY, JOIN) changed?
21 this.stale = true;
22 this.allRowsSelected = false;
23
24 $scope.controls = {};
25 $scope.joinTypes = [{k: false, v: ts('Optional')}, {k: true, v: ts('Required')}];
26 $scope.entities = formatForSelect2(CRM.vars.search.schema, 'name', 'title', ['description', 'icon']);
27 this.perm = {
28 editGroups: CRM.checkPerm('edit groups')
29 };
30
31 this.getEntity = searchMeta.getEntity;
32
33 this.paramExists = function(param) {
34 return _.includes(searchMeta.getEntity(ctrl.entity).params, param);
35 };
36
37 $scope.getJoinEntities = function() {
38 var joinEntities = _.transform(CRM.vars.search.links[ctrl.entity], function(joinEntities, link) {
39 var entity = searchMeta.getEntity(link.entity);
40 if (entity) {
41 joinEntities.push({
42 id: link.entity + ' AS ' + link.alias,
43 text: entity.title,
44 description: '(' + link.alias + ')',
45 icon: entity.icon
46 });
47 }
48 }, []);
49 return {results: joinEntities};
50 };
51
52 $scope.addJoin = function() {
53 // Debounce the onchange event using timeout
54 $timeout(function() {
55 if ($scope.controls.join) {
56 ctrl.params.join = ctrl.params.join || [];
57 ctrl.params.join.push([$scope.controls.join, false]);
58 loadFieldOptions();
59 }
60 $scope.controls.join = '';
61 });
62 };
63
64 $scope.changeJoin = function(idx) {
65 if (ctrl.params.join[idx][0]) {
66 ctrl.params.join[idx].length = 2;
67 loadFieldOptions();
68 } else {
69 ctrl.clearParam('join', idx);
70 }
71 };
72
73 $scope.changeGroupBy = function(idx) {
74 if (!ctrl.params.groupBy[idx]) {
75 ctrl.clearParam('groupBy', idx);
76 }
77 };
78
79 /**
80 * Called when clicking on a column header
81 * @param col
82 * @param $event
83 */
84 $scope.setOrderBy = function(col, $event) {
85 var dir = $scope.getOrderBy(col) === 'fa-sort-asc' ? 'DESC' : 'ASC';
86 if (!$event.shiftKey) {
87 ctrl.params.orderBy = {};
88 }
89 ctrl.params.orderBy[col] = dir;
90 };
91
92 /**
93 * Returns crm-i icon class for a sortable column
94 * @param col
95 * @returns {string}
96 */
97 $scope.getOrderBy = function(col) {
98 var dir = ctrl.params.orderBy && ctrl.params.orderBy[col];
99 if (dir) {
100 return 'fa-sort-' + dir.toLowerCase();
101 }
102 return 'fa-sort disabled';
103 };
104
105 $scope.addParam = function(name) {
106 if ($scope.controls[name] && !_.contains(ctrl.params[name], $scope.controls[name])) {
107 ctrl.params[name].push($scope.controls[name]);
108 if (name === 'groupBy') {
109 // Expand the aggregate block
110 $timeout(function() {
111 $('#crm-search-build-group-aggregate.collapsed .collapsible-title').click();
112 }, 10);
113 }
114 }
115 $scope.controls[name] = '';
116 };
117
118 // Deletes an item from an array param
119 this.clearParam = function(name, idx) {
120 ctrl.params[name].splice(idx, 1);
121 };
122
123 // Prevent visual jumps in results table height during loading
124 function lockTableHeight() {
125 var $table = $('.crm-search-results', $element);
126 $table.css('height', $table.height());
127 }
128
129 function unlockTableHeight() {
130 $('.crm-search-results', $element).css('height', '');
131 }
132
133 // Debounced callback for loadResults
134 function _loadResultsCallback() {
135 // Multiply limit to read 2 pages at once & save ajax requests
136 var params = angular.merge({debug: true, limit: ctrl.limit * 2}, ctrl.params);
137 lockTableHeight();
138 $scope.error = false;
139 if (ctrl.stale) {
140 ctrl.page = 1;
141 ctrl.rowCount = false;
142 }
143 if (ctrl.rowCount === false) {
144 params.select.push('row_count');
145 }
146 params.offset = ctrl.limit * (ctrl.page - 1);
147 crmApi4(ctrl.entity, 'get', params).then(function(success) {
148 if (ctrl.stale) {
149 ctrl.results = {};
150 }
151 if (ctrl.rowCount === false) {
152 ctrl.rowCount = success.count;
153 }
154 ctrl.debug = success.debug;
155 // populate this page & the next
156 ctrl.results[ctrl.page] = success.slice(0, ctrl.limit);
157 if (success.length > ctrl.limit) {
158 ctrl.results[ctrl.page + 1] = success.slice(ctrl.limit);
159 }
160 $scope.loading = false;
161 ctrl.stale = false;
162 unlockTableHeight();
163 }, function(error) {
164 $scope.loading = false;
165 ctrl.results = {};
166 ctrl.stale = true;
167 ctrl.debug = error.debug;
168 $scope.error = errorMsg(error);
9c4a1dae
CW
169 })
170 .finally(function() {
171 if (ctrl.debug) {
172 ctrl.debug.params = JSON.stringify(ctrl.params, null, 2);
173 }
174 });
25523059
CW
175 }
176
177 var _loadResults = _.debounce(_loadResultsCallback, 250);
178
179 function loadResults() {
180 $scope.loading = true;
181 _loadResults();
182 }
183
184 // What to tell the user when search returns an error from the server
185 // Todo: parse error codes and give helpful feedback.
186 function errorMsg(error) {
187 return ts('Ensure all search critera are set correctly and try again.');
188 }
189
190 this.changePage = function() {
191 if (ctrl.stale || !ctrl.results[ctrl.page]) {
192 lockTableHeight();
193 loadResults();
194 }
195 };
196
197 this.refreshAll = function() {
198 ctrl.stale = true;
199 ctrl.selectedRows.length = 0;
200 loadResults();
201 };
202
203 // Refresh results while staying on current page.
204 this.refreshPage = function() {
205 lockTableHeight();
206 ctrl.results = {};
207 loadResults();
208 };
209
210 $scope.onClickSearch = function() {
211 if (ctrl.autoSearch) {
212 ctrl.autoSearch = false;
213 } else {
214 ctrl.refreshAll();
215 }
216 };
217
218 $scope.onClickAuto = function() {
219 ctrl.autoSearch = !ctrl.autoSearch;
220 if (ctrl.autoSearch && ctrl.stale) {
221 ctrl.refreshAll();
222 }
0b1769c6 223 $('.crm-search-auto-toggle').blur();
25523059
CW
224 };
225
226 $scope.onChangeLimit = function() {
227 // Refresh only if search has already been run
228 if (ctrl.autoSearch || ctrl.results) {
229 // Save page size in localStorage
230 CRM.cache.set('searchPageSize', ctrl.limit);
231 ctrl.refreshAll();
232 }
233 };
234
235 function onChangeSelect(newSelect, oldSelect) {
236 // Re-arranging or removing columns doesn't merit a refresh, only adding columns does
237 if (!oldSelect || _.difference(newSelect, oldSelect).length) {
238 if (ctrl.autoSearch) {
239 ctrl.refreshPage();
240 } else {
241 ctrl.stale = true;
242 }
243 }
244 }
245
246 function onChangeOrderBy() {
247 if (ctrl.results) {
248 ctrl.refreshPage();
249 }
250 }
251
252 function onChangeFilters() {
253 ctrl.stale = true;
254 ctrl.selectedRows.length = 0;
255 if (ctrl.autoSearch) {
256 ctrl.refreshAll();
257 }
258 }
259
260 $scope.selectAllRows = function() {
261 // Deselect all
262 if (ctrl.allRowsSelected) {
263 ctrl.allRowsSelected = false;
264 ctrl.selectedRows.length = 0;
265 return;
266 }
267 // Select all
268 ctrl.allRowsSelected = true;
269 if (ctrl.page === 1 && ctrl.results[1].length < ctrl.limit) {
270 ctrl.selectedRows = _.pluck(ctrl.results[1], 'id');
271 return;
272 }
273 // If more than one page of results, use ajax to fetch all ids
274 $scope.loadingAllRows = true;
275 var params = _.cloneDeep(ctrl.params);
276 params.select = ['id'];
277 crmApi4(ctrl.entity, 'get', params, ['id']).then(function(ids) {
278 $scope.loadingAllRows = false;
279 ctrl.selectedRows = _.toArray(ids);
280 });
281 };
282
283 $scope.selectRow = function(row) {
284 var index = ctrl.selectedRows.indexOf(row.id);
285 if (index < 0) {
286 ctrl.selectedRows.push(row.id);
287 ctrl.allRowsSelected = (ctrl.rowCount === ctrl.selectedRows.length);
288 } else {
289 ctrl.allRowsSelected = false;
290 ctrl.selectedRows.splice(index, 1);
291 }
292 };
293
294 $scope.isRowSelected = function(row) {
295 return ctrl.allRowsSelected || _.includes(ctrl.selectedRows, row.id);
296 };
297
298 this.getFieldLabel = function(col) {
299 var info = searchMeta.parseExpr(col),
b6b6cb2d 300 label = info.field.label;
25523059
CW
301 if (info.fn) {
302 label = '(' + info.fn.title + ') ' + label;
303 }
304 return label;
305 };
306
307 // Is a column eligible to use an aggregate function?
308 this.canAggregate = function(col) {
309 // If the column is used for a groupBy, no
310 if (ctrl.params.groupBy.indexOf(col) > -1) {
311 return false;
312 }
313 // If the entity this column belongs to is being grouped by id, then also no
314 var info = searchMeta.parseExpr(col);
315 return ctrl.params.groupBy.indexOf(info.prefix + 'id') < 0;
316 };
317
318 $scope.formatResult = function formatResult(row, col) {
319 var info = searchMeta.parseExpr(col),
320 key = info.fn ? (info.fn.name + ':' + info.path + info.suffix) : col,
321 value = row[key];
322 // Handle grouped results
323 if (info.fn && info.fn.name === 'GROUP_CONCAT' && value) {
324 return formatGroupConcatValues(info, value);
325 }
2d198bb4
CW
326 else if (info.fn && info.fn.name === 'COUNT') {
327 return value;
328 }
25523059
CW
329 return formatFieldValue(info.field, value);
330 };
331
332 function formatFieldValue(field, value) {
333 var type = field.data_type;
334 if (value && (type === 'Date' || type === 'Timestamp') && /^\d{4}-\d{2}-\d{2}/.test(value)) {
335 return CRM.utils.formatDate(value, null, type === 'Timestamp');
336 }
337 else if (type === 'Boolean' && typeof value === 'boolean') {
338 return value ? ts('Yes') : ts('No');
339 }
2d198bb4
CW
340 else if (type === 'Money') {
341 return CRM.formatMoney(value);
342 }
25523059
CW
343 return value;
344 }
345
346 function formatGroupConcatValues(info, values) {
347 return _.transform(values.split(','), function(result, val) {
348 if (info.field.options && !info.suffix) {
349 result.push(_.result(getOption(info.field, val), 'label'));
350 } else {
351 result.push(formatFieldValue(info.field, val));
352 }
353 }).join(', ');
354 }
355
356 function getOption(field, value) {
357 return _.find(field.options, function(option) {
358 // Type coersion is intentional
359 return option.id == value;
360 });
361 }
362
363 $scope.fieldsForGroupBy = function() {
364 return {results: getAllFields('', function(key) {
365 return _.contains(ctrl.params.groupBy, key);
366 })
367 };
368 };
369
370 $scope.fieldsForSelect = function() {
371 return {results: getAllFields(':label', function(key) {
372 return _.contains(ctrl.params.select, key);
373 })
374 };
375 };
376
377 $scope.fieldsForWhere = function() {
378 return {results: getAllFields(':name', _.noop)};
379 };
380
381 $scope.fieldsForHaving = function() {
382 return {results: _.transform(ctrl.params.select, function(fields, name) {
383 fields.push({id: name, text: ctrl.getFieldLabel(name)});
384 })};
385 };
386
387 function getDefaultSelect() {
388 return _.filter(['id', 'display_name', 'label', 'title', 'location_type_id:label'], searchMeta.getField);
389 }
390
391 function getAllFields(suffix, disabledIf) {
392 function formatFields(entityName, prefix) {
393 return _.transform(searchMeta.getEntity(entityName).fields, function(result, field) {
394 var item = {
395 id: prefix + field.name + (field.options ? suffix : ''),
b6b6cb2d 396 text: field.label,
25523059
CW
397 description: field.description
398 };
399 if (disabledIf(item.id)) {
400 item.disabled = true;
401 }
402 result.push(item);
403 }, []);
404 }
405
406 var mainEntity = searchMeta.getEntity(ctrl.entity),
407 result = [{
408 text: mainEntity.title,
409 icon: mainEntity.icon,
410 children: formatFields(ctrl.entity, '')
411 }];
412 _.each(ctrl.params.join, function(join) {
413 var joinName = join[0].split(' AS '),
414 joinEntity = searchMeta.getEntity(joinName[0]);
415 result.push({
416 text: joinEntity.title + ' (' + joinName[1] + ')',
417 icon: joinEntity.icon,
418 children: formatFields(joinEntity.name, joinName[1] + '.')
419 });
420 });
421 return result;
422 }
423
424 /**
425 * Fetch pseudoconstants for main entity + joined entities
426 *
427 * Sets an optionsLoaded property on each entity to avoid duplicate requests
428 */
429 function loadFieldOptions() {
430 var mainEntity = searchMeta.getEntity(ctrl.entity),
431 entities = {};
432
433 function enqueue(entity) {
434 entity.optionsLoaded = false;
435 entities[entity.name] = [entity.name, 'getFields', {
436 loadOptions: CRM.vars.search.loadOptions,
437 where: [['options', '!=', false]],
438 select: ['options']
439 }, {name: 'options'}];
440 }
441
442 if (typeof mainEntity.optionsLoaded === 'undefined') {
443 enqueue(mainEntity);
444 }
445 _.each(ctrl.params.join, function(join) {
446 var joinName = join[0].split(' AS '),
447 joinEntity = searchMeta.getEntity(joinName[0]);
448 if (typeof joinEntity.optionsLoaded === 'undefined') {
449 enqueue(joinEntity);
450 }
451 });
452 if (!_.isEmpty(entities)) {
453 crmApi4(entities).then(function(results) {
454 _.each(results, function(fields, entityName) {
455 var entity = searchMeta.getEntity(entityName);
456 _.each(fields, function(options, fieldName) {
457 _.find(entity.fields, {name: fieldName}).options = options;
458 });
459 entity.optionsLoaded = true;
460 });
461 });
462 }
463 }
464
465 this.$onInit = function() {
25523059
CW
466 $scope.$bindToRoute({
467 expr: '$ctrl.params.select',
468 param: 'select',
469 format: 'json',
470 default: getDefaultSelect()
471 });
472 $scope.$watchCollection('$ctrl.params.select', onChangeSelect);
473
474 $scope.$bindToRoute({
475 expr: '$ctrl.params.orderBy',
476 param: 'orderBy',
477 format: 'json',
478 default: {}
479 });
480 $scope.$watchCollection('$ctrl.params.orderBy', onChangeOrderBy);
481
482 $scope.$bindToRoute({
483 expr: '$ctrl.params.where',
484 param: 'where',
485 format: 'json',
486 default: [],
487 deep: true
488 });
489 $scope.$watch('$ctrl.params.where', onChangeFilters, true);
490
491 if (this.paramExists('groupBy')) {
492 $scope.$bindToRoute({
493 expr: '$ctrl.params.groupBy',
494 param: 'groupBy',
495 format: 'json',
496 default: []
497 });
498 }
499 $scope.$watchCollection('$ctrl.params.groupBy', onChangeFilters);
500
501 if (this.paramExists('join')) {
502 $scope.$bindToRoute({
503 expr: '$ctrl.params.join',
504 param: 'join',
505 format: 'json',
506 default: [],
507 deep: true
508 });
509 }
510 $scope.$watch('$ctrl.params.join', onChangeFilters, true);
511
512 if (this.paramExists('having')) {
513 $scope.$bindToRoute({
514 expr: '$ctrl.params.having',
515 param: 'having',
516 format: 'json',
517 default: [],
518 deep: true
519 });
520 }
521 $scope.$watch('$ctrl.params.having', onChangeFilters, true);
27bed3cb
CW
522
523 loadFieldOptions();
25523059
CW
524 };
525
526 $scope.saveGroup = function() {
527 var selectField = ctrl.entity === 'Contact' ? 'id' : 'contact_id';
528 if (ctrl.entity !== 'Contact' && !searchMeta.getField('contact_id')) {
529 CRM.alert(ts('Cannot create smart group from %1.', {1: searchMeta.getEntity(true).title}), ts('Missing contact_id'), 'error', {expires: 5000});
530 return;
531 }
532 var model = {
533 title: '',
534 description: '',
535 visibility: 'User and User Admin Only',
536 group_type: [],
537 id: null,
538 entity: ctrl.entity,
539 params: angular.extend({}, ctrl.params, {version: 4, select: [selectField]})
540 };
541 delete model.params.orderBy;
542 var options = CRM.utils.adjustDialogDefaults({
543 autoOpen: false,
544 title: ts('Save smart group')
545 });
546 dialogService.open('saveSearchDialog', '~/search/saveSmartGroup.html', model, options);
547 };
548 }
549 });
550
551})(angular, CRM.$, CRM._);