Merge pull request #16477 from demeritcowboy/unit-test-env
[civicrm-core.git] / ang / api4Explorer / Explorer.js
1 (function(angular, $, _, undefined) {
2
3 // Schema metadata
4 var schema = CRM.vars.api4.schema;
5 // FK schema data
6 var links = CRM.vars.api4.links;
7 // Cache list of entities
8 var entities = [];
9 // Cache list of actions
10 var actions = [];
11 // Field options
12 var fieldOptions = {};
13
14
15 angular.module('api4Explorer').config(function($routeProvider) {
16 $routeProvider.when('/explorer/:api4entity?/:api4action?', {
17 controller: 'Api4Explorer',
18 templateUrl: '~/api4Explorer/Explorer.html',
19 reloadOnSearch: false
20 });
21 });
22
23 angular.module('api4Explorer').controller('Api4Explorer', function($scope, $routeParams, $location, $timeout, $http, crmUiHelp, crmApi4) {
24 var ts = $scope.ts = CRM.ts();
25 $scope.entities = entities;
26 $scope.actions = actions;
27 $scope.fields = [];
28 $scope.fieldsAndJoins = [];
29 $scope.selectFieldsAndJoins = [];
30 $scope.availableParams = {};
31 $scope.params = {};
32 $scope.index = '';
33 $scope.selectedTab = {result: 'result', code: 'php'};
34 $scope.perm = {
35 accessDebugOutput: CRM.checkPerm('access debug output')
36 };
37 marked.setOptions({highlight: prettyPrintOne});
38 var getMetaParams = {},
39 objectParams = {orderBy: 'ASC', values: '', defaults: '', chain: ['Entity', '', '{}']},
40 docs = CRM.vars.api4.docs,
41 helpTitle = '',
42 helpContent = {};
43 $scope.helpTitle = '';
44 $scope.helpContent = {};
45 $scope.entity = $routeParams.api4entity;
46 $scope.result = [];
47 $scope.debug = null;
48 $scope.status = 'default';
49 $scope.loading = false;
50 $scope.controls = {};
51 $scope.code = [
52 {
53 lang: 'php',
54 style: [
55 {name: 'oop', label: ts('OOP Style'), code: ''},
56 {name: 'php', label: ts('Traditional'), code: ''}
57 ]
58 },
59 {
60 lang: 'js',
61 style: [
62 {name: 'js', label: ts('Single Call'), code: ''},
63 {name: 'js2', label: ts('Batch Calls'), code: ''}
64 ]
65 },
66 {
67 lang: 'ang',
68 style: [
69 {name: 'ang', label: ts('Single Call'), code: ''},
70 {name: 'ang2', label: ts('Batch Calls'), code: ''}
71 ]
72 },
73 {
74 lang: 'cli',
75 style: [
76 {name: 'cv', label: ts('CV'), code: ''}
77 ]
78 },
79 ];
80
81 if (!entities.length) {
82 formatForSelect2(schema, entities, 'name', ['description']);
83 }
84
85 $scope.$bindToRoute({
86 expr: 'index',
87 param: 'index',
88 default: ''
89 });
90
91 function ucfirst(str) {
92 return str[0].toUpperCase() + str.slice(1);
93 }
94
95 function lcfirst(str) {
96 return str[0].toLowerCase() + str.slice(1);
97 }
98
99 function pluralize(str) {
100 switch (str[str.length-1]) {
101 case 's':
102 return str + 'es';
103 case 'y':
104 return str.slice(0, -1) + 'ies';
105 default:
106 return str + 's';
107 }
108 }
109
110 // Turn a flat array into a select2 array
111 function arrayToSelect2(array) {
112 var out = [];
113 _.each(array, function(item) {
114 out.push({id: item, text: item});
115 });
116 return out;
117 }
118
119 // Reformat an existing array of objects for compatibility with select2
120 function formatForSelect2(input, container, key, extra, prefix) {
121 _.each(input, function(item) {
122 var id = (prefix || '') + item[key];
123 var formatted = {id: id, text: id};
124 if (extra) {
125 _.merge(formatted, _.pick(item, extra));
126 }
127 container.push(formatted);
128 });
129 return container;
130 }
131
132 function getFieldList(action) {
133 var fields = [],
134 fieldInfo = _.findWhere(getEntity().actions, {name: action}).fields;
135 formatForSelect2(fieldInfo, fields, 'name', ['description', 'required', 'default_value']);
136 return fields;
137 }
138
139 function addJoins(fieldList, addWildcard) {
140 var fields = _.cloneDeep(fieldList),
141 fks = _.findWhere(links, {entity: $scope.entity}) || {};
142 _.each(fks.links, function(link) {
143 var linkFields = _.cloneDeep(entityFields(link.entity)),
144 wildCard = addWildcard ? [{id: link.alias + '.*', text: link.alias + '.*', 'description': 'All core ' + link.entity + ' fields'}] : [];
145 if (linkFields) {
146 fields.push({
147 text: link.alias,
148 description: 'Join to ' + link.entity,
149 children: wildCard.concat(formatForSelect2(linkFields, [], 'name', ['description'], link.alias + '.'))
150 });
151 }
152 });
153 return fields;
154 }
155
156 $scope.help = function(title, content) {
157 if (!content) {
158 $scope.helpTitle = helpTitle;
159 $scope.helpContent = helpContent;
160 } else {
161 $scope.helpTitle = title;
162 $scope.helpContent = formatHelp(content);
163 }
164 };
165
166 // Sets the static help text (which gets overridden by mousing over other elements)
167 function setHelp(title, content) {
168 $scope.helpTitle = helpTitle = title;
169 $scope.helpContent = helpContent = formatHelp(content);
170 }
171
172 // Convert plain-text help to markdown; replace variables and format links
173 function formatHelp(rawContent) {
174 function formatRefs(see) {
175 _.each(see, function(ref, idx) {
176 var match = ref.match(/^\\Civi\\Api4\\([a-zA-Z]+)$/);
177 if (match) {
178 ref = '#/explorer/' + match[1];
179 }
180 if (ref[0] === '\\') {
181 ref = 'https://github.com/civicrm/civicrm-core/blob/master' + ref.replace(/\\/i, '/') + '.php';
182 }
183 see[idx] = '<a target="' + (ref[0] === '#' ? '_self' : '_blank') + '" href="' + ref + '">' + see[idx] + '</a>';
184 });
185 }
186 var formatted = _.cloneDeep(rawContent);
187 if (formatted.description) {
188 formatted.description = marked(formatted.description);
189 }
190 if (formatted.comment) {
191 formatted.comment = marked(formatted.comment);
192 }
193 formatRefs(formatted.see);
194 return formatted;
195 }
196
197 $scope.fieldHelp = function(fieldName) {
198 var field = getField(fieldName, $scope.entity, $scope.action);
199 if (!field) {
200 return;
201 }
202 var info = {
203 description: field.description,
204 type: field.data_type
205 };
206 if (field.default_value) {
207 info.default = field.default_value;
208 }
209 if (field.required_if) {
210 info.required_if = field.required_if;
211 } else if (field.required) {
212 info.required = 'true';
213 }
214 return info;
215 };
216
217 $scope.fieldList = function(param) {
218 return function() {
219 var fields = _.cloneDeep($scope.action === 'getFields' ? getFieldList($scope.params.action || 'get') : $scope.fields);
220 // Disable fields that are already in use
221 _.each($scope.params[param] || [], function(val) {
222 (_.findWhere(fields, {id: val[0]}) || {}).disabled = true;
223 });
224 return {results: fields};
225 };
226 };
227
228 $scope.formatSelect2Item = function(row) {
229 return _.escape(row.text) +
230 (row.required ? '<span class="crm-marker"> *</span>' : '') +
231 (row.description ? '<div class="crm-select2-row-description"><p>' + _.escape(row.description) + '</p></div>' : '');
232 };
233
234 $scope.clearParam = function(name) {
235 $scope.params[name] = $scope.availableParams[name].default;
236 };
237
238 $scope.isSpecial = function(name) {
239 var specialParams = ['select', 'fields', 'action', 'where', 'values', 'defaults', 'orderBy', 'chain'];
240 return _.contains(specialParams, name);
241 };
242
243 $scope.selectRowCount = function() {
244 if ($scope.isSelectRowCount()) {
245 $scope.params.select = [];
246 } else {
247 $scope.params.select = ['row_count'];
248 $scope.index = '';
249 if ($scope.params.limit == 25) {
250 $scope.params.limit = 0;
251 }
252 }
253 };
254
255 $scope.isSelectRowCount = function() {
256 return isSelectRowCount($scope.params);
257 };
258
259 function isSelectRowCount(params) {
260 return params && params.select && params.select.length === 1 && params.select[0] === 'row_count';
261 }
262
263 function getEntity(entityName) {
264 return _.findWhere(schema, {name: entityName || $scope.entity});
265 }
266
267 // Get all params that have been set
268 function getParams() {
269 var params = {};
270 _.each($scope.params, function(param, key) {
271 if (param != $scope.availableParams[key].default && !(typeof param === 'object' && _.isEmpty(param))) {
272 if (_.contains($scope.availableParams[key].type, 'array') && (typeof objectParams[key] === 'undefined')) {
273 params[key] = parseYaml(JSON.parse(angular.toJson(param)));
274 } else {
275 params[key] = param;
276 }
277 }
278 });
279 _.each(objectParams, function(defaultVal, key) {
280 if (params[key]) {
281 var newParam = {};
282 _.each(params[key], function(item) {
283 var val = _.cloneDeep(item[1]);
284 // Remove blank items from "chain" array
285 if (_.isArray(val)) {
286 _.eachRight(item[1], function(v, k) {
287 if (v) {
288 return false;
289 }
290 val.length--;
291 });
292 }
293 newParam[item[0]] = parseYaml(val);
294 });
295 params[key] = newParam;
296 }
297 });
298 return params;
299 }
300
301 function parseYaml(input) {
302 if (typeof input === 'undefined') {
303 return undefined;
304 }
305 if (input === '') {
306 return '';
307 }
308 if (_.isObject(input) || _.isArray(input)) {
309 _.each(input, function(item, index) {
310 input[index] = parseYaml(item);
311 });
312 return input;
313 }
314 try {
315 var output = (input === '>') ? '>' : jsyaml.safeLoad(input);
316 // We don't want dates parsed to js objects
317 return _.isDate(output) ? input : output;
318 } catch (e) {
319 return input;
320 }
321 }
322
323 function selectAction() {
324 $scope.action = $routeParams.api4action;
325 $scope.fieldsAndJoins.length = 0;
326 $scope.selectFieldsAndJoins.length = 0;
327 if (!actions.length) {
328 formatForSelect2(getEntity().actions, actions, 'name', ['description', 'params']);
329 }
330 if ($scope.action) {
331 var actionInfo = _.findWhere(actions, {id: $scope.action});
332 $scope.fields = getFieldList($scope.action);
333 if (_.contains(['get', 'update', 'delete', 'replace'], $scope.action)) {
334 $scope.fieldsAndJoins = addJoins($scope.fields);
335 $scope.selectFieldsAndJoins = addJoins($scope.fields, true);
336 } else {
337 $scope.fieldsAndJoins = $scope.fields;
338 $scope.selectFieldsAndJoins = _.cloneDeep($scope.fields);
339 }
340 $scope.selectFieldsAndJoins.unshift({id: '*', text: '*', 'description': 'All core ' + $scope.entity + ' fields'});
341 _.each(actionInfo.params, function (param, name) {
342 var format,
343 defaultVal = _.cloneDeep(param.default);
344 if (param.type) {
345 switch (param.type[0]) {
346 case 'int':
347 case 'bool':
348 format = param.type[0];
349 break;
350
351 case 'array':
352 case 'object':
353 format = 'json';
354 break;
355
356 default:
357 format = 'raw';
358 }
359 if (name === 'limit') {
360 defaultVal = 25;
361 }
362 if (name === 'debug') {
363 defaultVal = true;
364 }
365 if (name === 'values') {
366 defaultVal = defaultValues(defaultVal);
367 }
368 $scope.$bindToRoute({
369 expr: 'params["' + name + '"]',
370 param: name,
371 format: format,
372 default: defaultVal,
373 deep: format === 'json'
374 });
375 }
376 if (typeof objectParams[name] !== 'undefined') {
377 $scope.$watch('params.' + name, function(values) {
378 // Remove empty values
379 _.each(values, function(clause, index) {
380 if (!clause || !clause[0]) {
381 $scope.params[name].splice(index, 1);
382 }
383 });
384 }, true);
385 $scope.$watch('controls.' + name, function(value) {
386 var field = value;
387 $timeout(function() {
388 if (field) {
389 var defaultOp = _.cloneDeep(objectParams[name]);
390 if (name === 'chain') {
391 var num = $scope.params.chain.length;
392 defaultOp[0] = field;
393 field = 'name_me_' + num;
394 }
395 $scope.params[name].push([field, defaultOp]);
396 $scope.controls[name] = null;
397 }
398 });
399 });
400 }
401 });
402 $scope.availableParams = actionInfo.params;
403 }
404 writeCode();
405 }
406
407 function defaultValues(defaultVal) {
408 _.each($scope.fields, function(field) {
409 if (field.required) {
410 defaultVal.push([field.id, '']);
411 }
412 });
413 return defaultVal;
414 }
415
416 function stringify(value, trim) {
417 if (typeof value === 'undefined') {
418 return '';
419 }
420 var str = JSON.stringify(value).replace(/,/g, ', ');
421 if (trim) {
422 str = str.slice(1, -1);
423 }
424 return str.trim();
425 }
426
427 function writeCode() {
428 var code = {},
429 entity = $scope.entity,
430 action = $scope.action,
431 params = getParams(),
432 index = isInt($scope.index) ? +$scope.index : parseYaml($scope.index),
433 result = 'result';
434 if ($scope.entity && $scope.action) {
435 delete params.debug;
436 if (action.slice(0, 3) === 'get') {
437 result = entity.substr(0, 7) === 'Custom_' ? _.camelCase(entity.substr(7)) : entity;
438 result = lcfirst(action.replace(/s$/, '').slice(3) || result);
439 }
440 var results = lcfirst(_.isNumber(index) ? result : pluralize(result)),
441 paramCount = _.size(params),
442 i = 0;
443
444 if (isSelectRowCount(params)) {
445 results = result + 'Count';
446 }
447
448 // Write javascript
449 var js = "'" + entity + "', '" + action + "', {";
450 _.each(params, function(param, key) {
451 js += "\n " + key + ': ' + stringify(param) +
452 (++i < paramCount ? ',' : '');
453 if (key === 'checkPermissions') {
454 js += ' // IGNORED: permissions are always enforced from client-side requests';
455 }
456 });
457 js += "\n}";
458 if (index || index === 0) {
459 js += ', ' + JSON.stringify(index);
460 }
461 code.js = "CRM.api4(" + js + ").then(function(" + results + ") {\n // do something with " + results + " array\n}, function(failure) {\n // handle failure\n});";
462 code.js2 = "CRM.api4({" + results + ': [' + js + "]}).then(function(batch) {\n // do something with batch." + results + " array\n}, function(failure) {\n // handle failure\n});";
463 code.ang = "crmApi4(" + js + ").then(function(" + results + ") {\n // do something with " + results + " array\n}, function(failure) {\n // handle failure\n});";
464 code.ang2 = "crmApi4({" + results + ': [' + js + "]}).then(function(batch) {\n // do something with batch." + results + " array\n}, function(failure) {\n // handle failure\n});";
465
466 // Write php code
467 code.php = '$' + results + " = civicrm_api4('" + entity + "', '" + action + "', [";
468 _.each(params, function(param, key) {
469 code.php += "\n '" + key + "' => " + phpFormat(param, 4) + ',';
470 });
471 code.php += "\n]";
472 if (index || index === 0) {
473 code.php += ', ' + phpFormat(index);
474 }
475 code.php += ");";
476
477 // Write oop code
478 code.oop = '$' + results + " = " + formatOOP(entity, action, params, 2) + "\n ->execute()";
479 if (isSelectRowCount(params)) {
480 code.oop += "\n ->count()";
481 } else if (_.isNumber(index)) {
482 code.oop += !index ? '\n ->first()' : (index === -1 ? '\n ->last()' : '\n ->itemAt(' + index + ')');
483 } else if (index) {
484 if (_.isString(index) || (_.isPlainObject(index) && !index[0] && !index['0'])) {
485 code.oop += "\n ->indexBy('" + (_.isPlainObject(index) ? _.keys(index)[0] : index) + "')";
486 }
487 if (_.isArray(index) || _.isPlainObject(index)) {
488 code.oop += "\n ->column('" + (_.isArray(index) ? index[0] : _.values(index)[0]) + "')";
489 }
490 }
491 code.oop += ";\n";
492 if (!_.isNumber(index) && !isSelectRowCount(params)) {
493 code.oop += "foreach ($" + results + ' as $' + ((_.isString(index) && index) ? index + ' => $' : '') + result + ') {\n // do something\n}';
494 }
495
496 // Write cli code
497 code.cv = 'cv api4 ' + entity + '.' + action + " '" + stringify(params) + "'";
498 }
499 _.each($scope.code, function(vals) {
500 _.each(vals.style, function(style) {
501 style.code = code[style.name] ? prettyPrintOne(code[style.name]) : '';
502 });
503 });
504 }
505
506 // Format oop params
507 function formatOOP(entity, action, params, indent) {
508 var code = '',
509 newLine = "\n" + _.repeat(' ', indent);
510 if (entity.substr(0, 7) !== 'Custom_') {
511 code = "\\Civi\\Api4\\" + entity + '::' + action + '()';
512 } else {
513 code = "\\Civi\\Api4\\CustomValue::" + action + "('" + entity.substr(7) + "')";
514 }
515 _.each(params, function(param, key) {
516 var val = '';
517 if (typeof objectParams[key] !== 'undefined' && key !== 'chain') {
518 _.each(param, function(item, index) {
519 val = phpFormat(index) + ', ' + phpFormat(item, 2 + indent);
520 code += newLine + "->add" + ucfirst(key).replace(/s$/, '') + '(' + val + ')';
521 });
522 } else if (key === 'where') {
523 _.each(param, function (clause) {
524 if (clause[0] === 'AND' || clause[0] === 'OR' || clause[0] === 'NOT') {
525 code += newLine + "->addClause(" + phpFormat(clause[0]) + ", " + phpFormat(clause[1]).slice(1, -1) + ')';
526 } else {
527 code += newLine + "->addWhere(" + phpFormat(clause).slice(1, -1) + ")";
528 }
529 });
530 } else if (key === 'select') {
531 code += newLine;
532 // addSelect() is a variadic function & can take multiple arguments; selectRowCount() is a shortcut for addSelect('row_count')
533 code += isSelectRowCount(params) ? '->selectRowCount()' : '->addSelect(' + phpFormat(param).slice(1, -1) + ')';
534 } else if (key === 'chain') {
535 _.each(param, function(chain, name) {
536 code += newLine + "->addChain('" + name + "', " + formatOOP(chain[0], chain[1], chain[2], 2 + indent);
537 code += (chain.length > 3 ? ',' : '') + (!_.isEmpty(chain[2]) ? newLine : ' ') + (chain.length > 3 ? phpFormat(chain[3]) : '') + ')';
538 });
539 }
540 else {
541 code += newLine + "->set" + ucfirst(key) + '(' + phpFormat(param, 2 + indent) + ')';
542 }
543 });
544 return code;
545 }
546
547 function isInt(value) {
548 if (_.isFinite(value)) {
549 return true;
550 }
551 if (!_.isString(value)) {
552 return false;
553 }
554 return /^-{0,1}\d+$/.test(value);
555 }
556
557 function formatMeta(resp) {
558 var ret = '';
559 _.each(resp, function(val, key) {
560 if (key !== 'values' && !_.isPlainObject(val) && !_.isFunction(val)) {
561 ret += (ret.length ? ', ' : '') + key + ': ' + (_.isArray(val) ? '[' + val + ']' : val);
562 }
563 });
564 return prettyPrintOne(_.escape(ret));
565 }
566
567 $scope.execute = function() {
568 $scope.status = 'warning';
569 $scope.loading = true;
570 $http.post(CRM.url('civicrm/ajax/api4/' + $scope.entity + '/' + $scope.action, {
571 params: angular.toJson(getParams()),
572 index: isInt($scope.index) ? +$scope.index : parseYaml($scope.index)
573 }), null, {
574 headers: {
575 'X-Requested-With': 'XMLHttpRequest'
576 }
577 }).then(function(resp) {
578 $scope.loading = false;
579 $scope.status = 'success';
580 $scope.debug = debugFormat(resp.data);
581 $scope.result = [formatMeta(resp.data), prettyPrintOne(_.escape(JSON.stringify(resp.data.values, null, 2)), 'js', 1)];
582 }, function(resp) {
583 $scope.loading = false;
584 $scope.status = 'danger';
585 $scope.debug = debugFormat(resp.data);
586 $scope.result = [formatMeta(resp), prettyPrintOne(_.escape(JSON.stringify(resp.data, null, 2)))];
587 });
588 };
589
590 function debugFormat(data) {
591 var debug = data.debug ? prettyPrintOne(_.escape(JSON.stringify(data.debug, null, 2)).replace(/\\n/g, "\n")) : null;
592 delete data.debug;
593 return debug;
594 }
595
596 /**
597 * Format value to look like php code
598 */
599 function phpFormat(val, indent) {
600 if (typeof val === 'undefined') {
601 return '';
602 }
603 if (val === null || val === true || val === false) {
604 return JSON.stringify(val).toUpperCase();
605 }
606 indent = (typeof indent === 'number') ? _.repeat(' ', indent) : (indent || '');
607 var ret = '',
608 baseLine = indent ? indent.slice(0, -2) : '',
609 newLine = indent ? '\n' : '',
610 trailingComma = indent ? ',' : '';
611 if ($.isPlainObject(val)) {
612 $.each(val, function(k, v) {
613 ret += (ret ? ', ' : '') + newLine + indent + "'" + k + "' => " + phpFormat(v);
614 });
615 return '[' + ret + trailingComma + newLine + baseLine + ']';
616 }
617 if ($.isArray(val)) {
618 $.each(val, function(k, v) {
619 ret += (ret ? ', ' : '') + newLine + indent + phpFormat(v);
620 });
621 return '[' + ret + trailingComma + newLine + baseLine + ']';
622 }
623 if (_.isString(val) && !_.contains(val, "'")) {
624 return "'" + val + "'";
625 }
626 return JSON.stringify(val).replace(/\$/g, '\\$');
627 }
628
629 function fetchMeta() {
630 crmApi4(getMetaParams)
631 .then(function(data) {
632 if (data.actions) {
633 getEntity().actions = data.actions;
634 selectAction();
635 }
636 });
637 }
638
639 // Help for an entity with no action selected
640 function showEntityHelp(entityName) {
641 var entityInfo = getEntity(entityName);
642 setHelp($scope.entity, {
643 description: entityInfo.description,
644 comment: entityInfo.comment,
645 see: entityInfo.see
646 });
647 }
648
649 if (!$scope.entity) {
650 setHelp(ts('APIv4 Explorer'), {description: docs.description, comment: docs.comment, see: docs.see});
651 } else if (!actions.length && !getEntity().actions) {
652 getMetaParams.actions = [$scope.entity, 'getActions', {chain: {fields: [$scope.entity, 'getFields', {action: '$name'}]}}];
653 fetchMeta();
654 } else {
655 selectAction();
656 }
657
658 if ($scope.entity) {
659 showEntityHelp($scope.entity);
660 }
661
662 // Update route when changing entity
663 $scope.$watch('entity', function(newVal, oldVal) {
664 if (oldVal !== newVal) {
665 // Flush actions cache to re-fetch for new entity
666 actions = [];
667 $location.url('/explorer/' + newVal);
668 }
669 });
670
671 // Update route when changing actions
672 $scope.$watch('action', function(newVal, oldVal) {
673 if ($scope.entity && $routeParams.api4action !== newVal && !_.isUndefined(newVal)) {
674 $location.url('/explorer/' + $scope.entity + '/' + newVal);
675 } else if (newVal) {
676 setHelp($scope.entity + '::' + newVal, _.pick(_.findWhere(getEntity().actions, {name: newVal}), ['description', 'comment', 'see']));
677 }
678 });
679
680 $scope.paramDoc = function(name) {
681 return docs.params[name];
682 };
683
684 $scope.$watch('params', writeCode, true);
685 $scope.$watch('index', writeCode);
686 writeCode();
687
688 });
689
690 angular.module('api4Explorer').directive('crmApi4WhereClause', function($timeout) {
691 return {
692 scope: {
693 data: '=crmApi4WhereClause'
694 },
695 templateUrl: '~/api4Explorer/WhereClause.html',
696 link: function (scope, element, attrs) {
697 var ts = scope.ts = CRM.ts();
698 scope.newClause = '';
699 scope.conjunctions = ['AND', 'OR', 'NOT'];
700 scope.operators = CRM.vars.api4.operators;
701
702 scope.addGroup = function(op) {
703 scope.data.where.push([op, []]);
704 };
705
706 scope.removeGroup = function() {
707 scope.data.groupParent.splice(scope.data.groupIndex, 1);
708 };
709
710 scope.onSort = function(event, ui) {
711 $('.api4-where-fieldset').toggleClass('api4-sorting', event.type === 'sortstart');
712 $('.api4-input.form-inline').css('margin-left', '');
713 };
714
715 // Indent clause while dragging between nested groups
716 scope.onSortOver = function(event, ui) {
717 var offset = 0;
718 if (ui.sender) {
719 offset = $(ui.placeholder).offset().left - $(ui.sender).offset().left;
720 }
721 $('.api4-input.form-inline.ui-sortable-helper').css('margin-left', '' + offset + 'px');
722 };
723
724 scope.$watch('newClause', function(value) {
725 var field = value;
726 $timeout(function() {
727 if (field) {
728 scope.data.where.push([field, '=', '']);
729 scope.newClause = null;
730 }
731 });
732 });
733 scope.$watch('data.where', function(values) {
734 // Remove empty values
735 _.each(values, function(clause, index) {
736 if (typeof clause !== 'undefined' && !clause[0]) {
737 values.splice(index, 1);
738 }
739 if (typeof clause[1] === 'string' && _.contains(clause[1], 'NULL')) {
740 clause.length = 2;
741 } else if (typeof clause[1] === 'string' && clause.length == 2) {
742 clause.push('');
743 }
744 });
745 }, true);
746 }
747 };
748 });
749
750 angular.module('api4Explorer').directive('api4ExpValue', function($routeParams, crmApi4) {
751 return {
752 scope: {
753 data: '=api4ExpValue'
754 },
755 require: 'ngModel',
756 link: function (scope, element, attrs, ctrl) {
757 var ts = scope.ts = CRM.ts(),
758 multi = _.includes(['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'], scope.data.op),
759 entity = $routeParams.api4entity,
760 action = scope.data.action || $routeParams.api4action;
761
762 function destroyWidget() {
763 var $el = $(element);
764 if ($el.is('.crm-form-date-wrapper .crm-hidden-date')) {
765 $el.crmDatepicker('destroy');
766 }
767 if ($el.is('.select2-container + input')) {
768 $el.crmEntityRef('destroy');
769 }
770 $(element).removeData().removeAttr('type').removeAttr('placeholder').show();
771 }
772
773 function makeWidget(field, op) {
774 var $el = $(element),
775 inputType = field.input_type,
776 dataType = field.data_type;
777 if (!op) {
778 op = field.serialize || dataType === 'Array' ? 'IN' : '=';
779 }
780 multi = _.includes(['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'], op);
781 if (op === 'IS NULL' || op === 'IS NOT NULL') {
782 $el.hide();
783 return;
784 }
785 if (inputType === 'Date') {
786 if (_.includes(['=', '!=', '<>', '<', '>=', '<', '<='], op)) {
787 $el.crmDatepicker({time: (field.input_attrs && field.input_attrs.time) || false});
788 }
789 } else if (_.includes(['=', '!=', '<>', 'IN', 'NOT IN'], op) && (field.fk_entity || field.options || dataType === 'Boolean')) {
790 if (field.fk_entity) {
791 $el.crmEntityRef({entity: field.fk_entity, select:{multiple: multi}});
792 } else if (field.options) {
793 $el.addClass('loading').attr('placeholder', ts('- select -')).crmSelect2({multiple: multi, data: [{id: '', text: ''}]});
794 loadFieldOptions(field.entity || entity).then(function(data) {
795 var options = [];
796 _.each(_.findWhere(data, {name: field.name}).options, function(val, key) {
797 options.push({id: key, text: val});
798 });
799 $el.removeClass('loading').select2({data: options, multiple: multi});
800 });
801 } else if (dataType === 'Boolean') {
802 $el.attr('placeholder', ts('- select -')).crmSelect2({allowClear: false, multiple: multi, placeholder: ts('- select -'), data: [
803 {id: 'true', text: ts('Yes')},
804 {id: 'false', text: ts('No')}
805 ]});
806 }
807 } else if (dataType === 'Integer' && !multi) {
808 $el.attr('type', 'number');
809 }
810 }
811
812 function loadFieldOptions(entity) {
813 if (!fieldOptions[entity + action]) {
814 fieldOptions[entity + action] = crmApi4(entity, 'getFields', {
815 loadOptions: true,
816 action: action,
817 where: [["options", "!=", false]],
818 select: ["name", "options"]
819 });
820 }
821 return fieldOptions[entity + action];
822 }
823
824 // Copied from ng-list but applied conditionally if field is multi-valued
825 var parseList = function(viewValue) {
826 // If the viewValue is invalid (say required but empty) it will be `undefined`
827 if (_.isUndefined(viewValue)) return;
828
829 if (!multi) {
830 return viewValue;
831 }
832
833 var list = [];
834
835 if (viewValue) {
836 _.each(viewValue.split(','), function(value) {
837 if (value) list.push(_.trim(value));
838 });
839 }
840
841 return list;
842 };
843
844 // Copied from ng-list
845 ctrl.$parsers.push(parseList);
846 ctrl.$formatters.push(function(value) {
847 return _.isArray(value) ? value.join(', ') : value;
848 });
849
850 // Copied from ng-list
851 ctrl.$isEmpty = function(value) {
852 return !value || !value.length;
853 };
854
855 scope.$watchCollection('data', function(data) {
856 destroyWidget();
857 var field = getField(data.field, entity, action);
858 if (field) {
859 makeWidget(field, data.op);
860 }
861 });
862 }
863 };
864 });
865
866
867 angular.module('api4Explorer').directive('api4ExpChain', function(crmApi4) {
868 return {
869 scope: {
870 chain: '=api4ExpChain',
871 mainEntity: '=',
872 entities: '='
873 },
874 templateUrl: '~/api4Explorer/Chain.html',
875 link: function (scope, element, attrs) {
876 var ts = scope.ts = CRM.ts();
877
878 function changeEntity(newEntity, oldEntity) {
879 // When clearing entity remove this chain
880 if (!newEntity) {
881 scope.chain[0] = '';
882 return;
883 }
884 // Reset action && index
885 if (newEntity !== oldEntity) {
886 scope.chain[1][1] = scope.chain[1][2] = '';
887 }
888 if (getEntity(newEntity).actions) {
889 setActions();
890 } else {
891 crmApi4(newEntity, 'getActions', {chain: {fields: [newEntity, 'getFields', {action: '$name'}]}})
892 .then(function(data) {
893 getEntity(data.entity).actions = data;
894 if (data.entity === scope.chain[1][0]) {
895 setActions();
896 }
897 });
898 }
899 }
900
901 function setActions() {
902 scope.actions = [''].concat(_.pluck(getEntity(scope.chain[1][0]).actions, 'name'));
903 }
904
905 // Set default params when choosing action
906 function changeAction(newAction, oldAction) {
907 var link;
908 // Prepopulate links
909 if (newAction && newAction !== oldAction) {
910 // Clear index
911 scope.chain[1][3] = '';
912 // Look for links back to main entity
913 _.each(entityFields(scope.chain[1][0]), function(field) {
914 if (field.fk_entity === scope.mainEntity) {
915 link = [field.name, '$id'];
916 }
917 });
918 // Look for links from main entity
919 if (!link && newAction !== 'create') {
920 _.each(entityFields(scope.mainEntity), function(field) {
921 if (field.fk_entity === scope.chain[1][0]) {
922 link = ['id', '$' + field.name];
923 // Since we're specifying the id, set index to getsingle
924 scope.chain[1][3] = '0';
925 }
926 });
927 }
928 if (link && _.contains(['get', 'update', 'replace', 'delete'], newAction)) {
929 scope.chain[1][2] = '{where: [[' + link[0] + ', =, ' + link[1] + ']]}';
930 }
931 else if (link && _.contains(['create'], newAction)) {
932 scope.chain[1][2] = '{values: {' + link[0] + ': ' + link[1] + '}}';
933 }
934 else if (link && _.contains(['save'], newAction)) {
935 scope.chain[1][2] = '{records: [{' + link[0] + ': ' + link[1] + '}]}';
936 } else {
937 scope.chain[1][2] = '{}';
938 }
939 }
940 }
941
942 scope.$watch("chain[1][0]", changeEntity);
943 scope.$watch("chain[1][1]", changeAction);
944 }
945 };
946 });
947
948 function getEntity(entityName) {
949 return _.findWhere(schema, {name: entityName});
950 }
951
952 function entityFields(entityName, action) {
953 var entity = getEntity(entityName);
954 if (entity && action && entity.actions) {
955 return _.findWhere(entity.actions, {name: action}).fields;
956 }
957 return _.result(entity, 'fields');
958 }
959
960 function getField(fieldName, entity, action) {
961 var fieldNames = fieldName.split('.');
962 return get(entity, fieldNames);
963
964 function get(entity, fieldNames) {
965 if (fieldNames.length === 1) {
966 return _.findWhere(entityFields(entity, action), {name: fieldNames[0]});
967 }
968 var comboName = _.findWhere(entityFields(entity, action), {name: fieldNames[0] + '.' + fieldNames[1]});
969 if (comboName) {
970 return comboName;
971 }
972 var linkName = fieldNames.shift(),
973 entityLinks = _.findWhere(links, {entity: entity}).links,
974 newEntity = _.findWhere(entityLinks, {alias: linkName}).entity;
975 return get(newEntity, fieldNames);
976 }
977 }
978
979 // Collapsible optgroups for select2
980 $(function() {
981 $('body')
982 .on('select2-open', function(e) {
983 if ($(e.target).hasClass('collapsible-optgroups')) {
984 $('#select2-drop')
985 .off('.collapseOptionGroup')
986 .addClass('collapsible-optgroups-enabled')
987 .on('click.collapseOptionGroup', '.select2-result-with-children > .select2-result-label', function() {
988 $(this).parent().toggleClass('optgroup-expanded');
989 });
990 }
991 })
992 .on('select2-close', function() {
993 $('#select2-drop').off('.collapseOptionGroup').removeClass('collapsible-optgroups-enabled');
994 });
995 });
996 })(angular, CRM.$, CRM._);