$fields = $this->_itemsToGet('name');
/** @var \Civi\Api4\Service\Spec\SpecGatherer $gatherer */
$gatherer = \Civi::container()->get('spec_gatherer');
- $spec = $gatherer->getSpec('Custom_' . $this->getCustomGroup(), $this->getAction(), $this->includeCustom);
+ $spec = $gatherer->getSpec('Custom_' . $this->getCustomGroup(), $this->getAction(), $this->includeCustom, $this->values);
return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions);
}
* @method $this setLoadOptions(bool $value)
* @method bool getLoadOptions()
* @method $this setAction(string $value)
+ * @method $this addValue(string $value)
+ * @method $this setValues(array $values)
+ * @method array getValues()
*/
class BasicGetFieldsAction extends BasicGetAction {
protected $loadOptions = FALSE;
/**
+ * Fields will be returned appropriate to the specified action (get, create, delete, etc.)
+ *
* @var string
*/
protected $action = 'get';
+ /**
+ * Fields will be returned appropriate to the specified values (e.g. ['contact_type' => 'Individual'])
+ *
+ * @var array
+ */
+ protected $values = [];
+
/**
* To implement getFields for your own entity:
*
if ($fields) {
$this->includeCustom = strpos(implode('', $fields), '.') !== FALSE;
}
- $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom);
+ $spec = $gatherer->getSpec($this->getEntityName(), $this->getAction(), $this->includeCustom, $this->values);
return SpecFormatter::specToArray($spec->getFields($fields), $this->loadOptions);
}
*
* @param string $entity
* @param string $action
- * @param $includeCustom
+ * @param bool $includeCustom
+ * @param array $values
*
* @return \Civi\Api4\Service\Spec\RequestSpec
*/
- public function getSpec($entity, $action, $includeCustom) {
+ public function getSpec($entity, $action, $includeCustom, $values = []) {
$specification = new RequestSpec($entity, $action);
// Real entities
if (strpos($entity, 'Custom_') !== 0) {
$this->addDAOFields($entity, $action, $specification);
if ($includeCustom && array_key_exists($entity, \CRM_Core_SelectValues::customGroupExtends())) {
- $this->addCustomFields($entity, $specification);
+ $this->addCustomFields($entity, $specification, $values);
}
}
// Custom pseudo-entities
/**
* @param string $entity
* @param \Civi\Api4\Service\Spec\RequestSpec $specification
+ * @param array $values
+ * @throws \API_Exception
*/
- private function addCustomFields($entity, RequestSpec $specification) {
- $extends = ($entity == 'Contact') ? ['Contact', 'Individual', 'Organization', 'Household'] : [$entity];
+ private function addCustomFields($entity, RequestSpec $specification, $values = []) {
+ $extends = [$entity];
+ if ($entity === 'Contact') {
+ $extends = !empty($values['contact_type']) ? [$values['contact_type'], 'Contact'] : ['Contact', 'Individual', 'Organization', 'Household'];
+ }
$customFields = CustomField::get()
->setCheckPermissions(FALSE)
->addWhere('custom_group.extends', 'IN', $extends)
+ ->addWhere('custom_group.is_multiple', '=', '0')
->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value', 'date_format', 'time_format', 'start_date_years', 'end_date_years', 'help_pre', 'help_post'])
->execute();
<legend>values<span class="crm-marker" ng-if="availableParams.values.required"> *</span></legend>
<div class="api4-input form-inline" ng-repeat="clause in params.values" ng-mouseenter="help('value: ' + clause[0], fieldHelp(clause[0]))" ng-mouseleave="help('values', availableParams.values)">
<input class="collapsible-optgroups form-control" ng-model="clause[0]" crm-ui-select="{formatResult: formatSelect2Item, formatSelection: formatSelect2Item, data: valuesFields, allowClear: true, placeholder: 'Field'}" />
- <input class="form-control" ng-model="clause[1]" api4-exp-value="{field: clause[0]}" />
+ <input class="form-control" ng-model="clause[1]" api4-exp-value="{field: clause[0], action: action === 'getFields' ? params.action || 'get' : action}" />
</div>
<div class="api4-input form-inline">
<input class="collapsible-optgroups form-control" ng-model="controls.values" crm-ui-select="{formatResult: formatSelect2Item, formatSelection: formatSelect2Item, data: valuesFields}" placeholder="Add value" />
return container;
}
- function getFieldList(source) {
+ function getFieldList(action) {
var fields = [],
- fieldInfo = _.findWhere(getEntity().actions, {name: $scope.action}).fields;
+ fieldInfo = _.findWhere(getEntity().actions, {name: action}).fields;
formatForSelect2(fieldInfo, fields, 'name', ['description', 'required', 'default_value']);
return fields;
}
};
$scope.valuesFields = function() {
- var fields = _.cloneDeep($scope.fields);
+ 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;
}
if ($scope.action) {
var actionInfo = _.findWhere(actions, {id: $scope.action});
- $scope.fields = getFieldList();
+ $scope.fields = getFieldList($scope.action);
if (_.contains(['get', 'update', 'delete', 'replace'], $scope.action)) {
$scope.fieldsAndJoins = addJoins($scope.fields);
} else {
var ts = scope.ts = CRM.ts(),
multi = _.includes(['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'], scope.data.op),
entity = $routeParams.api4entity,
- action = $routeParams.api4action;
+ action = scope.data.action || $routeParams.api4action;
function destroyWidget() {
var $el = $(element);
],
];
- $this->dropByPrefix('civicrm_value_mycontact');
+ $this->dropByPrefix('civicrm_value_my');
$this->cleanup($cleanup_params);
}
$customGroup = CustomGroup::create()
->setCheckPermissions(FALSE)
- ->addValue('name', 'MyContactFields')
- ->addValue('extends', 'Contact')
+ ->addValue('name', 'MyIndividualFields')
+ ->addValue('extends', 'Individual')
->execute()
->first();
->addValue('data_type', 'String')
->execute();
+ // Individual fields should show up when contact_type = null|Individual but not other contact types
+ $getFields = Contact::getFields()->setCheckPermissions(FALSE);
+ $this->assertContains('MyIndividualFields.FavColor', $getFields->execute()->column('name'));
+ $this->assertContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name'));
+ $this->assertNotContains('MyIndividualFields.FavColor', $getFields->setValues(['contact_type' => 'Household'])->execute()->column('name'));
+
$contactId = Contact::create()
->setCheckPermissions(FALSE)
->addValue('first_name', 'Johann')
->addValue('last_name', 'Tester')
->addValue('contact_type', 'Individual')
- ->addValue('MyContactFields.FavColor', 'Red')
+ ->addValue('MyIndividualFields.FavColor', 'Red')
->execute()
->first()['id'];
$contact = Contact::get()
->setCheckPermissions(FALSE)
->addSelect('first_name')
- ->addSelect('MyContactFields.FavColor')
+ ->addSelect('MyIndividualFields.FavColor')
->addWhere('id', '=', $contactId)
- ->addWhere('MyContactFields.FavColor', '=', 'Red')
+ ->addWhere('MyIndividualFields.FavColor', '=', 'Red')
->execute()
->first();
- $this->assertEquals('Red', $contact['MyContactFields.FavColor']);
+ $this->assertEquals('Red', $contact['MyIndividualFields.FavColor']);
Contact::update()
->addWhere('id', '=', $contactId)
- ->addValue('MyContactFields.FavColor', 'Blue')
+ ->addValue('MyIndividualFields.FavColor', 'Blue')
->execute();
$contact = Contact::get()
->setCheckPermissions(FALSE)
- ->addSelect('MyContactFields.FavColor')
+ ->addSelect('MyIndividualFields.FavColor')
->addWhere('id', '=', $contactId)
->execute()
->first();
- $this->assertEquals('Blue', $contact['MyContactFields.FavColor']);
+ $this->assertEquals('Blue', $contact['MyIndividualFields.FavColor']);
}
public function testWithTwoFields() {