3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\CustomGroup
;
15 * Trait Custom Data trait.
17 * Trait for setting up custom data in tests.
19 trait CRMTraits_Custom_CustomDataTrait
{
22 * Create a custom group with fields of multiple types.
24 * @param array $groupParams
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
29 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
30 $this->createCustomGroup($groupParams);
31 $this->ids
['CustomField'] = $this->createCustomFieldsOfAllTypes();
35 * Create a custom group.
37 * @param array $params
41 * @throws \API_Exception
42 * @throws \Civi\API\Exception\UnauthorizedException
44 public function createCustomGroup($params = []) {
45 $params = array_merge([
46 'title' => 'Custom Group',
47 'extends' => [$this->entity ??
'Contact'],
52 $identifier = $params['name'] ??
$params['title'];
53 $this->ids
['CustomGroup'][$identifier] = CustomGroup
::create(FALSE)->setValues($params)->execute()->first()['id'];
54 return $this->ids
['CustomGroup'][$identifier];
58 * Get the table_name for the specified custom group.
60 * @param string $identifier
64 public function getCustomGroupTable($identifier = 'Custom Group') {
65 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids
['CustomGroup'][$identifier], 'return' => 'table_name']);
69 * Get the the column name for the identified custom field.
72 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
76 protected function getCustomFieldColumnName($key) {
77 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
81 * Create a custom group with a single field.
83 * @param array $groupParams
84 * Params for the group to be created.
85 * @param string $customFieldType
87 * @param string $identifier
89 * @throws \API_Exception
90 * @throws \CRM_Core_Exception
91 * @throws \Civi\API\Exception\UnauthorizedException
93 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
94 $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio'];
95 if (!in_array($customFieldType, $supported, TRUE)) {
96 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
98 $groupParams['title'] = empty($groupParams['title']) ?
$identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
99 $groupParams['name'] = $identifier ??
'Custom Group';
100 $this->createCustomGroup($groupParams);
101 $reference = &$this->ids
['CustomField'][$identifier . $customFieldType];
102 $fieldParams = ['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]];
103 switch ($customFieldType) {
105 $reference = $this->createTextCustomField($fieldParams)['id'];
109 $reference = $this->createSelectCustomField($fieldParams)['id'];
113 $reference = $this->createIntCustomField($fieldParams)['id'];
117 $reference = $this->createDateCustomField($fieldParams)['id'];
120 case 'contact_reference':
121 $reference = $this->createContactReferenceCustomField($fieldParams)['id'];
125 $reference = $this->createIntegerRadioCustomField($fieldParams)['id'];
134 public function createCustomFieldsOfAllTypes() {
135 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
137 $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id'];
138 $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id'];
139 $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id'];
140 $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id'];
141 $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id'];
142 $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id'];
143 $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id'];
144 $ids['multi_country'] = (int) $this->createMultiCountryCustomField(['custom_group_id' => $customGroupID])['id'];
145 $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id'];
146 $ids['state'] = (int) $this->createStateCustomField(['custom_group_id' => $customGroupID])['id'];
147 $ids['multi_state'] = (int) $this->createMultiStateCustomField(['custom_group_id' => $customGroupID])['id'];
148 $ids['boolean'] = (int) $this->createBooleanCustomField(['custom_group_id' => $customGroupID])['id'];
153 * Get the custom field name for the relevant key.
155 * e.g returns 'custom_5' where 5 is the id of the field using the key.
157 * Generally keys map to data types.
163 protected function getCustomFieldName($key) {
164 return 'custom_' . $this->getCustomFieldID($key);
168 * Get the custom field name for the relevant key.
170 * e.g returns 'custom_5' where 5 is the id of the field using the key.
172 * Generally keys map to data types.
178 protected function getCustomFieldID($key) {
179 return $this->ids
['CustomField'][$key];
183 * Create a custom text fields.
185 * @param array $params
186 * Parameter overrides, must include custom_group_id.
190 protected function createIntCustomField($params = []) {
191 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
192 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
196 * Create a custom text fields.
198 * @param array $params
199 * Parameter overrides, must include custom_group_id.
203 protected function createBooleanCustomField($params = []) {
204 $params = array_merge($this->getFieldsValuesByType('Boolean'), $params);
205 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
209 * Create a custom text fields.
211 * @param array $params
212 * Parameter overrides, must include custom_group_id.
216 protected function createContactReferenceCustomField($params = []) {
217 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
218 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
222 * Create a custom text fields.
224 * @param array $params
225 * Parameter overrides, must include custom_group_id.
229 protected function createTextCustomField($params = []) {
230 $params = array_merge($this->getFieldsValuesByType('String'), $params);
231 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
235 * Create a custom text fields.
237 * @param array $params
238 * Parameter overrides, must include custom_group_id.
242 protected function createLinkCustomField($params = []) {
243 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
244 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
248 * Create a custom country fields.
250 * @param array $params
251 * Parameter overrides, must include custom_group_id.
255 protected function createCountryCustomField($params = []) {
256 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
257 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
261 * Create a custom multi select country fields.
263 * @param array $params
264 * Parameter overrides, must include custom_group_id.
268 protected function createMultiCountryCustomField($params = []) {
269 $params = array_merge($this->getFieldsValuesByType('Country', 'Multi-Select Country'), $params);
270 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
274 * Create a custom state fields.
276 * @param array $params
277 * Parameter overrides, must include custom_group_id.
281 protected function createStateCustomField($params = []) {
282 $params = array_merge($this->getFieldsValuesByType('StateProvince'), $params);
283 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
287 * Create a custom multi select state fields.
289 * @param array $params
290 * Parameter overrides, must include custom_group_id.
294 protected function createMultiStateCustomField($params = []) {
295 $params = array_merge($this->getFieldsValuesByType('StateProvince', 'Multi-Select State/Province'), $params);
296 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
300 * Create a custom text fields.
302 * @param array $params
303 * Parameter overrides, must include custom_group_id.
307 protected function createFileCustomField($params = []) {
308 $params = array_merge($this->getFieldsValuesByType('File'), $params);
309 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
313 * Create custom select field.
315 * @param array $params
316 * Parameter overrides, must include custom_group_id.
320 protected function createSelectCustomField(array $params): array {
321 $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params);
322 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
326 * Create a custom field of type date.
328 * @param array $params
332 protected function createDateCustomField($params): array {
333 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
334 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
338 * Create a custom field of type radio with integer values.
340 * @param array $params
344 protected function createIntegerRadioCustomField($params): array {
345 $params = array_merge($this->getFieldsValuesByType('Int', 'Radio'), $params);
346 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
350 * Get default field values for the type of field.
353 * @param string $htmlType
357 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
358 $values = $this->getAvailableFieldCombinations()[$dataType];
360 'is_searchable' => 1,
362 'default_value' => '',
364 ], array_merge($values['default'], $values[$htmlType])
369 * Get data available for custom fields.
371 * The 'default' key holds general values. Where more than one html type is an option
372 * then the any values that differ to the defaults are keyed by html key.
374 * The order below is consistent with the UI.
378 protected function getAvailableFieldCombinations() {
382 'label' => 'Enter text here',
383 'html_type' => 'Text',
384 'data_type' => 'String',
385 'default_value' => 'xyz',
386 'text_length' => 300,
389 'label' => 'Pick Color',
390 'html_type' => 'Select',
391 'data_type' => 'String',
393 'default_value' => '',
416 'label' => 'Pick Color',
417 'html_type' => 'Radio',
418 'data_type' => 'String',
420 'default_value' => '',
443 'label' => 'Pick Color',
444 'html_type' => 'CheckBox',
445 'data_type' => 'String',
447 'default_value' => '',
470 'label' => 'Pick Color',
471 'html_type' => 'Multi-Select',
472 'data_type' => 'String',
474 'default_value' => '',
496 'Autocomplete-Select' => [
497 'label' => 'Pick Color',
498 'html_type' => 'Autocomplete-Select',
499 'data_type' => 'String',
501 'default_value' => '',
526 'label' => 'Enter integer here',
527 'html_type' => 'Text',
528 'data_type' => 'Int',
529 'default_value' => '4',
530 'is_search_range' => 1,
533 'label' => 'Integer select',
534 'html_type' => 'Select',
551 'label' => 'Integer radio',
552 'html_type' => 'Radio',
567 'label' => 'Red Testing',
577 'name' => 'test_date',
578 'label' => 'Test Date',
579 'html_type' => 'Select Date',
580 'data_type' => 'Date',
581 'default_value' => '20090711',
583 'is_search_range' => 1,
590 'html_type' => 'Text',
591 'data_type' => 'Float',
594 'label' => 'Number select',
595 'html_type' => 'Select',
612 'label' => 'Number radio',
613 'html_type' => 'Radio',
633 'html_type' => 'Text',
634 'data_type' => 'Money',
637 'label' => 'Money select',
638 'html_type' => 'Select',
655 'label' => 'Money radio',
656 'html_type' => 'Radio',
676 'html_type' => 'TextArea',
677 'data_type' => 'Memo',
678 'attributes' => 'rows=4, cols=60',
680 'RichTextEditor' => [
681 'label' => 'Memo Rich Text Editor',
682 'html_type' => 'Memo',
687 'data_type' => 'Boolean',
688 'html_type' => 'Radio',
694 'data_type' => 'StateProvince',
695 'html_type' => 'Select State/Province',
699 'Multi-Select State/Province' => [
700 'html_type' => 'Multi-Select State/Province',
701 'label' => 'State-multi',
706 'data_type' => 'Country',
707 'html_type' => 'Select Country',
708 'label' => 'Country',
711 'Multi-Select Country' => [
712 'html_type' => 'Multi-Select Country',
713 'label' => 'Country-multi',
719 'label' => 'My file',
720 'data_type' => 'File',
721 'html_type' => 'File',
726 'name' => 'test_link',
727 'label' => 'test_link',
728 'html_type' => 'Link',
729 'data_type' => 'Link',
730 'default_value' => 'http://civicrm.org',
733 'ContactReference' => [
735 'label' => 'Contact reference field',
736 'html_type' => 'Autocomplete-Select',
737 'data_type' => 'ContactReference',