X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=ang%2Fapi4Explorer%2FExplorer.js;h=09db7d1d02a2023fee9ea88a87af0f7e462f5bf5;hb=95eb4cfc8aaa03141ac2a0793e66ac517ed748ed;hp=7a20910ba8a223557e03894579a84a9f66f7f843;hpb=fc95d9a569ece20b0377610923be9c09f82321d5;p=civicrm-core.git diff --git a/ang/api4Explorer/Explorer.js b/ang/api4Explorer/Explorer.js index 7a20910ba8..09db7d1d02 100644 --- a/ang/api4Explorer/Explorer.js +++ b/ang/api4Explorer/Explorer.js @@ -20,7 +20,7 @@ }); }); - angular.module('api4Explorer').controller('Api4Explorer', function($scope, $routeParams, $location, $timeout, $http, crmUiHelp, crmApi4) { + angular.module('api4Explorer').controller('Api4Explorer', function($scope, $routeParams, $location, $timeout, $http, crmUiHelp, crmApi4, dialogService) { var ts = $scope.ts = CRM.ts(); $scope.entities = entities; $scope.actions = actions; @@ -32,11 +32,12 @@ $scope.index = ''; $scope.selectedTab = {result: 'result', code: 'php'}; $scope.perm = { - accessDebugOutput: CRM.checkPerm('access debug output') + accessDebugOutput: CRM.checkPerm('access debug output'), + editGroups: CRM.checkPerm('edit groups') }; marked.setOptions({highlight: prettyPrintOne}); var getMetaParams = {}, - objectParams = {orderBy: 'ASC', values: '', chain: ['Entity', '', '{}']}, + objectParams = {orderBy: 'ASC', values: '', defaults: '', chain: ['Entity', '', '{}']}, docs = CRM.vars.api4.docs, helpTitle = '', helpContent = {}; @@ -214,13 +215,15 @@ return info; }; - $scope.valuesFields = function() { - var fields = _.cloneDeep($scope.action === 'getFields' ? getFieldList($scope.params.action || 'get') : $scope.fields); - // Disable fields that are already in use - _.each($scope.params.values || [], function(val) { - (_.findWhere(fields, {id: val[0]}) || {}).disabled = true; - }); - return {results: fields}; + $scope.fieldList = function(param) { + return function() { + var fields = _.cloneDeep($scope.action === 'getFields' ? getFieldList($scope.params.action || 'get') : $scope.fields); + // Disable fields that are already in use + _.each($scope.params[param] || [], function(val) { + (_.findWhere(fields, {id: val[0]}) || {}).disabled = true; + }); + return {results: fields}; + }; }; $scope.formatSelect2Item = function(row) { @@ -234,7 +237,7 @@ }; $scope.isSpecial = function(name) { - var specialParams = ['select', 'fields', 'action', 'where', 'values', 'orderBy', 'chain']; + var specialParams = ['select', 'fields', 'action', 'where', 'values', 'defaults', 'orderBy', 'chain']; return _.contains(specialParams, name); }; @@ -251,9 +254,13 @@ }; $scope.isSelectRowCount = function() { - return $scope.params && $scope.params.select && $scope.params.select.length === 1 && $scope.params.select[0] === 'row_count'; + return isSelectRowCount($scope.params); }; + function isSelectRowCount(params) { + return params && params.select && params.select.length === 1 && params.select[0] === 'row_count'; + } + function getEntity(entityName) { return _.findWhere(schema, {name: entityName || $scope.entity}); } @@ -433,10 +440,9 @@ } var results = lcfirst(_.isNumber(index) ? result : pluralize(result)), paramCount = _.size(params), - isSelectRowCount = params.select && params.select.length === 1 && params.select[0] === 'row_count', i = 0; - if (isSelectRowCount) { + if (isSelectRowCount(params)) { results = result + 'Count'; } @@ -470,34 +476,8 @@ code.php += ");"; // Write oop code - if (entity.substr(0, 7) !== 'Custom_') { - code.oop = '$' + results + " = \\Civi\\Api4\\" + entity + '::' + action + '()'; - } else { - code.oop = '$' + results + " = \\Civi\\Api4\\CustomValue::" + action + "('" + entity.substr(7) + "')"; - } - _.each(params, function(param, key) { - var val = ''; - if (typeof objectParams[key] !== 'undefined' && key !== 'chain') { - _.each(param, function(item, index) { - val = phpFormat(index) + ', ' + phpFormat(item, 4); - code.oop += "\n ->add" + ucfirst(key).replace(/s$/, '') + '(' + val + ')'; - }); - } else if (key === 'where') { - _.each(param, function (clause) { - if (clause[0] === 'AND' || clause[0] === 'OR' || clause[0] === 'NOT') { - code.oop += "\n ->addClause(" + phpFormat(clause[0]) + ", " + phpFormat(clause[1]).slice(1, -1) + ')'; - } else { - code.oop += "\n ->addWhere(" + phpFormat(clause).slice(1, -1) + ")"; - } - }); - } else if (key === 'select' && isSelectRowCount) { - code.oop += "\n ->selectRowCount()"; - } else { - code.oop += "\n ->set" + ucfirst(key) + '(' + phpFormat(param, 4) + ')'; - } - }); - code.oop += "\n ->execute()"; - if (isSelectRowCount) { + code.oop = '$' + results + " = " + formatOOP(entity, action, params, 2) + "\n ->execute()"; + if (isSelectRowCount(params)) { code.oop += "\n ->count()"; } else if (_.isNumber(index)) { code.oop += !index ? '\n ->first()' : (index === -1 ? '\n ->last()' : '\n ->itemAt(' + index + ')'); @@ -510,7 +490,7 @@ } } code.oop += ";\n"; - if (!_.isNumber(index) && !isSelectRowCount) { + if (!_.isNumber(index) && !isSelectRowCount(params)) { code.oop += "foreach ($" + results + ' as $' + ((_.isString(index) && index) ? index + ' => $' : '') + result + ') {\n // do something\n}'; } @@ -524,6 +504,47 @@ }); } + // Format oop params + function formatOOP(entity, action, params, indent) { + var code = '', + newLine = "\n" + _.repeat(' ', indent); + if (entity.substr(0, 7) !== 'Custom_') { + code = "\\Civi\\Api4\\" + entity + '::' + action + '()'; + } else { + code = "\\Civi\\Api4\\CustomValue::" + action + "('" + entity.substr(7) + "')"; + } + _.each(params, function(param, key) { + var val = ''; + if (typeof objectParams[key] !== 'undefined' && key !== 'chain') { + _.each(param, function(item, index) { + val = phpFormat(index) + ', ' + phpFormat(item, 2 + indent); + code += newLine + "->add" + ucfirst(key).replace(/s$/, '') + '(' + val + ')'; + }); + } else if (key === 'where') { + _.each(param, function (clause) { + if (clause[0] === 'AND' || clause[0] === 'OR' || clause[0] === 'NOT') { + code += newLine + "->addClause(" + phpFormat(clause[0]) + ", " + phpFormat(clause[1]).slice(1, -1) + ')'; + } else { + code += newLine + "->addWhere(" + phpFormat(clause).slice(1, -1) + ")"; + } + }); + } else if (key === 'select') { + code += newLine; + // addSelect() is a variadic function & can take multiple arguments; selectRowCount() is a shortcut for addSelect('row_count') + code += isSelectRowCount(params) ? '->selectRowCount()' : '->addSelect(' + phpFormat(param).slice(1, -1) + ')'; + } else if (key === 'chain') { + _.each(param, function(chain, name) { + code += newLine + "->addChain('" + name + "', " + formatOOP(chain[0], chain[1], chain[2], 2 + indent); + code += (chain.length > 3 ? ',' : '') + (!_.isEmpty(chain[2]) ? newLine : ' ') + (chain.length > 3 ? phpFormat(chain[3]) : '') + ')'; + }); + } + else { + code += newLine + "->set" + ucfirst(key) + '(' + phpFormat(param, 2 + indent) + ')'; + } + }); + return code; + } + function isInt(value) { if (_.isFinite(value)) { return true; @@ -661,10 +682,105 @@ return docs.params[name]; }; + $scope.executeDoc = function() { + var doc = { + description: ts('Runs API call on the CiviCRM database.'), + comment: ts('Results and debugging info will be displayed below.') + }; + if ($scope.action === 'delete') { + doc.WARNING = ts('This API call will be executed on the real database. Deleting data cannot be undone.'); + } + else if ($scope.action && $scope.action.slice(0, 3) !== 'get') { + doc.WARNING = ts('This API call will be executed on the real database. It cannot be undone.'); + } + return doc; + }; + + $scope.saveDoc = function() { + return { + description: ts('Save API call as a smart group.'), + comment: ts('Allows you to create a SavedSearch containing the WHERE clause of this API call.'), + }; + }; + $scope.$watch('params', writeCode, true); $scope.$watch('index', writeCode); writeCode(); + $scope.save = function() { + var model = { + title: '', + description: '', + visibility: 'User and User Admin Only', + group_type: [], + id: null, + entity: $scope.entity, + params: JSON.parse(angular.toJson($scope.params)) + }; + model.params.version = 4; + delete model.params.select; + delete model.params.chain; + delete model.params.debug; + delete model.params.limit; + delete model.params.checkPermissions; + var options = CRM.utils.adjustDialogDefaults({ + width: '500px', + autoOpen: false, + title: ts('Save smart group') + }); + dialogService.open('saveSearchDialog', '~/api4Explorer/SaveSearch.html', model, options); + }; + }); + + angular.module('api4Explorer').controller('SaveSearchCtrl', function($scope, crmApi4, dialogService) { + var ts = $scope.ts = CRM.ts(), + model = $scope.model; + $scope.groupEntityRefParams = { + entity: 'Group', + api: { + params: {is_hidden: 0, is_active: 1, 'saved_search_id.api_entity': model.entity}, + extra: ['saved_search_id', 'description', 'visibility', 'group_type'] + }, + select: { + allowClear: true, + minimumInputLength: 0, + placeholder: ts('Select existing group') + } + }; + if (!CRM.checkPerm('administer reserved groups')) { + $scope.groupEntityRefParams.api.params.is_reserved = 0; + } + $scope.perm = { + administerReservedGroups: CRM.checkPerm('administer reserved groups') + }; + $scope.options = CRM.vars.api4.groupOptions; + $scope.$watch('model.id', function(id) { + if (id) { + _.assign(model, $('#api-save-search-select-group').select2('data').extra); + } + }); + $scope.cancel = function() { + dialogService.cancel('saveSearchDialog'); + }; + $scope.save = function() { + $('.ui-dialog:visible').block(); + var group = model.id ? {id: model.id} : {title: model.title}; + group.description = model.description; + group.visibility = model.visibility; + group.group_type = model.group_type; + group.saved_search_id = '$id'; + var savedSearch = { + api_entity: model.entity, + api_params: model.params + }; + if (group.id) { + savedSearch.id = model.saved_search_id; + } + crmApi4('SavedSearch', 'save', {records: [savedSearch], chain: {group: ['Group', 'save', {'records': [group]}]}}) + .then(function(result) { + dialogService.close('saveSearchDialog', result[0]); + }); + }; }); angular.module('api4Explorer').directive('crmApi4WhereClause', function($timeout) {