Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 Explorer: show joins for write actions #20731

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CRM/Api4/Page/Api4Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function run() {
'groupOptions' => array_column((array) $groupOptions, 'options', 'name'),
];
Civi::resources()
->addBundle('bootstrap3')
->addVars('api4', $vars)
->addPermissions(['access debug output', 'edit groups', 'administer reserved groups'])
->addScriptFile('civicrm', 'bower_components/js-yaml/dist/js-yaml.min.js')
->addScriptFile('civicrm', 'bower_components/marked/marked.min.js')
->addScriptFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.js')
Expand Down
4 changes: 3 additions & 1 deletion ang/api4Explorer.ang.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// Autoloader data for Api4 explorer.
// Autoloader data for Api4 Explorer Angular module.
return [
'ext' => 'civicrm',
'js' => [
Expand All @@ -13,5 +13,7 @@
'ang/api4Explorer',
],
'basePages' => [],
'bundles' => ['bootstrap3'],
'permissions' => ['access debug output', 'edit groups', 'administer reserved groups'],
'requires' => ['crmUi', 'crmUtil', 'ngRoute', 'crmRouteBinder', 'ui.sortable', 'api4', 'ngSanitize', 'dialogService', 'checklist-model'],
];
33 changes: 28 additions & 5 deletions ang/api4Explorer/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@
}

// Replaces contents of fieldList array with current fields formatted for select2
function getFieldList(fieldList, action, addPseudoconstant) {
function getFieldList(fieldList, action, addPseudoconstant, addWriteJoins) {
var fieldInfo = _.cloneDeep(_.findWhere(getEntity().actions, {name: action}).fields);
fieldList.length = 0;
if (addPseudoconstant) {
addPseudoconstants(fieldInfo, addPseudoconstant);
}
if (addWriteJoins) {
addWriteJoinFields(fieldInfo);
}
formatForSelect2(fieldInfo, fieldList, 'name', ['description', 'required', 'default_value']);
}

Expand Down Expand Up @@ -194,6 +197,19 @@
});
}

// Adds join fields for create actions
// Note: this function transforms a raw list a-la getFields; not a select2-formatted list
function addWriteJoinFields(fieldList) {
_.eachRight(fieldList, function(field, pos) {
var fkNameField = field.fk_entity && getField('name', field.fk_entity, $scope.action);
if (fkNameField) {
var newField = _.cloneDeep(fkNameField);
newField.name = field.name + '.' + newField.name;
fieldList.splice(pos, 0, newField);
}
});
}

$scope.help = function(title, content) {
if (!content) {
$scope.helpTitle = helpTitle;
Expand Down Expand Up @@ -263,12 +279,13 @@
$scope.fieldList = function(param) {
return function() {
var fields = [];
getFieldList(fields, $scope.action === 'getFields' ? ($scope.params.action || 'get') : $scope.action, ['name']);
getFieldList(fields, $scope.action === 'getFields' ? ($scope.params.action || 'get') : $scope.action, ['name'], true);
// Disable fields that are already in use
_.each($scope.params[param] || [], function(val) {
var usedField = val[0].replace(':name', '');
var usedField = val[0].replace(/[:.]name/, '');
(_.findWhere(fields, {id: usedField}) || {}).disabled = true;
(_.findWhere(fields, {id: usedField + ':name'}) || {}).disabled = true;
(_.findWhere(fields, {id: usedField + '.name'}) || {}).disabled = true;
});
return {results: fields};
};
Expand Down Expand Up @@ -1194,7 +1211,8 @@
$el.removeClass('loading').crmSelect2({data: options, multiple: multi});
});
} else if (field.fk_entity) {
$el.crmEntityRef({entity: field.fk_entity, select:{multiple: multi}, static: field.fk_entity === 'Contact' ? ['user_contact_id'] : []});
var apiParams = field.id_field ? {id_field: field.id_field} : {};
$el.crmEntityRef({entity: field.fk_entity, api: apiParams, select: {multiple: multi}, static: field.fk_entity === 'Contact' ? ['user_contact_id'] : []});
} else if (dataType === 'Boolean') {
$el.attr('placeholder', ts('- select -')).crmSelect2({allowClear: false, multiple: multi, placeholder: ts('- select -'), data: [
{id: 'true', text: ts('Yes')},
Expand Down Expand Up @@ -1374,10 +1392,15 @@
var suffix = fieldName.split(':')[1];
fieldName = fieldName.split(':')[0];
var fieldNames = fieldName.split('.');
var field = get(entity, fieldNames);
var field = _.cloneDeep(get(entity, fieldNames));
if (field && suffix) {
field.pseudoconstant = suffix;
}
// When joining to a 'name' field, value fields should render an appropriate entityRef
if (field && field.type === 'Field' && field.name === 'name' && _.includes(fieldName, '.')) {
field.fk_entity = field.entity;
field.id_field = 'name';
}
return field;

function get(entity, fieldNames) {
Expand Down
2 changes: 2 additions & 0 deletions api/v3/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@ function _civicrm_api3_activity_getlist_params(&$request) {
'activity_type_id',
'subject',
'source_contact_id',
$request['id_field'],
$request['label_field'],
];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
$request['params']['options']['sort'] = 'activity_date_time DESC';
Expand Down
10 changes: 9 additions & 1 deletion api/v3/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ function civicrm_api3_campaign_delete($params) {
* @param array $request
*/
function _civicrm_api3_campaign_getlist_params(&$request) {
$fieldsToReturn = ['title', 'campaign_type_id', 'status_id', 'start_date', 'end_date'];
$fieldsToReturn = [
'title',
'campaign_type_id',
'status_id',
'start_date',
'end_date',
$request['id_field'],
$request['label_field'],
];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
if (empty($request['params']['id'])) {
$request['params']['options']['sort'] = 'start_date DESC, title';
Expand Down
9 changes: 8 additions & 1 deletion api/v3/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,14 @@ function _civicrm_api3_event_getisfull(&$event, $event_id) {
* @param array $request
*/
function _civicrm_api3_event_getlist_params(&$request) {
$fieldsToReturn = ['start_date', 'event_type_id', 'title', 'summary'];
$fieldsToReturn = [
'start_date',
'event_type_id',
'title',
'summary',
$request['id_field'],
$request['label_field'],
];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
$request['params']['options']['sort'] = 'start_date DESC';
if (empty($request['params']['id'])) {
Expand Down
1 change: 1 addition & 0 deletions api/v3/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function _civicrm_api3_membership_type_get_spec(&$params) {
* Array of parameters determined by getfields.
*/
function _civicrm_api3_membership_type_getlist_params(&$request) {
_civicrm_api3_generic_getlist_params($request);
if (!isset($request['params']['is_active']) && empty($request['params']['id'])) {
$request['params']['is_active'] = 1;
}
Expand Down
9 changes: 8 additions & 1 deletion ext/afform/core/api/v3/Afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ function _civicrm_api3_afform_get_spec(&$fields) {
* API request.
*/
function _civicrm_api3_afform_getlist_params(&$request) {
$fieldsToReturn = ['name', 'title', 'type', 'description'];
$fieldsToReturn = [
'name',
'title',
'type',
'description',
$request['id_field'],
$request['label_field'],
];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
}

Expand Down