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()->setCheckPermissions(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'];
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'];
129 public function createCustomFieldsOfAllTypes() {
130 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
132 $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id'];
133 $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id'];
134 $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id'];
135 $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id'];
136 $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id'];
137 $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id'];
138 $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id'];
139 $ids['multi_country'] = (int) $this->createMultiCountryCustomField(['custom_group_id' => $customGroupID])['id'];
140 $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id'];
141 $ids['state'] = (int) $this->createStateCustomField(['custom_group_id' => $customGroupID])['id'];
142 $ids['multi_state'] = (int) $this->createMultiStateCustomField(['custom_group_id' => $customGroupID])['id'];
143 $ids['boolean'] = (int) $this->createBooleanCustomField(['custom_group_id' => $customGroupID])['id'];
148 * Get the custom field name for the relevant key.
150 * e.g returns 'custom_5' where 5 is the id of the field using the key.
152 * Generally keys map to data types.
158 protected function getCustomFieldName($key) {
159 return 'custom_' . $this->getCustomFieldID($key);
163 * Get the custom field name for the relevant key.
165 * e.g returns 'custom_5' where 5 is the id of the field using the key.
167 * Generally keys map to data types.
173 protected function getCustomFieldID($key) {
174 return $this->ids
['CustomField'][$key];
178 * Create a custom text fields.
180 * @param array $params
181 * Parameter overrides, must include custom_group_id.
185 protected function createIntCustomField($params = []) {
186 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
187 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
191 * Create a custom text fields.
193 * @param array $params
194 * Parameter overrides, must include custom_group_id.
198 protected function createBooleanCustomField($params = []) {
199 $params = array_merge($this->getFieldsValuesByType('Boolean'), $params);
200 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
204 * Create a custom text fields.
206 * @param array $params
207 * Parameter overrides, must include custom_group_id.
211 protected function createContactReferenceCustomField($params = []) {
212 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
213 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
217 * Create a custom text fields.
219 * @param array $params
220 * Parameter overrides, must include custom_group_id.
224 protected function createTextCustomField($params = []) {
225 $params = array_merge($this->getFieldsValuesByType('String'), $params);
226 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
230 * Create a custom text fields.
232 * @param array $params
233 * Parameter overrides, must include custom_group_id.
237 protected function createLinkCustomField($params = []) {
238 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
239 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
243 * Create a custom country fields.
245 * @param array $params
246 * Parameter overrides, must include custom_group_id.
250 protected function createCountryCustomField($params = []) {
251 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
252 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
256 * Create a custom multi select country fields.
258 * @param array $params
259 * Parameter overrides, must include custom_group_id.
263 protected function createMultiCountryCustomField($params = []) {
264 $params = array_merge($this->getFieldsValuesByType('Country', 'Multi-Select Country'), $params);
265 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
269 * Create a custom state fields.
271 * @param array $params
272 * Parameter overrides, must include custom_group_id.
276 protected function createStateCustomField($params = []) {
277 $params = array_merge($this->getFieldsValuesByType('StateProvince'), $params);
278 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
282 * Create a custom multi select state fields.
284 * @param array $params
285 * Parameter overrides, must include custom_group_id.
289 protected function createMultiStateCustomField($params = []) {
290 $params = array_merge($this->getFieldsValuesByType('StateProvince', 'Multi-Select State/Province'), $params);
291 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
295 * Create a custom text fields.
297 * @param array $params
298 * Parameter overrides, must include custom_group_id.
302 protected function createFileCustomField($params = []) {
303 $params = array_merge($this->getFieldsValuesByType('File'), $params);
304 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
308 * Create custom select field.
310 * @param array $params
311 * Parameter overrides, must include custom_group_id.
315 protected function createSelectCustomField(array $params): array {
316 $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params);
317 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
321 * Create a custom field of type date.
323 * @param array $params
327 protected function createDateCustomField($params): array {
328 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
329 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
333 * Get default field values for the type of field.
336 * @param string $htmlType
340 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
341 $values = $this->getAvailableFieldCombinations()[$dataType];
343 'is_searchable' => 1,
345 'default_value' => '',
347 ], array_merge($values['default'], $values[$htmlType])
352 * Get data available for custom fields.
354 * The 'default' key holds general values. Where more than one html type is an option
355 * then the any values that differ to the defaults are keyed by html key.
357 * The order below is consistent with the UI.
361 protected function getAvailableFieldCombinations() {
365 'label' => 'Enter text here',
366 'html_type' => 'Text',
367 'data_type' => 'String',
368 'default_value' => 'xyz',
369 'text_length' => 300,
372 'label' => 'Pick Color',
373 'html_type' => 'Select',
374 'data_type' => 'String',
376 'default_value' => '',
399 'label' => 'Pick Color',
400 'html_type' => 'Radio',
401 'data_type' => 'String',
403 'default_value' => '',
426 'label' => 'Pick Color',
427 'html_type' => 'Checkbox',
428 'data_type' => 'String',
430 'default_value' => '',
453 'label' => 'Pick Color',
454 'html_type' => 'Multi-Select',
455 'data_type' => 'String',
457 'default_value' => '',
479 'Autocomplete-Select' => [
480 'label' => 'Pick Color',
481 'html_type' => 'Autocomplete-Select',
482 'data_type' => 'String',
484 'default_value' => '',
509 'label' => 'Enter integer here',
510 'html_type' => 'Text',
511 'data_type' => 'Int',
512 'default_value' => '4',
513 'is_search_range' => 1,
516 'label' => 'Integer select',
517 'html_type' => 'Select',
534 'label' => 'Integer radio',
535 'html_type' => 'Radio',
554 'name' => 'test_date',
555 'label' => 'Test Date',
556 'html_type' => 'Select Date',
557 'data_type' => 'Date',
558 'default_value' => '20090711',
560 'is_search_range' => 1,
567 'html_type' => 'Text',
568 'data_type' => 'Float',
571 'label' => 'Number select',
572 'html_type' => 'Select',
589 'label' => 'Number radio',
590 'html_type' => 'Radio',
610 'html_type' => 'Text',
611 'data_type' => 'Money',
614 'label' => 'Money select',
615 'html_type' => 'Select',
632 'label' => 'Money radio',
633 'html_type' => 'Radio',
653 'html_type' => 'TextArea',
654 'data_type' => 'Memo',
655 'attributes' => 'rows=4, cols=60',
657 'RichTextEditor' => [
658 'label' => 'Memo Rich Text Editor',
659 'html_type' => 'Memo',
664 'data_type' => 'Boolean',
665 'html_type' => 'Radio',
671 'data_type' => 'StateProvince',
672 'html_type' => 'Select State/Province',
676 'Multi-Select State/Province' => [
677 'html_type' => 'Multi-Select State/Province',
678 'label' => 'State-multi',
683 'data_type' => 'Country',
684 'html_type' => 'Select Country',
685 'label' => 'Country',
688 'Multi-Select Country' => [
689 'html_type' => 'Multi-Select Country',
690 'label' => 'Country-multi',
696 'label' => 'My file',
697 'data_type' => 'File',
698 'html_type' => 'File',
703 'name' => 'test_link',
704 'label' => 'test_link',
705 'html_type' => 'Link',
706 'data_type' => 'Link',
707 'default_value' => 'http://civicrm.org',
710 'ContactReference' => [
712 'label' => 'Contact reference field',
713 'html_type' => 'Autocomplete-Select',
714 'data_type' => 'ContactReference',