Merge pull request #18589 from eileenmcnaughton/ex_class
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Business objects for managing custom data fields.
20 */
21 class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
22
23 /**
24 * Array to hold (formatted) fields for import
25 *
26 * @var array
27 */
28 public static $_importFields = NULL;
29
30 /**
31 * Build and retrieve the list of data types and descriptions.
32 *
33 * @return array
34 * Data type => Description
35 */
36 public static function dataType() {
37 return [
38 'String' => ts('Alphanumeric'),
39 'Int' => ts('Integer'),
40 'Float' => ts('Number'),
41 'Money' => ts('Money'),
42 'Memo' => ts('Note'),
43 'Date' => ts('Date'),
44 'Boolean' => ts('Yes or No'),
45 'StateProvince' => ts('State/Province'),
46 'Country' => ts('Country'),
47 'File' => ts('File'),
48 'Link' => ts('Link'),
49 'ContactReference' => ts('Contact Reference'),
50 ];
51 }
52
53 /**
54 * Build the map of custom field's data types and there respective Util type
55 *
56 * @return array
57 * Data data-type => CRM_Utils_Type
58 */
59 public static function dataToType() {
60 return [
61 'String' => CRM_Utils_Type::T_STRING,
62 'Int' => CRM_Utils_Type::T_INT,
63 'Money' => CRM_Utils_Type::T_MONEY,
64 'Memo' => CRM_Utils_Type::T_LONGTEXT,
65 'Float' => CRM_Utils_Type::T_FLOAT,
66 'Date' => CRM_Utils_Type::T_DATE,
67 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
68 'Boolean' => CRM_Utils_Type::T_BOOLEAN,
69 'StateProvince' => CRM_Utils_Type::T_INT,
70 'File' => CRM_Utils_Type::T_STRING,
71 'Link' => CRM_Utils_Type::T_STRING,
72 'ContactReference' => CRM_Utils_Type::T_INT,
73 'Country' => CRM_Utils_Type::T_INT,
74 ];
75 }
76
77 /**
78 * Takes an associative array and creates a custom field object.
79 *
80 * This function is invoked from within the web form layer and also from the api layer
81 *
82 * @param array $params
83 * (reference) an assoc array of name/value pairs.
84 *
85 * @return CRM_Core_DAO_CustomField
86 */
87 public static function create($params) {
88 $customField = self::createCustomFieldRecord($params);
89 $op = empty($params['id']) ? 'add' : 'modify';
90 self::createField($customField, $op);
91
92 CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField);
93
94 CRM_Utils_System::flushCache();
95 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
96 // so do it manually. Ideally we wouldn't need to clear others...
97 Civi::cache('metadata')->clear();
98
99 return $customField;
100 }
101
102 /**
103 * Create/ update several fields at once in a mysql efficient way.
104 *
105 * https://lab.civicrm.org/dev/core/issues/1093
106 *
107 * The intention is that apiv4 would expose any BAO with bulkSave as a new action.
108 *
109 * @param array $bulkParams
110 * Array of arrays as would be passed into create
111 * @param array $defaults
112 * Default parameters to be be merged into each of the params.
113 *
114 * @throws \CiviCRM_API3_Exception
115 */
116 public static function bulkSave($bulkParams, $defaults = []) {
117 $addedColumns = $sql = $tables = $customFields = [];
118 foreach ($bulkParams as $index => $fieldParams) {
119 $params = array_merge($defaults, $fieldParams);
120 $customField = self::createCustomFieldRecord($params);
121 $fieldSQL = self::getAlterFieldSQL($customField, empty($params['id']) ? 'add' : 'modify');
122 if (!isset($params['custom_group_id'])) {
123 $params['custom_group_id'] = civicrm_api3('CustomField', 'getvalue', ['id' => $customField->id, 'return' => 'custom_group_id']);
124 }
125 if (!isset($params['table_name'])) {
126 if (!isset($tables[$params['custom_group_id']])) {
127 $tables[$params['custom_group_id']] = civicrm_api3('CustomGroup', 'getvalue', [
128 'id' => $params['custom_group_id'],
129 'return' => 'table_name',
130 ]);
131 }
132 $params['table_name'] = $tables[$params['custom_group_id']];
133 }
134 $sql[$params['table_name']][] = $fieldSQL;
135 $addedColumns[$params['table_name']][] = $customField->name;
136 $customFields[$index] = $customField;
137 }
138
139 foreach ($sql as $tableName => $statements) {
140 // CRM-7007: do not i18n-rewrite this query
141 CRM_Core_DAO::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE);
142
143 if (CRM_Core_Config::singleton()->logging) {
144 $logging = new CRM_Logging_Schema();
145 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
146 }
147
148 Civi::service('sql_triggers')->rebuild($params['table_name'], TRUE);
149 }
150 CRM_Utils_System::flushCache();
151 foreach ($customFields as $index => $customField) {
152 CRM_Utils_Hook::post(empty($bulkParams[$index]['id']) ? 'create' : 'edit', 'CustomField', $customField->id, $customField);
153 }
154 }
155
156 /**
157 * Fetch object based on array of properties.
158 *
159 * @param array $params
160 * An assoc array of name/value pairs.
161 * @param array $defaults
162 * (reference ) an assoc array to hold the flattened values.
163 *
164 * @return CRM_Core_DAO_CustomField
165 */
166 public static function retrieve($params, &$defaults) {
167 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
168 }
169
170 /**
171 * Update the is_active flag in the db.
172 *
173 * @param int $id
174 * Id of the database record.
175 * @param bool $is_active
176 * Value we want to set the is_active field.
177 *
178 * @return bool
179 * true if we found and updated the object, else false
180 */
181 public static function setIsActive($id, $is_active) {
182
183 CRM_Utils_System::flushCache();
184
185 //enable-disable CustomField
186 CRM_Core_BAO_UFField::setUFField($id, $is_active);
187 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomField', $id, 'is_active', $is_active);
188 }
189
190 /**
191 * Get the field title.
192 *
193 * @param int $id
194 * Id of field.
195 *
196 * @return string
197 * name
198 */
199 public static function getTitle($id) {
200 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
201 }
202
203 /**
204 * @param string $context
205 * @return array|bool
206 */
207 public function getOptions($context = NULL) {
208 CRM_Core_DAO::buildOptionsContext($context);
209
210 if (!$this->id) {
211 return FALSE;
212 }
213 $cacheKey = "CRM_Core_BAO_CustomField_getOptions_{$this->id}_$context";
214 $cache = CRM_Utils_Cache::singleton();
215 $options = $cache->get($cacheKey);
216 if (!isset($options)) {
217 if (!$this->data_type || !$this->custom_group_id) {
218 $this->find(TRUE);
219 }
220
221 // This will hold the list of options in format key => label
222 $options = [];
223
224 if (!empty($this->option_group_id)) {
225 $options = CRM_Core_OptionGroup::valuesByID(
226 $this->option_group_id, FALSE, FALSE, FALSE, $context == 'validate' ? 'name' : 'label', !($context == 'validate' || $context == 'get')
227 );
228 }
229 elseif ($this->data_type === 'StateProvince') {
230 $options = CRM_Core_PseudoConstant::stateProvince();
231 }
232 elseif ($this->data_type === 'Country') {
233 $options = $context == 'validate' ? CRM_Core_PseudoConstant::countryIsoCode() : CRM_Core_PseudoConstant::country();
234 }
235 elseif ($this->data_type === 'Boolean') {
236 $options = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
237 }
238 CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
239 CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, ['context' => $context]);
240 $cache->set($cacheKey, $options);
241 }
242 return $options;
243 }
244
245 /**
246 * @inheritDoc
247 */
248 public static function buildOptions($fieldName, $context = NULL, $props = []) {
249 $options = parent::buildOptions($fieldName, $context, $props);
250 // This provides legacy support for APIv3, allowing no-longer-existent html types
251 if ($fieldName == 'html_type' && isset($props['version']) && $props['version'] == 3) {
252 $options['Multi-Select'] = 'Multi-Select';
253 $options['Select Country'] = 'Select Country';
254 $options['Multi-Select Country'] = 'Multi-Select Country';
255 $options['Select State/Province'] = 'Select State/Province';
256 $options['Multi-Select State/Province'] = 'Multi-Select State/Province';
257 }
258 return $options;
259 }
260
261 /**
262 * Store and return an array of all active custom fields.
263 *
264 * @param string $customDataType
265 * Type of Custom Data; 'ANY' is a synonym for "all contact data types".
266 * @param bool $showAll
267 * If true returns all fields (includes disabled fields).
268 * @param bool $inline
269 * If true returns all inline fields (includes disabled fields).
270 * @param int $customDataSubType
271 * Custom Data sub type value.
272 * @param int $customDataSubName
273 * Custom Data sub name value.
274 * @param bool $onlyParent
275 * Return only top level custom data, for eg, only Participant and ignore subname and subtype.
276 * @param bool $onlySubType
277 * Return only custom data for subtype.
278 * @param bool $checkPermission
279 * If false, do not include permissioning clause.
280 *
281 * @return array
282 * an array of active custom fields.
283 * @throws \CRM_Core_Exception
284 */
285 public static function &getFields(
286 $customDataType = 'Individual',
287 $showAll = FALSE,
288 $inline = FALSE,
289 $customDataSubType = NULL,
290 $customDataSubName = NULL,
291 $onlyParent = FALSE,
292 $onlySubType = FALSE,
293 $checkPermission = TRUE
294 ) {
295 if (empty($customDataType)) {
296 $customDataType = ['Contact', 'Individual', 'Organization', 'Household'];
297 }
298 if ($customDataType === 'ANY') {
299 // NULL should have been respected but the line above broke that.
300 // boo for us not having enough unit tests back them.
301 $customDataType = NULL;
302 }
303 if ($customDataType && !is_array($customDataType)) {
304
305 if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes())) {
306 // This is the case when getFieldsForImport() requires fields
307 // limited strictly to a subtype.
308 $customDataSubType = $customDataType;
309 $customDataType = CRM_Contact_BAO_ContactType::getBasicType($customDataType);
310 $onlySubType = TRUE;
311 }
312
313 if (in_array($customDataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) {
314 // this makes the method flexible to support retrieving fields
315 // for multiple extends value.
316 $customDataType = [$customDataType];
317 }
318 }
319
320 $customDataSubType = CRM_Utils_Array::explodePadded($customDataSubType);
321
322 if (is_array($customDataType)) {
323 $cacheKey = implode('_', $customDataType);
324 }
325 else {
326 $cacheKey = $customDataType;
327 }
328
329 $cacheKey .= !empty($customDataSubType) ? ('_' . implode('_', $customDataSubType)) : '_0';
330 $cacheKey .= $customDataSubName ? "{$customDataSubName}_" : '_0';
331 $cacheKey .= $showAll ? '_1' : '_0';
332 $cacheKey .= $inline ? '_1_' : '_0_';
333 $cacheKey .= $onlyParent ? '_1_' : '_0_';
334 $cacheKey .= $onlySubType ? '_1_' : '_0_';
335 $cacheKey .= $checkPermission ? '_1_' . CRM_Core_Session::getLoggedInContactID() . '_' : '_0_0_';
336 $cacheKey .= '_' . CRM_Core_Config::domainID() . '_';
337
338 $cgTable = CRM_Core_DAO_CustomGroup::getTableName();
339
340 // also get the permission stuff here
341 if ($checkPermission) {
342 $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW,
343 "{$cgTable}."
344 );
345 }
346 else {
347 $permissionClause = '(1)';
348 }
349
350 // lets md5 permission clause and take first 8 characters
351 $cacheKey .= substr(md5($permissionClause), 0, 8);
352
353 if (strlen($cacheKey) > 40) {
354 $cacheKey = md5($cacheKey);
355 }
356
357 if (!self::$_importFields ||
358 CRM_Utils_Array::value($cacheKey, self::$_importFields) === NULL
359 ) {
360 if (!self::$_importFields) {
361 self::$_importFields = [];
362 }
363
364 // check if we can retrieve from database cache
365 $fields = Civi::Cache('fields')->get("custom importableFields $cacheKey");
366
367 if ($fields === NULL) {
368
369 $extends = '';
370 if (is_array($customDataType)) {
371 $value = NULL;
372 foreach ($customDataType as $dataType) {
373 if (in_array($dataType, array_keys(CRM_Core_SelectValues::customGroupExtends()))) {
374 if (in_array($dataType, ['Individual', 'Household', 'Organization'])) {
375 $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' ";
376 }
377 else {
378 $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "'";
379 }
380 $value = $value ? $value . ", {$val}" : $val;
381 }
382 }
383 if ($value) {
384 $extends = "AND $cgTable.extends IN ( $value ) ";
385 }
386 }
387
388 if (!empty($customDataType) && empty($extends)) {
389 // $customDataType specified a filter, but there is no corresponding SQL ($extends)
390 self::$_importFields[$cacheKey] = [];
391 return self::$_importFields[$cacheKey];
392 }
393
394 if ($onlyParent) {
395 $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL ";
396 }
397 // Temporary hack - in 5.27 a new field is added to civicrm_custom_field. There is a high
398 // risk this function is called before the upgrade page can be reached and if
399 // so it will potentially result in fatal error.
400 $serializeField = CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1') ? "custom_field.serialize," : '';
401
402 $query = "SELECT custom_field.id, custom_field.label,
403 $cgTable.title,
404 custom_field.data_type,
405 custom_field.html_type,
406 custom_field.default_value,
407 custom_field.options_per_line, custom_field.text_length,
408 custom_field.custom_group_id,
409 custom_field.is_required,
410 custom_field.column_name,
411 $cgTable.extends, custom_field.is_search_range,
412 $cgTable.extends_entity_column_value,
413 $cgTable.extends_entity_column_id,
414 custom_field.is_view,
415 custom_field.option_group_id,
416 custom_field.date_format,
417 custom_field.time_format,
418 $cgTable.is_multiple,
419 $serializeField
420 $cgTable.table_name,
421 og.name as option_group_name
422 FROM civicrm_custom_field custom_field
423 INNER JOIN $cgTable
424 ON custom_field.custom_group_id = $cgTable.id
425 LEFT JOIN civicrm_option_group og
426 ON custom_field.option_group_id = og.id
427 WHERE ( 1 ) ";
428
429 if (!$showAll) {
430 $query .= " AND custom_field.is_active = 1 AND $cgTable.is_active = 1 ";
431 }
432
433 if ($inline) {
434 $query .= " AND $cgTable.style = 'Inline' ";
435 }
436
437 //get the custom fields for specific type in
438 //combination with fields those support any type.
439 if (!empty($customDataSubType)) {
440 $subtypeClause = [];
441 foreach ($customDataSubType as $subtype) {
442 $subtype = CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($subtype, 'String') . CRM_Core_DAO::VALUE_SEPARATOR;
443 $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'";
444 }
445 if (!$onlySubType) {
446 $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL";
447 }
448 $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )";
449 }
450
451 if ($customDataSubName) {
452 $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) ";
453 }
454
455 // also get the permission stuff here
456 if ($checkPermission) {
457 $permissionClause = CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW,
458 "{$cgTable}.", TRUE
459 );
460 }
461 else {
462 $permissionClause = '(1)';
463 }
464
465 $query .= " $extends AND $permissionClause
466 ORDER BY $cgTable.weight, $cgTable.title,
467 custom_field.weight, custom_field.label";
468
469 $dao = CRM_Core_DAO::executeQuery($query);
470
471 $fields = [];
472 while (($dao->fetch()) != NULL) {
473 $regexp = preg_replace('/[.,;:!?]/', '', NULL);
474 $fields[$dao->id]['id'] = $dao->id;
475 $fields[$dao->id]['label'] = $dao->label;
476 // This seems broken, but not in a new way.
477 $fields[$dao->id]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
478 // To support the consolidation of various functions & their expectations.
479 $fields[$dao->id]['title'] = $dao->label;
480 $fields[$dao->id]['custom_field_id'] = $dao->id;
481 $fields[$dao->id]['groupTitle'] = $dao->title;
482 $fields[$dao->id]['data_type'] = $dao->data_type;
483 $fields[$dao->id]['name'] = 'custom_' . $dao->id;
484 $fields[$dao->id]['type'] = CRM_Utils_Array::value($dao->data_type, self::dataToType());
485 $fields[$dao->id]['html_type'] = $dao->html_type;
486 $fields[$dao->id]['default_value'] = $dao->default_value;
487 $fields[$dao->id]['text_length'] = $dao->text_length;
488 $fields[$dao->id]['options_per_line'] = $dao->options_per_line;
489 $fields[$dao->id]['custom_group_id'] = $dao->custom_group_id;
490 $fields[$dao->id]['extends'] = $dao->extends;
491 $fields[$dao->id]['is_search_range'] = $dao->is_search_range;
492 $fields[$dao->id]['extends_entity_column_value'] = $dao->extends_entity_column_value;
493 $fields[$dao->id]['extends_entity_column_id'] = $dao->extends_entity_column_id;
494 $fields[$dao->id]['is_view'] = $dao->is_view;
495 $fields[$dao->id]['is_multiple'] = $dao->is_multiple;
496 $fields[$dao->id]['option_group_id'] = $dao->option_group_id;
497 $fields[$dao->id]['date_format'] = $dao->date_format;
498 $fields[$dao->id]['time_format'] = $dao->time_format;
499 $fields[$dao->id]['is_required'] = $dao->is_required;
500 $fields[$dao->id]['table_name'] = $dao->table_name;
501 $fields[$dao->id]['column_name'] = $dao->column_name;
502 $fields[$dao->id]['serialize'] = $serializeField ? $dao->serialize : (int) self::isSerialized($dao);
503 $fields[$dao->id]['where'] = $dao->table_name . '.' . $dao->column_name;
504 // Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
505 $fields[$dao->id]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery::$extendsMap) ? CRM_Core_BAO_CustomQuery::$extendsMap[$dao->extends] : '';
506 if (in_array($dao->extends, CRM_Contact_BAO_ContactType::subTypes())) {
507 // if $extends is a subtype, refer contact table
508 $fields[$dao->id]['extends_table'] = 'civicrm_contact';
509 }
510 // Search table is used by query object searches..
511 $fields[$dao->id]['search_table'] = ($fields[$dao->id]['extends_table'] == 'civicrm_contact') ? 'contact_a' : $fields[$dao->id]['extends_table'];
512 self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
513 }
514
515 Civi::cache('fields')->set("custom importableFields $cacheKey", $fields);
516 }
517 self::$_importFields[$cacheKey] = $fields;
518 }
519
520 return self::$_importFields[$cacheKey];
521 }
522
523 /**
524 * Return the field ids and names (with groups) for import purpose.
525 *
526 * @param int|string $contactType Contact type
527 * @param bool $showAll
528 * If true returns all fields (includes disabled fields).
529 * @param bool $onlyParent
530 * Return fields ONLY related to basic types.
531 * @param bool $search
532 * When called from search and multiple records need to be returned.
533 * @param bool $checkPermission
534 * If false, do not include permissioning clause.
535 *
536 * @param bool $withMultiple
537 *
538 * @return array
539 */
540 public static function getFieldsForImport(
541 $contactType = 'Individual',
542 $showAll = FALSE,
543 $onlyParent = FALSE,
544 $search = FALSE,
545 $checkPermission = TRUE,
546 $withMultiple = FALSE
547 ) {
548 // Note: there are situations when we want getFieldsForImport() return fields related
549 // ONLY to basic contact types, but NOT subtypes. And thats where $onlyParent is helpful
550 $fields = &self::getFields($contactType,
551 $showAll,
552 FALSE,
553 NULL,
554 NULL,
555 $onlyParent,
556 FALSE,
557 $checkPermission
558 );
559
560 $importableFields = [];
561 foreach ($fields as $id => $values) {
562 // for now we should not allow multiple fields in profile / export etc, hence unsetting
563 if (!$search &&
564 (!empty($values['is_multiple']) && !$withMultiple)
565 ) {
566 continue;
567 }
568 if (!empty($values['text_length'])) {
569 $values['maxlength'] = (int) $values['text_length'];
570 }
571
572 /* generate the key for the fields array */
573
574 $key = "custom_$id";
575 $importableFields[$key] = $values;
576 $importableFields[$key]['import'] = 1;
577 }
578
579 return $importableFields;
580 }
581
582 /**
583 * Get the field id from an import key.
584 *
585 * @param string $key
586 * The key to parse.
587 *
588 * @param bool $all
589 *
590 * @return int|null
591 * The id (if exists)
592 */
593 public static function getKeyID($key, $all = FALSE) {
594 $match = [];
595 if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) {
596 if (!$all) {
597 return $match[1];
598 }
599 else {
600 return [
601 $match[1],
602 CRM_Utils_Array::value(2, $match),
603 ];
604 }
605 }
606 return $all ? [NULL, NULL] : NULL;
607 }
608
609 /**
610 * Use the cache to get all values of a specific custom field.
611 *
612 * @param int $fieldID
613 * The custom field ID.
614 *
615 * @return CRM_Core_BAO_CustomField
616 * The field object.
617 * @throws CRM_Core_Exception
618 */
619 public static function getFieldObject($fieldID) {
620 $field = new CRM_Core_BAO_CustomField();
621
622 // check if we can get the field values from the system cache
623 $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
624 $cache = CRM_Utils_Cache::singleton();
625 $fieldValues = $cache->get($cacheKey);
626 if (empty($fieldValues)) {
627 $field->id = $fieldID;
628 if (!$field->find(TRUE)) {
629 throw new CRM_Core_Exception('Cannot find Custom Field');
630 }
631
632 $fieldValues = [];
633 CRM_Core_DAO::storeValues($field, $fieldValues);
634
635 $cache->set($cacheKey, $fieldValues);
636 }
637 else {
638 $field->copyValues($fieldValues);
639 }
640
641 return $field;
642 }
643
644 /**
645 * Add a custom field to an existing form.
646 *
647 * @param CRM_Core_Form $qf
648 * Form object (reference).
649 * @param string $elementName
650 * Name of the custom field.
651 * @param int $fieldId
652 * @param bool $useRequired
653 * True if required else false.
654 * @param bool $search
655 * True if used for search else false.
656 * @param string $label
657 * Label for custom field.
658 * @return \HTML_QuickForm_Element|null
659 * @throws \CiviCRM_API3_Exception
660 */
661 public static function addQuickFormElement(
662 $qf, $elementName, $fieldId, $useRequired = TRUE, $search = FALSE, $label = NULL
663 ) {
664 $field = self::getFieldObject($fieldId);
665 $widget = $field->html_type;
666 $element = NULL;
667 $customFieldAttributes = [];
668
669 if (!isset($label)) {
670 $label = $field->label;
671 }
672
673 // DAO stores attributes as a string, but it's hard to manipulate and
674 // CRM_Core_Form::add() wants them as an array.
675 $fieldAttributes = self::attributesFromString($field->attributes);
676
677 // Custom field HTML should indicate group+field name
678 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id);
679 $fieldAttributes['data-crm-custom'] = $groupName . ':' . $field->name;
680
681 // Fixed for Issue CRM-2183
682 if ($widget == 'TextArea' && $search) {
683 $widget = 'Text';
684 }
685
686 $placeholder = $search ? ts('- any %1 -', [1 => $label]) : ts('- select %1 -', [1 => $label]);
687
688 if (in_array($widget, [
689 'Select',
690 'CheckBox',
691 'Radio',
692 ])) {
693 $options = $field->getOptions($search ? 'search' : 'create');
694
695 // Consolidate widget types to simplify the below switch statement
696 if ($search) {
697 $widget = 'Select';
698 }
699
700 // Search field is always multi-select
701 if ($search || (self::isSerialized($field) && $widget === 'Select')) {
702 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' huge');
703 $fieldAttributes['multiple'] = 'multiple';
704 $fieldAttributes['placeholder'] = $placeholder;
705 }
706
707 // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
708 $canEditOptions = CRM_Core_Permission::check('administer CiviCRM');
709 if ($field->option_group_id && !$search && $canEditOptions) {
710 $customFieldAttributes += [
711 'data-api-entity' => $field->getEntity(),
712 'data-api-field' => 'custom_' . $field->id,
713 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id),
714 ];
715 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
716 }
717 }
718
719 $rangeDataTypes = ['Int', 'Float', 'Money'];
720
721 // at some point in time we might want to split the below into small functions
722
723 switch ($widget) {
724 case 'Text':
725 case 'Link':
726 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
727 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
728 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
729 }
730 else {
731 if ($field->text_length) {
732 $fieldAttributes['maxlength'] = $field->text_length;
733 if ($field->text_length < 20) {
734 $fieldAttributes['size'] = $field->text_length;
735 }
736 }
737 $element = $qf->add('text', $elementName, $label,
738 $fieldAttributes,
739 $useRequired && !$search
740 );
741 }
742 break;
743
744 case 'TextArea':
745 $fieldAttributes['rows'] = $field->note_rows ?? 4;
746 $fieldAttributes['cols'] = $field->note_columns ?? 60;
747
748 if ($field->text_length) {
749 $fieldAttributes['maxlength'] = $field->text_length;
750 }
751 $element = $qf->add('textarea',
752 $elementName,
753 $label,
754 $fieldAttributes,
755 $useRequired && !$search
756 );
757 break;
758
759 case 'Select Date':
760 //CRM-18379: Fix for date range of 'Select Date' custom field when include in profile.
761 $minYear = isset($field->start_date_years) ? (date('Y') - $field->start_date_years) : NULL;
762 $maxYear = isset($field->end_date_years) ? (date('Y') + $field->end_date_years) : NULL;
763
764 $params = [
765 'date' => $field->date_format,
766 'minDate' => isset($minYear) ? $minYear . '-01-01' : NULL,
767 //CRM-18487 - max date should be the last date of the year.
768 'maxDate' => isset($maxYear) ? $maxYear . '-12-31' : NULL,
769 'time' => $field->time_format ? $field->time_format * 12 : FALSE,
770 ];
771 if ($field->is_search_range && $search) {
772 $qf->add('datepicker', $elementName . '_from', $label, $fieldAttributes + array('placeholder' => ts('From')), FALSE, $params);
773 $qf->add('datepicker', $elementName . '_to', NULL, $fieldAttributes + array('placeholder' => ts('To')), FALSE, $params);
774 }
775 else {
776 $element = $qf->add('datepicker', $elementName, $label, $fieldAttributes, $useRequired && !$search, $params);
777 }
778 break;
779
780 case 'Radio':
781 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
782 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
783 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
784 }
785 else {
786 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
787 if ($search || empty($useRequired)) {
788 $fieldAttributes['allowClear'] = TRUE;
789 }
790 $qf->addRadio($elementName, $label, $options, $fieldAttributes, NULL, $useRequired);
791 }
792 break;
793
794 // For all select elements
795 case 'Select':
796 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-select2');
797 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
798 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
799 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
800 }
801 else {
802 if (empty($fieldAttributes['multiple'])) {
803 $options = ['' => $placeholder] + $options;
804 }
805 $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $fieldAttributes);
806
807 // Add and/or option for fields that store multiple values
808 if ($search && self::isSerialized($field)) {
809 $qf->addRadio($elementName . '_operator', '', [
810 'or' => ts('Any'),
811 'and' => ts('All'),
812 ], [], NULL, FALSE, [
813 'or' => ['title' => ts('Results may contain any of the selected options')],
814 'and' => ['title' => ts('Results must have all of the selected options')],
815 ]);
816 $qf->setDefaults([$elementName . '_operator' => 'or']);
817 }
818 }
819 break;
820
821 case 'CheckBox':
822 $check = [];
823 foreach ($options as $v => $l) {
824 // TODO: I'm not sure if this is supposed to exclude whatever might be
825 // in $field->attributes (available in array format as
826 // $fieldAttributes). Leaving as-is for now.
827 $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, $customFieldAttributes);
828 }
829
830 $group = $element = $qf->addGroup($check, $elementName, $label);
831 $optionEditKey = 'data-option-edit-path';
832 if (isset($customFieldAttributes[$optionEditKey])) {
833 $group->setAttribute($optionEditKey, $customFieldAttributes[$optionEditKey]);
834 }
835
836 if ($useRequired && !$search) {
837 $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
838 }
839 break;
840
841 case 'File':
842 // we should not build upload file in search mode
843 if ($search) {
844 return NULL;
845 }
846 $element = $qf->add(
847 strtolower($field->html_type),
848 $elementName,
849 $label,
850 $fieldAttributes,
851 $useRequired && !$search
852 );
853 $qf->addUploadElement($elementName);
854 break;
855
856 case 'RichTextEditor':
857 $fieldAttributes['rows'] = $field->note_rows;
858 $fieldAttributes['cols'] = $field->note_columns;
859 if ($field->text_length) {
860 $fieldAttributes['maxlength'] = $field->text_length;
861 }
862 $element = $qf->add('wysiwyg', $elementName, $label, $fieldAttributes, $useRequired && !$search);
863 break;
864
865 case 'Autocomplete-Select':
866 static $customUrls = [];
867 if ($field->data_type == 'ContactReference') {
868 // break if contact does not have permission to access ContactReference
869 if (!CRM_Core_Permission::check('access contact reference fields')) {
870 break;
871 }
872 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-form-contact-reference huge');
873 $fieldAttributes['data-api-entity'] = 'Contact';
874 $element = $qf->add('text', $elementName, $label, $fieldAttributes, $useRequired && !$search);
875
876 $urlParams = "context=customfield&id={$field->id}";
877 $idOfelement = $elementName;
878 // dev/core#362 if in an onbehalf profile clean up the name to get rid of square brackets that break the select 2 js
879 // However this caused regression https://lab.civicrm.org/dev/core/issues/619 so it has been hacked back to
880 // only affecting on behalf - next time someone looks at this code it should be with a view to overhauling it
881 // rather than layering on more hacks.
882 if (substr($elementName, 0, 8) === 'onbehalf' && strpos($elementName, '[') && strpos($elementName, ']')) {
883 $idOfelement = substr(substr($elementName, (strpos($elementName, '[') + 1)), 0, -1);
884 }
885 $customUrls[$idOfelement] = CRM_Utils_System::url('civicrm/ajax/contactref',
886 $urlParams,
887 FALSE, NULL, FALSE
888 );
889
890 }
891 else {
892 // FIXME: This won't work with customFieldOptions hook
893 $fieldAttributes += [
894 'entity' => 'OptionValue',
895 'placeholder' => $placeholder,
896 'multiple' => $search,
897 'api' => [
898 'params' => ['option_group_id' => $field->option_group_id, 'is_active' => 1],
899 ],
900 ];
901 $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
902 }
903
904 $qf->assign('customUrls', $customUrls);
905 break;
906 }
907
908 switch ($field->data_type) {
909 case 'Int':
910 // integers will have numeric rule applied to them.
911 if ($field->is_search_range && $search) {
912 $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', [1 => $label]), 'integer');
913 $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', [1 => $label]), 'integer');
914 }
915 elseif ($widget == 'Text') {
916 $qf->addRule($elementName, ts('%1 must be an integer (whole number).', [1 => $label]), 'integer');
917 }
918 break;
919
920 case 'Float':
921 if ($field->is_search_range && $search) {
922 $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', [1 => $label]), 'numeric');
923 $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', [1 => $label]), 'numeric');
924 }
925 elseif ($widget == 'Text') {
926 $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', [1 => $label]), 'numeric');
927 }
928 break;
929
930 case 'Money':
931 if ($field->is_search_range && $search) {
932 $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
933 $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
934 }
935 elseif ($widget == 'Text') {
936 $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
937 }
938 break;
939
940 case 'Link':
941 $element->setAttribute('class', "url");
942 $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
943 break;
944 }
945 if ($field->is_view && !$search) {
946 $qf->freeze($elementName);
947 }
948 return $element;
949 }
950
951 /**
952 * Take a string of HTML element attributes and turn it into an associative
953 * array.
954 *
955 * @param string $attrString
956 * The attributes as a string, e.g. `rows=3 cols=40`.
957 *
958 * @return array
959 * The attributes as an array, e.g. `['rows' => 3, 'cols' => 40]`.
960 */
961 public static function attributesFromString($attrString) {
962 $attributes = [];
963 foreach (explode(' ', $attrString) as $at) {
964 if (strpos($at, '=')) {
965 list($k, $v) = explode('=', $at);
966 $attributes[$k] = trim($v, ' "');
967 }
968 }
969 return $attributes;
970 }
971
972 /**
973 * Delete the Custom Field.
974 *
975 * @param object $field
976 * The field object.
977 */
978 public static function deleteField($field) {
979 CRM_Utils_System::flushCache();
980
981 // first delete the custom option group and values associated with this field
982 if ($field->option_group_id) {
983 //check if option group is related to any other field, if
984 //not delete the option group and related option values
985 self::checkOptionGroup($field->option_group_id);
986 }
987
988 // next drop the column from the custom value table
989 self::createField($field, 'delete');
990
991 $field->delete();
992 CRM_Core_BAO_UFField::delUFField($field->id);
993 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');
994
995 CRM_Utils_Hook::post('delete', 'CustomField', $field->id, $field);
996 }
997
998 /**
999 * @param string|int|array|null $value
1000 * @param CRM_Core_BAO_CustomField|int|array|string $field
1001 * @param int $entityId
1002 *
1003 * @return string
1004 *
1005 * @throws \CRM_Core_Exception
1006 */
1007 public static function displayValue($value, $field, $entityId = NULL) {
1008 $field = is_array($field) ? $field['id'] : $field;
1009 $fieldId = is_object($field) ? $field->id : (int) str_replace('custom_', '', $field);
1010
1011 if (!$fieldId) {
1012 throw new CRM_Core_Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
1013 }
1014
1015 if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
1016 $field = self::getFieldObject($fieldId);
1017 }
1018
1019 $fieldInfo = ['options' => $field->getOptions()] + (array) $field;
1020
1021 $displayValue = self::formatDisplayValue($value, $fieldInfo, $entityId);
1022
1023 // Call hook to alter display value.
1024 CRM_Utils_Hook::alterCustomFieldDisplayValue($displayValue, $value, $entityId, $fieldInfo);
1025
1026 return $displayValue;
1027 }
1028
1029 /**
1030 * Lower-level logic for rendering a custom field value
1031 *
1032 * @param string|array $value
1033 * @param array $field
1034 * @param int|null $entityId
1035 *
1036 * @return string
1037 */
1038 private static function formatDisplayValue($value, $field, $entityId = NULL) {
1039
1040 if (self::isSerialized($field) && !is_array($value)) {
1041 $value = CRM_Utils_Array::explodePadded($value);
1042 }
1043 // CRM-12989 fix
1044 if ($field['html_type'] == 'CheckBox' && $value) {
1045 $value = CRM_Utils_Array::convertCheckboxFormatToArray($value);
1046 }
1047
1048 $display = is_array($value) ? implode(', ', $value) : (string) $value;
1049
1050 switch ($field['html_type']) {
1051
1052 case 'Select':
1053 case 'Autocomplete-Select':
1054 case 'Radio':
1055 case 'CheckBox':
1056 if ($field['data_type'] == 'ContactReference' && $value) {
1057 if (is_numeric($value)) {
1058 $display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'display_name');
1059 }
1060 else {
1061 $display = $value;
1062 }
1063 }
1064 elseif (is_array($value)) {
1065 $v = [];
1066 foreach ($value as $key => $val) {
1067 $v[] = $field['options'][$val] ?? NULL;
1068 }
1069 $display = implode(', ', $v);
1070 }
1071 else {
1072 $display = CRM_Utils_Array::value($value, $field['options'], '');
1073 // For float type (see Number and Money) $value would be decimal like
1074 // 1.00 (because it is stored in db as decimal), while options array
1075 // key would be integer like 1. In this case expression on line above
1076 // would return empty string (default value), despite the fact that
1077 // matching option exists in the array.
1078 // In such cases we could just get intval($value) and fetch matching
1079 // option again, but this would not work if key is float like 5.6.
1080 // So we need to truncate trailing zeros to make it work as expected.
1081 if ($display === '' && strpos($value, '.') !== FALSE) {
1082 // Use round() to truncate trailing zeros, e.g:
1083 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1084 $value = (string) round($value, 5);
1085 $display = $field['options'][$value] ?? '';
1086 }
1087 }
1088 break;
1089
1090 case 'Select Date':
1091 $customFormat = NULL;
1092
1093 // FIXME: Are there any legitimate reasons why $value would be an array?
1094 // Or should we throw an exception here if it is?
1095 $value = is_array($value) ? CRM_Utils_Array::first($value) : $value;
1096
1097 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
1098 $format = $field['date_format'] ?? NULL;
1099
1100 if ($format) {
1101 if (array_key_exists($format, $actualPHPFormats)) {
1102 $customTimeFormat = (array) $actualPHPFormats[$format];
1103 switch (CRM_Utils_Array::value('time_format', $field)) {
1104 case 1:
1105 $customTimeFormat[] = 'g:iA';
1106 break;
1107
1108 case 2:
1109 $customTimeFormat[] = 'G:i';
1110 break;
1111
1112 default:
1113 //If time is not selected remove time from value.
1114 $value = $value ? date('Y-m-d', strtotime($value)) : '';
1115 }
1116 $customFormat = implode(" ", $customTimeFormat);
1117 }
1118 }
1119 $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
1120 break;
1121
1122 case 'File':
1123 // In the context of displaying a profile, show file/image
1124 if ($value) {
1125 if ($entityId) {
1126 if (CRM_Utils_Rule::positiveInteger($value)) {
1127 $fileId = $value;
1128 }
1129 else {
1130 $fileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1131 }
1132 $url = self::getFileURL($entityId, $field['id'], $fileId);
1133 if ($url) {
1134 $display = $url['file_url'];
1135 }
1136 }
1137 else {
1138 // In other contexts show a paperclip icon
1139 if (CRM_Utils_Rule::integer($value)) {
1140 $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
1141
1142 $file_description = '';
1143 try {
1144 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1145 }
1146 catch (CiviCRM_API3_Exception $dontcare) {
1147 // don't care
1148 }
1149
1150 $display = "{$icons[$value]}{$file_description}";
1151 }
1152 else {
1153 //CRM-18396, if filename is passed instead
1154 $display = $value;
1155 }
1156 }
1157 }
1158 break;
1159
1160 case 'Link':
1161 $display = $display ? "<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1162 break;
1163
1164 case 'TextArea':
1165 $display = nl2br($display);
1166 break;
1167
1168 case 'Text':
1169 if ($field['data_type'] == 'Money' && isset($value)) {
1170 // $value can also be an array(while using IN operator from search builder or api).
1171 foreach ((array) $value as $val) {
1172 $disp[] = CRM_Utils_Money::format($val, NULL, NULL, TRUE);
1173 }
1174 $display = implode(', ', $disp);
1175 }
1176 break;
1177 }
1178 return $display;
1179 }
1180
1181 /**
1182 * Set default values for custom data used in profile.
1183 *
1184 * @param int $customFieldId
1185 * Custom field id.
1186 * @param string $elementName
1187 * Custom field name.
1188 * @param array $defaults
1189 * Associated array of fields.
1190 * @param int $contactId
1191 * Contact id.
1192 * @param int $mode
1193 * Profile mode.
1194 * @param mixed $value
1195 * If passed - dont fetch value from db,.
1196 * just format the given value
1197 */
1198 public static function setProfileDefaults(
1199 $customFieldId,
1200 $elementName,
1201 &$defaults,
1202 $contactId = NULL,
1203 $mode = NULL,
1204 $value = NULL
1205 ) {
1206 //get the type of custom field
1207 $customField = new CRM_Core_BAO_CustomField();
1208 $customField->id = $customFieldId;
1209 $customField->find(TRUE);
1210
1211 if (!$contactId) {
1212 if ($mode == CRM_Profile_Form::MODE_CREATE) {
1213 $value = $customField->default_value;
1214 }
1215 }
1216 else {
1217 if (!isset($value)) {
1218 $info = self::getTableColumnGroup($customFieldId);
1219 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1220 $result = CRM_Core_DAO::executeQuery($query);
1221 if ($result->fetch()) {
1222 $value = $result->value;
1223 }
1224 }
1225
1226 if ($customField->data_type == 'Country') {
1227 if (!$value) {
1228 $config = CRM_Core_Config::singleton();
1229 if ($config->defaultContactCountry) {
1230 $value = CRM_Core_BAO_Country::defaultContactCountry();
1231 }
1232 }
1233 }
1234 }
1235
1236 //set defaults if mode is registration
1237 if (!trim($value) &&
1238 ($value !== 0) &&
1239 (!in_array($mode, [CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH]))
1240 ) {
1241 $value = $customField->default_value;
1242 }
1243
1244 if ($customField->data_type == 'Money' && isset($value)) {
1245 $value = number_format($value, 2);
1246 }
1247 if (self::isSerialized($customField) && $value) {
1248 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE);
1249 $defaults[$elementName] = [];
1250 $checkedValue = CRM_Utils_Array::explodePadded($value);
1251 foreach ($customOption as $val) {
1252 if (in_array($val['value'], $checkedValue)) {
1253 if ($customField->html_type == 'CheckBox') {
1254 $defaults[$elementName][$val['value']] = 1;
1255 }
1256 else {
1257 $defaults[$elementName][$val['value']] = $val['value'];
1258 }
1259 }
1260 }
1261 }
1262 else {
1263 $defaults[$elementName] = $value;
1264 }
1265 }
1266
1267 /**
1268 * Get file url.
1269 *
1270 * @param int $contactID
1271 * @param int $cfID
1272 * @param int $fileID
1273 * @param bool $absolute
1274 *
1275 * @param string $multiRecordWhereClause
1276 *
1277 * @return array
1278 */
1279 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1280 if ($contactID) {
1281 if (!$fileID) {
1282 $params = ['id' => $cfID];
1283 $defaults = [];
1284 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1285 $columnName = $defaults['column_name'];
1286
1287 //table name of custom data
1288 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
1289 $defaults['custom_group_id'],
1290 'table_name', 'id'
1291 );
1292
1293 //query to fetch id from civicrm_file
1294 if ($multiRecordWhereClause) {
1295 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1296 }
1297 else {
1298 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1299 }
1300 $fileID = CRM_Core_DAO::singleValueQuery($query);
1301 }
1302
1303 $result = [];
1304 if ($fileID) {
1305 $fileType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1306 $fileID,
1307 'mime_type',
1308 'id'
1309 );
1310 $result['file_id'] = $fileID;
1311
1312 if ($fileType == 'image/jpeg' ||
1313 $fileType == 'image/pjpeg' ||
1314 $fileType == 'image/gif' ||
1315 $fileType == 'image/x-png' ||
1316 $fileType == 'image/png'
1317 ) {
1318 $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile',
1319 $fileID,
1320 'entity_id',
1321 'file_id'
1322 );
1323 list($path) = CRM_Core_BAO_File::path($fileID, $entityId);
1324 $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileID);
1325 $url = CRM_Utils_System::url('civicrm/file',
1326 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1327 $absolute, NULL, TRUE, TRUE
1328 );
1329 $result['file_url'] = CRM_Utils_File::getFileURL($path, $fileType, $url);
1330 }
1331 // for non image files
1332 else {
1333 $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1334 $fileID,
1335 'uri'
1336 );
1337 $fileHash = CRM_Core_BAO_File::generateFileHash($contactID, $fileID);
1338 $url = CRM_Utils_System::url('civicrm/file',
1339 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1340 $absolute, NULL, TRUE, TRUE
1341 );
1342 $result['file_url'] = CRM_Utils_File::getFileURL($uri, $fileType, $url);
1343 }
1344 }
1345 return $result;
1346 }
1347 }
1348
1349 /**
1350 * Format custom fields before inserting.
1351 *
1352 * @param int $customFieldId
1353 * Custom field id.
1354 * @param array $customFormatted
1355 * Formatted array.
1356 * @param mixed $value
1357 * Value of custom field.
1358 * @param string $customFieldExtend
1359 * Custom field extends.
1360 * @param int $customValueId
1361 * Custom option value id.
1362 * @param int $entityId
1363 * Entity id (contribution, membership...).
1364 * @param bool $inline
1365 * Consider inline custom groups only.
1366 * @param bool $checkPermission
1367 * If false, do not include permissioning clause.
1368 * @param bool $includeViewOnly
1369 * If true, fields marked 'View Only' are included. Required for APIv3.
1370 *
1371 * @return array|NULL
1372 * formatted custom field array
1373 */
1374 public static function formatCustomField(
1375 $customFieldId, &$customFormatted, $value,
1376 $customFieldExtend, $customValueId = NULL,
1377 $entityId = NULL,
1378 $inline = FALSE,
1379 $checkPermission = TRUE,
1380 $includeViewOnly = FALSE
1381 ) {
1382 //get the custom fields for the entity
1383 //subtype and basic type
1384 $customDataSubType = NULL;
1385 if ($customFieldExtend) {
1386 // This is the case when getFieldsForImport() requires fields
1387 // of subtype and its parent.CRM-5143
1388 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1389 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend);
1390 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1391 $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType);
1392 if (is_array($customFieldExtend)) {
1393 $customFieldExtend = array_unique(array_values($customFieldExtend));
1394 }
1395 }
1396 }
1397
1398 $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend,
1399 FALSE,
1400 $inline,
1401 $customDataSubType,
1402 NULL,
1403 FALSE,
1404 FALSE,
1405 $checkPermission
1406 );
1407
1408 if (!array_key_exists($customFieldId, $customFields)) {
1409 return NULL;
1410 }
1411
1412 // return if field is a 'code' field
1413 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1414 return NULL;
1415 }
1416
1417 list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId);
1418
1419 if (!$customValueId &&
1420 // we always create new entites for is_multiple unless specified
1421 !$customFields[$customFieldId]['is_multiple'] &&
1422 $entityId
1423 ) {
1424 $query = "
1425 SELECT id
1426 FROM $tableName
1427 WHERE entity_id={$entityId}";
1428
1429 $customValueId = CRM_Core_DAO::singleValueQuery($query);
1430 }
1431
1432 //fix checkbox, now check box always submits values
1433 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1434 if ($value) {
1435 // Note that only during merge this is not an array, and you can directly use value
1436 if (is_array($value)) {
1437 $selectedValues = [];
1438 foreach ($value as $selId => $val) {
1439 if ($val) {
1440 $selectedValues[] = $selId;
1441 }
1442 }
1443 if (!empty($selectedValues)) {
1444 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
1445 $selectedValues
1446 ) . CRM_Core_DAO::VALUE_SEPARATOR;
1447 }
1448 else {
1449 $value = '';
1450 }
1451 }
1452 }
1453 }
1454 elseif (self::isSerialized($customFields[$customFieldId])) {
1455 $value = $value ? CRM_Utils_Array::implodePadded($value) : '';
1456 }
1457
1458 if (self::isSerialized($customFields[$customFieldId]) &&
1459 $customFields[$customFieldId]['data_type'] == 'String' &&
1460 !empty($customFields[$customFieldId]['text_length']) &&
1461 !empty($value)
1462 ) {
1463 // lets make sure that value is less than the length, else we'll
1464 // be losing some data, CRM-7481
1465 if (strlen($value) >= $customFields[$customFieldId]['text_length']) {
1466 // need to do a few things here
1467
1468 // 1. lets find a new length
1469 $newLength = $customFields[$customFieldId]['text_length'];
1470 $minLength = strlen($value);
1471 while ($newLength < $minLength) {
1472 $newLength = $newLength * 2;
1473 }
1474
1475 // set the custom field meta data to have a length larger than value
1476 // alter the custom value table column to match this length
1477 CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength);
1478 }
1479 }
1480
1481 $date = NULL;
1482 if ($customFields[$customFieldId]['data_type'] == 'Date') {
1483 if (!CRM_Utils_System::isNull($value)) {
1484 $format = $customFields[$customFieldId]['date_format'];
1485 $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format);
1486 }
1487 $value = $date;
1488 }
1489
1490 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1491 $customFields[$customFieldId]['data_type'] == 'Money'
1492 ) {
1493 if (!$value) {
1494 $value = 0;
1495 }
1496
1497 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1498 $value = CRM_Utils_Rule::cleanMoney($value);
1499 }
1500 }
1501
1502 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1503 $customFields[$customFieldId]['data_type'] == 'Country'
1504 ) &&
1505 empty($value)
1506 ) {
1507 // CRM-3415
1508 $value = 0;
1509 }
1510
1511 $fileID = NULL;
1512
1513 if ($customFields[$customFieldId]['data_type'] == 'File') {
1514 if (empty($value)) {
1515 return;
1516 }
1517
1518 $config = CRM_Core_Config::singleton();
1519
1520 // If we are already passing the file id as a value then retrieve and set the file data
1521 if (CRM_Utils_Rule::integer($value)) {
1522 $fileDAO = new CRM_Core_DAO_File();
1523 $fileDAO->id = $value;
1524 $fileDAO->find(TRUE);
1525 if ($fileDAO->N) {
1526 $fileID = $value;
1527 $fName = $fileDAO->uri;
1528 $mimeType = $fileDAO->mime_type;
1529 }
1530 }
1531 else {
1532 $fName = $value['name'];
1533 $mimeType = $value['type'];
1534 }
1535
1536 $filename = pathinfo($fName, PATHINFO_BASENAME);
1537
1538 // rename this file to go into the secure directory only if
1539 // user has uploaded new file not existing verfied on the basis of $fileID
1540 if (empty($fileID) && !rename($fName, $config->customFileUploadDir . $filename)) {
1541 CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory'));
1542 }
1543
1544 if ($customValueId && empty($fileID)) {
1545 $query = "
1546 SELECT $columnName
1547 FROM $tableName
1548 WHERE id = %1";
1549 $params = [1 => [$customValueId, 'Integer']];
1550 $fileID = CRM_Core_DAO::singleValueQuery($query, $params);
1551 }
1552
1553 $fileDAO = new CRM_Core_DAO_File();
1554
1555 if ($fileID) {
1556 $fileDAO->id = $fileID;
1557 }
1558
1559 $fileDAO->uri = $filename;
1560 $fileDAO->mime_type = $mimeType;
1561 $fileDAO->upload_date = date('YmdHis');
1562 $fileDAO->save();
1563 $fileID = $fileDAO->id;
1564 $value = $filename;
1565 }
1566
1567 if (!is_array($customFormatted)) {
1568 $customFormatted = [];
1569 }
1570
1571 if (!array_key_exists($customFieldId, $customFormatted)) {
1572 $customFormatted[$customFieldId] = [];
1573 }
1574
1575 $index = -1;
1576 if ($customValueId) {
1577 $index = $customValueId;
1578 }
1579
1580 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1581 $customFormatted[$customFieldId][$index] = [];
1582 }
1583 $customFormatted[$customFieldId][$index] = [
1584 'id' => $customValueId > 0 ? $customValueId : NULL,
1585 'value' => $value,
1586 'type' => $customFields[$customFieldId]['data_type'],
1587 'custom_field_id' => $customFieldId,
1588 'custom_group_id' => $groupID,
1589 'table_name' => $tableName,
1590 'column_name' => $columnName,
1591 'file_id' => $fileID,
1592 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1593 ];
1594
1595 //we need to sort so that custom fields are created in the order of entry
1596 krsort($customFormatted[$customFieldId]);
1597 return $customFormatted;
1598 }
1599
1600 /**
1601 * Get default custom table schema.
1602 *
1603 * @param array $params
1604 *
1605 * @return array
1606 */
1607 public static function defaultCustomTableSchema($params) {
1608 // add the id and extends_id
1609 $collation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
1610 $characterSet = 'utf8';
1611 if (stripos($collation, 'utf8mb4') !== FALSE) {
1612 $characterSet = 'utf8mb4';
1613 }
1614 $table = [
1615 'name' => $params['name'],
1616 'is_multiple' => $params['is_multiple'],
1617 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1618 'fields' => [
1619 [
1620 'name' => 'id',
1621 'type' => 'int unsigned',
1622 'primary' => TRUE,
1623 'required' => TRUE,
1624 'attributes' => 'AUTO_INCREMENT',
1625 'comment' => 'Default MySQL primary key',
1626 ],
1627 [
1628 'name' => 'entity_id',
1629 'type' => 'int unsigned',
1630 'required' => TRUE,
1631 'comment' => 'Table that this extends',
1632 'fk_table_name' => $params['extends_name'],
1633 'fk_field_name' => 'id',
1634 'fk_attributes' => 'ON DELETE CASCADE',
1635 ],
1636 ],
1637 ];
1638
1639 // If on MySQL 5.6 include ROW_FORMAT=DYNAMIC to fix unit tests
1640 $databaseVersion = CRM_Utils_SQL::getDatabaseVersion();
1641 if (version_compare($databaseVersion, '5.7', '<') && version_compare($databaseVersion, '5.6', '>=')) {
1642 $table['attributes'] = $table['attributes'] . ' ROW_FORMAT=DYNAMIC';
1643 }
1644
1645 if (!$params['is_multiple']) {
1646 $table['indexes'] = [
1647 [
1648 'unique' => TRUE,
1649 'field_name_1' => 'entity_id',
1650 ],
1651 ];
1652 }
1653 return $table;
1654 }
1655
1656 /**
1657 * Create custom field.
1658 *
1659 * @param CRM_Core_DAO_CustomField $field
1660 * @param string $operation
1661 */
1662 public static function createField($field, $operation) {
1663 $sql = str_repeat(' ', 8);
1664 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1665 $sql .= "ALTER TABLE " . $tableName;
1666 $sql .= self::getAlterFieldSQL($field, $operation);
1667
1668 // CRM-7007: do not i18n-rewrite this query
1669 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1670
1671 $config = CRM_Core_Config::singleton();
1672 if ($config->logging) {
1673 // CRM-16717 not sure why this was originally limited to add.
1674 // For example custom tables can have field length changes - which need to flow through to logging.
1675 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
1676 if ($operation === 'add' || $operation === 'modify') {
1677 $logging = new CRM_Logging_Schema();
1678 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name]]);
1679 }
1680 }
1681
1682 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
1683 }
1684
1685 /**
1686 * @param CRM_Core_DAO_CustomField $field
1687 * @param string $operation
1688 *
1689 * @return bool
1690 */
1691 public static function getAlterFieldSQL($field, $operation) {
1692 $indexExist = $operation === 'add' ? FALSE : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $field->id, 'is_searchable');
1693 $params = self::prepareCreateParams($field, $operation);
1694 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1695 // a function only used by Custom Field creation...
1696 $params['required'] = FALSE;
1697 return CRM_Core_BAO_SchemaHandler::getFieldAlterSQL($params, $indexExist);
1698 }
1699
1700 /**
1701 * Reformat existing values for a field when changing its serialize attribute
1702 *
1703 * @param CRM_Core_DAO_CustomField $field
1704 * @throws CRM_Core_Exception
1705 */
1706 private static function alterSerialize($field) {
1707 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1708 $col = $field->column_name;
1709 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
1710 if ($field->serialize) {
1711 $sql = "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1712 }
1713 else {
1714 $sql = "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1715 }
1716 CRM_Core_DAO::executeQuery($sql);
1717 }
1718
1719 /**
1720 * Determine whether it would be safe to move a field.
1721 *
1722 * @param int $fieldID
1723 * FK to civicrm_custom_field.
1724 * @param int $newGroupID
1725 * FK to civicrm_custom_group.
1726 *
1727 * @return array
1728 * array(string) or TRUE
1729 */
1730 public static function _moveFieldValidate($fieldID, $newGroupID) {
1731 $errors = [];
1732
1733 $field = new CRM_Core_DAO_CustomField();
1734 $field->id = $fieldID;
1735 if (!$field->find(TRUE)) {
1736 $errors['fieldID'] = 'Invalid ID for custom field';
1737 return $errors;
1738 }
1739
1740 $oldGroup = new CRM_Core_DAO_CustomGroup();
1741 $oldGroup->id = $field->custom_group_id;
1742 if (!$oldGroup->find(TRUE)) {
1743 $errors['fieldID'] = 'Invalid ID for old custom group';
1744 return $errors;
1745 }
1746
1747 $newGroup = new CRM_Core_DAO_CustomGroup();
1748 $newGroup->id = $newGroupID;
1749 if (!$newGroup->find(TRUE)) {
1750 $errors['newGroupID'] = 'Invalid ID for new custom group';
1751 return $errors;
1752 }
1753
1754 $query = "
1755 SELECT b.id
1756 FROM civicrm_custom_field a
1757 INNER JOIN civicrm_custom_field b
1758 WHERE a.id = %1
1759 AND a.label = b.label
1760 AND b.custom_group_id = %2
1761 ";
1762 $params = [
1763 1 => [$field->id, 'Integer'],
1764 2 => [$newGroup->id, 'Integer'],
1765 ];
1766 $count = CRM_Core_DAO::singleValueQuery($query, $params);
1767 if ($count > 0) {
1768 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1769 }
1770
1771 $tableName = $oldGroup->table_name;
1772 $columnName = $field->column_name;
1773
1774 $query = "
1775 SELECT count(*)
1776 FROM $tableName
1777 WHERE $columnName is not null
1778 ";
1779 $count = CRM_Core_DAO::singleValueQuery($query);
1780 if ($count > 0) {
1781 $query = "
1782 SELECT extends
1783 FROM civicrm_custom_group
1784 WHERE id IN ( %1, %2 )
1785 ";
1786 $params = [
1787 1 => [$oldGroup->id, 'Integer'],
1788 2 => [$newGroup->id, 'Integer'],
1789 ];
1790
1791 $dao = CRM_Core_DAO::executeQuery($query, $params);
1792 $extends = [];
1793 while ($dao->fetch()) {
1794 $extends[] = $dao->extends;
1795 }
1796 if ($extends[0] != $extends[1]) {
1797 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1798 }
1799 }
1800
1801 return empty($errors) ? TRUE : $errors;
1802 }
1803
1804 /**
1805 * Move a custom data field from one group (table) to another.
1806 *
1807 * @param int $fieldID
1808 * FK to civicrm_custom_field.
1809 * @param int $newGroupID
1810 * FK to civicrm_custom_group.
1811 *
1812 * @throws CRM_Core_Exception
1813 */
1814 public static function moveField($fieldID, $newGroupID) {
1815 $validation = self::_moveFieldValidate($fieldID, $newGroupID);
1816 if (TRUE !== $validation) {
1817 throw new CRM_Core_Exception(implode(' ', $validation));
1818 }
1819 $field = new CRM_Core_DAO_CustomField();
1820 $field->id = $fieldID;
1821 $field->find(TRUE);
1822
1823 $newGroup = new CRM_Core_DAO_CustomGroup();
1824 $newGroup->id = $newGroupID;
1825 $newGroup->find(TRUE);
1826
1827 $oldGroup = new CRM_Core_DAO_CustomGroup();
1828 $oldGroup->id = $field->custom_group_id;
1829 $oldGroup->find(TRUE);
1830
1831 $add = clone$field;
1832 $add->custom_group_id = $newGroup->id;
1833 self::createField($add, 'add');
1834
1835 $sql = "INSERT INTO {$newGroup->table_name} (entity_id, `{$field->column_name}`)
1836 SELECT entity_id, `{$field->column_name}` FROM {$oldGroup->table_name}
1837 ON DUPLICATE KEY UPDATE `{$field->column_name}` = {$oldGroup->table_name}.`{$field->column_name}`
1838 ";
1839 CRM_Core_DAO::executeQuery($sql);
1840
1841 $del = clone$field;
1842 $del->custom_group_id = $oldGroup->id;
1843 self::createField($del, 'delete');
1844
1845 $add->save();
1846
1847 CRM_Utils_System::flushCache();
1848 }
1849
1850 /**
1851 * Create an option value for a custom field option group ID.
1852 *
1853 * @param array $params
1854 * @param string $value
1855 * @param \CRM_Core_DAO_OptionGroup $optionGroup
1856 * @param string $index
1857 * @param string $dataType
1858 */
1859 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup $optionGroup, $index, $dataType) {
1860 if (strlen(trim($value))) {
1861 $optionValue = new CRM_Core_DAO_OptionValue();
1862 $optionValue->option_group_id = $optionGroup->id;
1863 $optionValue->label = $params['option_label'][$index];
1864 $optionValue->name = $params['option_name'][$index] ?? CRM_Utils_String::titleToVar($params['option_label'][$index]);
1865 switch ($dataType) {
1866 case 'Money':
1867 $optionValue->value = CRM_Utils_Rule::cleanMoney($value);
1868 break;
1869
1870 case 'Int':
1871 $optionValue->value = intval($value);
1872 break;
1873
1874 case 'Float':
1875 $optionValue->value = floatval($value);
1876 break;
1877
1878 default:
1879 $optionValue->value = trim($value);
1880 }
1881
1882 $optionValue->weight = $params['option_weight'][$index];
1883 $optionValue->is_active = $params['option_status'][$index] ?? FALSE;
1884 $optionValue->description = $params['option_description'][$index] ?? NULL;
1885 $optionValue->color = $params['option_color'][$index] ?? NULL;
1886 $optionValue->icon = $params['option_icon'][$index] ?? NULL;
1887 $optionValue->save();
1888 }
1889 }
1890
1891 /**
1892 * Prepare for the create operation.
1893 *
1894 * Munge params, create the option values if needed.
1895 *
1896 * This could be called by a single create or a batchCreate.
1897 *
1898 * @param array $params
1899 *
1900 * @return array
1901 */
1902 protected static function prepareCreate($params) {
1903 $op = empty($params['id']) ? 'create' : 'edit';
1904 CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params);
1905 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
1906 if ($op === 'create') {
1907 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
1908 if (!isset($params['column_name'])) {
1909 // if add mode & column_name not present, calculate it.
1910 $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32));
1911 }
1912 if (!isset($params['name'])) {
1913 $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64);
1914 }
1915 }
1916 else {
1917 $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
1918 }
1919
1920 $htmlType = $params['html_type'] ?? NULL;
1921 $dataType = $params['data_type'] ?? NULL;
1922
1923 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1924 $params['date_format'] = Civi::settings()->get('dateInputFormat');
1925 }
1926
1927 // Checkboxes are always serialized in current schema
1928 if ($htmlType == 'CheckBox') {
1929 $params['serialize'] = CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND;
1930 }
1931
1932 if (!empty($params['serialize'])) {
1933 if (isset($params['default_checkbox_option'])) {
1934 $defaultArray = [];
1935 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
1936 if ($params['option_value'][$v]) {
1937 $defaultArray[] = $params['option_value'][$v];
1938 }
1939 }
1940
1941 if (!empty($defaultArray)) {
1942 // also add the separator before and after the value per new convention (CRM-1604)
1943 $params['default_value'] = CRM_Utils_Array::implodePadded($defaultArray);
1944 }
1945 }
1946 else {
1947 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
1948 $params['default_value'] = $params['option_value'][$params['default_option']];
1949 }
1950 }
1951 }
1952
1953 // create any option group & values if required
1954 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
1955 if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes)) {
1956 //CRM-16659: if option_value then create an option group for this custom field.
1957 if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) {
1958 // first create an option group for this custom group
1959 $optionGroup = new CRM_Core_DAO_OptionGroup();
1960 $optionGroup->name = "{$params['column_name']}_" . date('YmdHis');
1961 $optionGroup->title = $params['label'];
1962 $optionGroup->is_active = 1;
1963 // Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
1964 $optionGroup->is_reserved = 0;
1965 $optionGroup->data_type = $dataType;
1966 $optionGroup->save();
1967 $params['option_group_id'] = $optionGroup->id;
1968 if (!empty($params['option_value']) && is_array($params['option_value'])) {
1969 foreach ($params['option_value'] as $k => $v) {
1970 self::createOptionValue($params, $v, $optionGroup, $k, $dataType);
1971 }
1972 }
1973 }
1974 }
1975
1976 // check for orphan option groups
1977 if (!empty($params['option_group_id'])) {
1978 if (!empty($params['id'])) {
1979 self::fixOptionGroups($params['id'], $params['option_group_id']);
1980 }
1981
1982 // if we do not have a default value
1983 // retrieve it from one of the other custom fields which use this option group
1984 if (empty($params['default_value'])) {
1985 //don't insert only value separator as default value, CRM-4579
1986 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
1987
1988 if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) {
1989 $params['default_value'] = $defaultValue;
1990 }
1991 }
1992 }
1993
1994 // Set default textarea attributes
1995 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
1996 $params['attributes'] = 'rows=4, cols=60';
1997 }
1998 return $params;
1999 }
2000
2001 /**
2002 * Create database entry for custom field and related option groups.
2003 *
2004 * @param array $params
2005 *
2006 * @return CRM_Core_DAO_CustomField
2007 */
2008 protected static function createCustomFieldRecord($params) {
2009 $transaction = new CRM_Core_Transaction();
2010 $params = self::prepareCreate($params);
2011
2012 $alterSerialize = isset($params['serialize']) && !empty($params['id'])
2013 && ($params['serialize'] != CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'serialize'));
2014
2015 $customField = new CRM_Core_DAO_CustomField();
2016 $customField->copyValues($params);
2017 $customField->save();
2018
2019 //create/drop the index when we toggle the is_searchable flag
2020 $op = empty($params['id']) ? 'add' : 'modify';
2021 if ($op !== 'modify') {
2022 if ($params['is_append_field_id_to_column_name']) {
2023 $params['column_name'] .= "_{$customField->id}";
2024 }
2025 $customField->column_name = $params['column_name'];
2026 $customField->save();
2027 }
2028
2029 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2030 $transaction->commit();
2031
2032 // make sure all values are present in the object for further processing
2033 $customField->find(TRUE);
2034
2035 if ($alterSerialize) {
2036 self::alterSerialize($customField);
2037 }
2038
2039 return $customField;
2040 }
2041
2042 /**
2043 * Move custom data from one contact to another.
2044 *
2045 * This is currently the start of a refactoring. The theory is each
2046 * entity could have a 'move' function with a DAO default one to fall back on.
2047 *
2048 * At the moment this only does a small part of the process - ie deleting a file field that
2049 * is about to be overwritten. However, the goal is the whole process around the move for
2050 * custom data should be in here.
2051 *
2052 * This is currently called by the merge class but it makes sense that api could
2053 * expose move actions as moving (e.g) contributions feels like a common
2054 * ask that should be handled by the form layer.
2055 *
2056 * @param int $oldContactID
2057 * @param int $newContactID
2058 * @param int[] $fieldIDs
2059 * Optional list field ids to move.
2060 *
2061 * @throws \CiviCRM_API3_Exception
2062 * @throws \Exception
2063 */
2064 public function move($oldContactID, $newContactID, $fieldIDs) {
2065 if (empty($fieldIDs)) {
2066 return;
2067 }
2068 $fields = civicrm_api3('CustomField', 'get', ['id' => ['IN' => $fieldIDs], 'return' => ['custom_group_id.is_multiple', 'custom_group_id.table_name', 'column_name', 'data_type'], 'options' => ['limit' => 0]])['values'];
2069 $return = [];
2070 foreach ($fieldIDs as $fieldID) {
2071 $return[] = 'custom_' . $fieldID;
2072 }
2073 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2074 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2075
2076 // The moveAllBelongings function has functionality to move custom fields. It doesn't work very well...
2077 // @todo handle all fields here but more immediately Country since that is broken at the moment.
2078 $fieldTypesNotHandledInMergeAttempt = ['File'];
2079 foreach ($fields as $field) {
2080 $isMultiple = !empty($field['custom_group_id.is_multiple']);
2081 if ($field['data_type'] === 'File' && !$isMultiple) {
2082 if (!empty($oldContact['custom_' . $field['id']]) && !empty($newContact['custom_' . $field['id']])) {
2083 CRM_Core_BAO_File::deleteFileReferences($oldContact['custom_' . $field['id']], $oldContactID, $field['id']);
2084 }
2085 if (!empty($oldContact['custom_' . $field['id']])) {
2086 CRM_Core_DAO::executeQuery("
2087 UPDATE civicrm_entity_file
2088 SET entity_id = $newContactID
2089 WHERE file_id = {$oldContact['custom_' . $field['id']]}"
2090 );
2091 }
2092 }
2093 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2094 CRM_Core_DAO::executeQuery(
2095 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2096 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2097 ON DUPLICATE KEY UPDATE
2098 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2099 ");
2100 }
2101 }
2102 }
2103
2104 /**
2105 * Get the database table name and column name for a custom field.
2106 *
2107 * @param int $fieldID
2108 * The fieldID of the custom field.
2109 * @param bool $force
2110 * Force the sql to be run again (primarily used for tests).
2111 *
2112 * @return array
2113 * fatal is fieldID does not exists, else array of tableName, columnName
2114 * @throws \CRM_Core_Exception
2115 */
2116 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2117 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2118 $cache = CRM_Utils_Cache::singleton();
2119 $fieldValues = $cache->get($cacheKey);
2120 if (empty($fieldValues) || $force) {
2121 $query = "
2122 SELECT cg.table_name, cf.column_name, cg.id
2123 FROM civicrm_custom_group cg,
2124 civicrm_custom_field cf
2125 WHERE cf.custom_group_id = cg.id
2126 AND cf.id = %1";
2127 $params = [1 => [$fieldID, 'Integer']];
2128 $dao = CRM_Core_DAO::executeQuery($query, $params);
2129
2130 if (!$dao->fetch()) {
2131 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2132 }
2133 $fieldValues = [$dao->table_name, $dao->column_name, $dao->id];
2134 $cache->set($cacheKey, $fieldValues);
2135 }
2136 return $fieldValues;
2137 }
2138
2139 /**
2140 * Get custom option groups.
2141 *
2142 * @deprecated Use the API OptionGroup.get
2143 *
2144 * @param array $includeFieldIds
2145 * Ids of custom fields for which option groups must be included.
2146 *
2147 * Currently this is required in the cases where option groups are to be included
2148 * for inactive fields : CRM-5369
2149 *
2150 * @return mixed
2151 */
2152 public static function customOptionGroup($includeFieldIds = NULL) {
2153 static $customOptionGroup = NULL;
2154
2155 $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force';
2156 if ($cacheKey == 'force') {
2157 $customOptionGroup[$cacheKey] = NULL;
2158 }
2159
2160 if (empty($customOptionGroup[$cacheKey])) {
2161 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2162
2163 //support for single as well as array format.
2164 if (!empty($includeFieldIds)) {
2165 if (is_array($includeFieldIds)) {
2166 $includeFieldIds = implode(',', $includeFieldIds);
2167 }
2168 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2169 }
2170
2171 $query = "
2172 SELECT g.id, g.title
2173 FROM civicrm_option_group g
2174 INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2175 WHERE {$whereClause}";
2176
2177 $dao = CRM_Core_DAO::executeQuery($query);
2178 while ($dao->fetch()) {
2179 $customOptionGroup[$cacheKey][$dao->id] = $dao->title;
2180 }
2181 }
2182
2183 return $customOptionGroup[$cacheKey];
2184 }
2185
2186 /**
2187 * Get defaults for new entity.
2188 *
2189 * @return array
2190 */
2191 public static function getDefaults() {
2192 return [
2193 'is_required' => FALSE,
2194 'is_searchable' => FALSE,
2195 'in_selector' => FALSE,
2196 'is_search_range' => FALSE,
2197 //CRM-15792 - Custom field gets disabled if is_active not set
2198 // this would ideally be a mysql default.
2199 'is_active' => TRUE,
2200 'is_view' => FALSE,
2201 ];
2202 }
2203
2204 /**
2205 * Fix orphan groups.
2206 *
2207 * @param int $customFieldId
2208 * Custom field id.
2209 * @param int $optionGroupId
2210 * Option group id.
2211 */
2212 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2213 // check if option group belongs to any custom Field else delete
2214 // get the current option group
2215 $currentOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
2216 $customFieldId,
2217 'option_group_id'
2218 );
2219 // get the updated option group
2220 // if both are same return
2221 if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) {
2222 return;
2223 }
2224
2225 // check if option group is related to any other field
2226 self::checkOptionGroup($currentOptionGroupId);
2227 }
2228
2229 /**
2230 * Check if option group is related to more than one custom field.
2231 *
2232 * @param int $optionGroupId
2233 * Option group id.
2234 */
2235 public static function checkOptionGroup($optionGroupId) {
2236 $query = "
2237 SELECT count(*)
2238 FROM civicrm_custom_field
2239 WHERE option_group_id = {$optionGroupId}";
2240
2241 $count = CRM_Core_DAO::singleValueQuery($query);
2242
2243 if ($count < 2) {
2244 //delete the option group
2245 CRM_Core_BAO_OptionGroup::del($optionGroupId);
2246 }
2247 }
2248
2249 /**
2250 * Get option group default.
2251 *
2252 * @param int $optionGroupId
2253 * @param bool $serialize
2254 *
2255 * @return null|string
2256 */
2257 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2258 $query = "
2259 SELECT default_value, serialize
2260 FROM civicrm_custom_field
2261 WHERE option_group_id = {$optionGroupId}
2262 AND default_value IS NOT NULL";
2263
2264 $dao = CRM_Core_DAO::executeQuery($query);
2265 while ($dao->fetch()) {
2266 if ($dao->serialize == $serialize) {
2267 return $dao->default_value;
2268 }
2269 $defaultValue = $dao->default_value;
2270 }
2271
2272 // Convert serialization
2273 if (isset($defaultValue) && $serialize) {
2274 return CRM_Utils_Array::implodePadded([$defaultValue]);
2275 }
2276 elseif (isset($defaultValue)) {
2277 return CRM_Utils_Array::explodePadded($defaultValue)[0];
2278 }
2279 return NULL;
2280 }
2281
2282 /**
2283 * Post process function.
2284 *
2285 * @param array $params
2286 * @param int $entityID
2287 * @param string $customFieldExtends
2288 * @param bool $inline
2289 * @param bool $checkPermissions
2290 *
2291 * @return array
2292 */
2293 public static function postProcess(
2294 &$params,
2295 $entityID,
2296 $customFieldExtends,
2297 $inline = FALSE,
2298 $checkPermissions = TRUE
2299 ) {
2300 $customData = [];
2301
2302 foreach ($params as $key => $value) {
2303 if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE)) {
2304
2305 // for autocomplete transfer hidden value instead of label
2306 if ($params[$key] && isset($params[$key . '_id'])) {
2307 $value = $params[$key . '_id'];
2308 }
2309
2310 // we need to append time with date
2311 if ($params[$key] && isset($params[$key . '_time'])) {
2312 $value .= ' ' . $params[$key . '_time'];
2313 }
2314
2315 CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0],
2316 $customData,
2317 $value,
2318 $customFieldExtends,
2319 $customFieldInfo[1],
2320 $entityID,
2321 $inline,
2322 $checkPermissions
2323 );
2324 }
2325 }
2326 return $customData;
2327 }
2328
2329 /**
2330 * Get custom field ID from field/group name/title.
2331 *
2332 * @param string $fieldName Field name or label
2333 * @param string|null $groupName (Optional) Group name or label
2334 * @param bool $fullString Whether to return "custom_123" or "123"
2335 *
2336 * @return string|int|null
2337 * @throws \CiviCRM_API3_Exception
2338 */
2339 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2340 $cacheKey = $groupName . '.' . $fieldName;
2341 if (!isset(Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2342 $customFieldParams = [
2343 'name' => $fieldName,
2344 'label' => $fieldName,
2345 'options' => ['or' => [["name", "label"]]],
2346 ];
2347
2348 if ($groupName) {
2349 $customFieldParams['custom_group_id.name'] = $groupName;
2350 $customFieldParams['custom_group_id.title'] = $groupName;
2351 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2352 }
2353
2354 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2355
2356 if (empty($field['id'])) {
2357 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2358 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2359 }
2360 else {
2361 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2362 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2363 }
2364 }
2365
2366 if ($fullString) {
2367 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2368 }
2369 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
2370 }
2371
2372 /**
2373 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
2374 *
2375 * @param array $ids
2376 *
2377 * @return array
2378 */
2379 public static function getNameFromID($ids) {
2380 if (is_array($ids)) {
2381 $ids = implode(',', $ids);
2382 }
2383 $sql = "
2384 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2385 FROM civicrm_custom_field f
2386 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2387 WHERE f.id IN ($ids)";
2388
2389 $dao = CRM_Core_DAO::executeQuery($sql);
2390 $result = [];
2391 while ($dao->fetch()) {
2392 $result[$dao->id] = [
2393 'field_name' => $dao->field_name,
2394 'field_label' => $dao->field_label,
2395 'group_name' => $dao->group_name,
2396 'group_title' => $dao->group_title,
2397 ];
2398 }
2399 return $result;
2400 }
2401
2402 /**
2403 * Validate custom data.
2404 *
2405 * @param array $params
2406 * Custom data submitted.
2407 * ie array( 'custom_1' => 'validate me' );
2408 *
2409 * @return array
2410 * validation errors.
2411 */
2412 public static function validateCustomData($params) {
2413 $errors = [];
2414 if (!is_array($params) || empty($params)) {
2415 return $errors;
2416 }
2417
2418 //pick up profile fields.
2419 $profileFields = [];
2420 $ufGroupId = $params['ufGroupId'] ?? NULL;
2421 if ($ufGroupId) {
2422 $profileFields = CRM_Core_BAO_UFGroup::getFields($ufGroupId,
2423 FALSE,
2424 CRM_Core_Action::VIEW
2425 );
2426 }
2427
2428 //lets start w/ params.
2429 foreach ($params as $key => $value) {
2430 $customFieldID = self::getKeyID($key);
2431 if (!$customFieldID) {
2432 continue;
2433 }
2434
2435 //load the structural info for given field.
2436 $field = new CRM_Core_DAO_CustomField();
2437 $field->id = $customFieldID;
2438 if (!$field->find(TRUE)) {
2439 continue;
2440 }
2441 $dataType = $field->data_type;
2442
2443 $profileField = CRM_Utils_Array::value($key, $profileFields, []);
2444 $fieldTitle = $profileField['title'] ?? NULL;
2445 $isRequired = $profileField['is_required'] ?? NULL;
2446 if (!$fieldTitle) {
2447 $fieldTitle = $field->label;
2448 }
2449
2450 //no need to validate.
2451 if (CRM_Utils_System::isNull($value) && !$isRequired) {
2452 continue;
2453 }
2454
2455 //lets validate first for required field.
2456 if ($isRequired && CRM_Utils_System::isNull($value)) {
2457 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
2458 continue;
2459 }
2460
2461 //now time to take care of custom field form rules.
2462 $ruleName = $errorMsg = NULL;
2463 switch ($dataType) {
2464 case 'Int':
2465 $ruleName = 'integer';
2466 $errorMsg = ts('%1 must be an integer (whole number).',
2467 array(1 => $fieldTitle)
2468 );
2469 break;
2470
2471 case 'Money':
2472 $ruleName = 'money';
2473 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2474 array(1 => $fieldTitle)
2475 );
2476 break;
2477
2478 case 'Float':
2479 $ruleName = 'numeric';
2480 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2481 array(1 => $fieldTitle)
2482 );
2483 break;
2484
2485 case 'Link':
2486 $ruleName = 'wikiURL';
2487 $errorMsg = ts('%1 must be valid Website.',
2488 array(1 => $fieldTitle)
2489 );
2490 break;
2491 }
2492
2493 if ($ruleName && !CRM_Utils_System::isNull($value)) {
2494 $valid = FALSE;
2495 $funName = "CRM_Utils_Rule::{$ruleName}";
2496 if (is_callable($funName)) {
2497 $valid = call_user_func($funName, $value);
2498 }
2499 if (!$valid) {
2500 $errors[$key] = $errorMsg;
2501 }
2502 }
2503 }
2504
2505 return $errors;
2506 }
2507
2508 /**
2509 * Is this field a multi record field.
2510 *
2511 * @param int $customId
2512 *
2513 * @return bool
2514 */
2515 public static function isMultiRecordField($customId) {
2516 $isMultipleWithGid = FALSE;
2517 if (!is_numeric($customId)) {
2518 $customId = self::getKeyID($customId);
2519 }
2520 if (is_numeric($customId)) {
2521 $sql = "SELECT cg.id cgId
2522 FROM civicrm_custom_group cg
2523 INNER JOIN civicrm_custom_field cf
2524 ON cg.id = cf.custom_group_id
2525 WHERE cf.id = %1 AND cg.is_multiple = 1";
2526 $params[1] = [$customId, 'Integer'];
2527 $dao = CRM_Core_DAO::executeQuery($sql, $params);
2528 if ($dao->fetch()) {
2529 if ($dao->cgId) {
2530 $isMultipleWithGid = $dao->cgId;
2531 }
2532 }
2533 }
2534
2535 return $isMultipleWithGid;
2536 }
2537
2538 /**
2539 * Does this field type have any select options?
2540 *
2541 * @param array $field
2542 *
2543 * @return bool
2544 */
2545 public static function hasOptions($field) {
2546 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2547 $field = (array) $field;
2548 // This will include boolean fields with Yes/No options.
2549 if (in_array($field['html_type'], ['Radio', 'CheckBox'])) {
2550 return TRUE;
2551 }
2552 // Do this before the "Select" string search because date fields have a "Select Date" html_type
2553 // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list.
2554 if (in_array($field['data_type'], ['ContactReference', 'Date'])) {
2555 return FALSE;
2556 }
2557 if (strpos($field['html_type'], 'Select') !== FALSE) {
2558 return TRUE;
2559 }
2560 return !empty($field['option_group_id']);
2561 }
2562
2563 /**
2564 * Does this field store a serialized string?
2565 *
2566 * @param CRM_Core_DAO_CustomField|array $field
2567 *
2568 * @return bool
2569 */
2570 public static function isSerialized($field) {
2571 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2572 $html_type = is_object($field) ? $field->html_type : $field['html_type'];
2573 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2574 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2575 if ($html_type === 'CheckBox' || strpos($html_type, 'Multi') !== FALSE) {
2576 return TRUE;
2577 }
2578 // Otherwise this is the new standard as of 5.27
2579 return is_object($field) ? !empty($field->serialize) : !empty($field['serialize']);
2580 }
2581
2582 /**
2583 * Get api entity for this field
2584 *
2585 * @return string
2586 */
2587 public function getEntity() {
2588 $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
2589 return in_array($entity, ['Individual', 'Household', 'Organization']) ? 'Contact' : $entity;
2590 }
2591
2592 /**
2593 * Set pseudoconstant properties for field metadata.
2594 *
2595 * @param array $field
2596 * @param string|null $optionGroupName
2597 */
2598 private static function getOptionsForField(&$field, $optionGroupName) {
2599 if ($optionGroupName) {
2600 $field['pseudoconstant'] = [
2601 'optionGroupName' => $optionGroupName,
2602 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2603 ];
2604 }
2605 elseif ($field['data_type'] == 'Boolean') {
2606 $field['pseudoconstant'] = [
2607 'callback' => 'CRM_Core_SelectValues::boolean',
2608 ];
2609 }
2610 elseif ($field['data_type'] == 'Country') {
2611 $field['pseudoconstant'] = [
2612 'table' => 'civicrm_country',
2613 'keyColumn' => 'id',
2614 'labelColumn' => 'name',
2615 'nameColumn' => 'iso_code',
2616 ];
2617 }
2618 elseif ($field['data_type'] == 'StateProvince') {
2619 $field['pseudoconstant'] = [
2620 'table' => 'civicrm_state_province',
2621 'keyColumn' => 'id',
2622 'labelColumn' => 'name',
2623 ];
2624 }
2625 }
2626
2627 /**
2628 * @param CRM_Core_DAO_CustomField $field
2629 * @param 'add|modify' $operation
2630 *
2631 * @return array
2632 */
2633 protected static function prepareCreateParams($field, $operation) {
2634 $tableName = CRM_Core_DAO::getFieldValue(
2635 'CRM_Core_DAO_CustomGroup',
2636 $field->custom_group_id,
2637 'table_name'
2638 );
2639
2640 $params = [
2641 'table_name' => $tableName,
2642 'operation' => $operation,
2643 'name' => $field->column_name,
2644 'type' => CRM_Core_BAO_CustomValueTable::fieldToSQLType(
2645 $field->data_type,
2646 $field->text_length
2647 ),
2648 'required' => $field->is_required,
2649 'searchable' => $field->is_searchable,
2650 ];
2651
2652 if ($operation == 'delete') {
2653 $fkName = "{$tableName}_{$field->column_name}";
2654 if (strlen($fkName) >= 48) {
2655 $fkName = substr($fkName, 0, 32) . '_' . substr(md5($fkName), 0, 16);
2656 }
2657 $params['fkName'] = $fkName;
2658 }
2659 if ($field->data_type == 'Country' && !self::isSerialized($field)) {
2660 $params['fk_table_name'] = 'civicrm_country';
2661 $params['fk_field_name'] = 'id';
2662 $params['fk_attributes'] = 'ON DELETE SET NULL';
2663 }
2664 elseif ($field->data_type == 'StateProvince' && !self::isSerialized($field)) {
2665 $params['fk_table_name'] = 'civicrm_state_province';
2666 $params['fk_field_name'] = 'id';
2667 $params['fk_attributes'] = 'ON DELETE SET NULL';
2668 }
2669 elseif ($field->data_type == 'StateProvince' || $field->data_type == 'Country') {
2670 $params['type'] = 'varchar(255)';
2671 }
2672 elseif ($field->data_type == 'File') {
2673 $params['fk_table_name'] = 'civicrm_file';
2674 $params['fk_field_name'] = 'id';
2675 $params['fk_attributes'] = 'ON DELETE SET NULL';
2676 }
2677 elseif ($field->data_type == 'ContactReference') {
2678 $params['fk_table_name'] = 'civicrm_contact';
2679 $params['fk_field_name'] = 'id';
2680 $params['fk_attributes'] = 'ON DELETE SET NULL';
2681 }
2682 if (isset($field->default_value)) {
2683 $params['default'] = "'{$field->default_value}'";
2684 }
2685 return $params;
2686 }
2687
2688 }