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() {
40 'name' => 'Alphanumeric',
41 'label' => ts('Alphanumeric'),
46 'label' => ts('Integer'),
51 'label' => ts('Number'),
56 'label' => ts('Money'),
61 'label' => ts('Note'),
66 'label' => ts('Date'),
70 'name' => 'Yes or No',
71 'label' => ts('Yes or No'),
74 'id' => 'StateProvince',
75 'name' => 'State/Province',
76 'label' => ts('State/Province'),
81 'label' => ts('Country'),
86 'label' => ts('File'),
91 'label' => ts('Link'),
94 'id' => 'ContactReference',
95 'name' => 'Contact Reference',
96 'label' => ts('Contact Reference'),
102 * Build the map of custom field's data types and there respective Util type
105 * Data data-type => CRM_Utils_Type
107 public static function dataToType() {
109 'String' => CRM_Utils_Type
::T_STRING
,
110 'Int' => CRM_Utils_Type
::T_INT
,
111 'Money' => CRM_Utils_Type
::T_MONEY
,
112 'Memo' => CRM_Utils_Type
::T_LONGTEXT
,
113 'Float' => CRM_Utils_Type
::T_FLOAT
,
114 'Date' => CRM_Utils_Type
::T_DATE
,
115 'DateTime' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
116 'Boolean' => CRM_Utils_Type
::T_BOOLEAN
,
117 'StateProvince' => CRM_Utils_Type
::T_INT
,
118 'File' => CRM_Utils_Type
::T_STRING
,
119 'Link' => CRM_Utils_Type
::T_STRING
,
120 'ContactReference' => CRM_Utils_Type
::T_INT
,
121 'Country' => CRM_Utils_Type
::T_INT
,
126 * Deprecated in favor of writeRecords & APIv4
129 * @param array $params
130 * @return CRM_Core_DAO_CustomField
132 public static function create($params) {
133 $changeSerialize = self
::getChangeSerialize($params);
134 $customField = self
::createCustomFieldRecord($params);
135 // When deserializing a field, the update needs to run before the schema change
136 if ($changeSerialize === 0) {
137 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
139 $op = empty($params['id']) ?
'add' : 'modify';
140 self
::createField($customField, $op);
141 // When serializing a field, the update needs to run after the schema change
142 if ($changeSerialize === 1) {
143 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
146 CRM_Utils_Hook
::post(($op === 'add' ?
'create' : 'edit'), 'CustomField', $customField->id
, $customField);
148 CRM_Utils_System
::flushCache();
149 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
150 // so do it manually. Ideally we wouldn't need to clear others...
151 Civi
::cache('metadata')->clear();
157 * Save multiple fields, now deprecated in favor of self::writeRecords.
158 * https://lab.civicrm.org/dev/core/issues/1093
161 * @param array $bulkParams
162 * Array of arrays as would be passed into create
163 * @param array $defaults
164 * Default parameters to be be merged into each of the params.
166 * @throws \CiviCRM_API3_Exception
168 public static function bulkSave($bulkParams, $defaults = []) {
169 CRM_Core_Error
::deprecatedFunctionWarning(__CLASS__
. '::writeRecords');
170 foreach ($bulkParams as $index => $fieldParams) {
171 $bulkParams[$index] = array_merge($defaults, $fieldParams);
173 self
::writeRecords($bulkParams);
177 * Create/update several fields at once in a mysql efficient way.
179 * @param array $records
180 * @return CRM_Core_DAO_CustomField[]
181 * @throws CRM_Core_Exception
183 public static function writeRecords(array $records): array {
184 $addedColumns = $sql = $customFields = $pre = $post = [];
185 foreach ($records as $index => $params) {
186 CRM_Utils_Hook
::pre(empty($params['id']) ?
'create' : 'edit', 'CustomField', $params['id'] ??
NULL, $params);
188 $changeSerialize = self
::getChangeSerialize($params);
189 $customField = self
::createCustomFieldRecord($params);
190 // Serialize/deserialize sql must run after/before the table is altered
191 if ($changeSerialize === 0) {
192 $pre[] = self
::getAlterSerializeSQL($customField);
194 if ($changeSerialize === 1) {
195 $post[] = self
::getAlterSerializeSQL($customField);
197 $fieldSQL = self
::getAlterFieldSQL($customField, empty($params['id']) ?
'add' : 'modify');
199 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id
, 'table_name');
200 $sql[$tableName][] = $fieldSQL;
201 $addedColumns[$tableName][] = $customField->column_name
;
202 $customFields[$index] = $customField;
205 foreach ($pre as $query) {
206 CRM_Core_DAO
::executeQuery($query);
209 foreach ($sql as $tableName => $statements) {
210 // CRM-7007: do not i18n-rewrite this query
211 CRM_Core_DAO
::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE);
213 if (CRM_Core_Config
::singleton()->logging
) {
214 $logging = new CRM_Logging_Schema();
215 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
218 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
221 foreach ($post as $query) {
222 CRM_Core_DAO
::executeQuery($query);
225 CRM_Utils_System
::flushCache();
226 Civi
::cache('metadata')->clear();
228 foreach ($customFields as $index => $customField) {
229 $op = empty($records[$index]['id']) ?
'create' : 'edit';
230 // Theoretically a custom field could have custom fields! Trippy...
231 if (!empty($records[$index]['custom']) && is_array($records[$index]['custom'])) {
232 CRM_Core_BAO_CustomValueTable
::store($records[$index]['custom'], static::$_tableName, $customField->id
, $op);
234 CRM_Utils_Hook
::post($op, 'CustomField', $customField->id
, $customField);
236 return $customFields;
240 * Retrieve DB object and copy to defaults array.
242 * @param array $params
243 * Array of criteria values.
244 * @param array $defaults
245 * Array to be populated with found values.
248 * The DAO object, if found.
252 public static function retrieve($params, &$defaults) {
253 return self
::commonRetrieve(self
::class, $params, $defaults);
257 * Update the is_active flag in the db.
260 * Id of the database record.
261 * @param bool $is_active
262 * Value we want to set the is_active field.
265 * true if we found and updated the object, else false
267 public static function setIsActive($id, $is_active) {
269 CRM_Utils_System
::flushCache();
271 //enable-disable CustomField
272 CRM_Core_BAO_UFField
::setUFField($id, $is_active);
273 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_CustomField', $id, 'is_active', $is_active);
277 * Get the field title.
285 public static function getTitle($id) {
286 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
290 * @param string $context
293 public function getOptions($context = NULL) {
294 CRM_Core_DAO
::buildOptionsContext($context);
299 $cacheKey = "CRM_Core_BAO_CustomField_getOptions_{$this->id}_$context";
300 $cache = CRM_Utils_Cache
::singleton();
301 $options = $cache->get($cacheKey);
302 if (!isset($options)) {
303 if (!$this->data_type ||
!$this->custom_group_id
) {
307 // This will hold the list of options in format key => label
310 if (!empty($this->option_group_id
)) {
311 $options = CRM_Core_OptionGroup
::valuesByID(
312 $this->option_group_id
, FALSE, FALSE, FALSE, $context == 'validate' ?
'name' : 'label', !($context == 'validate' ||
$context == 'get')
315 elseif ($this->data_type
=== 'StateProvince') {
316 $options = CRM_Core_PseudoConstant
::stateProvince();
318 elseif ($this->data_type
=== 'Country') {
319 $options = $context == 'validate' ? CRM_Core_PseudoConstant
::countryIsoCode() : CRM_Core_PseudoConstant
::country();
321 elseif ($this->data_type
=== 'Boolean') {
322 $options = $context == 'validate' ?
[0, 1] : CRM_Core_SelectValues
::boolean();
324 CRM_Utils_Hook
::customFieldOptions($this->id
, $options, FALSE);
325 CRM_Utils_Hook
::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, ['context' => $context]);
326 $cache->set($cacheKey, $options);
334 public static function buildOptions($fieldName, $context = NULL, $props = []) {
335 $options = parent
::buildOptions($fieldName, $context, $props);
336 // This provides legacy support for APIv3, allowing no-longer-existent html types
337 if ($fieldName == 'html_type' && isset($props['version']) && $props['version'] == 3) {
338 $options['Multi-Select'] = 'Multi-Select';
339 $options['Select Country'] = 'Select Country';
340 $options['Multi-Select Country'] = 'Multi-Select Country';
341 $options['Select State/Province'] = 'Select State/Province';
342 $options['Multi-Select State/Province'] = 'Multi-Select State/Province';
348 * Store and return an array of all active custom fields.
350 * @param string $customDataType
351 * Type of Custom Data; 'ANY' is a synonym for "all contact data types".
352 * @param bool $showAll
353 * If true returns all fields (includes disabled fields).
354 * @param bool $inline
355 * If true returns all inline fields (includes disabled fields).
356 * @param int $customDataSubType
357 * Custom Data sub type value.
358 * @param int $customDataSubName
359 * Custom Data sub name value.
360 * @param bool $onlyParent
361 * Return only top level custom data, for eg, only Participant and ignore subname and subtype.
362 * @param bool $onlySubType
363 * Return only custom data for subtype.
364 * @param bool|int $checkPermission
365 * Either a CRM_Core_Permission constant or FALSE to disable checks
368 * an array of active custom fields.
369 * @throws \CRM_Core_Exception
371 public static function &getFields(
372 $customDataType = 'Individual',
375 $customDataSubType = NULL,
376 $customDataSubName = NULL,
378 $onlySubType = FALSE,
379 $checkPermission = CRM_Core_Permission
::EDIT
381 if ($checkPermission === TRUE) {
382 CRM_Core_Error
::deprecatedWarning('Unexpected TRUE passed to CustomField::getFields $checkPermission param.');
383 $checkPermission = CRM_Core_Permission
::EDIT
;
385 if (empty($customDataType)) {
386 $customDataType = array_merge(['Contact'], CRM_Contact_BAO_ContactType
::basicTypes());
388 if ($customDataType === 'ANY') {
389 // NULL should have been respected but the line above broke that.
390 // boo for us not having enough unit tests back them.
391 $customDataType = NULL;
393 if ($customDataType && !is_array($customDataType)) {
395 if (in_array($customDataType, CRM_Contact_BAO_ContactType
::subTypes(), TRUE)) {
396 // This is the case when getFieldsForImport() requires fields
397 // limited strictly to a subtype.
398 $customDataSubType = $customDataType;
399 $customDataType = CRM_Contact_BAO_ContactType
::getBasicType($customDataType);
403 if (array_key_exists($customDataType, CRM_Core_SelectValues
::customGroupExtends())) {
404 // this makes the method flexible to support retrieving fields
405 // for multiple extends value.
406 $customDataType = [$customDataType];
410 $customDataSubType = CRM_Utils_Array
::explodePadded($customDataSubType);
412 if (is_array($customDataType)) {
413 $cacheKey = implode('_', $customDataType);
416 $cacheKey = $customDataType;
419 $cacheKey .= !empty($customDataSubType) ?
('_' . implode('_', $customDataSubType)) : '_0';
420 $cacheKey .= $customDataSubName ?
"{$customDataSubName}_" : '_0';
421 $cacheKey .= $showAll ?
'_1' : '_0';
422 $cacheKey .= $inline ?
'_1_' : '_0_';
423 $cacheKey .= $onlyParent ?
'_1_' : '_0_';
424 $cacheKey .= $onlySubType ?
'_1_' : '_0_';
425 $cacheKey .= $checkPermission ?
$checkPermission . CRM_Core_Session
::getLoggedInContactID() . '_' : '_0_0_';
426 $cacheKey .= '_' . CRM_Core_Config
::domainID() . '_';
428 $cgTable = CRM_Core_DAO_CustomGroup
::getTableName();
430 // also get the permission stuff here
431 if ($checkPermission) {
432 $permissionClause = CRM_Core_Permission
::customGroupClause($checkPermission,
437 $permissionClause = '(1)';
440 // lets md5 permission clause and take first 8 characters
441 $cacheKey .= substr(md5($permissionClause), 0, 8);
443 if (strlen($cacheKey) > 40) {
444 $cacheKey = md5($cacheKey);
447 if (!self
::$_importFields ||
448 CRM_Utils_Array
::value($cacheKey, self
::$_importFields) === NULL
450 if (!self
::$_importFields) {
451 self
::$_importFields = [];
454 // check if we can retrieve from database cache
455 $fields = Civi
::Cache('fields')->get("custom importableFields $cacheKey");
457 if ($fields === NULL) {
460 if (is_array($customDataType)) {
462 foreach ($customDataType as $dataType) {
463 if (array_key_exists($dataType, CRM_Core_SelectValues
::customGroupExtends())) {
464 if (in_array($dataType, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
465 $val = "'" . CRM_Utils_Type
::escape($dataType, 'String') . "', 'Contact' ";
468 $val = "'" . CRM_Utils_Type
::escape($dataType, 'String') . "'";
470 $value = $value ?
$value . ", {$val}" : $val;
474 $extends = "AND $cgTable.extends IN ( $value ) ";
478 if (!empty($customDataType) && empty($extends)) {
479 // $customDataType specified a filter, but there is no corresponding SQL ($extends)
480 self
::$_importFields[$cacheKey] = [];
481 return self
::$_importFields[$cacheKey];
485 $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL ";
487 // Temporary hack - in 5.27 a new field is added to civicrm_custom_field. There is a high
488 // risk this function is called before the upgrade page can be reached and if
489 // so it will potentially result in fatal error.
490 $serializeField = CRM_Core_BAO_Domain
::isDBVersionAtLeast('5.27.alpha1') ?
"custom_field.serialize," : '';
492 $query = "SELECT custom_field.id, custom_field.label,
494 custom_field.data_type,
495 custom_field.html_type,
496 custom_field.default_value,
497 custom_field.options_per_line, custom_field.text_length,
498 custom_field.custom_group_id,
499 custom_field.is_required,
500 custom_field.column_name,
501 $cgTable.extends, custom_field.is_search_range,
502 $cgTable.extends_entity_column_value,
503 $cgTable.extends_entity_column_id,
504 custom_field.is_view,
505 custom_field.option_group_id,
506 custom_field.date_format,
507 custom_field.time_format,
508 $cgTable.is_multiple,
511 og.name as option_group_name
512 FROM civicrm_custom_field custom_field
514 ON custom_field.custom_group_id = $cgTable.id
515 LEFT JOIN civicrm_option_group og
516 ON custom_field.option_group_id = og.id
520 $query .= " AND custom_field.is_active = 1 AND $cgTable.is_active = 1 ";
524 $query .= " AND $cgTable.style = 'Inline' ";
527 //get the custom fields for specific type in
528 //combination with fields those support any type.
529 if (!empty($customDataSubType)) {
531 foreach ($customDataSubType as $subtype) {
532 $subtype = CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Utils_Type
::escape($subtype, 'String') . CRM_Core_DAO
::VALUE_SEPARATOR
;
533 $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'";
536 $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL";
538 $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )";
541 if ($customDataSubName) {
542 $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) ";
545 // also get the permission stuff here
546 if ($checkPermission) {
547 $permissionClause = CRM_Core_Permission
::customGroupClause($checkPermission,
552 $permissionClause = '(1)';
555 $query .= " $extends AND $permissionClause
556 ORDER BY $cgTable.weight, $cgTable.title,
557 custom_field.weight, custom_field.label";
559 $dao = CRM_Core_DAO
::executeQuery($query);
562 while (($dao->fetch()) != NULL) {
563 $regexp = preg_replace('/[.,;:!?]/', '', '');
564 $fields[$dao->id
]['id'] = $dao->id
;
565 $fields[$dao->id
]['label'] = $dao->label
;
566 // This seems broken, but not in a new way.
567 $fields[$dao->id
]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
568 // To support the consolidation of various functions & their expectations.
569 $fields[$dao->id
]['title'] = $dao->label
;
570 $fields[$dao->id
]['custom_field_id'] = $dao->id
;
571 $fields[$dao->id
]['groupTitle'] = $dao->title
;
572 $fields[$dao->id
]['data_type'] = $dao->data_type
;
573 $fields[$dao->id
]['name'] = 'custom_' . $dao->id
;
574 $fields[$dao->id
]['type'] = CRM_Utils_Array
::value($dao->data_type
, self
::dataToType());
575 $fields[$dao->id
]['html_type'] = $dao->html_type
;
576 $fields[$dao->id
]['default_value'] = $dao->default_value
;
577 $fields[$dao->id
]['text_length'] = $dao->text_length
;
578 $fields[$dao->id
]['options_per_line'] = $dao->options_per_line
;
579 $fields[$dao->id
]['custom_group_id'] = $dao->custom_group_id
;
580 $fields[$dao->id
]['extends'] = $dao->extends;
581 $fields[$dao->id
]['is_search_range'] = $dao->is_search_range
;
582 $fields[$dao->id
]['extends_entity_column_value'] = $dao->extends_entity_column_value
;
583 $fields[$dao->id
]['extends_entity_column_id'] = $dao->extends_entity_column_id
;
584 $fields[$dao->id
]['is_view'] = $dao->is_view
;
585 $fields[$dao->id
]['is_multiple'] = $dao->is_multiple
;
586 $fields[$dao->id
]['option_group_id'] = $dao->option_group_id
;
587 $fields[$dao->id
]['date_format'] = $dao->date_format
;
588 $fields[$dao->id
]['time_format'] = $dao->time_format
;
589 $fields[$dao->id
]['is_required'] = $dao->is_required
;
590 $fields[$dao->id
]['table_name'] = $dao->table_name
;
591 $fields[$dao->id
]['column_name'] = $dao->column_name
;
592 $fields[$dao->id
]['serialize'] = $serializeField ?
$dao->serialize
: (int) self
::isSerialized($dao);
593 $fields[$dao->id
]['where'] = $dao->table_name
. '.' . $dao->column_name
;
594 // Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
595 $fields[$dao->id
]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery
::$extendsMap) ? CRM_Core_BAO_CustomQuery
::$extendsMap[$dao->extends] : '';
596 if (in_array($dao->extends, CRM_Contact_BAO_ContactType
::subTypes())) {
597 // if $extends is a subtype, refer contact table
598 $fields[$dao->id
]['extends_table'] = 'civicrm_contact';
600 // Search table is used by query object searches..
601 $fields[$dao->id
]['search_table'] = ($fields[$dao->id
]['extends_table'] == 'civicrm_contact') ?
'contact_a' : $fields[$dao->id
]['extends_table'];
602 self
::getOptionsForField($fields[$dao->id
], $dao->option_group_name
);
605 Civi
::cache('fields')->set("custom importableFields $cacheKey", $fields);
607 self
::$_importFields[$cacheKey] = $fields;
610 return self
::$_importFields[$cacheKey];
614 * Return field ids and names (with groups).
616 * NOTE: Despite this function's name, it is used both for IMPORT and EXPORT.
617 * The $checkPermission variable should be set to VIEW for export and EDIT for import.
619 * @param int|string $contactType Contact type
620 * @param bool $showAll
621 * If true returns all fields (includes disabled fields).
622 * @param bool $onlyParent
623 * Return fields ONLY related to basic types.
624 * @param bool $search
625 * When called from search and multiple records need to be returned.
626 * @param bool|int $checkPermission
627 * Either a CRM_Core_Permission constant or FALSE to disable checks
629 * @param bool $withMultiple
633 public static function getFieldsForImport(
634 $contactType = 'Individual',
638 $checkPermission = TRUE,
639 $withMultiple = FALSE
641 if ($checkPermission === TRUE) {
642 // TODO: Trigger deprecation notice for passing TRUE
643 $checkPermission = CRM_Core_Permission
::EDIT
;
645 // Note: there are situations when we want getFieldsForImport() return fields related
646 // ONLY to basic contact types, but NOT subtypes. And thats where $onlyParent is helpful
647 $fields = &self
::getFields($contactType,
657 $importableFields = [];
658 foreach ($fields as $id => $values) {
659 // for now we should not allow multiple fields in profile / export etc, hence unsetting
661 (!empty($values['is_multiple']) && !$withMultiple)
665 if (!empty($values['text_length'])) {
666 $values['maxlength'] = (int) $values['text_length'];
669 /* generate the key for the fields array */
672 $importableFields[$key] = $values;
673 $importableFields[$key]['import'] = 1;
676 return $importableFields;
680 * Get the field id from an import key.
690 public static function getKeyID($key, $all = FALSE) {
692 if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) {
699 CRM_Utils_Array
::value(2, $match),
703 return $all ?
[NULL, NULL] : NULL;
707 * Use the cache to get all values of a specific custom field.
709 * @param int $fieldID
710 * The custom field ID.
712 * @return CRM_Core_BAO_CustomField
714 * @throws CRM_Core_Exception
716 public static function getFieldObject($fieldID) {
717 $field = new CRM_Core_BAO_CustomField();
719 // check if we can get the field values from the system cache
720 $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
721 $cache = CRM_Utils_Cache
::singleton();
722 $fieldValues = $cache->get($cacheKey);
723 if (empty($fieldValues)) {
724 $field->id
= $fieldID;
725 if (!$field->find(TRUE)) {
726 throw new CRM_Core_Exception('Cannot find Custom Field');
730 CRM_Core_DAO
::storeValues($field, $fieldValues);
732 $cache->set($cacheKey, $fieldValues);
735 $field->copyValues($fieldValues);
742 * Add a custom field to an existing form.
744 * @param CRM_Core_Form $qf
745 * Form object (reference).
746 * @param string $elementName
747 * Name of the custom field.
748 * @param int $fieldId
749 * @param bool $useRequired
750 * True if required else false.
751 * @param bool $search
752 * True if used for search else false.
753 * @param string $label
754 * Label for custom field.
755 * @return \HTML_QuickForm_Element|null
756 * @throws \CiviCRM_API3_Exception
758 public static function addQuickFormElement(
759 $qf, $elementName, $fieldId, $useRequired = TRUE, $search = FALSE, $label = NULL
761 $field = self
::getFieldObject($fieldId);
762 $widget = $field->html_type
;
764 $customFieldAttributes = [];
766 if (!isset($label)) {
767 $label = $field->label
;
770 // DAO stores attributes as a string, but it's hard to manipulate and
771 // CRM_Core_Form::add() wants them as an array.
772 $fieldAttributes = self
::attributesFromString($field->attributes ??
'');
774 // Custom field HTML should indicate group+field name
775 $groupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
);
776 $fieldAttributes['data-crm-custom'] = $groupName . ':' . $field->name
;
778 // Fixed for Issue CRM-2183
779 if ($widget == 'TextArea' && $search) {
783 $placeholder = $search ?
ts('- any %1 -', [1 => $label]) : ts('- select %1 -', [1 => $label]);
785 if (in_array($widget, [
790 $options = $field->getOptions($search ?
'search' : 'create');
792 // Consolidate widget types to simplify the below switch statement
797 // Search field is always multi-select
798 if ($search ||
(self
::isSerialized($field) && $widget === 'Select')) {
799 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' huge');
800 $fieldAttributes['multiple'] = 'multiple';
801 $fieldAttributes['placeholder'] = $placeholder;
804 // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
805 $canEditOptions = CRM_Core_Permission
::check('administer CiviCRM');
806 if ($field->option_group_id
&& !$search && $canEditOptions) {
807 $customFieldAttributes +
= [
808 'data-api-entity' => $field->getEntity(),
809 'data-api-field' => 'custom_' . $field->id
,
810 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id
),
812 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
816 $rangeDataTypes = ['Int', 'Float', 'Money'];
818 // at some point in time we might want to split the below into small functions
823 if ($field->is_search_range
&& $search && in_array($field->data_type
, $rangeDataTypes)) {
824 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
825 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
828 if ($field->text_length
) {
829 $fieldAttributes['maxlength'] = $field->text_length
;
830 if ($field->text_length
< 20) {
831 $fieldAttributes['size'] = $field->text_length
;
834 $element = $qf->add('text', $elementName, $label,
836 $useRequired && !$search
842 $fieldAttributes['rows'] = $field->note_rows ??
4;
843 $fieldAttributes['cols'] = $field->note_columns ??
60;
845 if ($field->text_length
) {
846 $fieldAttributes['maxlength'] = $field->text_length
;
848 $element = $qf->add('textarea',
852 $useRequired && !$search
857 //CRM-18379: Fix for date range of 'Select Date' custom field when include in profile.
858 $minYear = isset($field->start_date_years
) ?
(date('Y') - $field->start_date_years
) : NULL;
859 $maxYear = isset($field->end_date_years
) ?
(date('Y') +
$field->end_date_years
) : NULL;
862 'date' => $field->date_format
,
863 'minDate' => isset($minYear) ?
$minYear . '-01-01' : NULL,
864 //CRM-18487 - max date should be the last date of the year.
865 'maxDate' => isset($maxYear) ?
$maxYear . '-12-31' : NULL,
866 'time' => $field->time_format ?
$field->time_format
* 12 : FALSE,
868 if ($field->is_search_range
&& $search) {
869 $qf->add('datepicker', $elementName . '_from', $label, $fieldAttributes +
array('placeholder' => ts('From')), FALSE, $params);
870 $qf->add('datepicker', $elementName . '_to', NULL, $fieldAttributes +
array('placeholder' => ts('To')), FALSE, $params);
873 $element = $qf->add('datepicker', $elementName, $label, $fieldAttributes, $useRequired && !$search, $params);
878 if ($field->is_search_range
&& $search && in_array($field->data_type
, $rangeDataTypes)) {
879 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
880 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
883 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
884 if ($search ||
empty($useRequired)) {
885 $fieldAttributes['allowClear'] = TRUE;
887 $qf->addRadio($elementName, $label, $options, $fieldAttributes, NULL, $useRequired);
891 // For all select elements
893 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' crm-select2');
895 if (empty($fieldAttributes['multiple'])) {
896 $options = ['' => $placeholder] +
$options;
898 $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $fieldAttributes);
900 // Add and/or option for fields that store multiple values
901 if ($search && self
::isSerialized($field)) {
902 $qf->addRadio($elementName . '_operator', '', [
905 ], [], NULL, FALSE, [
906 'or' => ['title' => ts('Results may contain any of the selected options')],
907 'and' => ['title' => ts('Results must have all of the selected options')],
909 $qf->setDefaults([$elementName . '_operator' => 'or']);
915 foreach ($options as $v => $l) {
916 // TODO: I'm not sure if this is supposed to exclude whatever might be
917 // in $field->attributes (available in array format as
918 // $fieldAttributes). Leaving as-is for now.
919 $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, $customFieldAttributes);
922 $group = $element = $qf->addGroup($check, $elementName, $label);
923 $optionEditKey = 'data-option-edit-path';
924 if (isset($customFieldAttributes[$optionEditKey])) {
925 $group->setAttribute($optionEditKey, $customFieldAttributes[$optionEditKey]);
928 if ($useRequired && !$search) {
929 $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
934 // we should not build upload file in search mode
939 strtolower($field->html_type
),
943 $useRequired && !$search
945 $qf->addUploadElement($elementName);
948 case 'RichTextEditor':
949 $fieldAttributes['rows'] = $field->note_rows
;
950 $fieldAttributes['cols'] = $field->note_columns
;
951 if ($field->text_length
) {
952 $fieldAttributes['maxlength'] = $field->text_length
;
954 $element = $qf->add('wysiwyg', $elementName, $label, $fieldAttributes, $useRequired && !$search);
957 case 'Autocomplete-Select':
958 static $customUrls = [];
959 if ($field->data_type
== 'ContactReference') {
960 // break if contact does not have permission to access ContactReference
961 if (!CRM_Core_Permission
::check('access contact reference fields')) {
964 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ??
'') . ' crm-form-contact-reference huge');
965 $fieldAttributes['data-api-entity'] = 'Contact';
966 if (!empty($field->serialize
) ||
$search) {
967 $fieldAttributes['multiple'] = TRUE;
969 $element = $qf->add('text', $elementName, $label, $fieldAttributes, $useRequired && !$search);
971 $urlParams = "context=customfield&id={$field->id}";
972 $idOfelement = $elementName;
973 // dev/core#362 if in an onbehalf profile clean up the name to get rid of square brackets that break the select 2 js
974 // However this caused regression https://lab.civicrm.org/dev/core/issues/619 so it has been hacked back to
975 // only affecting on behalf - next time someone looks at this code it should be with a view to overhauling it
976 // rather than layering on more hacks.
977 if (substr($elementName, 0, 8) === 'onbehalf' && strpos($elementName, '[') && strpos($elementName, ']')) {
978 $idOfelement = substr(substr($elementName, (strpos($elementName, '[') +
1)), 0, -1);
980 $customUrls[$idOfelement] = CRM_Utils_System
::url('civicrm/ajax/contactref',
987 // FIXME: This won't work with customFieldOptions hook
988 $fieldAttributes +
= [
989 'entity' => 'OptionValue',
990 'placeholder' => $placeholder,
991 'multiple' => $search ?
TRUE : !empty($field->serialize
),
993 'params' => ['option_group_id' => $field->option_group_id
, 'is_active' => 1],
996 $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
999 $qf->assign('customUrls', $customUrls);
1003 switch ($field->data_type
) {
1005 // integers will have numeric rule applied to them.
1006 if ($field->is_search_range
&& $search && $widget != 'Select') {
1007 $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', [1 => $label]), 'integer');
1008 $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', [1 => $label]), 'integer');
1010 elseif ($widget == 'Text') {
1011 $qf->addRule($elementName, ts('%1 must be an integer (whole number).', [1 => $label]), 'integer');
1016 if ($field->is_search_range
&& $search) {
1017 $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', [1 => $label]), 'numeric');
1018 $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', [1 => $label]), 'numeric');
1020 elseif ($widget == 'Text') {
1021 $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', [1 => $label]), 'numeric');
1026 if ($field->is_search_range
&& $search) {
1027 $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
1028 $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
1030 elseif ($widget == 'Text') {
1031 $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
1036 $element->setAttribute('class', "url");
1037 $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
1040 if ($field->is_view
&& !$search) {
1041 $qf->freeze($elementName);
1047 * Take a string of HTML element attributes and turn it into an associative
1050 * @param string $attrString
1051 * The attributes as a string, e.g. `rows=3 cols=40`.
1054 * The attributes as an array, e.g. `['rows' => 3, 'cols' => 40]`.
1056 public static function attributesFromString($attrString) {
1058 foreach (explode(' ', $attrString) as $at) {
1059 if (strpos($at, '=')) {
1060 list($k, $v) = explode('=', $at);
1061 $attributes[$k] = trim($v, ' "');
1068 * Delete the Custom Field.
1070 * @param object $field
1073 public static function deleteField($field) {
1074 CRM_Utils_System
::flushCache();
1076 // first delete the custom option group and values associated with this field
1077 if ($field->option_group_id
) {
1078 //check if option group is related to any other field, if
1079 //not delete the option group and related option values
1080 self
::checkOptionGroup($field->option_group_id
);
1082 // next drop the column from the custom value table
1083 self
::createField($field, 'delete');
1086 CRM_Core_BAO_UFField
::delUFField($field->id
);
1087 CRM_Core_BAO_Mapping
::removeFieldFromMapping('custom_' . $field->id
);
1088 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_CustomField');
1090 CRM_Utils_Hook
::post('delete', 'CustomField', $field->id
, $field);
1094 * @param string|int|array|null $value
1095 * @param CRM_Core_BAO_CustomField|int|array|string $field
1096 * @param int $entityId
1100 * @throws \CRM_Core_Exception
1102 public static function displayValue($value, $field, $entityId = NULL) {
1103 $field = is_array($field) ?
$field['id'] : $field;
1104 $fieldId = is_object($field) ?
$field->id
: (int) str_replace('custom_', '', $field);
1107 throw new CRM_Core_Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
1110 if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
1111 $field = self
::getFieldObject($fieldId);
1114 $fieldInfo = ['options' => $field->getOptions()] +
(array) $field;
1116 $displayValue = self
::formatDisplayValue($value, $fieldInfo, $entityId);
1118 // Call hook to alter display value.
1119 CRM_Utils_Hook
::alterCustomFieldDisplayValue($displayValue, $value, $entityId, $fieldInfo);
1121 return $displayValue;
1125 * Lower-level logic for rendering a custom field value
1127 * @param string|array $value
1128 * @param array $field
1129 * @param int|null $entityId
1133 private static function formatDisplayValue($value, $field, $entityId = NULL) {
1135 if (self
::isSerialized($field) && !is_array($value)) {
1136 $value = CRM_Utils_Array
::explodePadded($value);
1139 if ($field['html_type'] == 'CheckBox' && $value) {
1140 $value = CRM_Utils_Array
::convertCheckboxFormatToArray($value);
1143 $display = is_array($value) ?
implode(', ', $value) : (string) $value;
1145 switch ($field['html_type']) {
1148 case 'Autocomplete-Select':
1151 if ($field['data_type'] == 'ContactReference' && (is_array($value) ||
is_numeric($value))) {
1152 // Issue #2939 - guard against passing empty values to CRM_Core_DAO::getFieldValue(), which would throw an exception
1153 if (empty($value)) {
1158 foreach ((array) $value as $contactId) {
1159 $displayNames[] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');
1161 $display = implode(', ', $displayNames);
1164 elseif ($field['data_type'] == 'ContactReference') {
1167 elseif (is_array($value)) {
1169 foreach ($value as $key => $val) {
1170 $v[] = $field['options'][$val] ??
NULL;
1172 $display = implode(', ', $v);
1175 $display = CRM_Utils_Array
::value($value, $field['options'], '');
1176 // For float type (see Number and Money) $value would be decimal like
1177 // 1.00 (because it is stored in db as decimal), while options array
1178 // key would be integer like 1. In this case expression on line above
1179 // would return empty string (default value), despite the fact that
1180 // matching option exists in the array.
1181 // In such cases we could just get intval($value) and fetch matching
1182 // option again, but this would not work if key is float like 5.6.
1183 // So we need to truncate trailing zeros to make it work as expected.
1184 if ($display === '' && strpos(($value ??
''), '.') !== FALSE) {
1185 // Use round() to truncate trailing zeros, e.g:
1186 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1187 $value = (string) round($value, 5);
1188 $display = $field['options'][$value] ??
'';
1194 $customFormat = NULL;
1196 // FIXME: Are there any legitimate reasons why $value would be an array?
1197 // Or should we throw an exception here if it is?
1198 $value = is_array($value) ? CRM_Utils_Array
::first($value) : $value;
1200 $actualPHPFormats = CRM_Utils_Date
::datePluginToPHPFormats();
1201 $format = $field['date_format'] ??
NULL;
1204 if (array_key_exists($format, $actualPHPFormats)) {
1205 $customTimeFormat = (array) $actualPHPFormats[$format];
1206 switch (CRM_Utils_Array
::value('time_format', $field)) {
1208 $customTimeFormat[] = 'g:iA';
1212 $customTimeFormat[] = 'G:i';
1216 //If time is not selected remove time from value.
1217 $value = $value ?
date('Y-m-d', strtotime($value)) : '';
1219 $customFormat = implode(" ", $customTimeFormat);
1222 $display = CRM_Utils_Date
::processDate($value, NULL, FALSE, $customFormat);
1226 // In the context of displaying a profile, show file/image
1229 if (CRM_Utils_Rule
::positiveInteger($value)) {
1233 $fileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1235 $url = self
::getFileURL($entityId, $field['id'], $fileId);
1237 $display = $url['file_url'];
1241 // In other contexts show a paperclip icon
1242 if (CRM_Utils_Rule
::integer($value)) {
1243 $icons = CRM_Core_BAO_File
::paperIconAttachment('*', $value);
1245 $file_description = '';
1247 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1249 catch (CiviCRM_API3_Exception
$dontcare) {
1253 $display = "{$icons[$value]}{$file_description}";
1256 //CRM-18396, if filename is passed instead
1264 $display = $display ?
"<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1268 $display = nl2br($display);
1272 if ($field['data_type'] == 'Money' && isset($value)) {
1273 // $value can also be an array(while using IN operator from search builder or api).
1274 foreach ((array) $value as $val) {
1275 $disp[] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($val);
1277 $display = implode(', ', $disp);
1279 elseif ($field['data_type'] == 'Float' && isset($value)) {
1280 // $value can also be an array(while using IN operator from search builder or api).
1281 foreach ((array) $value as $val) {
1282 $disp[] = CRM_Utils_Number
::formatLocaleNumeric($val);
1284 $display = implode(', ', $disp);
1292 * Set default values for custom data used in profile.
1294 * @param int $customFieldId
1296 * @param string $elementName
1297 * Custom field name.
1298 * @param array $defaults
1299 * Associated array of fields.
1300 * @param int $contactId
1304 * @param mixed $value
1305 * If passed - dont fetch value from db,.
1306 * just format the given value
1308 public static function setProfileDefaults(
1316 //get the type of custom field
1317 $customField = new CRM_Core_BAO_CustomField();
1318 $customField->id
= $customFieldId;
1319 $customField->find(TRUE);
1322 if ($mode == CRM_Profile_Form
::MODE_CREATE
) {
1323 $value = $customField->default_value
;
1327 if (!isset($value)) {
1328 $info = self
::getTableColumnGroup($customFieldId);
1329 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1330 $result = CRM_Core_DAO
::executeQuery($query);
1331 if ($result->fetch()) {
1332 $value = $result->value
;
1336 if ($customField->data_type
== 'Country') {
1338 $config = CRM_Core_Config
::singleton();
1339 if ($config->defaultContactCountry
) {
1340 $value = CRM_Core_BAO_Country
::defaultContactCountry();
1346 //set defaults if mode is registration
1347 if (!trim($value) &&
1349 (!in_array($mode, [CRM_Profile_Form
::MODE_EDIT
, CRM_Profile_Form
::MODE_SEARCH
]))
1351 $value = $customField->default_value
;
1354 if ($customField->data_type
== 'Money' && isset($value)) {
1355 $value = number_format($value, 2);
1357 if (self
::isSerialized($customField) && $value) {
1358 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldId, FALSE);
1359 $defaults[$elementName] = [];
1360 $checkedValue = CRM_Utils_Array
::explodePadded($value);
1361 foreach ($customOption as $val) {
1362 if (in_array($val['value'], $checkedValue)) {
1363 if ($customField->html_type
== 'CheckBox') {
1364 $defaults[$elementName][$val['value']] = 1;
1367 $defaults[$elementName][$val['value']] = $val['value'];
1373 $defaults[$elementName] = $value;
1380 * @param int $contactID
1382 * @param int $fileID
1383 * @param bool $absolute
1385 * @param string $multiRecordWhereClause
1389 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1392 $params = ['id' => $cfID];
1394 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1395 $columnName = $defaults['column_name'];
1397 //table name of custom data
1398 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
1399 $defaults['custom_group_id'],
1403 //query to fetch id from civicrm_file
1404 if ($multiRecordWhereClause) {
1405 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1408 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1410 $fileID = CRM_Core_DAO
::singleValueQuery($query);
1415 $fileType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1420 $result['file_id'] = $fileID;
1422 if ($fileType == 'image/jpeg' ||
1423 $fileType == 'image/pjpeg' ||
1424 $fileType == 'image/gif' ||
1425 $fileType == 'image/x-png' ||
1426 $fileType == 'image/png'
1428 $entityId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_EntityFile',
1433 list($path) = CRM_Core_BAO_File
::path($fileID, $entityId);
1434 $fileHash = CRM_Core_BAO_File
::generateFileHash($entityId, $fileID);
1435 $url = CRM_Utils_System
::url('civicrm/file',
1436 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1437 $absolute, NULL, TRUE, TRUE
1439 $result['file_url'] = CRM_Utils_File
::getFileURL($path, $fileType, $url);
1441 // for non image files
1443 $uri = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1447 $fileHash = CRM_Core_BAO_File
::generateFileHash($contactID, $fileID);
1448 $url = CRM_Utils_System
::url('civicrm/file',
1449 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1450 $absolute, NULL, TRUE, TRUE
1452 $result['file_url'] = CRM_Utils_File
::getFileURL($uri, $fileType, $url);
1460 * Format custom fields before inserting.
1462 * @param int $customFieldId
1464 * @param array $customFormatted
1466 * @param mixed $value
1467 * Value of custom field.
1468 * @param string $customFieldExtend
1469 * Custom field extends.
1470 * @param int $customValueId
1471 * Custom option value id.
1472 * @param int $entityId
1473 * Entity id (contribution, membership...).
1474 * @param bool $inline
1475 * Consider inline custom groups only.
1476 * @param bool $checkPermission
1477 * If false, do not include permissioning clause.
1478 * @param bool $includeViewOnly
1479 * If true, fields marked 'View Only' are included. Required for APIv3.
1481 * @return array|NULL
1482 * formatted custom field array
1484 public static function formatCustomField(
1485 $customFieldId, &$customFormatted, $value,
1486 $customFieldExtend, $customValueId = NULL,
1489 $checkPermission = TRUE,
1490 $includeViewOnly = FALSE
1492 //get the custom fields for the entity
1493 //subtype and basic type
1494 $customDataSubType = NULL;
1495 if ($customFieldExtend) {
1496 // This is the case when getFieldsForImport() requires fields
1497 // of subtype and its parent.CRM-5143
1498 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1499 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType
::subTypes(), (array) $customFieldExtend);
1500 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1501 $customFieldExtend = CRM_Contact_BAO_ContactType
::getBasicType($customDataSubType);
1502 if (is_array($customFieldExtend)) {
1503 $customFieldExtend = array_unique(array_values($customFieldExtend));
1508 $customFields = CRM_Core_BAO_CustomField
::getFields($customFieldExtend,
1515 $checkPermission ? CRM_Core_Permission
::EDIT
: FALSE
1518 if (!array_key_exists($customFieldId, $customFields)) {
1522 // return if field is a 'code' field
1523 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1527 list($tableName, $columnName, $groupID) = self
::getTableColumnGroup($customFieldId);
1529 if (!$customValueId &&
1530 // we always create new entites for is_multiple unless specified
1531 !$customFields[$customFieldId]['is_multiple'] &&
1537 WHERE entity_id={$entityId}";
1539 $customValueId = CRM_Core_DAO
::singleValueQuery($query);
1542 //fix checkbox, now check box always submits values
1543 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1545 // Note that only during merge this is not an array, and you can directly use value
1546 if (is_array($value)) {
1547 $selectedValues = [];
1548 foreach ($value as $selId => $val) {
1550 $selectedValues[] = $selId;
1553 if (!empty($selectedValues)) {
1554 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1556 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
1564 elseif (self
::isSerialized($customFields[$customFieldId])) {
1565 // Select2 v3 returns a comma-separated string.
1566 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1567 $value = explode(',', $value);
1570 $value = $value ? CRM_Utils_Array
::implodePadded($value) : '';
1573 if (self
::isSerialized($customFields[$customFieldId]) &&
1574 $customFields[$customFieldId]['data_type'] == 'String' &&
1575 !empty($customFields[$customFieldId]['text_length']) &&
1578 // lets make sure that value is less than the length, else we'll
1579 // be losing some data, CRM-7481
1580 if (strlen($value) >= $customFields[$customFieldId]['text_length']) {
1581 // need to do a few things here
1583 // 1. lets find a new length
1584 $newLength = $customFields[$customFieldId]['text_length'];
1585 $minLength = strlen($value);
1586 while ($newLength < $minLength) {
1587 $newLength = $newLength * 2;
1590 // set the custom field meta data to have a length larger than value
1591 // alter the custom value table column to match this length
1592 CRM_Core_BAO_SchemaHandler
::alterFieldLength($customFieldId, $tableName, $columnName, $newLength);
1597 if ($customFields[$customFieldId]['data_type'] == 'Date') {
1598 if (!CRM_Utils_System
::isNull($value)) {
1599 $format = $customFields[$customFieldId]['date_format'];
1600 $date = CRM_Utils_Date
::processDate($value, NULL, FALSE, 'YmdHis', $format);
1605 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1606 $customFields[$customFieldId]['data_type'] == 'Money'
1612 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1613 $value = CRM_Utils_Rule
::cleanMoney($value);
1617 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1618 $customFields[$customFieldId]['data_type'] == 'Country'
1628 if ($customFields[$customFieldId]['data_type'] == 'File') {
1629 if (empty($value)) {
1633 $config = CRM_Core_Config
::singleton();
1635 // If we are already passing the file id as a value then retrieve and set the file data
1636 if (CRM_Utils_Rule
::integer($value)) {
1637 $fileDAO = new CRM_Core_DAO_File();
1638 $fileDAO->id
= $value;
1639 $fileDAO->find(TRUE);
1642 $fName = $fileDAO->uri
;
1643 $mimeType = $fileDAO->mime_type
;
1646 elseif (empty($value['name'])) {
1647 // Happens when calling the API to update custom fields values, but the filename
1648 // is empty, for an existing entity (in a specific case, was from a d7-webform
1649 // that was updating a relationship with a File customfield, so $value['id'] was
1650 // not empty, but the filename was empty.
1654 $fName = $value['name'];
1655 $mimeType = $value['type'];
1658 $filename = pathinfo($fName, PATHINFO_BASENAME
);
1660 // rename this file to go into the secure directory only if
1661 // user has uploaded new file not existing verfied on the basis of $fileID
1662 if (empty($fileID) && !rename($fName, $config->customFileUploadDir
. $filename)) {
1663 CRM_Core_Error
::statusBounce(ts('Could not move custom file to custom upload directory'));
1666 if ($customValueId && empty($fileID)) {
1671 $params = [1 => [$customValueId, 'Integer']];
1672 $fileID = CRM_Core_DAO
::singleValueQuery($query, $params);
1675 $fileDAO = new CRM_Core_DAO_File();
1678 $fileDAO->id
= $fileID;
1681 $fileDAO->uri
= $filename;
1682 $fileDAO->mime_type
= $mimeType;
1683 $fileDAO->upload_date
= date('YmdHis');
1685 $fileID = $fileDAO->id
;
1689 if (!is_array($customFormatted)) {
1690 $customFormatted = [];
1693 if (!array_key_exists($customFieldId, $customFormatted)) {
1694 $customFormatted[$customFieldId] = [];
1698 if ($customValueId) {
1699 $index = $customValueId;
1702 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1703 $customFormatted[$customFieldId][$index] = [];
1705 $customFormatted[$customFieldId][$index] = [
1706 'id' => $customValueId > 0 ?
$customValueId : NULL,
1708 'type' => $customFields[$customFieldId]['data_type'],
1709 'custom_field_id' => $customFieldId,
1710 'custom_group_id' => $groupID,
1711 'table_name' => $tableName,
1712 'column_name' => $columnName,
1713 'file_id' => $fileID,
1714 // is_multiple refers to the custom group, serialize refers to the field.
1715 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1716 'serialize' => $customFields[$customFieldId]['serialize'],
1719 //we need to sort so that custom fields are created in the order of entry
1720 krsort($customFormatted[$customFieldId]);
1721 return $customFormatted;
1725 * Get default custom table schema.
1727 * @param array $params
1731 public static function defaultCustomTableSchema($params) {
1732 // add the id and extends_id
1733 $collation = CRM_Core_BAO_SchemaHandler
::getInUseCollation();
1734 $characterSet = 'utf8';
1735 if (stripos($collation, 'utf8mb4') !== FALSE) {
1736 $characterSet = 'utf8mb4';
1739 'name' => $params['name'],
1740 'is_multiple' => $params['is_multiple'],
1741 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1745 'type' => 'int unsigned',
1748 'attributes' => 'AUTO_INCREMENT',
1749 'comment' => 'Default MySQL primary key',
1752 'name' => 'entity_id',
1753 'type' => 'int unsigned',
1755 'comment' => 'Table that this extends',
1756 'fk_table_name' => $params['extends_name'],
1757 'fk_field_name' => 'id',
1758 'fk_attributes' => 'ON DELETE CASCADE',
1763 // If on MySQL 5.6 include ROW_FORMAT=DYNAMIC to fix unit tests
1764 $databaseVersion = CRM_Utils_SQL
::getDatabaseVersion();
1765 if (version_compare($databaseVersion, '5.7', '<') && version_compare($databaseVersion, '5.6', '>=')) {
1766 $table['attributes'] = $table['attributes'] . ' ROW_FORMAT=DYNAMIC';
1769 if (!$params['is_multiple']) {
1770 $table['indexes'] = [
1773 'field_name_1' => 'entity_id',
1781 * Create custom field.
1783 * @param CRM_Core_DAO_CustomField $field
1784 * @param string $operation
1786 public static function createField($field, $operation) {
1787 $sql = str_repeat(' ', 8);
1788 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1789 $sql .= "ALTER TABLE " . $tableName;
1790 $sql .= self
::getAlterFieldSQL($field, $operation);
1792 // CRM-7007: do not i18n-rewrite this query
1793 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1795 $config = CRM_Core_Config
::singleton();
1796 if ($config->logging
) {
1797 // CRM-16717 not sure why this was originally limited to add.
1798 // For example custom tables can have field length changes - which need to flow through to logging.
1799 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
1800 if ($operation === 'add' ||
$operation === 'modify') {
1801 $logging = new CRM_Logging_Schema();
1802 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name
]]);
1806 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
1810 * @param CRM_Core_DAO_CustomField $field
1811 * @param string $operation add|modify|delete
1815 public static function getAlterFieldSQL($field, $operation) {
1816 $params = self
::prepareCreateParams($field, $operation);
1817 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1818 // a function only used by Custom Field creation...
1819 $params['required'] = FALSE;
1820 return CRM_Core_BAO_SchemaHandler
::getFieldAlterSQL($params);
1824 * Get query to reformat existing values for a field when changing its serialize attribute
1826 * @param CRM_Core_DAO_CustomField $field
1829 private static function getAlterSerializeSQL($field) {
1830 $table = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1831 $col = $field->column_name
;
1832 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
1833 if ($field->serialize
) {
1834 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1837 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1842 * Determine whether it would be safe to move a field.
1844 * @param int $fieldID
1845 * FK to civicrm_custom_field.
1846 * @param int $newGroupID
1847 * FK to civicrm_custom_group.
1850 * array(string) or TRUE
1852 public static function _moveFieldValidate($fieldID, $newGroupID) {
1855 $field = new CRM_Core_DAO_CustomField();
1856 $field->id
= $fieldID;
1857 if (!$field->find(TRUE)) {
1858 $errors['fieldID'] = ts('Invalid ID for custom field');
1862 $oldGroup = new CRM_Core_DAO_CustomGroup();
1863 $oldGroup->id
= $field->custom_group_id
;
1864 if (!$oldGroup->find(TRUE)) {
1865 $errors['fieldID'] = ts('Invalid ID for old custom group');
1869 $newGroup = new CRM_Core_DAO_CustomGroup();
1870 $newGroup->id
= $newGroupID;
1871 if (!$newGroup->find(TRUE)) {
1872 $errors['newGroupID'] = ts('Invalid ID for new custom group');
1878 FROM civicrm_custom_field a
1879 INNER JOIN civicrm_custom_field b
1881 AND a.label = b.label
1882 AND b.custom_group_id = %2
1885 1 => [$field->id
, 'Integer'],
1886 2 => [$newGroup->id
, 'Integer'],
1888 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
1890 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1893 $tableName = $oldGroup->table_name
;
1894 $columnName = $field->column_name
;
1899 WHERE $columnName is not null
1901 $count = CRM_Core_DAO
::singleValueQuery($query);
1905 FROM civicrm_custom_group
1906 WHERE id IN ( %1, %2 )
1909 1 => [$oldGroup->id
, 'Integer'],
1910 2 => [$newGroup->id
, 'Integer'],
1913 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1915 while ($dao->fetch()) {
1916 $extends[] = $dao->extends;
1918 if ($extends[0] != $extends[1]) {
1919 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1923 return empty($errors) ?
TRUE : $errors;
1927 * Move a custom data field from one group (table) to another.
1929 * @param int $fieldID
1930 * FK to civicrm_custom_field.
1931 * @param int $newGroupID
1932 * FK to civicrm_custom_group.
1934 * @throws CRM_Core_Exception
1936 public static function moveField($fieldID, $newGroupID) {
1937 $validation = self
::_moveFieldValidate($fieldID, $newGroupID);
1938 if (TRUE !== $validation) {
1939 throw new CRM_Core_Exception(implode(' ', $validation));
1941 $field = new CRM_Core_DAO_CustomField();
1942 $field->id
= $fieldID;
1945 $newGroup = new CRM_Core_DAO_CustomGroup();
1946 $newGroup->id
= $newGroupID;
1947 $newGroup->find(TRUE);
1949 $oldGroup = new CRM_Core_DAO_CustomGroup();
1950 $oldGroup->id
= $field->custom_group_id
;
1951 $oldGroup->find(TRUE);
1954 $add->custom_group_id
= $newGroup->id
;
1955 self
::createField($add, 'add');
1957 $sql = "INSERT INTO {$newGroup->table_name} (entity_id, `{$field->column_name}`)
1958 SELECT entity_id, `{$field->column_name}` FROM {$oldGroup->table_name}
1959 ON DUPLICATE KEY UPDATE `{$field->column_name}` = {$oldGroup->table_name}.`{$field->column_name}`
1961 CRM_Core_DAO
::executeQuery($sql);
1964 $del->custom_group_id
= $oldGroup->id
;
1965 self
::createField($del, 'delete');
1969 CRM_Utils_System
::flushCache();
1973 * Create an option value for a custom field option group ID.
1975 * @param array $params
1976 * @param string $value
1977 * @param \CRM_Core_DAO_OptionGroup $optionGroup
1978 * @param string $index
1979 * @param string $dataType
1981 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup
$optionGroup, $index, $dataType) {
1982 if (strlen(trim($value))) {
1983 $optionValue = new CRM_Core_DAO_OptionValue();
1984 $optionValue->option_group_id
= $optionGroup->id
;
1985 $optionValue->label
= $params['option_label'][$index];
1986 $optionValue->name
= $params['option_name'][$index] ?? CRM_Utils_String
::titleToVar($params['option_label'][$index]);
1987 switch ($dataType) {
1989 $optionValue->value
= CRM_Utils_Rule
::cleanMoney($value);
1993 $optionValue->value
= intval($value);
1997 $optionValue->value
= floatval($value);
2001 $optionValue->value
= trim($value);
2004 $optionValue->weight
= $params['option_weight'][$index];
2005 $optionValue->is_active
= $params['option_status'][$index] ??
FALSE;
2006 $optionValue->description
= $params['option_description'][$index] ??
NULL;
2007 $optionValue->color
= $params['option_color'][$index] ??
NULL;
2008 $optionValue->icon
= $params['option_icon'][$index] ??
NULL;
2009 $optionValue->save();
2014 * Prepare for the create operation.
2016 * Munge params, create the option values if needed.
2018 * This could be called by a single create or a batchCreate.
2020 * @param array $params
2024 protected static function prepareCreate($params) {
2025 $op = empty($params['id']) ?
'create' : 'edit';
2026 CRM_Utils_Hook
::pre($op, 'CustomField', CRM_Utils_Array
::value('id', $params), $params);
2027 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
2028 if ($op === 'create') {
2029 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
2030 if (!isset($params['column_name'])) {
2031 // if add mode & column_name not present, calculate it.
2032 $params['column_name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 32));
2034 if (!isset($params['name'])) {
2035 $params['name'] = CRM_Utils_String
::munge($params['label'], '_', 64);
2039 $params['column_name'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
2042 $htmlType = $params['html_type'] ??
NULL;
2043 $dataType = $params['data_type'] ??
NULL;
2045 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
2046 $params['date_format'] = Civi
::settings()->get('dateInputFormat');
2049 // Checkboxes are always serialized in current schema
2050 if ($htmlType == 'CheckBox') {
2051 $params['serialize'] = CRM_Core_DAO
::SERIALIZE_SEPARATOR_BOOKEND
;
2054 if (!empty($params['serialize'])) {
2055 if (isset($params['default_checkbox_option'])) {
2057 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
2058 if ($params['option_value'][$v]) {
2059 $defaultArray[] = $params['option_value'][$v];
2063 if (!empty($defaultArray)) {
2064 // also add the separator before and after the value per new convention (CRM-1604)
2065 $params['default_value'] = CRM_Utils_Array
::implodePadded($defaultArray);
2069 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
2070 $params['default_value'] = $params['option_value'][$params['default_option']];
2075 // create any option group & values if required
2076 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
2077 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
2078 //CRM-16659: if option_value then create an option group for this custom field.
2079 // An option_type of 2 would be a 'message' from the form layer not to handle
2080 // the option_values key. If not set then it is not ignored.
2081 $optionsType = (int) ($params['option_type'] ??
0);
2082 if (($optionsType !== 2 && empty($params['id']))
2083 && (empty($params['option_group_id']) && !empty($params['option_value'])
2086 // first create an option group for this custom group
2087 $customGroupTitle = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $params['custom_group_id'], 'title');
2088 $optionGroup = CRM_Core_DAO_OptionGroup
::writeRecord([
2089 'title' => "$customGroupTitle :: {$params['label']}",
2090 // Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
2092 'data_type' => $dataType,
2094 $params['option_group_id'] = $optionGroup->id
;
2095 if (!empty($params['option_value']) && is_array($params['option_value'])) {
2096 foreach ($params['option_value'] as $k => $v) {
2097 self
::createOptionValue($params, $v, $optionGroup, $k, $dataType);
2103 // Remove option group IDs from fields changed to Text html_type.
2104 if ($htmlType == 'Text') {
2105 $params['option_group_id'] = '';
2108 // check for orphan option groups
2109 if (!empty($params['option_group_id'])) {
2110 if (!empty($params['id'])) {
2111 self
::fixOptionGroups($params['id'], $params['option_group_id']);
2114 // if we do not have a default value
2115 // retrieve it from one of the other custom fields which use this option group
2116 if (empty($params['default_value'])) {
2117 //don't insert only value separator as default value, CRM-4579
2118 $defaultValue = self
::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
2120 if ($defaultValue !== NULL && !CRM_Utils_System
::isNull(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaultValue))) {
2121 $params['default_value'] = $defaultValue;
2126 // Set default textarea attributes
2127 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
2128 $params['attributes'] = 'rows=4, cols=60';
2134 * Create database entry for custom field and related option groups.
2136 * @param array $params
2138 * @return CRM_Core_DAO_CustomField
2140 protected static function createCustomFieldRecord($params) {
2141 $transaction = new CRM_Core_Transaction();
2142 $params = self
::prepareCreate($params);
2144 $customField = new CRM_Core_DAO_CustomField();
2145 $customField->copyValues($params);
2146 $customField->save();
2148 //create/drop the index when we toggle the is_searchable flag
2149 $op = empty($params['id']) ?
'add' : 'modify';
2150 if ($op !== 'modify') {
2151 if ($params['is_append_field_id_to_column_name']) {
2152 $params['column_name'] .= "_{$customField->id}";
2154 $customField->column_name
= $params['column_name'];
2155 $customField->save();
2158 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2159 $transaction->commit();
2161 // make sure all values are present in the object for further processing
2162 $customField->find(TRUE);
2164 return $customField;
2171 protected static function getChangeSerialize($params) {
2172 if (isset($params['serialize']) && !empty($params['id'])) {
2173 if ($params['serialize'] != CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'serialize')) {
2174 return (int) $params['serialize'];
2181 * Move custom data from one contact to another.
2183 * This is currently the start of a refactoring. The theory is each
2184 * entity could have a 'move' function with a DAO default one to fall back on.
2186 * At the moment this only does a small part of the process - ie deleting a file field that
2187 * is about to be overwritten. However, the goal is the whole process around the move for
2188 * custom data should be in here.
2190 * This is currently called by the merge class but it makes sense that api could
2191 * expose move actions as moving (e.g) contributions feels like a common
2192 * ask that should be handled by the form layer.
2194 * @param int $oldContactID
2195 * @param int $newContactID
2196 * @param int[] $fieldIDs
2197 * Optional list field ids to move.
2199 * @throws \CiviCRM_API3_Exception
2200 * @throws \Exception
2202 public function move($oldContactID, $newContactID, $fieldIDs) {
2203 if (empty($fieldIDs)) {
2206 $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'];
2208 foreach ($fieldIDs as $fieldID) {
2209 $return[] = 'custom_' . $fieldID;
2211 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2212 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2214 // The moveAllBelongings function has functionality to move custom fields. It doesn't work very well...
2215 // @todo handle all fields here but more immediately Country since that is broken at the moment.
2216 $fieldTypesNotHandledInMergeAttempt = ['File'];
2217 foreach ($fields as $field) {
2218 $isMultiple = !empty($field['custom_group_id.is_multiple']);
2219 if ($field['data_type'] === 'File' && !$isMultiple) {
2220 if (!empty($oldContact['custom_' . $field['id']]) && !empty($newContact['custom_' . $field['id']])) {
2221 CRM_Core_BAO_File
::deleteFileReferences($oldContact['custom_' . $field['id']], $oldContactID, $field['id']);
2223 if (!empty($oldContact['custom_' . $field['id']])) {
2224 CRM_Core_DAO
::executeQuery("
2225 UPDATE civicrm_entity_file
2226 SET entity_id = $newContactID
2227 WHERE file_id = {$oldContact['custom_' . $field['id']]}"
2231 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2232 CRM_Core_DAO
::executeQuery(
2233 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2234 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2235 ON DUPLICATE KEY UPDATE
2236 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2243 * Get the database table name and column name for a custom field.
2245 * @param int $fieldID
2246 * The fieldID of the custom field.
2247 * @param bool $force
2248 * Force the sql to be run again (primarily used for tests).
2251 * fatal is fieldID does not exists, else array of tableName, columnName
2252 * @throws \CRM_Core_Exception
2254 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2255 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2256 $cache = CRM_Utils_Cache
::singleton();
2257 $fieldValues = $cache->get($cacheKey);
2258 if (empty($fieldValues) ||
$force) {
2260 SELECT cg.table_name, cf.column_name, cg.id
2261 FROM civicrm_custom_group cg,
2262 civicrm_custom_field cf
2263 WHERE cf.custom_group_id = cg.id
2265 $params = [1 => [$fieldID, 'Integer']];
2266 $dao = CRM_Core_DAO
::executeQuery($query, $params);
2268 if (!$dao->fetch()) {
2269 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2271 $fieldValues = [$dao->table_name
, $dao->column_name
, $dao->id
];
2272 $cache->set($cacheKey, $fieldValues);
2274 return $fieldValues;
2278 * Get custom option groups.
2280 * @deprecated Use the API OptionGroup.get
2282 * @param array $includeFieldIds
2283 * Ids of custom fields for which option groups must be included.
2285 * Currently this is required in the cases where option groups are to be included
2286 * for inactive fields : CRM-5369
2290 public static function customOptionGroup($includeFieldIds = NULL) {
2291 static $customOptionGroup = NULL;
2293 $cacheKey = (empty($includeFieldIds)) ?
'onlyActive' : 'force';
2294 if ($cacheKey == 'force') {
2295 $customOptionGroup[$cacheKey] = NULL;
2298 if (empty($customOptionGroup[$cacheKey])) {
2299 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2301 //support for single as well as array format.
2302 if (!empty($includeFieldIds)) {
2303 if (is_array($includeFieldIds)) {
2304 $includeFieldIds = implode(',', $includeFieldIds);
2306 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2310 SELECT g.id, g.title
2311 FROM civicrm_option_group g
2312 INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2313 WHERE {$whereClause}";
2315 $dao = CRM_Core_DAO
::executeQuery($query);
2316 while ($dao->fetch()) {
2317 $customOptionGroup[$cacheKey][$dao->id
] = $dao->title
;
2321 return $customOptionGroup[$cacheKey];
2325 * Get defaults for new entity.
2329 public static function getDefaults() {
2331 'is_required' => FALSE,
2332 'is_searchable' => FALSE,
2333 'in_selector' => FALSE,
2334 'is_search_range' => FALSE,
2335 //CRM-15792 - Custom field gets disabled if is_active not set
2336 // this would ideally be a mysql default.
2337 'is_active' => TRUE,
2343 * Fix orphan groups.
2345 * @param int $customFieldId
2347 * @param int $optionGroupId
2350 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2351 // check if option group belongs to any custom Field else delete
2352 // get the current option group
2353 $currentOptionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
2357 // get the updated option group
2358 // if both are same return
2359 if (!$currentOptionGroupId ||
$currentOptionGroupId == $optionGroupId) {
2363 // check if option group is related to any other field
2364 self
::checkOptionGroup($currentOptionGroupId);
2368 * Check if option group is related to more than one custom field.
2370 * @param int $optionGroupId
2373 public static function checkOptionGroup($optionGroupId) {
2376 FROM civicrm_custom_field
2377 WHERE option_group_id = {$optionGroupId}";
2379 $count = CRM_Core_DAO
::singleValueQuery($query);
2382 //delete the option group
2383 CRM_Core_BAO_OptionGroup
::del($optionGroupId);
2388 * Get option group default.
2390 * @param int $optionGroupId
2391 * @param bool $serialize
2393 * @return null|string
2395 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2397 SELECT default_value, serialize
2398 FROM civicrm_custom_field
2399 WHERE option_group_id = {$optionGroupId}
2400 AND default_value IS NOT NULL";
2402 $dao = CRM_Core_DAO
::executeQuery($query);
2403 while ($dao->fetch()) {
2404 if ($dao->serialize
== $serialize) {
2405 return $dao->default_value
;
2407 $defaultValue = $dao->default_value
;
2410 // Convert serialization
2411 if (isset($defaultValue) && $serialize) {
2412 return CRM_Utils_Array
::implodePadded([$defaultValue]);
2414 elseif (isset($defaultValue)) {
2415 return CRM_Utils_Array
::explodePadded($defaultValue)[0];
2421 * Post process function.
2423 * @param array $params
2424 * @param int $entityID
2425 * @param string $customFieldExtends
2426 * @param bool $inline
2427 * @param bool $checkPermissions
2431 public static function postProcess(
2434 $customFieldExtends,
2436 $checkPermissions = TRUE
2440 foreach ($params as $key => $value) {
2441 if ($customFieldInfo = CRM_Core_BAO_CustomField
::getKeyID($key, TRUE)) {
2443 // for autocomplete transfer hidden value instead of label
2444 if ($params[$key] && isset($params[$key . '_id'])) {
2445 $value = $params[$key . '_id'];
2448 // we need to append time with date
2449 if ($params[$key] && isset($params[$key . '_time'])) {
2450 $value .= ' ' . $params[$key . '_time'];
2453 CRM_Core_BAO_CustomField
::formatCustomField($customFieldInfo[0],
2456 $customFieldExtends,
2457 $customFieldInfo[1],
2468 * Get custom field ID from field/group name/title.
2470 * @param string $fieldName Field name or label
2471 * @param string|null $groupName (Optional) Group name or label
2472 * @param bool $fullString Whether to return "custom_123" or "123"
2474 * @return string|int|null
2475 * @throws \CiviCRM_API3_Exception
2477 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2478 $cacheKey = $groupName . '.' . $fieldName;
2479 if (!isset(Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2480 $customFieldParams = [
2481 'name' => $fieldName,
2482 'label' => $fieldName,
2483 'options' => ['or' => [["name", "label"]]],
2487 $customFieldParams['custom_group_id.name'] = $groupName;
2488 $customFieldParams['custom_group_id.title'] = $groupName;
2489 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2492 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2494 if (empty($field['id'])) {
2495 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2496 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2499 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2500 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2505 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2507 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
2511 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
2517 public static function getNameFromID($ids) {
2518 if (is_array($ids)) {
2519 $ids = implode(',', $ids);
2522 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2523 FROM civicrm_custom_field f
2524 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2525 WHERE f.id IN ($ids)";
2527 $dao = CRM_Core_DAO
::executeQuery($sql);
2529 while ($dao->fetch()) {
2530 $result[$dao->id
] = [
2531 'field_name' => $dao->field_name
,
2532 'field_label' => $dao->field_label
,
2533 'group_name' => $dao->group_name
,
2534 'group_title' => $dao->group_title
,
2541 * Validate custom data.
2543 * @param array $params
2544 * Custom data submitted.
2545 * ie array( 'custom_1' => 'validate me' );
2548 * validation errors.
2550 public static function validateCustomData($params) {
2552 if (!is_array($params) ||
empty($params)) {
2556 //pick up profile fields.
2557 $profileFields = [];
2558 $ufGroupId = $params['ufGroupId'] ??
NULL;
2560 $profileFields = CRM_Core_BAO_UFGroup
::getFields($ufGroupId,
2562 CRM_Core_Action
::VIEW
2566 //lets start w/ params.
2567 foreach ($params as $key => $value) {
2568 $customFieldID = self
::getKeyID($key);
2569 if (!$customFieldID) {
2573 //load the structural info for given field.
2574 $field = new CRM_Core_DAO_CustomField();
2575 $field->id
= $customFieldID;
2576 if (!$field->find(TRUE)) {
2579 $dataType = $field->data_type
;
2581 $profileField = CRM_Utils_Array
::value($key, $profileFields, []);
2582 $fieldTitle = $profileField['title'] ??
NULL;
2583 $isRequired = $profileField['is_required'] ??
NULL;
2585 $fieldTitle = $field->label
;
2588 //no need to validate.
2589 if (CRM_Utils_System
::isNull($value) && !$isRequired) {
2593 //lets validate first for required field.
2594 if ($isRequired && CRM_Utils_System
::isNull($value)) {
2595 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
2599 //now time to take care of custom field form rules.
2600 $ruleName = $errorMsg = NULL;
2601 switch ($dataType) {
2603 $ruleName = 'integer';
2604 $errorMsg = ts('%1 must be an integer (whole number).',
2605 array(1 => $fieldTitle)
2610 $ruleName = 'money';
2611 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2612 array(1 => $fieldTitle)
2617 $ruleName = 'numeric';
2618 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2619 array(1 => $fieldTitle)
2624 $ruleName = 'wikiURL';
2625 $errorMsg = ts('%1 must be valid Website.',
2626 array(1 => $fieldTitle)
2631 if ($ruleName && !CRM_Utils_System
::isNull($value)) {
2633 $funName = "CRM_Utils_Rule::{$ruleName}";
2634 if (is_callable($funName)) {
2635 $valid = call_user_func($funName, $value);
2638 $errors[$key] = $errorMsg;
2647 * Is this field a multi record field.
2649 * @param int $customId
2653 public static function isMultiRecordField($customId) {
2654 $isMultipleWithGid = FALSE;
2655 if (!is_numeric($customId)) {
2656 $customId = self
::getKeyID($customId);
2658 if (is_numeric($customId)) {
2659 $sql = "SELECT cg.id cgId
2660 FROM civicrm_custom_group cg
2661 INNER JOIN civicrm_custom_field cf
2662 ON cg.id = cf.custom_group_id
2663 WHERE cf.id = %1 AND cg.is_multiple = 1";
2664 $params[1] = [$customId, 'Integer'];
2665 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2666 if ($dao->fetch()) {
2668 $isMultipleWithGid = $dao->cgId
;
2673 return $isMultipleWithGid;
2677 * Does this field type have any select options?
2679 * @param array $field
2683 public static function hasOptions($field) {
2684 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2685 $field = (array) $field;
2686 // This will include boolean fields with Yes/No options.
2687 if (in_array($field['html_type'], ['Radio', 'CheckBox'])) {
2690 // Do this before the "Select" string search because date fields have a "Select Date" html_type
2691 // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list.
2692 if (in_array($field['data_type'], ['ContactReference', 'Date'])) {
2695 if (strpos($field['html_type'], 'Select') !== FALSE) {
2698 return !empty($field['option_group_id']);
2702 * Does this field store a serialized string?
2704 * @param CRM_Core_DAO_CustomField|array $field
2708 public static function isSerialized($field) {
2709 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2710 $html_type = is_object($field) ?
$field->html_type
: $field['html_type'];
2711 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2712 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2713 if ($html_type === 'CheckBox' ||
strpos($html_type, 'Multi') !== FALSE) {
2716 // Otherwise this is the new standard as of 5.27
2717 return is_object($field) ?
!empty($field->serialize
) : !empty($field['serialize']);
2721 * Get api entity for this field
2725 public function getEntity() {
2726 $entity = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id
, 'extends');
2727 return in_array($entity, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE) ?
'Contact' : $entity;
2731 * Set pseudoconstant properties for field metadata.
2733 * @param array $field
2734 * @param string|null $optionGroupName
2736 private static function getOptionsForField(&$field, $optionGroupName) {
2737 if ($optionGroupName) {
2738 $field['pseudoconstant'] = [
2739 'optionGroupName' => $optionGroupName,
2740 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2743 elseif ($field['data_type'] == 'Boolean') {
2744 $field['pseudoconstant'] = [
2745 'callback' => 'CRM_Core_SelectValues::boolean',
2748 elseif ($field['data_type'] == 'Country') {
2749 $field['pseudoconstant'] = [
2750 'table' => 'civicrm_country',
2751 'keyColumn' => 'id',
2752 'labelColumn' => 'name',
2753 'nameColumn' => 'iso_code',
2756 elseif ($field['data_type'] == 'StateProvince') {
2757 $field['pseudoconstant'] = [
2758 'table' => 'civicrm_state_province',
2759 'keyColumn' => 'id',
2760 'labelColumn' => 'name',
2766 * Prepare params for the create operation.
2768 * @param CRM_Core_DAO_CustomField $field
2769 * @param string $operation
2773 * @throws \CRM_Core_Exception
2775 protected static function prepareCreateParams($field, $operation): array {
2776 $tableName = CRM_Core_DAO
::getFieldValue(
2777 'CRM_Core_DAO_CustomGroup',
2778 $field->custom_group_id
,
2783 'table_name' => $tableName,
2784 'operation' => $operation,
2785 'name' => $field->column_name
,
2786 'type' => CRM_Core_BAO_CustomValueTable
::fieldToSQLType(
2790 'required' => $field->is_required
,
2791 'searchable' => $field->is_searchable
,
2794 // For adding/dropping FK constraints
2795 $params['fkName'] = CRM_Core_BAO_SchemaHandler
::getIndexName($tableName, $field->column_name
);
2798 'Country' => 'civicrm_country',
2799 'StateProvince' => 'civicrm_state_province',
2800 'ContactReference' => 'civicrm_contact',
2801 'File' => 'civicrm_file',
2803 if (isset($fkFields[$field->data_type
])) {
2804 // Serialized fields store value-separated strings which are incompatible with FK constraints
2805 if (!$field->serialize
) {
2806 $params['fk_table_name'] = $fkFields[$field->data_type
];
2807 $params['fk_field_name'] = 'id';
2808 $params['fk_attributes'] = 'ON DELETE SET NULL';
2811 if ($field->serialize
) {
2812 $params['type'] = 'varchar(255)';
2814 if (isset($field->default_value
)) {
2815 $params['default'] = "'{$field->default_value}'";