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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Business objects for managing custom data fields.
21 class CRM_Core_BAO_CustomField
extends CRM_Core_DAO_CustomField
{
24 * Array to hold (formatted) fields for import
28 public static $_importFields = NULL;
31 * Build and retrieve the list of data types and descriptions.
34 * Data type => Description
36 public static function dataType() {
38 'String' => ts('Alphanumeric'),
39 'Int' => ts('Integer'),
40 'Float' => ts('Number'),
41 'Money' => ts('Money'),
44 'Boolean' => ts('Yes or No'),
45 'StateProvince' => ts('State/Province'),
46 'Country' => ts('Country'),
49 'ContactReference' => ts('Contact Reference'),
54 * Build the map of custom field's data types and there respective Util type
57 * Data data-type => CRM_Utils_Type
59 public static function dataToType() {
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
,
78 * Deprecated in favor of writeRecords & APIv4
81 * @param array $params
82 * @return CRM_Core_DAO_CustomField
84 public static function create($params) {
85 $changeSerialize = self
::getChangeSerialize($params);
86 $customField = self
::createCustomFieldRecord($params);
87 // When deserializing a field, the update needs to run before the schema change
88 if ($changeSerialize === 0) {
89 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
91 $op = empty($params['id']) ?
'add' : 'modify';
92 self
::createField($customField, $op);
93 // When serializing a field, the update needs to run after the schema change
94 if ($changeSerialize === 1) {
95 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
98 CRM_Utils_Hook
::post(($op === 'add' ?
'create' : 'edit'), 'CustomField', $customField->id
, $customField);
100 CRM_Utils_System
::flushCache();
101 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
102 // so do it manually. Ideally we wouldn't need to clear others...
103 Civi
::cache('metadata')->clear();
109 * Save multiple fields, now deprecated in favor of self::writeRecords.
110 * https://lab.civicrm.org/dev/core/issues/1093
113 * @param array $bulkParams
114 * Array of arrays as would be passed into create
115 * @param array $defaults
116 * Default parameters to be be merged into each of the params.
118 * @throws \CiviCRM_API3_Exception
120 public static function bulkSave($bulkParams, $defaults = []) {
121 CRM_Core_Error
::deprecatedFunctionWarning(__CLASS__
. '::writeRecords');
122 foreach ($bulkParams as $index => $fieldParams) {
123 $bulkParams[$index] = array_merge($defaults, $fieldParams);
125 self
::writeRecords($bulkParams);
129 * Create/update several fields at once in a mysql efficient way.
131 * @param array $records
132 * @return CRM_Core_DAO_CustomField[]
133 * @throws CRM_Core_Exception
135 public static function writeRecords(array $records): array {
136 $addedColumns = $sql = $customFields = $pre = $post = [];
137 foreach ($records as $index => $params) {
138 CRM_Utils_Hook
::pre(empty($params['id']) ?
'create' : 'edit', 'CustomField', $params['id'] ??
NULL, $params);
140 $changeSerialize = self
::getChangeSerialize($params);
141 $customField = self
::createCustomFieldRecord($params);
142 // Serialize/deserialize sql must run after/before the table is altered
143 if ($changeSerialize === 0) {
144 $pre[] = self
::getAlterSerializeSQL($customField);
146 if ($changeSerialize === 1) {
147 $post[] = self
::getAlterSerializeSQL($customField);
149 $fieldSQL = self
::getAlterFieldSQL($customField, empty($params['id']) ?
'add' : 'modify');
151 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id
, 'table_name');
152 $sql[$tableName][] = $fieldSQL;
153 $addedColumns[$tableName][] = $customField->column_name
;
154 $customFields[$index] = $customField;
157 foreach ($pre as $query) {
158 CRM_Core_DAO
::executeQuery($query);
161 foreach ($sql as $tableName => $statements) {
162 // CRM-7007: do not i18n-rewrite this query
163 CRM_Core_DAO
::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE);
165 if (CRM_Core_Config
::singleton()->logging
) {
166 $logging = new CRM_Logging_Schema();
167 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
170 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
173 foreach ($post as $query) {
174 CRM_Core_DAO
::executeQuery($query);
177 CRM_Utils_System
::flushCache();
178 Civi
::cache('metadata')->clear();
180 foreach ($customFields as $index => $customField) {
181 CRM_Utils_Hook
::post(empty($records[$index]['id']) ?
'create' : 'edit', 'CustomField', $customField->id
, $customField);
183 return $customFields;
187 * Retrieve DB object and copy to defaults array.
189 * @param array $params
190 * Array of criteria values.
191 * @param array $defaults
192 * Array to be populated with found values.
195 * The DAO object, if found.
199 public static function retrieve($params, &$defaults) {
200 return self
::commonRetrieve(self
::class, $params, $defaults);
204 * Update the is_active flag in the db.
207 * Id of the database record.
208 * @param bool $is_active
209 * Value we want to set the is_active field.
212 * true if we found and updated the object, else false
214 public static function setIsActive($id, $is_active) {
216 CRM_Utils_System
::flushCache();
218 //enable-disable CustomField
219 CRM_Core_BAO_UFField
::setUFField($id, $is_active);
220 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_CustomField', $id, 'is_active', $is_active);
224 * Get the field title.
232 public static function getTitle($id) {
233 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
237 * @param string $context
240 public function getOptions($context = NULL) {
241 CRM_Core_DAO
::buildOptionsContext($context);
246 $cacheKey = "CRM_Core_BAO_CustomField_getOptions_{$this->id}_$context";
247 $cache = CRM_Utils_Cache
::singleton();
248 $options = $cache->get($cacheKey);
249 if (!isset($options)) {
250 if (!$this->data_type ||
!$this->custom_group_id
) {
254 // This will hold the list of options in format key => label
257 if (!empty($this->option_group_id
)) {
258 $options = CRM_Core_OptionGroup
::valuesByID(
259 $this->option_group_id
, FALSE, FALSE, FALSE, $context == 'validate' ?
'name' : 'label', !($context == 'validate' ||
$context == 'get')
262 elseif ($this->data_type
=== 'StateProvince') {
263 $options = CRM_Core_PseudoConstant
::stateProvince();
265 elseif ($this->data_type
=== 'Country') {
266 $options = $context == 'validate' ? CRM_Core_PseudoConstant
::countryIsoCode() : CRM_Core_PseudoConstant
::country();
268 elseif ($this->data_type
=== 'Boolean') {
269 $options = $context == 'validate' ?
[0, 1] : CRM_Core_SelectValues
::boolean();
271 CRM_Utils_Hook
::customFieldOptions($this->id
, $options, FALSE);
272 CRM_Utils_Hook
::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, ['context' => $context]);
273 $cache->set($cacheKey, $options);
281 public static function buildOptions($fieldName, $context = NULL, $props = []) {
282 $options = parent
::buildOptions($fieldName, $context, $props);
283 // This provides legacy support for APIv3, allowing no-longer-existent html types
284 if ($fieldName == 'html_type' && isset($props['version']) && $props['version'] == 3) {
285 $options['Multi-Select'] = 'Multi-Select';
286 $options['Select Country'] = 'Select Country';
287 $options['Multi-Select Country'] = 'Multi-Select Country';
288 $options['Select State/Province'] = 'Select State/Province';
289 $options['Multi-Select State/Province'] = 'Multi-Select State/Province';
295 * Store and return an array of all active custom fields.
297 * @param string $customDataType
298 * Type of Custom Data; 'ANY' is a synonym for "all contact data types".
299 * @param bool $showAll
300 * If true returns all fields (includes disabled fields).
301 * @param bool $inline
302 * If true returns all inline fields (includes disabled fields).
303 * @param int $customDataSubType
304 * Custom Data sub type value.
305 * @param int $customDataSubName
306 * Custom Data sub name value.
307 * @param bool $onlyParent
308 * Return only top level custom data, for eg, only Participant and ignore subname and subtype.
309 * @param bool $onlySubType
310 * Return only custom data for subtype.
311 * @param bool|int $checkPermission
312 * Either a CRM_Core_Permission constant or FALSE to disable checks
315 * an array of active custom fields.
316 * @throws \CRM_Core_Exception
318 public static function &getFields(
319 $customDataType = 'Individual',
322 $customDataSubType = NULL,
323 $customDataSubName = NULL,
325 $onlySubType = FALSE,
326 $checkPermission = CRM_Core_Permission
::EDIT
328 if ($checkPermission === TRUE) {
329 CRM_Core_Error
::deprecatedWarning('Unexpected TRUE passed to CustomField::getFields $checkPermission param.');
330 $checkPermission = CRM_Core_Permission
::EDIT
;
332 if (empty($customDataType)) {
333 $customDataType = array_merge(['Contact'], CRM_Contact_BAO_ContactType
::basicTypes());
335 if ($customDataType === 'ANY') {
336 // NULL should have been respected but the line above broke that.
337 // boo for us not having enough unit tests back them.
338 $customDataType = NULL;
340 if ($customDataType && !is_array($customDataType)) {
342 if (in_array($customDataType, CRM_Contact_BAO_ContactType
::subTypes(), TRUE)) {
343 // This is the case when getFieldsForImport() requires fields
344 // limited strictly to a subtype.
345 $customDataSubType = $customDataType;
346 $customDataType = CRM_Contact_BAO_ContactType
::getBasicType($customDataType);
350 if (array_key_exists($customDataType, CRM_Core_SelectValues
::customGroupExtends())) {
351 // this makes the method flexible to support retrieving fields
352 // for multiple extends value.
353 $customDataType = [$customDataType];
357 $customDataSubType = CRM_Utils_Array
::explodePadded($customDataSubType);
359 if (is_array($customDataType)) {
360 $cacheKey = implode('_', $customDataType);
363 $cacheKey = $customDataType;
366 $cacheKey .= !empty($customDataSubType) ?
('_' . implode('_', $customDataSubType)) : '_0';
367 $cacheKey .= $customDataSubName ?
"{$customDataSubName}_" : '_0';
368 $cacheKey .= $showAll ?
'_1' : '_0';
369 $cacheKey .= $inline ?
'_1_' : '_0_';
370 $cacheKey .= $onlyParent ?
'_1_' : '_0_';
371 $cacheKey .= $onlySubType ?
'_1_' : '_0_';
372 $cacheKey .= $checkPermission ?
$checkPermission . CRM_Core_Session
::getLoggedInContactID() . '_' : '_0_0_';
373 $cacheKey .= '_' . CRM_Core_Config
::domainID() . '_';
375 $cgTable = CRM_Core_DAO_CustomGroup
::getTableName();
377 // also get the permission stuff here
378 if ($checkPermission) {
379 $permissionClause = CRM_Core_Permission
::customGroupClause($checkPermission,
384 $permissionClause = '(1)';
387 // lets md5 permission clause and take first 8 characters
388 $cacheKey .= substr(md5($permissionClause), 0, 8);
390 if (strlen($cacheKey) > 40) {
391 $cacheKey = md5($cacheKey);
394 if (!self
::$_importFields ||
395 CRM_Utils_Array
::value($cacheKey, self
::$_importFields) === NULL
397 if (!self
::$_importFields) {
398 self
::$_importFields = [];
401 // check if we can retrieve from database cache
402 $fields = Civi
::Cache('fields')->get("custom importableFields $cacheKey");
404 if ($fields === NULL) {
407 if (is_array($customDataType)) {
409 foreach ($customDataType as $dataType) {
410 if (array_key_exists($dataType, CRM_Core_SelectValues
::customGroupExtends())) {
411 if (in_array($dataType, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
412 $val = "'" . CRM_Utils_Type
::escape($dataType, 'String') . "', 'Contact' ";
415 $val = "'" . CRM_Utils_Type
::escape($dataType, 'String') . "'";
417 $value = $value ?
$value . ", {$val}" : $val;
421 $extends = "AND $cgTable.extends IN ( $value ) ";
425 if (!empty($customDataType) && empty($extends)) {
426 // $customDataType specified a filter, but there is no corresponding SQL ($extends)
427 self
::$_importFields[$cacheKey] = [];
428 return self
::$_importFields[$cacheKey];
432 $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL ";
434 // Temporary hack - in 5.27 a new field is added to civicrm_custom_field. There is a high
435 // risk this function is called before the upgrade page can be reached and if
436 // so it will potentially result in fatal error.
437 $serializeField = CRM_Core_BAO_Domain
::isDBVersionAtLeast('5.27.alpha1') ?
"custom_field.serialize," : '';
439 $query = "SELECT custom_field.id, custom_field.label,
441 custom_field.data_type,
442 custom_field.html_type,
443 custom_field.default_value,
444 custom_field.options_per_line, custom_field.text_length,
445 custom_field.custom_group_id,
446 custom_field.is_required,
447 custom_field.column_name,
448 $cgTable.extends, custom_field.is_search_range,
449 $cgTable.extends_entity_column_value,
450 $cgTable.extends_entity_column_id,
451 custom_field.is_view,
452 custom_field.option_group_id,
453 custom_field.date_format,
454 custom_field.time_format,
455 $cgTable.is_multiple,
458 og.name as option_group_name
459 FROM civicrm_custom_field custom_field
461 ON custom_field.custom_group_id = $cgTable.id
462 LEFT JOIN civicrm_option_group og
463 ON custom_field.option_group_id = og.id
467 $query .= " AND custom_field.is_active = 1 AND $cgTable.is_active = 1 ";
471 $query .= " AND $cgTable.style = 'Inline' ";
474 //get the custom fields for specific type in
475 //combination with fields those support any type.
476 if (!empty($customDataSubType)) {
478 foreach ($customDataSubType as $subtype) {
479 $subtype = CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($subtype, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
;
480 $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'";
483 $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL";
485 $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )";
488 if ($customDataSubName) {
489 $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) ";
492 // also get the permission stuff here
493 if ($checkPermission) {
494 $permissionClause = CRM_Core_Permission
::customGroupClause($checkPermission,
499 $permissionClause = '(1)';
502 $query .= " $extends AND $permissionClause
503 ORDER BY $cgTable.weight, $cgTable.title,
504 custom_field.weight, custom_field.label";
506 $dao = CRM_Core_DAO
::executeQuery($query);
509 while (($dao->fetch()) != NULL) {
510 $regexp = preg_replace('/[.,;:!?]/', '', NULL);
511 $fields[$dao->id
]['id'] = $dao->id
;
512 $fields[$dao->id
]['label'] = $dao->label
;
513 // This seems broken, but not in a new way.
514 $fields[$dao->id
]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
515 // To support the consolidation of various functions & their expectations.
516 $fields[$dao->id
]['title'] = $dao->label
;
517 $fields[$dao->id
]['custom_field_id'] = $dao->id
;
518 $fields[$dao->id
]['groupTitle'] = $dao->title
;
519 $fields[$dao->id
]['data_type'] = $dao->data_type
;
520 $fields[$dao->id
]['name'] = 'custom_' . $dao->id
;
521 $fields[$dao->id
]['type'] = CRM_Utils_Array
::value($dao->data_type
, self
::dataToType());
522 $fields[$dao->id
]['html_type'] = $dao->html_type
;
523 $fields[$dao->id
]['default_value'] = $dao->default_value
;
524 $fields[$dao->id
]['text_length'] = $dao->text_length
;
525 $fields[$dao->id
]['options_per_line'] = $dao->options_per_line
;
526 $fields[$dao->id
]['custom_group_id'] = $dao->custom_group_id
;
527 $fields[$dao->id
]['extends'] = $dao->extends;
528 $fields[$dao->id
]['is_search_range'] = $dao->is_search_range
;
529 $fields[$dao->id
]['extends_entity_column_value'] = $dao->extends_entity_column_value
;
530 $fields[$dao->id
]['extends_entity_column_id'] = $dao->extends_entity_column_id
;
531 $fields[$dao->id
]['is_view'] = $dao->is_view
;
532 $fields[$dao->id
]['is_multiple'] = $dao->is_multiple
;
533 $fields[$dao->id
]['option_group_id'] = $dao->option_group_id
;
534 $fields[$dao->id
]['date_format'] = $dao->date_format
;
535 $fields[$dao->id
]['time_format'] = $dao->time_format
;
536 $fields[$dao->id
]['is_required'] = $dao->is_required
;
537 $fields[$dao->id
]['table_name'] = $dao->table_name
;
538 $fields[$dao->id
]['column_name'] = $dao->column_name
;
539 $fields[$dao->id
]['serialize'] = $serializeField ?
$dao->serialize
: (int) self
::isSerialized($dao);
540 $fields[$dao->id
]['where'] = $dao->table_name
. '.' . $dao->column_name
;
541 // Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
542 $fields[$dao->id
]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery
::$extendsMap) ? CRM_Core_BAO_CustomQuery
::$extendsMap[$dao->extends] : '';
543 if (in_array($dao->extends, CRM_Contact_BAO_ContactType
::subTypes())) {
544 // if $extends is a subtype, refer contact table
545 $fields[$dao->id
]['extends_table'] = 'civicrm_contact';
547 // Search table is used by query object searches..
548 $fields[$dao->id
]['search_table'] = ($fields[$dao->id
]['extends_table'] == 'civicrm_contact') ?
'contact_a' : $fields[$dao->id
]['extends_table'];
549 self
::getOptionsForField($fields[$dao->id
], $dao->option_group_name
);
552 Civi
::cache('fields')->set("custom importableFields $cacheKey", $fields);
554 self
::$_importFields[$cacheKey] = $fields;
557 return self
::$_importFields[$cacheKey];
561 * Return field ids and names (with groups).
563 * NOTE: Despite this function's name, it is used both for IMPORT and EXPORT.
564 * The $checkPermission variable should be set to VIEW for export and EDIT for import.
566 * @param int|string $contactType Contact type
567 * @param bool $showAll
568 * If true returns all fields (includes disabled fields).
569 * @param bool $onlyParent
570 * Return fields ONLY related to basic types.
571 * @param bool $search
572 * When called from search and multiple records need to be returned.
573 * @param bool|int $checkPermission
574 * Either a CRM_Core_Permission constant or FALSE to disable checks
576 * @param bool $withMultiple
580 public static function getFieldsForImport(
581 $contactType = 'Individual',
585 $checkPermission = TRUE,
586 $withMultiple = FALSE
588 if ($checkPermission === TRUE) {
589 // TODO: Trigger deprecation notice for passing TRUE
590 $checkPermission = CRM_Core_Permission
::EDIT
;
592 // Note: there are situations when we want getFieldsForImport() return fields related
593 // ONLY to basic contact types, but NOT subtypes. And thats where $onlyParent is helpful
594 $fields = &self
::getFields($contactType,
604 $importableFields = [];
605 foreach ($fields as $id => $values) {
606 // for now we should not allow multiple fields in profile / export etc, hence unsetting
608 (!empty($values['is_multiple']) && !$withMultiple)
612 if (!empty($values['text_length'])) {
613 $values['maxlength'] = (int) $values['text_length'];
616 /* generate the key for the fields array */
619 $importableFields[$key] = $values;
620 $importableFields[$key]['import'] = 1;
623 return $importableFields;
627 * Get the field id from an import key.
637 public static function getKeyID($key, $all = FALSE) {
639 if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) {
646 CRM_Utils_Array
::value(2, $match),
650 return $all ?
[NULL, NULL] : NULL;
654 * Use the cache to get all values of a specific custom field.
656 * @param int $fieldID
657 * The custom field ID.
659 * @return CRM_Core_BAO_CustomField
661 * @throws CRM_Core_Exception
663 public static function getFieldObject($fieldID) {
664 $field = new CRM_Core_BAO_CustomField();
666 // check if we can get the field values from the system cache
667 $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
668 $cache = CRM_Utils_Cache
::singleton();
669 $fieldValues = $cache->get($cacheKey);
670 if (empty($fieldValues)) {
671 $field->id
= $fieldID;
672 if (!$field->find(TRUE)) {
673 throw new CRM_Core_Exception('Cannot find Custom Field');
677 CRM_Core_DAO
::storeValues($field, $fieldValues);
679 $cache->set($cacheKey, $fieldValues);
682 $field->copyValues($fieldValues);
689 * Add a custom field to an existing form.
691 * @param CRM_Core_Form $qf
692 * Form object (reference).
693 * @param string $elementName
694 * Name of the custom field.
695 * @param int $fieldId
696 * @param bool $useRequired
697 * True if required else false.
698 * @param bool $search
699 * True if used for search else false.
700 * @param string $label
701 * Label for custom field.
702 * @return \HTML_QuickForm_Element|null
703 * @throws \CiviCRM_API3_Exception
705 public static function addQuickFormElement(
706 $qf, $elementName, $fieldId, $useRequired = TRUE, $search = FALSE, $label = NULL
708 $field = self
::getFieldObject($fieldId);
709 $widget = $field->html_type
;
711 $customFieldAttributes = [];
713 if (!isset($label)) {
714 $label = $field->label
;
717 // DAO stores attributes as a string, but it's hard to manipulate and
718 // CRM_Core_Form::add() wants them as an array.
719 $fieldAttributes = self
::attributesFromString($field->attributes
);
721 // Custom field HTML should indicate group+field name
722 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
);
723 $fieldAttributes['data-crm-custom'] = $groupName . ':' . $field->name
;
725 // Fixed for Issue CRM-2183
726 if ($widget == 'TextArea' && $search) {
730 $placeholder = $search ?
ts('- any %1 -', [1 => $label]) : ts('- select %1 -', [1 => $label]);
732 if (in_array($widget, [
737 $options = $field->getOptions($search ?
'search' : 'create');
739 // Consolidate widget types to simplify the below switch statement
744 // Search field is always multi-select
745 if ($search ||
(self
::isSerialized($field) && $widget === 'Select')) {
746 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' huge');
747 $fieldAttributes['multiple'] = 'multiple';
748 $fieldAttributes['placeholder'] = $placeholder;
751 // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
752 $canEditOptions = CRM_Core_Permission
::check('administer CiviCRM');
753 if ($field->option_group_id
&& !$search && $canEditOptions) {
754 $customFieldAttributes +
= [
755 'data-api-entity' => $field->getEntity(),
756 'data-api-field' => 'custom_' . $field->id
,
757 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id
),
759 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
763 $rangeDataTypes = ['Int', 'Float', 'Money'];
765 // at some point in time we might want to split the below into small functions
770 if ($field->is_search_range
&& $search && in_array($field->data_type
, $rangeDataTypes)) {
771 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
772 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
775 if ($field->text_length
) {
776 $fieldAttributes['maxlength'] = $field->text_length
;
777 if ($field->text_length
< 20) {
778 $fieldAttributes['size'] = $field->text_length
;
781 $element = $qf->add('text', $elementName, $label,
783 $useRequired && !$search
789 $fieldAttributes['rows'] = $field->note_rows ??
4;
790 $fieldAttributes['cols'] = $field->note_columns ??
60;
792 if ($field->text_length
) {
793 $fieldAttributes['maxlength'] = $field->text_length
;
795 $element = $qf->add('textarea',
799 $useRequired && !$search
804 //CRM-18379: Fix for date range of 'Select Date' custom field when include in profile.
805 $minYear = isset($field->start_date_years
) ?
(date('Y') - $field->start_date_years
) : NULL;
806 $maxYear = isset($field->end_date_years
) ?
(date('Y') +
$field->end_date_years
) : NULL;
809 'date' => $field->date_format
,
810 'minDate' => isset($minYear) ?
$minYear . '-01-01' : NULL,
811 //CRM-18487 - max date should be the last date of the year.
812 'maxDate' => isset($maxYear) ?
$maxYear . '-12-31' : NULL,
813 'time' => $field->time_format ?
$field->time_format
* 12 : FALSE,
815 if ($field->is_search_range
&& $search) {
816 $qf->add('datepicker', $elementName . '_from', $label, $fieldAttributes +
array('placeholder' => ts('From')), FALSE, $params);
817 $qf->add('datepicker', $elementName . '_to', NULL, $fieldAttributes +
array('placeholder' => ts('To')), FALSE, $params);
820 $element = $qf->add('datepicker', $elementName, $label, $fieldAttributes, $useRequired && !$search, $params);
825 if ($field->is_search_range
&& $search && in_array($field->data_type
, $rangeDataTypes)) {
826 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
827 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
830 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
831 if ($search ||
empty($useRequired)) {
832 $fieldAttributes['allowClear'] = TRUE;
834 $qf->addRadio($elementName, $label, $options, $fieldAttributes, NULL, $useRequired);
838 // For all select elements
840 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' crm-select2');
842 if (empty($fieldAttributes['multiple'])) {
843 $options = ['' => $placeholder] +
$options;
845 $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $fieldAttributes);
847 // Add and/or option for fields that store multiple values
848 if ($search && self
::isSerialized($field)) {
849 $qf->addRadio($elementName . '_operator', '', [
852 ], [], NULL, FALSE, [
853 'or' => ['title' => ts('Results may contain any of the selected options')],
854 'and' => ['title' => ts('Results must have all of the selected options')],
856 $qf->setDefaults([$elementName . '_operator' => 'or']);
862 foreach ($options as $v => $l) {
863 // TODO: I'm not sure if this is supposed to exclude whatever might be
864 // in $field->attributes (available in array format as
865 // $fieldAttributes). Leaving as-is for now.
866 $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, $customFieldAttributes);
869 $group = $element = $qf->addGroup($check, $elementName, $label);
870 $optionEditKey = 'data-option-edit-path';
871 if (isset($customFieldAttributes[$optionEditKey])) {
872 $group->setAttribute($optionEditKey, $customFieldAttributes[$optionEditKey]);
875 if ($useRequired && !$search) {
876 $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
881 // we should not build upload file in search mode
886 strtolower($field->html_type
),
890 $useRequired && !$search
892 $qf->addUploadElement($elementName);
895 case 'RichTextEditor':
896 $fieldAttributes['rows'] = $field->note_rows
;
897 $fieldAttributes['cols'] = $field->note_columns
;
898 if ($field->text_length
) {
899 $fieldAttributes['maxlength'] = $field->text_length
;
901 $element = $qf->add('wysiwyg', $elementName, $label, $fieldAttributes, $useRequired && !$search);
904 case 'Autocomplete-Select':
905 static $customUrls = [];
906 if ($field->data_type
== 'ContactReference') {
907 // break if contact does not have permission to access ContactReference
908 if (!CRM_Core_Permission
::check('access contact reference fields')) {
911 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' crm-form-contact-reference huge');
912 $fieldAttributes['data-api-entity'] = 'Contact';
913 if (!empty($field->serialize
) ||
$search) {
914 $fieldAttributes['multiple'] = TRUE;
916 $element = $qf->add('text', $elementName, $label, $fieldAttributes, $useRequired && !$search);
918 $urlParams = "context=customfield&id={$field->id}";
919 $idOfelement = $elementName;
920 // dev/core#362 if in an onbehalf profile clean up the name to get rid of square brackets that break the select 2 js
921 // However this caused regression https://lab.civicrm.org/dev/core/issues/619 so it has been hacked back to
922 // only affecting on behalf - next time someone looks at this code it should be with a view to overhauling it
923 // rather than layering on more hacks.
924 if (substr($elementName, 0, 8) === 'onbehalf' && strpos($elementName, '[') && strpos($elementName, ']')) {
925 $idOfelement = substr(substr($elementName, (strpos($elementName, '[') +
1)), 0, -1);
927 $customUrls[$idOfelement] = CRM_Utils_System
::url('civicrm/ajax/contactref',
934 // FIXME: This won't work with customFieldOptions hook
935 $fieldAttributes +
= [
936 'entity' => 'OptionValue',
937 'placeholder' => $placeholder,
938 'multiple' => $search ?
TRUE : !empty($field->serialize
),
940 'params' => ['option_group_id' => $field->option_group_id
, 'is_active' => 1],
943 $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
946 $qf->assign('customUrls', $customUrls);
950 switch ($field->data_type
) {
952 // integers will have numeric rule applied to them.
953 if ($field->is_search_range
&& $search && $widget != 'Select') {
954 $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', [1 => $label]), 'integer');
955 $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', [1 => $label]), 'integer');
957 elseif ($widget == 'Text') {
958 $qf->addRule($elementName, ts('%1 must be an integer (whole number).', [1 => $label]), 'integer');
963 if ($field->is_search_range
&& $search) {
964 $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', [1 => $label]), 'numeric');
965 $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', [1 => $label]), 'numeric');
967 elseif ($widget == 'Text') {
968 $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', [1 => $label]), 'numeric');
973 if ($field->is_search_range
&& $search) {
974 $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
975 $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
977 elseif ($widget == 'Text') {
978 $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
983 $element->setAttribute('class', "url");
984 $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
987 if ($field->is_view
&& !$search) {
988 $qf->freeze($elementName);
994 * Take a string of HTML element attributes and turn it into an associative
997 * @param string $attrString
998 * The attributes as a string, e.g. `rows=3 cols=40`.
1001 * The attributes as an array, e.g. `['rows' => 3, 'cols' => 40]`.
1003 public static function attributesFromString($attrString) {
1005 foreach (explode(' ', $attrString) as $at) {
1006 if (strpos($at, '=')) {
1007 list($k, $v) = explode('=', $at);
1008 $attributes[$k] = trim($v, ' "');
1015 * Delete the Custom Field.
1017 * @param object $field
1020 public static function deleteField($field) {
1021 CRM_Utils_System
::flushCache();
1023 // first delete the custom option group and values associated with this field
1024 if ($field->option_group_id
) {
1025 //check if option group is related to any other field, if
1026 //not delete the option group and related option values
1027 self
::checkOptionGroup($field->option_group_id
);
1029 // next drop the column from the custom value table
1030 self
::createField($field, 'delete');
1033 CRM_Core_BAO_UFField
::delUFField($field->id
);
1034 CRM_Core_BAO_Mapping
::removeFieldFromMapping('custom_' . $field->id
);
1035 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_CustomField');
1037 CRM_Utils_Hook
::post('delete', 'CustomField', $field->id
, $field);
1041 * @param string|int|array|null $value
1042 * @param CRM_Core_BAO_CustomField|int|array|string $field
1043 * @param int $entityId
1047 * @throws \CRM_Core_Exception
1049 public static function displayValue($value, $field, $entityId = NULL) {
1050 $field = is_array($field) ?
$field['id'] : $field;
1051 $fieldId = is_object($field) ?
$field->id
: (int) str_replace('custom_', '', $field);
1054 throw new CRM_Core_Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
1057 if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
1058 $field = self
::getFieldObject($fieldId);
1061 $fieldInfo = ['options' => $field->getOptions()] +
(array) $field;
1063 $displayValue = self
::formatDisplayValue($value, $fieldInfo, $entityId);
1065 // Call hook to alter display value.
1066 CRM_Utils_Hook
::alterCustomFieldDisplayValue($displayValue, $value, $entityId, $fieldInfo);
1068 return $displayValue;
1072 * Lower-level logic for rendering a custom field value
1074 * @param string|array $value
1075 * @param array $field
1076 * @param int|null $entityId
1080 private static function formatDisplayValue($value, $field, $entityId = NULL) {
1082 if (self
::isSerialized($field) && !is_array($value)) {
1083 $value = CRM_Utils_Array
::explodePadded($value);
1086 if ($field['html_type'] == 'CheckBox' && $value) {
1087 $value = CRM_Utils_Array
::convertCheckboxFormatToArray($value);
1090 $display = is_array($value) ?
implode(', ', $value) : (string) $value;
1092 switch ($field['html_type']) {
1095 case 'Autocomplete-Select':
1098 if ($field['data_type'] == 'ContactReference' && (is_array($value) ||
is_numeric($value))) {
1099 // Issue #2939 - guard against passing empty values to CRM_Core_DAO::getFieldValue(), which would throw an exception
1100 if (empty($value)) {
1105 foreach ((array) $value as $contactId) {
1106 $displayNames[] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');
1108 $display = implode(', ', $displayNames);
1111 elseif ($field['data_type'] == 'ContactReference') {
1114 elseif (is_array($value)) {
1116 foreach ($value as $key => $val) {
1117 $v[] = $field['options'][$val] ??
NULL;
1119 $display = implode(', ', $v);
1122 $display = CRM_Utils_Array
::value($value, $field['options'], '');
1123 // For float type (see Number and Money) $value would be decimal like
1124 // 1.00 (because it is stored in db as decimal), while options array
1125 // key would be integer like 1. In this case expression on line above
1126 // would return empty string (default value), despite the fact that
1127 // matching option exists in the array.
1128 // In such cases we could just get intval($value) and fetch matching
1129 // option again, but this would not work if key is float like 5.6.
1130 // So we need to truncate trailing zeros to make it work as expected.
1131 if ($display === '' && strpos($value, '.') !== FALSE) {
1132 // Use round() to truncate trailing zeros, e.g:
1133 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1134 $value = (string) round($value, 5);
1135 $display = $field['options'][$value] ??
'';
1141 $customFormat = NULL;
1143 // FIXME: Are there any legitimate reasons why $value would be an array?
1144 // Or should we throw an exception here if it is?
1145 $value = is_array($value) ? CRM_Utils_Array
::first($value) : $value;
1147 $actualPHPFormats = CRM_Utils_Date
::datePluginToPHPFormats();
1148 $format = $field['date_format'] ??
NULL;
1151 if (array_key_exists($format, $actualPHPFormats)) {
1152 $customTimeFormat = (array) $actualPHPFormats[$format];
1153 switch (CRM_Utils_Array
::value('time_format', $field)) {
1155 $customTimeFormat[] = 'g:iA';
1159 $customTimeFormat[] = 'G:i';
1163 //If time is not selected remove time from value.
1164 $value = $value ?
date('Y-m-d', strtotime($value)) : '';
1166 $customFormat = implode(" ", $customTimeFormat);
1169 $display = CRM_Utils_Date
::processDate($value, NULL, FALSE, $customFormat);
1173 // In the context of displaying a profile, show file/image
1176 if (CRM_Utils_Rule
::positiveInteger($value)) {
1180 $fileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1182 $url = self
::getFileURL($entityId, $field['id'], $fileId);
1184 $display = $url['file_url'];
1188 // In other contexts show a paperclip icon
1189 if (CRM_Utils_Rule
::integer($value)) {
1190 $icons = CRM_Core_BAO_File
::paperIconAttachment('*', $value);
1192 $file_description = '';
1194 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1196 catch (CiviCRM_API3_Exception
$dontcare) {
1200 $display = "{$icons[$value]}{$file_description}";
1203 //CRM-18396, if filename is passed instead
1211 $display = $display ?
"<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1215 $display = nl2br($display);
1219 if ($field['data_type'] == 'Money' && isset($value)) {
1220 // $value can also be an array(while using IN operator from search builder or api).
1221 foreach ((array) $value as $val) {
1222 $disp[] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($val);
1224 $display = implode(', ', $disp);
1226 elseif ($field['data_type'] == 'Float' && isset($value)) {
1227 // $value can also be an array(while using IN operator from search builder or api).
1228 foreach ((array) $value as $val) {
1229 $disp[] = CRM_Utils_Number
::formatLocaleNumeric($val);
1231 $display = implode(', ', $disp);
1239 * Set default values for custom data used in profile.
1241 * @param int $customFieldId
1243 * @param string $elementName
1244 * Custom field name.
1245 * @param array $defaults
1246 * Associated array of fields.
1247 * @param int $contactId
1251 * @param mixed $value
1252 * If passed - dont fetch value from db,.
1253 * just format the given value
1255 public static function setProfileDefaults(
1263 //get the type of custom field
1264 $customField = new CRM_Core_BAO_CustomField();
1265 $customField->id
= $customFieldId;
1266 $customField->find(TRUE);
1269 if ($mode == CRM_Profile_Form
::MODE_CREATE
) {
1270 $value = $customField->default_value
;
1274 if (!isset($value)) {
1275 $info = self
::getTableColumnGroup($customFieldId);
1276 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1277 $result = CRM_Core_DAO
::executeQuery($query);
1278 if ($result->fetch()) {
1279 $value = $result->value
;
1283 if ($customField->data_type
== 'Country') {
1285 $config = CRM_Core_Config
::singleton();
1286 if ($config->defaultContactCountry
) {
1287 $value = CRM_Core_BAO_Country
::defaultContactCountry();
1293 //set defaults if mode is registration
1294 if (!trim($value) &&
1296 (!in_array($mode, [CRM_Profile_Form
::MODE_EDIT
, CRM_Profile_Form
::MODE_SEARCH
]))
1298 $value = $customField->default_value
;
1301 if ($customField->data_type
== 'Money' && isset($value)) {
1302 $value = number_format($value, 2);
1304 if (self
::isSerialized($customField) && $value) {
1305 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldId, FALSE);
1306 $defaults[$elementName] = [];
1307 $checkedValue = CRM_Utils_Array
::explodePadded($value);
1308 foreach ($customOption as $val) {
1309 if (in_array($val['value'], $checkedValue)) {
1310 if ($customField->html_type
== 'CheckBox') {
1311 $defaults[$elementName][$val['value']] = 1;
1314 $defaults[$elementName][$val['value']] = $val['value'];
1320 $defaults[$elementName] = $value;
1327 * @param int $contactID
1329 * @param int $fileID
1330 * @param bool $absolute
1332 * @param string $multiRecordWhereClause
1336 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1339 $params = ['id' => $cfID];
1341 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1342 $columnName = $defaults['column_name'];
1344 //table name of custom data
1345 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
1346 $defaults['custom_group_id'],
1350 //query to fetch id from civicrm_file
1351 if ($multiRecordWhereClause) {
1352 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1355 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1357 $fileID = CRM_Core_DAO
::singleValueQuery($query);
1362 $fileType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1367 $result['file_id'] = $fileID;
1369 if ($fileType == 'image/jpeg' ||
1370 $fileType == 'image/pjpeg' ||
1371 $fileType == 'image/gif' ||
1372 $fileType == 'image/x-png' ||
1373 $fileType == 'image/png'
1375 $entityId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_EntityFile',
1380 list($path) = CRM_Core_BAO_File
::path($fileID, $entityId);
1381 $fileHash = CRM_Core_BAO_File
::generateFileHash($entityId, $fileID);
1382 $url = CRM_Utils_System
::url('civicrm/file',
1383 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1384 $absolute, NULL, TRUE, TRUE
1386 $result['file_url'] = CRM_Utils_File
::getFileURL($path, $fileType, $url);
1388 // for non image files
1390 $uri = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1394 $fileHash = CRM_Core_BAO_File
::generateFileHash($contactID, $fileID);
1395 $url = CRM_Utils_System
::url('civicrm/file',
1396 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1397 $absolute, NULL, TRUE, TRUE
1399 $result['file_url'] = CRM_Utils_File
::getFileURL($uri, $fileType, $url);
1407 * Format custom fields before inserting.
1409 * @param int $customFieldId
1411 * @param array $customFormatted
1413 * @param mixed $value
1414 * Value of custom field.
1415 * @param string $customFieldExtend
1416 * Custom field extends.
1417 * @param int $customValueId
1418 * Custom option value id.
1419 * @param int $entityId
1420 * Entity id (contribution, membership...).
1421 * @param bool $inline
1422 * Consider inline custom groups only.
1423 * @param bool $checkPermission
1424 * If false, do not include permissioning clause.
1425 * @param bool $includeViewOnly
1426 * If true, fields marked 'View Only' are included. Required for APIv3.
1428 * @return array|NULL
1429 * formatted custom field array
1431 public static function formatCustomField(
1432 $customFieldId, &$customFormatted, $value,
1433 $customFieldExtend, $customValueId = NULL,
1436 $checkPermission = TRUE,
1437 $includeViewOnly = FALSE
1439 //get the custom fields for the entity
1440 //subtype and basic type
1441 $customDataSubType = NULL;
1442 if ($customFieldExtend) {
1443 // This is the case when getFieldsForImport() requires fields
1444 // of subtype and its parent.CRM-5143
1445 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1446 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType
::subTypes(), (array) $customFieldExtend);
1447 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1448 $customFieldExtend = CRM_Contact_BAO_ContactType
::getBasicType($customDataSubType);
1449 if (is_array($customFieldExtend)) {
1450 $customFieldExtend = array_unique(array_values($customFieldExtend));
1455 $customFields = CRM_Core_BAO_CustomField
::getFields($customFieldExtend,
1462 $checkPermission ? CRM_Core_Permission
::EDIT
: FALSE
1465 if (!array_key_exists($customFieldId, $customFields)) {
1469 // return if field is a 'code' field
1470 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1474 list($tableName, $columnName, $groupID) = self
::getTableColumnGroup($customFieldId);
1476 if (!$customValueId &&
1477 // we always create new entites for is_multiple unless specified
1478 !$customFields[$customFieldId]['is_multiple'] &&
1484 WHERE entity_id={$entityId}";
1486 $customValueId = CRM_Core_DAO
::singleValueQuery($query);
1489 //fix checkbox, now check box always submits values
1490 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1492 // Note that only during merge this is not an array, and you can directly use value
1493 if (is_array($value)) {
1494 $selectedValues = [];
1495 foreach ($value as $selId => $val) {
1497 $selectedValues[] = $selId;
1500 if (!empty($selectedValues)) {
1501 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1503 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
1511 elseif (self
::isSerialized($customFields[$customFieldId])) {
1512 // Select2 v3 returns a comma-separated string.
1513 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1514 $value = explode(',', $value);
1517 $value = $value ? CRM_Utils_Array
::implodePadded($value) : '';
1520 if (self
::isSerialized($customFields[$customFieldId]) &&
1521 $customFields[$customFieldId]['data_type'] == 'String' &&
1522 !empty($customFields[$customFieldId]['text_length']) &&
1525 // lets make sure that value is less than the length, else we'll
1526 // be losing some data, CRM-7481
1527 if (strlen($value) >= $customFields[$customFieldId]['text_length']) {
1528 // need to do a few things here
1530 // 1. lets find a new length
1531 $newLength = $customFields[$customFieldId]['text_length'];
1532 $minLength = strlen($value);
1533 while ($newLength < $minLength) {
1534 $newLength = $newLength * 2;
1537 // set the custom field meta data to have a length larger than value
1538 // alter the custom value table column to match this length
1539 CRM_Core_BAO_SchemaHandler
::alterFieldLength($customFieldId, $tableName, $columnName, $newLength);
1544 if ($customFields[$customFieldId]['data_type'] == 'Date') {
1545 if (!CRM_Utils_System
::isNull($value)) {
1546 $format = $customFields[$customFieldId]['date_format'];
1547 $date = CRM_Utils_Date
::processDate($value, NULL, FALSE, 'YmdHis', $format);
1552 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1553 $customFields[$customFieldId]['data_type'] == 'Money'
1559 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1560 $value = CRM_Utils_Rule
::cleanMoney($value);
1564 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1565 $customFields[$customFieldId]['data_type'] == 'Country'
1575 if ($customFields[$customFieldId]['data_type'] == 'File') {
1576 if (empty($value)) {
1580 $config = CRM_Core_Config
::singleton();
1582 // If we are already passing the file id as a value then retrieve and set the file data
1583 if (CRM_Utils_Rule
::integer($value)) {
1584 $fileDAO = new CRM_Core_DAO_File();
1585 $fileDAO->id
= $value;
1586 $fileDAO->find(TRUE);
1589 $fName = $fileDAO->uri
;
1590 $mimeType = $fileDAO->mime_type
;
1593 elseif (empty($value['name'])) {
1594 // Happens when calling the API to update custom fields values, but the filename
1595 // is empty, for an existing entity (in a specific case, was from a d7-webform
1596 // that was updating a relationship with a File customfield, so $value['id'] was
1597 // not empty, but the filename was empty.
1601 $fName = $value['name'];
1602 $mimeType = $value['type'];
1605 $filename = pathinfo($fName, PATHINFO_BASENAME
);
1607 // rename this file to go into the secure directory only if
1608 // user has uploaded new file not existing verfied on the basis of $fileID
1609 if (empty($fileID) && !rename($fName, $config->customFileUploadDir
. $filename)) {
1610 CRM_Core_Error
::statusBounce(ts('Could not move custom file to custom upload directory'));
1613 if ($customValueId && empty($fileID)) {
1618 $params = [1 => [$customValueId, 'Integer']];
1619 $fileID = CRM_Core_DAO
::singleValueQuery($query, $params);
1622 $fileDAO = new CRM_Core_DAO_File();
1625 $fileDAO->id
= $fileID;
1628 $fileDAO->uri
= $filename;
1629 $fileDAO->mime_type
= $mimeType;
1630 $fileDAO->upload_date
= date('YmdHis');
1632 $fileID = $fileDAO->id
;
1636 if (!is_array($customFormatted)) {
1637 $customFormatted = [];
1640 if (!array_key_exists($customFieldId, $customFormatted)) {
1641 $customFormatted[$customFieldId] = [];
1645 if ($customValueId) {
1646 $index = $customValueId;
1649 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1650 $customFormatted[$customFieldId][$index] = [];
1652 $customFormatted[$customFieldId][$index] = [
1653 'id' => $customValueId > 0 ?
$customValueId : NULL,
1655 'type' => $customFields[$customFieldId]['data_type'],
1656 'custom_field_id' => $customFieldId,
1657 'custom_group_id' => $groupID,
1658 'table_name' => $tableName,
1659 'column_name' => $columnName,
1660 'file_id' => $fileID,
1661 // is_multiple refers to the custom group, serialize refers to the field.
1662 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1663 'serialize' => $customFields[$customFieldId]['serialize'],
1666 //we need to sort so that custom fields are created in the order of entry
1667 krsort($customFormatted[$customFieldId]);
1668 return $customFormatted;
1672 * Get default custom table schema.
1674 * @param array $params
1678 public static function defaultCustomTableSchema($params) {
1679 // add the id and extends_id
1680 $collation = CRM_Core_BAO_SchemaHandler
::getInUseCollation();
1681 $characterSet = 'utf8';
1682 if (stripos($collation, 'utf8mb4') !== FALSE) {
1683 $characterSet = 'utf8mb4';
1686 'name' => $params['name'],
1687 'is_multiple' => $params['is_multiple'],
1688 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1692 'type' => 'int unsigned',
1695 'attributes' => 'AUTO_INCREMENT',
1696 'comment' => 'Default MySQL primary key',
1699 'name' => 'entity_id',
1700 'type' => 'int unsigned',
1702 'comment' => 'Table that this extends',
1703 'fk_table_name' => $params['extends_name'],
1704 'fk_field_name' => 'id',
1705 'fk_attributes' => 'ON DELETE CASCADE',
1710 // If on MySQL 5.6 include ROW_FORMAT=DYNAMIC to fix unit tests
1711 $databaseVersion = CRM_Utils_SQL
::getDatabaseVersion();
1712 if (version_compare($databaseVersion, '5.7', '<') && version_compare($databaseVersion, '5.6', '>=')) {
1713 $table['attributes'] = $table['attributes'] . ' ROW_FORMAT=DYNAMIC';
1716 if (!$params['is_multiple']) {
1717 $table['indexes'] = [
1720 'field_name_1' => 'entity_id',
1728 * Create custom field.
1730 * @param CRM_Core_DAO_CustomField $field
1731 * @param string $operation
1733 public static function createField($field, $operation) {
1734 $sql = str_repeat(' ', 8);
1735 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1736 $sql .= "ALTER TABLE " . $tableName;
1737 $sql .= self
::getAlterFieldSQL($field, $operation);
1739 // CRM-7007: do not i18n-rewrite this query
1740 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1742 $config = CRM_Core_Config
::singleton();
1743 if ($config->logging
) {
1744 // CRM-16717 not sure why this was originally limited to add.
1745 // For example custom tables can have field length changes - which need to flow through to logging.
1746 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
1747 if ($operation === 'add' ||
$operation === 'modify') {
1748 $logging = new CRM_Logging_Schema();
1749 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name
]]);
1753 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
1757 * @param CRM_Core_DAO_CustomField $field
1758 * @param string $operation add|modify|delete
1762 public static function getAlterFieldSQL($field, $operation) {
1763 $params = self
::prepareCreateParams($field, $operation);
1764 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1765 // a function only used by Custom Field creation...
1766 $params['required'] = FALSE;
1767 return CRM_Core_BAO_SchemaHandler
::getFieldAlterSQL($params);
1771 * Get query to reformat existing values for a field when changing its serialize attribute
1773 * @param CRM_Core_DAO_CustomField $field
1776 private static function getAlterSerializeSQL($field) {
1777 $table = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1778 $col = $field->column_name
;
1779 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
1780 if ($field->serialize
) {
1781 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1784 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1789 * Determine whether it would be safe to move a field.
1791 * @param int $fieldID
1792 * FK to civicrm_custom_field.
1793 * @param int $newGroupID
1794 * FK to civicrm_custom_group.
1797 * array(string) or TRUE
1799 public static function _moveFieldValidate($fieldID, $newGroupID) {
1802 $field = new CRM_Core_DAO_CustomField();
1803 $field->id
= $fieldID;
1804 if (!$field->find(TRUE)) {
1805 $errors['fieldID'] = ts('Invalid ID for custom field');
1809 $oldGroup = new CRM_Core_DAO_CustomGroup();
1810 $oldGroup->id
= $field->custom_group_id
;
1811 if (!$oldGroup->find(TRUE)) {
1812 $errors['fieldID'] = ts('Invalid ID for old custom group');
1816 $newGroup = new CRM_Core_DAO_CustomGroup();
1817 $newGroup->id
= $newGroupID;
1818 if (!$newGroup->find(TRUE)) {
1819 $errors['newGroupID'] = ts('Invalid ID for new custom group');
1825 FROM civicrm_custom_field a
1826 INNER JOIN civicrm_custom_field b
1828 AND a.label = b.label
1829 AND b.custom_group_id = %2
1832 1 => [$field->id
, 'Integer'],
1833 2 => [$newGroup->id
, 'Integer'],
1835 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
1837 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1840 $tableName = $oldGroup->table_name
;
1841 $columnName = $field->column_name
;
1846 WHERE $columnName is not null
1848 $count = CRM_Core_DAO
::singleValueQuery($query);
1852 FROM civicrm_custom_group
1853 WHERE id IN ( %1, %2 )
1856 1 => [$oldGroup->id
, 'Integer'],
1857 2 => [$newGroup->id
, 'Integer'],
1860 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1862 while ($dao->fetch()) {
1863 $extends[] = $dao->extends;
1865 if ($extends[0] != $extends[1]) {
1866 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1870 return empty($errors) ?
TRUE : $errors;
1874 * Move a custom data field from one group (table) to another.
1876 * @param int $fieldID
1877 * FK to civicrm_custom_field.
1878 * @param int $newGroupID
1879 * FK to civicrm_custom_group.
1881 * @throws CRM_Core_Exception
1883 public static function moveField($fieldID, $newGroupID) {
1884 $validation = self
::_moveFieldValidate($fieldID, $newGroupID);
1885 if (TRUE !== $validation) {
1886 throw new CRM_Core_Exception(implode(' ', $validation));
1888 $field = new CRM_Core_DAO_CustomField();
1889 $field->id
= $fieldID;
1892 $newGroup = new CRM_Core_DAO_CustomGroup();
1893 $newGroup->id
= $newGroupID;
1894 $newGroup->find(TRUE);
1896 $oldGroup = new CRM_Core_DAO_CustomGroup();
1897 $oldGroup->id
= $field->custom_group_id
;
1898 $oldGroup->find(TRUE);
1901 $add->custom_group_id
= $newGroup->id
;
1902 self
::createField($add, 'add');
1904 $sql = "INSERT INTO {$newGroup->table_name} (entity_id, `{$field->column_name}`)
1905 SELECT entity_id, `{$field->column_name}` FROM {$oldGroup->table_name}
1906 ON DUPLICATE KEY UPDATE `{$field->column_name}` = {$oldGroup->table_name}.`{$field->column_name}`
1908 CRM_Core_DAO
::executeQuery($sql);
1911 $del->custom_group_id
= $oldGroup->id
;
1912 self
::createField($del, 'delete');
1916 CRM_Utils_System
::flushCache();
1920 * Create an option value for a custom field option group ID.
1922 * @param array $params
1923 * @param string $value
1924 * @param \CRM_Core_DAO_OptionGroup $optionGroup
1925 * @param string $index
1926 * @param string $dataType
1928 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup
$optionGroup, $index, $dataType) {
1929 if (strlen(trim($value))) {
1930 $optionValue = new CRM_Core_DAO_OptionValue();
1931 $optionValue->option_group_id
= $optionGroup->id
;
1932 $optionValue->label
= $params['option_label'][$index];
1933 $optionValue->name
= $params['option_name'][$index] ?? CRM_Utils_String
::titleToVar($params['option_label'][$index]);
1934 switch ($dataType) {
1936 $optionValue->value
= CRM_Utils_Rule
::cleanMoney($value);
1940 $optionValue->value
= intval($value);
1944 $optionValue->value
= floatval($value);
1948 $optionValue->value
= trim($value);
1951 $optionValue->weight
= $params['option_weight'][$index];
1952 $optionValue->is_active
= $params['option_status'][$index] ??
FALSE;
1953 $optionValue->description
= $params['option_description'][$index] ??
NULL;
1954 $optionValue->color
= $params['option_color'][$index] ??
NULL;
1955 $optionValue->icon
= $params['option_icon'][$index] ??
NULL;
1956 $optionValue->save();
1961 * Prepare for the create operation.
1963 * Munge params, create the option values if needed.
1965 * This could be called by a single create or a batchCreate.
1967 * @param array $params
1971 protected static function prepareCreate($params) {
1972 $op = empty($params['id']) ?
'create' : 'edit';
1973 CRM_Utils_Hook
::pre($op, 'CustomField', CRM_Utils_Array
::value('id', $params), $params);
1974 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
1975 if ($op === 'create') {
1976 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
1977 if (!isset($params['column_name'])) {
1978 // if add mode & column_name not present, calculate it.
1979 $params['column_name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 32));
1981 if (!isset($params['name'])) {
1982 $params['name'] = CRM_Utils_String
::munge($params['label'], '_', 64);
1986 $params['column_name'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
1989 $htmlType = $params['html_type'] ??
NULL;
1990 $dataType = $params['data_type'] ??
NULL;
1992 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1993 $params['date_format'] = Civi
::settings()->get('dateInputFormat');
1996 // Checkboxes are always serialized in current schema
1997 if ($htmlType == 'CheckBox') {
1998 $params['serialize'] = CRM_Core_DAO
::SERIALIZE_SEPARATOR_BOOKEND
;
2001 if (!empty($params['serialize'])) {
2002 if (isset($params['default_checkbox_option'])) {
2004 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
2005 if ($params['option_value'][$v]) {
2006 $defaultArray[] = $params['option_value'][$v];
2010 if (!empty($defaultArray)) {
2011 // also add the separator before and after the value per new convention (CRM-1604)
2012 $params['default_value'] = CRM_Utils_Array
::implodePadded($defaultArray);
2016 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
2017 $params['default_value'] = $params['option_value'][$params['default_option']];
2022 // create any option group & values if required
2023 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
2024 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
2025 //CRM-16659: if option_value then create an option group for this custom field.
2026 // An option_type of 2 would be a 'message' from the form layer not to handle
2027 // the option_values key. If not set then it is not ignored.
2028 $optionsType = (int) ($params['option_type'] ??
0);
2029 if (($optionsType !== 2 && empty($params['id']))
2030 && (empty($params['option_group_id']) && !empty($params['option_value'])
2033 // first create an option group for this custom group
2034 $optionGroup = new CRM_Core_DAO_OptionGroup();
2035 $optionGroup->name
= "{$params['column_name']}_" . date('YmdHis');
2036 $optionGroup->title
= $params['label'];
2037 $optionGroup->is_active
= 1;
2038 // Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
2039 $optionGroup->is_reserved
= 0;
2040 $optionGroup->data_type
= $dataType;
2041 $optionGroup->save();
2042 $params['option_group_id'] = $optionGroup->id
;
2043 if (!empty($params['option_value']) && is_array($params['option_value'])) {
2044 foreach ($params['option_value'] as $k => $v) {
2045 self
::createOptionValue($params, $v, $optionGroup, $k, $dataType);
2051 // Remove option group IDs from fields changed to Text html_type.
2052 if ($htmlType == 'Text') {
2053 $params['option_group_id'] = '';
2056 // check for orphan option groups
2057 if (!empty($params['option_group_id'])) {
2058 if (!empty($params['id'])) {
2059 self
::fixOptionGroups($params['id'], $params['option_group_id']);
2062 // if we do not have a default value
2063 // retrieve it from one of the other custom fields which use this option group
2064 if (empty($params['default_value'])) {
2065 //don't insert only value separator as default value, CRM-4579
2066 $defaultValue = self
::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
2068 if (!CRM_Utils_System
::isNull(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaultValue))) {
2069 $params['default_value'] = $defaultValue;
2074 // Set default textarea attributes
2075 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
2076 $params['attributes'] = 'rows=4, cols=60';
2082 * Create database entry for custom field and related option groups.
2084 * @param array $params
2086 * @return CRM_Core_DAO_CustomField
2088 protected static function createCustomFieldRecord($params) {
2089 $transaction = new CRM_Core_Transaction();
2090 $params = self
::prepareCreate($params);
2092 $customField = new CRM_Core_DAO_CustomField();
2093 $customField->copyValues($params);
2094 $customField->save();
2096 //create/drop the index when we toggle the is_searchable flag
2097 $op = empty($params['id']) ?
'add' : 'modify';
2098 if ($op !== 'modify') {
2099 if ($params['is_append_field_id_to_column_name']) {
2100 $params['column_name'] .= "_{$customField->id}";
2102 $customField->column_name
= $params['column_name'];
2103 $customField->save();
2106 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2107 $transaction->commit();
2109 // make sure all values are present in the object for further processing
2110 $customField->find(TRUE);
2112 return $customField;
2119 protected static function getChangeSerialize($params) {
2120 if (isset($params['serialize']) && !empty($params['id'])) {
2121 if ($params['serialize'] != CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'serialize')) {
2122 return (int) $params['serialize'];
2129 * Move custom data from one contact to another.
2131 * This is currently the start of a refactoring. The theory is each
2132 * entity could have a 'move' function with a DAO default one to fall back on.
2134 * At the moment this only does a small part of the process - ie deleting a file field that
2135 * is about to be overwritten. However, the goal is the whole process around the move for
2136 * custom data should be in here.
2138 * This is currently called by the merge class but it makes sense that api could
2139 * expose move actions as moving (e.g) contributions feels like a common
2140 * ask that should be handled by the form layer.
2142 * @param int $oldContactID
2143 * @param int $newContactID
2144 * @param int[] $fieldIDs
2145 * Optional list field ids to move.
2147 * @throws \CiviCRM_API3_Exception
2148 * @throws \Exception
2150 public function move($oldContactID, $newContactID, $fieldIDs) {
2151 if (empty($fieldIDs)) {
2154 $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'];
2156 foreach ($fieldIDs as $fieldID) {
2157 $return[] = 'custom_' . $fieldID;
2159 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2160 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2162 // The moveAllBelongings function has functionality to move custom fields. It doesn't work very well...
2163 // @todo handle all fields here but more immediately Country since that is broken at the moment.
2164 $fieldTypesNotHandledInMergeAttempt = ['File'];
2165 foreach ($fields as $field) {
2166 $isMultiple = !empty($field['custom_group_id.is_multiple']);
2167 if ($field['data_type'] === 'File' && !$isMultiple) {
2168 if (!empty($oldContact['custom_' . $field['id']]) && !empty($newContact['custom_' . $field['id']])) {
2169 CRM_Core_BAO_File
::deleteFileReferences($oldContact['custom_' . $field['id']], $oldContactID, $field['id']);
2171 if (!empty($oldContact['custom_' . $field['id']])) {
2172 CRM_Core_DAO
::executeQuery("
2173 UPDATE civicrm_entity_file
2174 SET entity_id = $newContactID
2175 WHERE file_id = {$oldContact['custom_' . $field['id']]}"
2179 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2180 CRM_Core_DAO
::executeQuery(
2181 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2182 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2183 ON DUPLICATE KEY UPDATE
2184 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2191 * Get the database table name and column name for a custom field.
2193 * @param int $fieldID
2194 * The fieldID of the custom field.
2195 * @param bool $force
2196 * Force the sql to be run again (primarily used for tests).
2199 * fatal is fieldID does not exists, else array of tableName, columnName
2200 * @throws \CRM_Core_Exception
2202 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2203 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2204 $cache = CRM_Utils_Cache
::singleton();
2205 $fieldValues = $cache->get($cacheKey);
2206 if (empty($fieldValues) ||
$force) {
2208 SELECT cg.table_name, cf.column_name, cg.id
2209 FROM civicrm_custom_group cg,
2210 civicrm_custom_field cf
2211 WHERE cf.custom_group_id = cg.id
2213 $params = [1 => [$fieldID, 'Integer']];
2214 $dao = CRM_Core_DAO
::executeQuery($query, $params);
2216 if (!$dao->fetch()) {
2217 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2219 $fieldValues = [$dao->table_name
, $dao->column_name
, $dao->id
];
2220 $cache->set($cacheKey, $fieldValues);
2222 return $fieldValues;
2226 * Get custom option groups.
2228 * @deprecated Use the API OptionGroup.get
2230 * @param array $includeFieldIds
2231 * Ids of custom fields for which option groups must be included.
2233 * Currently this is required in the cases where option groups are to be included
2234 * for inactive fields : CRM-5369
2238 public static function customOptionGroup($includeFieldIds = NULL) {
2239 static $customOptionGroup = NULL;
2241 $cacheKey = (empty($includeFieldIds)) ?
'onlyActive' : 'force';
2242 if ($cacheKey == 'force') {
2243 $customOptionGroup[$cacheKey] = NULL;
2246 if (empty($customOptionGroup[$cacheKey])) {
2247 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2249 //support for single as well as array format.
2250 if (!empty($includeFieldIds)) {
2251 if (is_array($includeFieldIds)) {
2252 $includeFieldIds = implode(',', $includeFieldIds);
2254 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2258 SELECT g.id, g.title
2259 FROM civicrm_option_group g
2260 INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2261 WHERE {$whereClause}";
2263 $dao = CRM_Core_DAO
::executeQuery($query);
2264 while ($dao->fetch()) {
2265 $customOptionGroup[$cacheKey][$dao->id
] = $dao->title
;
2269 return $customOptionGroup[$cacheKey];
2273 * Get defaults for new entity.
2277 public static function getDefaults() {
2279 'is_required' => FALSE,
2280 'is_searchable' => FALSE,
2281 'in_selector' => FALSE,
2282 'is_search_range' => FALSE,
2283 //CRM-15792 - Custom field gets disabled if is_active not set
2284 // this would ideally be a mysql default.
2285 'is_active' => TRUE,
2291 * Fix orphan groups.
2293 * @param int $customFieldId
2295 * @param int $optionGroupId
2298 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2299 // check if option group belongs to any custom Field else delete
2300 // get the current option group
2301 $currentOptionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
2305 // get the updated option group
2306 // if both are same return
2307 if (!$currentOptionGroupId ||
$currentOptionGroupId == $optionGroupId) {
2311 // check if option group is related to any other field
2312 self
::checkOptionGroup($currentOptionGroupId);
2316 * Check if option group is related to more than one custom field.
2318 * @param int $optionGroupId
2321 public static function checkOptionGroup($optionGroupId) {
2324 FROM civicrm_custom_field
2325 WHERE option_group_id = {$optionGroupId}";
2327 $count = CRM_Core_DAO
::singleValueQuery($query);
2330 //delete the option group
2331 CRM_Core_BAO_OptionGroup
::del($optionGroupId);
2336 * Get option group default.
2338 * @param int $optionGroupId
2339 * @param bool $serialize
2341 * @return null|string
2343 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2345 SELECT default_value, serialize
2346 FROM civicrm_custom_field
2347 WHERE option_group_id = {$optionGroupId}
2348 AND default_value IS NOT NULL";
2350 $dao = CRM_Core_DAO
::executeQuery($query);
2351 while ($dao->fetch()) {
2352 if ($dao->serialize
== $serialize) {
2353 return $dao->default_value
;
2355 $defaultValue = $dao->default_value
;
2358 // Convert serialization
2359 if (isset($defaultValue) && $serialize) {
2360 return CRM_Utils_Array
::implodePadded([$defaultValue]);
2362 elseif (isset($defaultValue)) {
2363 return CRM_Utils_Array
::explodePadded($defaultValue)[0];
2369 * Post process function.
2371 * @param array $params
2372 * @param int $entityID
2373 * @param string $customFieldExtends
2374 * @param bool $inline
2375 * @param bool $checkPermissions
2379 public static function postProcess(
2382 $customFieldExtends,
2384 $checkPermissions = TRUE
2388 foreach ($params as $key => $value) {
2389 if ($customFieldInfo = CRM_Core_BAO_CustomField
::getKeyID($key, TRUE)) {
2391 // for autocomplete transfer hidden value instead of label
2392 if ($params[$key] && isset($params[$key . '_id'])) {
2393 $value = $params[$key . '_id'];
2396 // we need to append time with date
2397 if ($params[$key] && isset($params[$key . '_time'])) {
2398 $value .= ' ' . $params[$key . '_time'];
2401 CRM_Core_BAO_CustomField
::formatCustomField($customFieldInfo[0],
2404 $customFieldExtends,
2405 $customFieldInfo[1],
2416 * Get custom field ID from field/group name/title.
2418 * @param string $fieldName Field name or label
2419 * @param string|null $groupName (Optional) Group name or label
2420 * @param bool $fullString Whether to return "custom_123" or "123"
2422 * @return string|int|null
2423 * @throws \CiviCRM_API3_Exception
2425 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2426 $cacheKey = $groupName . '.' . $fieldName;
2427 if (!isset(Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2428 $customFieldParams = [
2429 'name' => $fieldName,
2430 'label' => $fieldName,
2431 'options' => ['or' => [["name", "label"]]],
2435 $customFieldParams['custom_group_id.name'] = $groupName;
2436 $customFieldParams['custom_group_id.title'] = $groupName;
2437 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2440 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2442 if (empty($field['id'])) {
2443 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2444 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2447 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2448 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2453 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2455 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
2459 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
2465 public static function getNameFromID($ids) {
2466 if (is_array($ids)) {
2467 $ids = implode(',', $ids);
2470 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2471 FROM civicrm_custom_field f
2472 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2473 WHERE f.id IN ($ids)";
2475 $dao = CRM_Core_DAO
::executeQuery($sql);
2477 while ($dao->fetch()) {
2478 $result[$dao->id
] = [
2479 'field_name' => $dao->field_name
,
2480 'field_label' => $dao->field_label
,
2481 'group_name' => $dao->group_name
,
2482 'group_title' => $dao->group_title
,
2489 * Validate custom data.
2491 * @param array $params
2492 * Custom data submitted.
2493 * ie array( 'custom_1' => 'validate me' );
2496 * validation errors.
2498 public static function validateCustomData($params) {
2500 if (!is_array($params) ||
empty($params)) {
2504 //pick up profile fields.
2505 $profileFields = [];
2506 $ufGroupId = $params['ufGroupId'] ??
NULL;
2508 $profileFields = CRM_Core_BAO_UFGroup
::getFields($ufGroupId,
2510 CRM_Core_Action
::VIEW
2514 //lets start w/ params.
2515 foreach ($params as $key => $value) {
2516 $customFieldID = self
::getKeyID($key);
2517 if (!$customFieldID) {
2521 //load the structural info for given field.
2522 $field = new CRM_Core_DAO_CustomField();
2523 $field->id
= $customFieldID;
2524 if (!$field->find(TRUE)) {
2527 $dataType = $field->data_type
;
2529 $profileField = CRM_Utils_Array
::value($key, $profileFields, []);
2530 $fieldTitle = $profileField['title'] ??
NULL;
2531 $isRequired = $profileField['is_required'] ??
NULL;
2533 $fieldTitle = $field->label
;
2536 //no need to validate.
2537 if (CRM_Utils_System
::isNull($value) && !$isRequired) {
2541 //lets validate first for required field.
2542 if ($isRequired && CRM_Utils_System
::isNull($value)) {
2543 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
2547 //now time to take care of custom field form rules.
2548 $ruleName = $errorMsg = NULL;
2549 switch ($dataType) {
2551 $ruleName = 'integer';
2552 $errorMsg = ts('%1 must be an integer (whole number).',
2553 array(1 => $fieldTitle)
2558 $ruleName = 'money';
2559 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2560 array(1 => $fieldTitle)
2565 $ruleName = 'numeric';
2566 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2567 array(1 => $fieldTitle)
2572 $ruleName = 'wikiURL';
2573 $errorMsg = ts('%1 must be valid Website.',
2574 array(1 => $fieldTitle)
2579 if ($ruleName && !CRM_Utils_System
::isNull($value)) {
2581 $funName = "CRM_Utils_Rule::{$ruleName}";
2582 if (is_callable($funName)) {
2583 $valid = call_user_func($funName, $value);
2586 $errors[$key] = $errorMsg;
2595 * Is this field a multi record field.
2597 * @param int $customId
2601 public static function isMultiRecordField($customId) {
2602 $isMultipleWithGid = FALSE;
2603 if (!is_numeric($customId)) {
2604 $customId = self
::getKeyID($customId);
2606 if (is_numeric($customId)) {
2607 $sql = "SELECT cg.id cgId
2608 FROM civicrm_custom_group cg
2609 INNER JOIN civicrm_custom_field cf
2610 ON cg.id = cf.custom_group_id
2611 WHERE cf.id = %1 AND cg.is_multiple = 1";
2612 $params[1] = [$customId, 'Integer'];
2613 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2614 if ($dao->fetch()) {
2616 $isMultipleWithGid = $dao->cgId
;
2621 return $isMultipleWithGid;
2625 * Does this field type have any select options?
2627 * @param array $field
2631 public static function hasOptions($field) {
2632 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2633 $field = (array) $field;
2634 // This will include boolean fields with Yes/No options.
2635 if (in_array($field['html_type'], ['Radio', 'CheckBox'])) {
2638 // Do this before the "Select" string search because date fields have a "Select Date" html_type
2639 // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list.
2640 if (in_array($field['data_type'], ['ContactReference', 'Date'])) {
2643 if (strpos($field['html_type'], 'Select') !== FALSE) {
2646 return !empty($field['option_group_id']);
2650 * Does this field store a serialized string?
2652 * @param CRM_Core_DAO_CustomField|array $field
2656 public static function isSerialized($field) {
2657 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2658 $html_type = is_object($field) ?
$field->html_type
: $field['html_type'];
2659 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2660 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2661 if ($html_type === 'CheckBox' ||
strpos($html_type, 'Multi') !== FALSE) {
2664 // Otherwise this is the new standard as of 5.27
2665 return is_object($field) ?
!empty($field->serialize
) : !empty($field['serialize']);
2669 * Get api entity for this field
2673 public function getEntity() {
2674 $entity = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id
, 'extends');
2675 return in_array($entity, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE) ?
'Contact' : $entity;
2679 * Set pseudoconstant properties for field metadata.
2681 * @param array $field
2682 * @param string|null $optionGroupName
2684 private static function getOptionsForField(&$field, $optionGroupName) {
2685 if ($optionGroupName) {
2686 $field['pseudoconstant'] = [
2687 'optionGroupName' => $optionGroupName,
2688 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2691 elseif ($field['data_type'] == 'Boolean') {
2692 $field['pseudoconstant'] = [
2693 'callback' => 'CRM_Core_SelectValues::boolean',
2696 elseif ($field['data_type'] == 'Country') {
2697 $field['pseudoconstant'] = [
2698 'table' => 'civicrm_country',
2699 'keyColumn' => 'id',
2700 'labelColumn' => 'name',
2701 'nameColumn' => 'iso_code',
2704 elseif ($field['data_type'] == 'StateProvince') {
2705 $field['pseudoconstant'] = [
2706 'table' => 'civicrm_state_province',
2707 'keyColumn' => 'id',
2708 'labelColumn' => 'name',
2714 * Prepare params for the create operation.
2716 * @param CRM_Core_DAO_CustomField $field
2717 * @param string $operation
2721 * @throws \CRM_Core_Exception
2723 protected static function prepareCreateParams($field, $operation): array {
2724 $tableName = CRM_Core_DAO
::getFieldValue(
2725 'CRM_Core_DAO_CustomGroup',
2726 $field->custom_group_id
,
2731 'table_name' => $tableName,
2732 'operation' => $operation,
2733 'name' => $field->column_name
,
2734 'type' => CRM_Core_BAO_CustomValueTable
::fieldToSQLType(
2738 'required' => $field->is_required
,
2739 'searchable' => $field->is_searchable
,
2742 // For adding/dropping FK constraints
2743 $params['fkName'] = CRM_Core_BAO_SchemaHandler
::getIndexName($tableName, $field->column_name
);
2746 'Country' => 'civicrm_country',
2747 'StateProvince' => 'civicrm_state_province',
2748 'ContactReference' => 'civicrm_contact',
2749 'File' => 'civicrm_file',
2751 if (isset($fkFields[$field->data_type
])) {
2752 // Serialized fields store value-separated strings which are incompatible with FK constraints
2753 if (!$field->serialize
) {
2754 $params['fk_table_name'] = $fkFields[$field->data_type
];
2755 $params['fk_field_name'] = 'id';
2756 $params['fk_attributes'] = 'ON DELETE SET NULL';
2759 if ($field->serialize
) {
2760 $params['type'] = 'varchar(255)';
2762 if (isset($field->default_value
)) {
2763 $params['default'] = "'{$field->default_value}'";