From 7b6c5043a7ad633cb7a4c73dcba5b7006b75f345 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 23 Feb 2015 21:29:45 -0500 Subject: [PATCH] Support multi-valued fields in api explorer --- api/v3/Mailing.php | 1 + templates/CRM/Admin/Page/APIExplorer.js | 33 +++++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index d1dfbdc639..22fe9bcc52 100755 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -97,6 +97,7 @@ function _civicrm_api3_mailing_gettokens_spec(&$params) { $params['entity'] = array( 'api.default' => array('contact'), 'api.required' => 1, + 'api.multiple' => 1, 'title' => 'Entity', 'options' => array(), ); diff --git a/templates/CRM/Admin/Page/APIExplorer.js b/templates/CRM/Admin/Page/APIExplorer.js index 5c2420dbb1..b225d13d3b 100644 --- a/templates/CRM/Admin/Page/APIExplorer.js +++ b/templates/CRM/Admin/Page/APIExplorer.js @@ -22,17 +22,9 @@ // Operators with special properties BOOL = ['IS NULL', 'IS NOT NULL'], TEXT = ['LIKE', 'NOT LIKE'], + SINGLE = ['>', '>=', '<', '<=', '<>', '!='], MULTI = ['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']; - /** - * Call prettyPrint function if it successfully loaded from the cdn - */ - function prettyPrint() { - if (window.prettyPrint) { - window.prettyPrint(); - } - } - /** * Add a "fields" row * @param name @@ -125,9 +117,10 @@ fields.push({ id: field.name, text: field.title || field.name, - required: field['api.required'] || false + multi: !!field['api.multiple'], + required: !(!field['api.required'] || field['api.required'] === '0') }); - if (field['api.required']) { + if (field['api.required'] && field['api.required'] !== '0') { required.push(field.name); } if (field.options) { @@ -227,6 +220,11 @@ } } + function isMultiSelect(fieldName, operator) { + var field = fieldName && _.find(fields, 'id', fieldName); + return ($.inArray(operator, MULTI) > -1) || (field && field.multi); + } + /** * Render value input as a textfield, option list, entityRef, or hidden, * Depending on selected param name and operator @@ -235,11 +233,11 @@ var $row = $(this).closest('tr'), name = $('input.api-param-name', $row).val(), operator = $('.api-param-op', $row).val(), - operatorType = $.inArray(operator, MULTI) > -1 ? 'multi' : ($.inArray(operator, BOOL) > -1 ? 'bool' : 'single'), $valField = $('input.api-param-value', $row), + multiSelect = isMultiSelect(name, operator), currentVal = $valField.val(); // Boolean fields only have 1 possible value - if (operatorType == 'bool') { + if ($.inArray(operator, BOOL) > -1) { if ($valField.data('select2')) { $valField.select2('destroy'); } @@ -254,13 +252,13 @@ $valField.val(''); } // When switching from multi-select to single select - else if (operatorType == 'single' && currentVal.indexOf(',') > -1) { + else if (!multiSelect && currentVal.indexOf(',') > -1) { $valField.val(currentVal.split(',')[0]); } // Select options if (options[name]) { $valField.select2({ - multiple: (operatorType === 'multi'), + multiple: multiSelect, data: _.map(options[name], function (value, key) { return {id: key, text: value}; }) @@ -268,11 +266,10 @@ } // EntityRef else { - console.log($.inArray(getFieldData[name].FKApiName, OPEN_IMMEDIATELY)); $valField.crmEntityRef({ entity: getFieldData[name].FKApiName, select: { - multiple: (operatorType === 'multi'), + multiple: multiSelect, minimumInputLength: $.inArray(getFieldData[name].FKApiName, OPEN_IMMEDIATELY) > -1 ? 0 : 1 } }); @@ -376,7 +373,7 @@ var $row = $(this).closest('tr'), op = $('select.api-param-op', $row).val() || '=', name = $('input.api-param-name', $row).val(), - val = evaluate($(this).val(), $.inArray(op, MULTI) > -1); + val = evaluate($(this).val(), isMultiSelect(name, op)); // Ignore blank values for the return field if ($(this).is('#api-return-value') && !val) { -- 2.25.1