X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=ang%2Fapi4Explorer%2FExplorer.js;h=7c64b6e6671c9d1e39d974db47e59bce121fed48;hb=1ba1749e62e67560d4b4d769246dc67d55cc3d38;hp=0dca04e3bc463cacd76b7bbb8280a0dd5ddb7a74;hpb=5720098c50dad8e515c518752e354ac871fcbdd0;p=civicrm-core.git diff --git a/ang/api4Explorer/Explorer.js b/ang/api4Explorer/Explorer.js index 0dca04e3bc..7c64b6e667 100644 --- a/ang/api4Explorer/Explorer.js +++ b/ang/api4Explorer/Explorer.js @@ -72,7 +72,9 @@ {name: 'ang2', label: ts('Batch Calls'), code: ''} ], cli: [ - {name: 'cv', label: ts('CV'), code: ''} + {name: 'short', label: ts('CV (short)'), code: ''}, + {name: 'long', label: ts('CV (long)'), code: ''}, + {name: 'pipe', label: ts('CV (pipe)'), code: ''} ] }; @@ -324,7 +326,11 @@ } // Then lookup implicit links _.each(path, function(node) { - entity = _.find(links[entity], {alias: node}).entity; + var link = _.find(links[entity], {alias: node}); + if (!link) { + return false; + } + entity = link.entity; }); return entity; } @@ -654,8 +660,44 @@ break; case 'cli': - // Write cli code - code.cv = 'cv api4 ' + entity + '.' + action + " '" + stringify(params) + "'"; + // Cli code using json input + code.long = 'cv api4 ' + entity + '.' + action + ' ' + cliFormat(JSON.stringify(params)); + code.pipe = 'echo ' + cliFormat(JSON.stringify(params)) + ' | cv api4 ' + entity + '.' + action + ' --in=json'; + + // Cli code using short syntax + code.short = 'cv api4 ' + entity + '.' + action; + var limitSet = false; + _.each(params, function(param, key) { + switch (true) { + case (key === 'select' && !_.includes(param.join(), ' ')): + code.short += ' +s ' + cliFormat(param.join(',')); + break; + case (key === 'where' && !_.intersection(_.map(param, 0), ['AND', 'OR', 'NOT']).length): + _.each(param, function(clause) { + code.short += ' +w ' + cliFormat(clause[0] + ' ' + clause[1] + (clause.length > 2 ? (' ' + JSON.stringify(clause[2])) : '')); + }); + break; + case (key === 'orderBy'): + _.each(param, function(dir, field) { + code.short += ' +o ' + cliFormat(field + ' ' + dir); + }); + break; + case (key === 'values'): + _.each(param, function(val, field) { + code.short += ' +v ' + cliFormat(field + '=' + val); + }); + break; + case (key === 'limit' || key === 'offset'): + // These 2 get combined + if (!limitSet) { + limitSet = true; + code.short += ' +l ' + (params.limit || '0') + (params.offset ? ('@' + params.offset) : ''); + } + break; + default: + code.short += ' ' + key + '=' + (typeof param === 'string' ? cliFormat(param) : cliFormat(JSON.stringify(param))); + } + }); } } _.each($scope.code, function(vals) { @@ -749,7 +791,7 @@ $scope.debug = debugFormat(resp.data); $scope.result = [ formatMeta(resp.data), - prettyPrintOne('(' + resp.data.values.length + ') ' + _.escape(JSON.stringify(resp.data.values, null, 2)), 'js', 1) + prettyPrintOne((_.isArray(resp.data.values) ? '(' + resp.data.values.length + ') ' : '') + _.escape(JSON.stringify(resp.data.values, null, 2)), 'js', 1) ]; }, function(resp) { $scope.loading = false; @@ -801,6 +843,20 @@ return JSON.stringify(val).replace(/\$/g, '\\$'); } + // Format string to be cli-input-safe + function cliFormat(str) { + if (!_.includes(str, ' ') && !_.includes(str, '"') && !_.includes(str, "'")) { + return str; + } + if (!_.includes(str, "'")) { + return "'" + str + "'"; + } + if (!_.includes(str, '"')) { + return '"' + str + '"'; + } + return "'" + str.replace(/'/g, "\\'") + "'"; + } + function fetchMeta() { crmApi4(getMetaParams) .then(function(data) {