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 * Takes an associative array and creates a custom field object.
80 * This function is invoked from within the web form layer and also from the api layer
82 * @param array $params
83 * (reference) an assoc array of name/value pairs.
85 * @return CRM_Core_DAO_CustomField
87 public static function create($params) {
88 $changeSerialize = self
::getChangeSerialize($params);
89 $customField = self
::createCustomFieldRecord($params);
90 // When deserializing a field, the update needs to run before the schema change
91 if ($changeSerialize === 0) {
92 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
94 $op = empty($params['id']) ?
'add' : 'modify';
95 self
::createField($customField, $op);
96 // When serializing a field, the update needs to run after the schema change
97 if ($changeSerialize === 1) {
98 CRM_Core_DAO
::singleValueQuery(self
::getAlterSerializeSQL($customField));
101 CRM_Utils_Hook
::post(($op === 'add' ?
'create' : 'edit'), 'CustomField', $customField->id
, $customField);
103 CRM_Utils_System
::flushCache();
104 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
105 // so do it manually. Ideally we wouldn't need to clear others...
106 Civi
::cache('metadata')->clear();
112 * Save multiple fields, now deprecated in favor of self::writeRecords.
113 * https://lab.civicrm.org/dev/core/issues/1093
116 * @param array $bulkParams
117 * Array of arrays as would be passed into create
118 * @param array $defaults
119 * Default parameters to be be merged into each of the params.
121 * @throws \CiviCRM_API3_Exception
123 public static function bulkSave($bulkParams, $defaults = []) {
124 CRM_Core_Error
::deprecatedFunctionWarning(__CLASS__
. '::writeRecords');
125 foreach ($bulkParams as $index => $fieldParams) {
126 $bulkParams[$index] = array_merge($defaults, $fieldParams);
128 self
::writeRecords($bulkParams);
132 * Create/update several fields at once in a mysql efficient way.
134 * @param array $records
135 * @return CRM_Core_DAO_CustomField[]
136 * @throws CRM_Core_Exception
138 public static function writeRecords(array $records): array {
139 $addedColumns = $sql = $customFields = $pre = $post = [];
140 foreach ($records as $index => $params) {
141 CRM_Utils_Hook
::pre(empty($params['id']) ?
'create' : 'edit', 'CustomField', $params['id'] ??
NULL, $params);
143 $changeSerialize = self
::getChangeSerialize($params);
144 $customField = self
::createCustomFieldRecord($params);
145 // Serialize/deserialize sql must run after/before the table is altered
146 if ($changeSerialize === 0) {
147 $pre[] = self
::getAlterSerializeSQL($customField);
149 if ($changeSerialize === 1) {
150 $post[] = self
::getAlterSerializeSQL($customField);
152 $fieldSQL = self
::getAlterFieldSQL($customField, empty($params['id']) ?
'add' : 'modify');
154 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id
, 'table_name');
155 $sql[$tableName][] = $fieldSQL;
156 $addedColumns[$tableName][] = $customField->column_name
;
157 $customFields[$index] = $customField;
160 foreach ($pre as $query) {
161 CRM_Core_DAO
::executeQuery($query);
164 foreach ($sql as $tableName => $statements) {
165 // CRM-7007: do not i18n-rewrite this query
166 CRM_Core_DAO
::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE);
168 if (CRM_Core_Config
::singleton()->logging
) {
169 $logging = new CRM_Logging_Schema();
170 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
173 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
176 foreach ($post as $query) {
177 CRM_Core_DAO
::executeQuery($query);
180 CRM_Utils_System
::flushCache();
181 Civi
::cache('metadata')->clear();
183 foreach ($customFields as $index => $customField) {
184 CRM_Utils_Hook
::post(empty($records[$index]['id']) ?
'create' : 'edit', 'CustomField', $customField->id
, $customField);
186 return $customFields;
190 * Fetch object based on array of properties.
192 * @param array $params
193 * An assoc array of name/value pairs.
194 * @param array $defaults
195 * (reference ) an assoc array to hold the flattened values.
197 * @return CRM_Core_DAO_CustomField
199 public static function retrieve($params, &$defaults) {
200 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $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 = ['Contact', 'Individual', 'Organization', 'Household'];
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, ['Individual', 'Household', 'Organization'])) {
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))) {
1100 foreach ((array) $value as $contactId) {
1101 $displayNames[] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');
1103 $display = implode(', ', $displayNames);
1105 elseif ($field['data_type'] == 'ContactReference') {
1108 elseif (is_array($value)) {
1110 foreach ($value as $key => $val) {
1111 $v[] = $field['options'][$val] ??
NULL;
1113 $display = implode(', ', $v);
1116 $display = CRM_Utils_Array
::value($value, $field['options'], '');
1117 // For float type (see Number and Money) $value would be decimal like
1118 // 1.00 (because it is stored in db as decimal), while options array
1119 // key would be integer like 1. In this case expression on line above
1120 // would return empty string (default value), despite the fact that
1121 // matching option exists in the array.
1122 // In such cases we could just get intval($value) and fetch matching
1123 // option again, but this would not work if key is float like 5.6.
1124 // So we need to truncate trailing zeros to make it work as expected.
1125 if ($display === '' && strpos($value, '.') !== FALSE) {
1126 // Use round() to truncate trailing zeros, e.g:
1127 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1128 $value = (string) round($value, 5);
1129 $display = $field['options'][$value] ??
'';
1135 $customFormat = NULL;
1137 // FIXME: Are there any legitimate reasons why $value would be an array?
1138 // Or should we throw an exception here if it is?
1139 $value = is_array($value) ? CRM_Utils_Array
::first($value) : $value;
1141 $actualPHPFormats = CRM_Utils_Date
::datePluginToPHPFormats();
1142 $format = $field['date_format'] ??
NULL;
1145 if (array_key_exists($format, $actualPHPFormats)) {
1146 $customTimeFormat = (array) $actualPHPFormats[$format];
1147 switch (CRM_Utils_Array
::value('time_format', $field)) {
1149 $customTimeFormat[] = 'g:iA';
1153 $customTimeFormat[] = 'G:i';
1157 //If time is not selected remove time from value.
1158 $value = $value ?
date('Y-m-d', strtotime($value)) : '';
1160 $customFormat = implode(" ", $customTimeFormat);
1163 $display = CRM_Utils_Date
::processDate($value, NULL, FALSE, $customFormat);
1167 // In the context of displaying a profile, show file/image
1170 if (CRM_Utils_Rule
::positiveInteger($value)) {
1174 $fileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1176 $url = self
::getFileURL($entityId, $field['id'], $fileId);
1178 $display = $url['file_url'];
1182 // In other contexts show a paperclip icon
1183 if (CRM_Utils_Rule
::integer($value)) {
1184 $icons = CRM_Core_BAO_File
::paperIconAttachment('*', $value);
1186 $file_description = '';
1188 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1190 catch (CiviCRM_API3_Exception
$dontcare) {
1194 $display = "{$icons[$value]}{$file_description}";
1197 //CRM-18396, if filename is passed instead
1205 $display = $display ?
"<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1209 $display = nl2br($display);
1213 if ($field['data_type'] == 'Money' && isset($value)) {
1214 // $value can also be an array(while using IN operator from search builder or api).
1215 foreach ((array) $value as $val) {
1216 $disp[] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($val);
1218 $display = implode(', ', $disp);
1220 elseif ($field['data_type'] == 'Float' && isset($value)) {
1221 // $value can also be an array(while using IN operator from search builder or api).
1222 foreach ((array) $value as $val) {
1223 $disp[] = CRM_Utils_Number
::formatLocaleNumeric($val);
1225 $display = implode(', ', $disp);
1233 * Set default values for custom data used in profile.
1235 * @param int $customFieldId
1237 * @param string $elementName
1238 * Custom field name.
1239 * @param array $defaults
1240 * Associated array of fields.
1241 * @param int $contactId
1245 * @param mixed $value
1246 * If passed - dont fetch value from db,.
1247 * just format the given value
1249 public static function setProfileDefaults(
1257 //get the type of custom field
1258 $customField = new CRM_Core_BAO_CustomField();
1259 $customField->id
= $customFieldId;
1260 $customField->find(TRUE);
1263 if ($mode == CRM_Profile_Form
::MODE_CREATE
) {
1264 $value = $customField->default_value
;
1268 if (!isset($value)) {
1269 $info = self
::getTableColumnGroup($customFieldId);
1270 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1271 $result = CRM_Core_DAO
::executeQuery($query);
1272 if ($result->fetch()) {
1273 $value = $result->value
;
1277 if ($customField->data_type
== 'Country') {
1279 $config = CRM_Core_Config
::singleton();
1280 if ($config->defaultContactCountry
) {
1281 $value = CRM_Core_BAO_Country
::defaultContactCountry();
1287 //set defaults if mode is registration
1288 if (!trim($value) &&
1290 (!in_array($mode, [CRM_Profile_Form
::MODE_EDIT
, CRM_Profile_Form
::MODE_SEARCH
]))
1292 $value = $customField->default_value
;
1295 if ($customField->data_type
== 'Money' && isset($value)) {
1296 $value = number_format($value, 2);
1298 if (self
::isSerialized($customField) && $value) {
1299 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldId, FALSE);
1300 $defaults[$elementName] = [];
1301 $checkedValue = CRM_Utils_Array
::explodePadded($value);
1302 foreach ($customOption as $val) {
1303 if (in_array($val['value'], $checkedValue)) {
1304 if ($customField->html_type
== 'CheckBox') {
1305 $defaults[$elementName][$val['value']] = 1;
1308 $defaults[$elementName][$val['value']] = $val['value'];
1314 $defaults[$elementName] = $value;
1321 * @param int $contactID
1323 * @param int $fileID
1324 * @param bool $absolute
1326 * @param string $multiRecordWhereClause
1330 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1333 $params = ['id' => $cfID];
1335 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1336 $columnName = $defaults['column_name'];
1338 //table name of custom data
1339 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
1340 $defaults['custom_group_id'],
1344 //query to fetch id from civicrm_file
1345 if ($multiRecordWhereClause) {
1346 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1349 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1351 $fileID = CRM_Core_DAO
::singleValueQuery($query);
1356 $fileType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1361 $result['file_id'] = $fileID;
1363 if ($fileType == 'image/jpeg' ||
1364 $fileType == 'image/pjpeg' ||
1365 $fileType == 'image/gif' ||
1366 $fileType == 'image/x-png' ||
1367 $fileType == 'image/png'
1369 $entityId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_EntityFile',
1374 list($path) = CRM_Core_BAO_File
::path($fileID, $entityId);
1375 $fileHash = CRM_Core_BAO_File
::generateFileHash($entityId, $fileID);
1376 $url = CRM_Utils_System
::url('civicrm/file',
1377 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1378 $absolute, NULL, TRUE, TRUE
1380 $result['file_url'] = CRM_Utils_File
::getFileURL($path, $fileType, $url);
1382 // for non image files
1384 $uri = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_File',
1388 $fileHash = CRM_Core_BAO_File
::generateFileHash($contactID, $fileID);
1389 $url = CRM_Utils_System
::url('civicrm/file',
1390 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1391 $absolute, NULL, TRUE, TRUE
1393 $result['file_url'] = CRM_Utils_File
::getFileURL($uri, $fileType, $url);
1401 * Format custom fields before inserting.
1403 * @param int $customFieldId
1405 * @param array $customFormatted
1407 * @param mixed $value
1408 * Value of custom field.
1409 * @param string $customFieldExtend
1410 * Custom field extends.
1411 * @param int $customValueId
1412 * Custom option value id.
1413 * @param int $entityId
1414 * Entity id (contribution, membership...).
1415 * @param bool $inline
1416 * Consider inline custom groups only.
1417 * @param bool $checkPermission
1418 * If false, do not include permissioning clause.
1419 * @param bool $includeViewOnly
1420 * If true, fields marked 'View Only' are included. Required for APIv3.
1422 * @return array|NULL
1423 * formatted custom field array
1425 public static function formatCustomField(
1426 $customFieldId, &$customFormatted, $value,
1427 $customFieldExtend, $customValueId = NULL,
1430 $checkPermission = TRUE,
1431 $includeViewOnly = FALSE
1433 //get the custom fields for the entity
1434 //subtype and basic type
1435 $customDataSubType = NULL;
1436 if ($customFieldExtend) {
1437 // This is the case when getFieldsForImport() requires fields
1438 // of subtype and its parent.CRM-5143
1439 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1440 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType
::subTypes(), (array) $customFieldExtend);
1441 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1442 $customFieldExtend = CRM_Contact_BAO_ContactType
::getBasicType($customDataSubType);
1443 if (is_array($customFieldExtend)) {
1444 $customFieldExtend = array_unique(array_values($customFieldExtend));
1449 $customFields = CRM_Core_BAO_CustomField
::getFields($customFieldExtend,
1456 $checkPermission ? CRM_Core_Permission
::EDIT
: FALSE
1459 if (!array_key_exists($customFieldId, $customFields)) {
1463 // return if field is a 'code' field
1464 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1468 list($tableName, $columnName, $groupID) = self
::getTableColumnGroup($customFieldId);
1470 if (!$customValueId &&
1471 // we always create new entites for is_multiple unless specified
1472 !$customFields[$customFieldId]['is_multiple'] &&
1478 WHERE entity_id={$entityId}";
1480 $customValueId = CRM_Core_DAO
::singleValueQuery($query);
1483 //fix checkbox, now check box always submits values
1484 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1486 // Note that only during merge this is not an array, and you can directly use value
1487 if (is_array($value)) {
1488 $selectedValues = [];
1489 foreach ($value as $selId => $val) {
1491 $selectedValues[] = $selId;
1494 if (!empty($selectedValues)) {
1495 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1497 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
1505 elseif (self
::isSerialized($customFields[$customFieldId])) {
1506 // Select2 v3 returns a comma-separated string.
1507 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1508 $value = explode(',', $value);
1511 $value = $value ? CRM_Utils_Array
::implodePadded($value) : '';
1514 if (self
::isSerialized($customFields[$customFieldId]) &&
1515 $customFields[$customFieldId]['data_type'] == 'String' &&
1516 !empty($customFields[$customFieldId]['text_length']) &&
1519 // lets make sure that value is less than the length, else we'll
1520 // be losing some data, CRM-7481
1521 if (strlen($value) >= $customFields[$customFieldId]['text_length']) {
1522 // need to do a few things here
1524 // 1. lets find a new length
1525 $newLength = $customFields[$customFieldId]['text_length'];
1526 $minLength = strlen($value);
1527 while ($newLength < $minLength) {
1528 $newLength = $newLength * 2;
1531 // set the custom field meta data to have a length larger than value
1532 // alter the custom value table column to match this length
1533 CRM_Core_BAO_SchemaHandler
::alterFieldLength($customFieldId, $tableName, $columnName, $newLength);
1538 if ($customFields[$customFieldId]['data_type'] == 'Date') {
1539 if (!CRM_Utils_System
::isNull($value)) {
1540 $format = $customFields[$customFieldId]['date_format'];
1541 $date = CRM_Utils_Date
::processDate($value, NULL, FALSE, 'YmdHis', $format);
1546 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1547 $customFields[$customFieldId]['data_type'] == 'Money'
1553 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1554 $value = CRM_Utils_Rule
::cleanMoney($value);
1558 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1559 $customFields[$customFieldId]['data_type'] == 'Country'
1569 if ($customFields[$customFieldId]['data_type'] == 'File') {
1570 if (empty($value)) {
1574 $config = CRM_Core_Config
::singleton();
1576 // If we are already passing the file id as a value then retrieve and set the file data
1577 if (CRM_Utils_Rule
::integer($value)) {
1578 $fileDAO = new CRM_Core_DAO_File();
1579 $fileDAO->id
= $value;
1580 $fileDAO->find(TRUE);
1583 $fName = $fileDAO->uri
;
1584 $mimeType = $fileDAO->mime_type
;
1587 elseif (empty($value['name'])) {
1588 // Happens when calling the API to update custom fields values, but the filename
1589 // is empty, for an existing entity (in a specific case, was from a d7-webform
1590 // that was updating a relationship with a File customfield, so $value['id'] was
1591 // not empty, but the filename was empty.
1595 $fName = $value['name'];
1596 $mimeType = $value['type'];
1599 $filename = pathinfo($fName, PATHINFO_BASENAME
);
1601 // rename this file to go into the secure directory only if
1602 // user has uploaded new file not existing verfied on the basis of $fileID
1603 if (empty($fileID) && !rename($fName, $config->customFileUploadDir
. $filename)) {
1604 CRM_Core_Error
::statusBounce(ts('Could not move custom file to custom upload directory'));
1607 if ($customValueId && empty($fileID)) {
1612 $params = [1 => [$customValueId, 'Integer']];
1613 $fileID = CRM_Core_DAO
::singleValueQuery($query, $params);
1616 $fileDAO = new CRM_Core_DAO_File();
1619 $fileDAO->id
= $fileID;
1622 $fileDAO->uri
= $filename;
1623 $fileDAO->mime_type
= $mimeType;
1624 $fileDAO->upload_date
= date('YmdHis');
1626 $fileID = $fileDAO->id
;
1630 if (!is_array($customFormatted)) {
1631 $customFormatted = [];
1634 if (!array_key_exists($customFieldId, $customFormatted)) {
1635 $customFormatted[$customFieldId] = [];
1639 if ($customValueId) {
1640 $index = $customValueId;
1643 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1644 $customFormatted[$customFieldId][$index] = [];
1646 $customFormatted[$customFieldId][$index] = [
1647 'id' => $customValueId > 0 ?
$customValueId : NULL,
1649 'type' => $customFields[$customFieldId]['data_type'],
1650 'custom_field_id' => $customFieldId,
1651 'custom_group_id' => $groupID,
1652 'table_name' => $tableName,
1653 'column_name' => $columnName,
1654 'file_id' => $fileID,
1655 // is_multiple refers to the custom group, serialize refers to the field.
1656 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1657 'serialize' => $customFields[$customFieldId]['serialize'],
1660 //we need to sort so that custom fields are created in the order of entry
1661 krsort($customFormatted[$customFieldId]);
1662 return $customFormatted;
1666 * Get default custom table schema.
1668 * @param array $params
1672 public static function defaultCustomTableSchema($params) {
1673 // add the id and extends_id
1674 $collation = CRM_Core_BAO_SchemaHandler
::getInUseCollation();
1675 $characterSet = 'utf8';
1676 if (stripos($collation, 'utf8mb4') !== FALSE) {
1677 $characterSet = 'utf8mb4';
1680 'name' => $params['name'],
1681 'is_multiple' => $params['is_multiple'],
1682 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1686 'type' => 'int unsigned',
1689 'attributes' => 'AUTO_INCREMENT',
1690 'comment' => 'Default MySQL primary key',
1693 'name' => 'entity_id',
1694 'type' => 'int unsigned',
1696 'comment' => 'Table that this extends',
1697 'fk_table_name' => $params['extends_name'],
1698 'fk_field_name' => 'id',
1699 'fk_attributes' => 'ON DELETE CASCADE',
1704 // If on MySQL 5.6 include ROW_FORMAT=DYNAMIC to fix unit tests
1705 $databaseVersion = CRM_Utils_SQL
::getDatabaseVersion();
1706 if (version_compare($databaseVersion, '5.7', '<') && version_compare($databaseVersion, '5.6', '>=')) {
1707 $table['attributes'] = $table['attributes'] . ' ROW_FORMAT=DYNAMIC';
1710 if (!$params['is_multiple']) {
1711 $table['indexes'] = [
1714 'field_name_1' => 'entity_id',
1722 * Create custom field.
1724 * @param CRM_Core_DAO_CustomField $field
1725 * @param string $operation
1727 public static function createField($field, $operation) {
1728 $sql = str_repeat(' ', 8);
1729 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1730 $sql .= "ALTER TABLE " . $tableName;
1731 $sql .= self
::getAlterFieldSQL($field, $operation);
1733 // CRM-7007: do not i18n-rewrite this query
1734 CRM_Core_DAO
::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1736 $config = CRM_Core_Config
::singleton();
1737 if ($config->logging
) {
1738 // CRM-16717 not sure why this was originally limited to add.
1739 // For example custom tables can have field length changes - which need to flow through to logging.
1740 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
1741 if ($operation === 'add' ||
$operation === 'modify') {
1742 $logging = new CRM_Logging_Schema();
1743 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name
]]);
1747 Civi
::service('sql_triggers')->rebuild($tableName, TRUE);
1751 * @param CRM_Core_DAO_CustomField $field
1752 * @param string $operation add|modify|delete
1756 public static function getAlterFieldSQL($field, $operation) {
1757 $params = self
::prepareCreateParams($field, $operation);
1758 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1759 // a function only used by Custom Field creation...
1760 $params['required'] = FALSE;
1761 return CRM_Core_BAO_SchemaHandler
::getFieldAlterSQL($params);
1765 * Get query to reformat existing values for a field when changing its serialize attribute
1767 * @param CRM_Core_DAO_CustomField $field
1770 private static function getAlterSerializeSQL($field) {
1771 $table = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id
, 'table_name');
1772 $col = $field->column_name
;
1773 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
1774 if ($field->serialize
) {
1775 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1778 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1783 * Determine whether it would be safe to move a field.
1785 * @param int $fieldID
1786 * FK to civicrm_custom_field.
1787 * @param int $newGroupID
1788 * FK to civicrm_custom_group.
1791 * array(string) or TRUE
1793 public static function _moveFieldValidate($fieldID, $newGroupID) {
1796 $field = new CRM_Core_DAO_CustomField();
1797 $field->id
= $fieldID;
1798 if (!$field->find(TRUE)) {
1799 $errors['fieldID'] = ts('Invalid ID for custom field');
1803 $oldGroup = new CRM_Core_DAO_CustomGroup();
1804 $oldGroup->id
= $field->custom_group_id
;
1805 if (!$oldGroup->find(TRUE)) {
1806 $errors['fieldID'] = ts('Invalid ID for old custom group');
1810 $newGroup = new CRM_Core_DAO_CustomGroup();
1811 $newGroup->id
= $newGroupID;
1812 if (!$newGroup->find(TRUE)) {
1813 $errors['newGroupID'] = ts('Invalid ID for new custom group');
1819 FROM civicrm_custom_field a
1820 INNER JOIN civicrm_custom_field b
1822 AND a.label = b.label
1823 AND b.custom_group_id = %2
1826 1 => [$field->id
, 'Integer'],
1827 2 => [$newGroup->id
, 'Integer'],
1829 $count = CRM_Core_DAO
::singleValueQuery($query, $params);
1831 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1834 $tableName = $oldGroup->table_name
;
1835 $columnName = $field->column_name
;
1840 WHERE $columnName is not null
1842 $count = CRM_Core_DAO
::singleValueQuery($query);
1846 FROM civicrm_custom_group
1847 WHERE id IN ( %1, %2 )
1850 1 => [$oldGroup->id
, 'Integer'],
1851 2 => [$newGroup->id
, 'Integer'],
1854 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1856 while ($dao->fetch()) {
1857 $extends[] = $dao->extends;
1859 if ($extends[0] != $extends[1]) {
1860 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1864 return empty($errors) ?
TRUE : $errors;
1868 * Move a custom data field from one group (table) to another.
1870 * @param int $fieldID
1871 * FK to civicrm_custom_field.
1872 * @param int $newGroupID
1873 * FK to civicrm_custom_group.
1875 * @throws CRM_Core_Exception
1877 public static function moveField($fieldID, $newGroupID) {
1878 $validation = self
::_moveFieldValidate($fieldID, $newGroupID);
1879 if (TRUE !== $validation) {
1880 throw new CRM_Core_Exception(implode(' ', $validation));
1882 $field = new CRM_Core_DAO_CustomField();
1883 $field->id
= $fieldID;
1886 $newGroup = new CRM_Core_DAO_CustomGroup();
1887 $newGroup->id
= $newGroupID;
1888 $newGroup->find(TRUE);
1890 $oldGroup = new CRM_Core_DAO_CustomGroup();
1891 $oldGroup->id
= $field->custom_group_id
;
1892 $oldGroup->find(TRUE);
1895 $add->custom_group_id
= $newGroup->id
;
1896 self
::createField($add, 'add');
1898 $sql = "INSERT INTO {$newGroup->table_name} (entity_id, `{$field->column_name}`)
1899 SELECT entity_id, `{$field->column_name}` FROM {$oldGroup->table_name}
1900 ON DUPLICATE KEY UPDATE `{$field->column_name}` = {$oldGroup->table_name}.`{$field->column_name}`
1902 CRM_Core_DAO
::executeQuery($sql);
1905 $del->custom_group_id
= $oldGroup->id
;
1906 self
::createField($del, 'delete');
1910 CRM_Utils_System
::flushCache();
1914 * Create an option value for a custom field option group ID.
1916 * @param array $params
1917 * @param string $value
1918 * @param \CRM_Core_DAO_OptionGroup $optionGroup
1919 * @param string $index
1920 * @param string $dataType
1922 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup
$optionGroup, $index, $dataType) {
1923 if (strlen(trim($value))) {
1924 $optionValue = new CRM_Core_DAO_OptionValue();
1925 $optionValue->option_group_id
= $optionGroup->id
;
1926 $optionValue->label
= $params['option_label'][$index];
1927 $optionValue->name
= $params['option_name'][$index] ?? CRM_Utils_String
::titleToVar($params['option_label'][$index]);
1928 switch ($dataType) {
1930 $optionValue->value
= CRM_Utils_Rule
::cleanMoney($value);
1934 $optionValue->value
= intval($value);
1938 $optionValue->value
= floatval($value);
1942 $optionValue->value
= trim($value);
1945 $optionValue->weight
= $params['option_weight'][$index];
1946 $optionValue->is_active
= $params['option_status'][$index] ??
FALSE;
1947 $optionValue->description
= $params['option_description'][$index] ??
NULL;
1948 $optionValue->color
= $params['option_color'][$index] ??
NULL;
1949 $optionValue->icon
= $params['option_icon'][$index] ??
NULL;
1950 $optionValue->save();
1955 * Prepare for the create operation.
1957 * Munge params, create the option values if needed.
1959 * This could be called by a single create or a batchCreate.
1961 * @param array $params
1965 protected static function prepareCreate($params) {
1966 $op = empty($params['id']) ?
'create' : 'edit';
1967 CRM_Utils_Hook
::pre($op, 'CustomField', CRM_Utils_Array
::value('id', $params), $params);
1968 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
1969 if ($op === 'create') {
1970 CRM_Core_DAO
::setCreateDefaults($params, self
::getDefaults());
1971 if (!isset($params['column_name'])) {
1972 // if add mode & column_name not present, calculate it.
1973 $params['column_name'] = strtolower(CRM_Utils_String
::munge($params['label'], '_', 32));
1975 if (!isset($params['name'])) {
1976 $params['name'] = CRM_Utils_String
::munge($params['label'], '_', 64);
1980 $params['column_name'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
1983 $htmlType = $params['html_type'] ??
NULL;
1984 $dataType = $params['data_type'] ??
NULL;
1986 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1987 $params['date_format'] = Civi
::settings()->get('dateInputFormat');
1990 // Checkboxes are always serialized in current schema
1991 if ($htmlType == 'CheckBox') {
1992 $params['serialize'] = CRM_Core_DAO
::SERIALIZE_SEPARATOR_BOOKEND
;
1995 if (!empty($params['serialize'])) {
1996 if (isset($params['default_checkbox_option'])) {
1998 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
1999 if ($params['option_value'][$v]) {
2000 $defaultArray[] = $params['option_value'][$v];
2004 if (!empty($defaultArray)) {
2005 // also add the separator before and after the value per new convention (CRM-1604)
2006 $params['default_value'] = CRM_Utils_Array
::implodePadded($defaultArray);
2010 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
2011 $params['default_value'] = $params['option_value'][$params['default_option']];
2016 // create any option group & values if required
2017 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
2018 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
2019 //CRM-16659: if option_value then create an option group for this custom field.
2020 // An option_type of 2 would be a 'message' from the form layer not to handle
2021 // the option_values key. If not set then it is not ignored.
2022 $optionsType = (int) ($params['option_type'] ??
0);
2023 if (($optionsType !== 2 && empty($params['id']))
2024 && (empty($params['option_group_id']) && !empty($params['option_value'])
2027 // first create an option group for this custom group
2028 $optionGroup = new CRM_Core_DAO_OptionGroup();
2029 $optionGroup->name
= "{$params['column_name']}_" . date('YmdHis');
2030 $optionGroup->title
= $params['label'];
2031 $optionGroup->is_active
= 1;
2032 // Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
2033 $optionGroup->is_reserved
= 0;
2034 $optionGroup->data_type
= $dataType;
2035 $optionGroup->save();
2036 $params['option_group_id'] = $optionGroup->id
;
2037 if (!empty($params['option_value']) && is_array($params['option_value'])) {
2038 foreach ($params['option_value'] as $k => $v) {
2039 self
::createOptionValue($params, $v, $optionGroup, $k, $dataType);
2045 // Remove option group IDs from fields changed to Text html_type.
2046 if ($htmlType == 'Text') {
2047 $params['option_group_id'] = '';
2050 // check for orphan option groups
2051 if (!empty($params['option_group_id'])) {
2052 if (!empty($params['id'])) {
2053 self
::fixOptionGroups($params['id'], $params['option_group_id']);
2056 // if we do not have a default value
2057 // retrieve it from one of the other custom fields which use this option group
2058 if (empty($params['default_value'])) {
2059 //don't insert only value separator as default value, CRM-4579
2060 $defaultValue = self
::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
2062 if (!CRM_Utils_System
::isNull(explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $defaultValue))) {
2063 $params['default_value'] = $defaultValue;
2068 // Set default textarea attributes
2069 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
2070 $params['attributes'] = 'rows=4, cols=60';
2076 * Create database entry for custom field and related option groups.
2078 * @param array $params
2080 * @return CRM_Core_DAO_CustomField
2082 protected static function createCustomFieldRecord($params) {
2083 $transaction = new CRM_Core_Transaction();
2084 $params = self
::prepareCreate($params);
2086 $customField = new CRM_Core_DAO_CustomField();
2087 $customField->copyValues($params);
2088 $customField->save();
2090 //create/drop the index when we toggle the is_searchable flag
2091 $op = empty($params['id']) ?
'add' : 'modify';
2092 if ($op !== 'modify') {
2093 if ($params['is_append_field_id_to_column_name']) {
2094 $params['column_name'] .= "_{$customField->id}";
2096 $customField->column_name
= $params['column_name'];
2097 $customField->save();
2100 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2101 $transaction->commit();
2103 // make sure all values are present in the object for further processing
2104 $customField->find(TRUE);
2106 return $customField;
2113 protected static function getChangeSerialize($params) {
2114 if (isset($params['serialize']) && !empty($params['id'])) {
2115 if ($params['serialize'] != CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'serialize')) {
2116 return (int) $params['serialize'];
2123 * Move custom data from one contact to another.
2125 * This is currently the start of a refactoring. The theory is each
2126 * entity could have a 'move' function with a DAO default one to fall back on.
2128 * At the moment this only does a small part of the process - ie deleting a file field that
2129 * is about to be overwritten. However, the goal is the whole process around the move for
2130 * custom data should be in here.
2132 * This is currently called by the merge class but it makes sense that api could
2133 * expose move actions as moving (e.g) contributions feels like a common
2134 * ask that should be handled by the form layer.
2136 * @param int $oldContactID
2137 * @param int $newContactID
2138 * @param int[] $fieldIDs
2139 * Optional list field ids to move.
2141 * @throws \CiviCRM_API3_Exception
2142 * @throws \Exception
2144 public function move($oldContactID, $newContactID, $fieldIDs) {
2145 if (empty($fieldIDs)) {
2148 $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'];
2150 foreach ($fieldIDs as $fieldID) {
2151 $return[] = 'custom_' . $fieldID;
2153 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2154 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2156 // The moveAllBelongings function has functionality to move custom fields. It doesn't work very well...
2157 // @todo handle all fields here but more immediately Country since that is broken at the moment.
2158 $fieldTypesNotHandledInMergeAttempt = ['File'];
2159 foreach ($fields as $field) {
2160 $isMultiple = !empty($field['custom_group_id.is_multiple']);
2161 if ($field['data_type'] === 'File' && !$isMultiple) {
2162 if (!empty($oldContact['custom_' . $field['id']]) && !empty($newContact['custom_' . $field['id']])) {
2163 CRM_Core_BAO_File
::deleteFileReferences($oldContact['custom_' . $field['id']], $oldContactID, $field['id']);
2165 if (!empty($oldContact['custom_' . $field['id']])) {
2166 CRM_Core_DAO
::executeQuery("
2167 UPDATE civicrm_entity_file
2168 SET entity_id = $newContactID
2169 WHERE file_id = {$oldContact['custom_' . $field['id']]}"
2173 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2174 CRM_Core_DAO
::executeQuery(
2175 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2176 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2177 ON DUPLICATE KEY UPDATE
2178 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2185 * Get the database table name and column name for a custom field.
2187 * @param int $fieldID
2188 * The fieldID of the custom field.
2189 * @param bool $force
2190 * Force the sql to be run again (primarily used for tests).
2193 * fatal is fieldID does not exists, else array of tableName, columnName
2194 * @throws \CRM_Core_Exception
2196 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2197 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2198 $cache = CRM_Utils_Cache
::singleton();
2199 $fieldValues = $cache->get($cacheKey);
2200 if (empty($fieldValues) ||
$force) {
2202 SELECT cg.table_name, cf.column_name, cg.id
2203 FROM civicrm_custom_group cg,
2204 civicrm_custom_field cf
2205 WHERE cf.custom_group_id = cg.id
2207 $params = [1 => [$fieldID, 'Integer']];
2208 $dao = CRM_Core_DAO
::executeQuery($query, $params);
2210 if (!$dao->fetch()) {
2211 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2213 $fieldValues = [$dao->table_name
, $dao->column_name
, $dao->id
];
2214 $cache->set($cacheKey, $fieldValues);
2216 return $fieldValues;
2220 * Get custom option groups.
2222 * @deprecated Use the API OptionGroup.get
2224 * @param array $includeFieldIds
2225 * Ids of custom fields for which option groups must be included.
2227 * Currently this is required in the cases where option groups are to be included
2228 * for inactive fields : CRM-5369
2232 public static function customOptionGroup($includeFieldIds = NULL) {
2233 static $customOptionGroup = NULL;
2235 $cacheKey = (empty($includeFieldIds)) ?
'onlyActive' : 'force';
2236 if ($cacheKey == 'force') {
2237 $customOptionGroup[$cacheKey] = NULL;
2240 if (empty($customOptionGroup[$cacheKey])) {
2241 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2243 //support for single as well as array format.
2244 if (!empty($includeFieldIds)) {
2245 if (is_array($includeFieldIds)) {
2246 $includeFieldIds = implode(',', $includeFieldIds);
2248 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2252 SELECT g.id, g.title
2253 FROM civicrm_option_group g
2254 INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2255 WHERE {$whereClause}";
2257 $dao = CRM_Core_DAO
::executeQuery($query);
2258 while ($dao->fetch()) {
2259 $customOptionGroup[$cacheKey][$dao->id
] = $dao->title
;
2263 return $customOptionGroup[$cacheKey];
2267 * Get defaults for new entity.
2271 public static function getDefaults() {
2273 'is_required' => FALSE,
2274 'is_searchable' => FALSE,
2275 'in_selector' => FALSE,
2276 'is_search_range' => FALSE,
2277 //CRM-15792 - Custom field gets disabled if is_active not set
2278 // this would ideally be a mysql default.
2279 'is_active' => TRUE,
2285 * Fix orphan groups.
2287 * @param int $customFieldId
2289 * @param int $optionGroupId
2292 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2293 // check if option group belongs to any custom Field else delete
2294 // get the current option group
2295 $currentOptionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
2299 // get the updated option group
2300 // if both are same return
2301 if (!$currentOptionGroupId ||
$currentOptionGroupId == $optionGroupId) {
2305 // check if option group is related to any other field
2306 self
::checkOptionGroup($currentOptionGroupId);
2310 * Check if option group is related to more than one custom field.
2312 * @param int $optionGroupId
2315 public static function checkOptionGroup($optionGroupId) {
2318 FROM civicrm_custom_field
2319 WHERE option_group_id = {$optionGroupId}";
2321 $count = CRM_Core_DAO
::singleValueQuery($query);
2324 //delete the option group
2325 CRM_Core_BAO_OptionGroup
::del($optionGroupId);
2330 * Get option group default.
2332 * @param int $optionGroupId
2333 * @param bool $serialize
2335 * @return null|string
2337 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2339 SELECT default_value, serialize
2340 FROM civicrm_custom_field
2341 WHERE option_group_id = {$optionGroupId}
2342 AND default_value IS NOT NULL";
2344 $dao = CRM_Core_DAO
::executeQuery($query);
2345 while ($dao->fetch()) {
2346 if ($dao->serialize
== $serialize) {
2347 return $dao->default_value
;
2349 $defaultValue = $dao->default_value
;
2352 // Convert serialization
2353 if (isset($defaultValue) && $serialize) {
2354 return CRM_Utils_Array
::implodePadded([$defaultValue]);
2356 elseif (isset($defaultValue)) {
2357 return CRM_Utils_Array
::explodePadded($defaultValue)[0];
2363 * Post process function.
2365 * @param array $params
2366 * @param int $entityID
2367 * @param string $customFieldExtends
2368 * @param bool $inline
2369 * @param bool $checkPermissions
2373 public static function postProcess(
2376 $customFieldExtends,
2378 $checkPermissions = TRUE
2382 foreach ($params as $key => $value) {
2383 if ($customFieldInfo = CRM_Core_BAO_CustomField
::getKeyID($key, TRUE)) {
2385 // for autocomplete transfer hidden value instead of label
2386 if ($params[$key] && isset($params[$key . '_id'])) {
2387 $value = $params[$key . '_id'];
2390 // we need to append time with date
2391 if ($params[$key] && isset($params[$key . '_time'])) {
2392 $value .= ' ' . $params[$key . '_time'];
2395 CRM_Core_BAO_CustomField
::formatCustomField($customFieldInfo[0],
2398 $customFieldExtends,
2399 $customFieldInfo[1],
2410 * Get custom field ID from field/group name/title.
2412 * @param string $fieldName Field name or label
2413 * @param string|null $groupName (Optional) Group name or label
2414 * @param bool $fullString Whether to return "custom_123" or "123"
2416 * @return string|int|null
2417 * @throws \CiviCRM_API3_Exception
2419 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2420 $cacheKey = $groupName . '.' . $fieldName;
2421 if (!isset(Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2422 $customFieldParams = [
2423 'name' => $fieldName,
2424 'label' => $fieldName,
2425 'options' => ['or' => [["name", "label"]]],
2429 $customFieldParams['custom_group_id.name'] = $groupName;
2430 $customFieldParams['custom_group_id.title'] = $groupName;
2431 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2434 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2436 if (empty($field['id'])) {
2437 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2438 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2441 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2442 Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2447 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2449 return Civi
::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
2453 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
2459 public static function getNameFromID($ids) {
2460 if (is_array($ids)) {
2461 $ids = implode(',', $ids);
2464 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2465 FROM civicrm_custom_field f
2466 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2467 WHERE f.id IN ($ids)";
2469 $dao = CRM_Core_DAO
::executeQuery($sql);
2471 while ($dao->fetch()) {
2472 $result[$dao->id
] = [
2473 'field_name' => $dao->field_name
,
2474 'field_label' => $dao->field_label
,
2475 'group_name' => $dao->group_name
,
2476 'group_title' => $dao->group_title
,
2483 * Validate custom data.
2485 * @param array $params
2486 * Custom data submitted.
2487 * ie array( 'custom_1' => 'validate me' );
2490 * validation errors.
2492 public static function validateCustomData($params) {
2494 if (!is_array($params) ||
empty($params)) {
2498 //pick up profile fields.
2499 $profileFields = [];
2500 $ufGroupId = $params['ufGroupId'] ??
NULL;
2502 $profileFields = CRM_Core_BAO_UFGroup
::getFields($ufGroupId,
2504 CRM_Core_Action
::VIEW
2508 //lets start w/ params.
2509 foreach ($params as $key => $value) {
2510 $customFieldID = self
::getKeyID($key);
2511 if (!$customFieldID) {
2515 //load the structural info for given field.
2516 $field = new CRM_Core_DAO_CustomField();
2517 $field->id
= $customFieldID;
2518 if (!$field->find(TRUE)) {
2521 $dataType = $field->data_type
;
2523 $profileField = CRM_Utils_Array
::value($key, $profileFields, []);
2524 $fieldTitle = $profileField['title'] ??
NULL;
2525 $isRequired = $profileField['is_required'] ??
NULL;
2527 $fieldTitle = $field->label
;
2530 //no need to validate.
2531 if (CRM_Utils_System
::isNull($value) && !$isRequired) {
2535 //lets validate first for required field.
2536 if ($isRequired && CRM_Utils_System
::isNull($value)) {
2537 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
2541 //now time to take care of custom field form rules.
2542 $ruleName = $errorMsg = NULL;
2543 switch ($dataType) {
2545 $ruleName = 'integer';
2546 $errorMsg = ts('%1 must be an integer (whole number).',
2547 array(1 => $fieldTitle)
2552 $ruleName = 'money';
2553 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2554 array(1 => $fieldTitle)
2559 $ruleName = 'numeric';
2560 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2561 array(1 => $fieldTitle)
2566 $ruleName = 'wikiURL';
2567 $errorMsg = ts('%1 must be valid Website.',
2568 array(1 => $fieldTitle)
2573 if ($ruleName && !CRM_Utils_System
::isNull($value)) {
2575 $funName = "CRM_Utils_Rule::{$ruleName}";
2576 if (is_callable($funName)) {
2577 $valid = call_user_func($funName, $value);
2580 $errors[$key] = $errorMsg;
2589 * Is this field a multi record field.
2591 * @param int $customId
2595 public static function isMultiRecordField($customId) {
2596 $isMultipleWithGid = FALSE;
2597 if (!is_numeric($customId)) {
2598 $customId = self
::getKeyID($customId);
2600 if (is_numeric($customId)) {
2601 $sql = "SELECT cg.id cgId
2602 FROM civicrm_custom_group cg
2603 INNER JOIN civicrm_custom_field cf
2604 ON cg.id = cf.custom_group_id
2605 WHERE cf.id = %1 AND cg.is_multiple = 1";
2606 $params[1] = [$customId, 'Integer'];
2607 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
2608 if ($dao->fetch()) {
2610 $isMultipleWithGid = $dao->cgId
;
2615 return $isMultipleWithGid;
2619 * Does this field type have any select options?
2621 * @param array $field
2625 public static function hasOptions($field) {
2626 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2627 $field = (array) $field;
2628 // This will include boolean fields with Yes/No options.
2629 if (in_array($field['html_type'], ['Radio', 'CheckBox'])) {
2632 // Do this before the "Select" string search because date fields have a "Select Date" html_type
2633 // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list.
2634 if (in_array($field['data_type'], ['ContactReference', 'Date'])) {
2637 if (strpos($field['html_type'], 'Select') !== FALSE) {
2640 return !empty($field['option_group_id']);
2644 * Does this field store a serialized string?
2646 * @param CRM_Core_DAO_CustomField|array $field
2650 public static function isSerialized($field) {
2651 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2652 $html_type = is_object($field) ?
$field->html_type
: $field['html_type'];
2653 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2654 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2655 if ($html_type === 'CheckBox' ||
strpos($html_type, 'Multi') !== FALSE) {
2658 // Otherwise this is the new standard as of 5.27
2659 return is_object($field) ?
!empty($field->serialize
) : !empty($field['serialize']);
2663 * Get api entity for this field
2667 public function getEntity() {
2668 $entity = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id
, 'extends');
2669 return in_array($entity, ['Individual', 'Household', 'Organization']) ?
'Contact' : $entity;
2673 * Set pseudoconstant properties for field metadata.
2675 * @param array $field
2676 * @param string|null $optionGroupName
2678 private static function getOptionsForField(&$field, $optionGroupName) {
2679 if ($optionGroupName) {
2680 $field['pseudoconstant'] = [
2681 'optionGroupName' => $optionGroupName,
2682 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2685 elseif ($field['data_type'] == 'Boolean') {
2686 $field['pseudoconstant'] = [
2687 'callback' => 'CRM_Core_SelectValues::boolean',
2690 elseif ($field['data_type'] == 'Country') {
2691 $field['pseudoconstant'] = [
2692 'table' => 'civicrm_country',
2693 'keyColumn' => 'id',
2694 'labelColumn' => 'name',
2695 'nameColumn' => 'iso_code',
2698 elseif ($field['data_type'] == 'StateProvince') {
2699 $field['pseudoconstant'] = [
2700 'table' => 'civicrm_state_province',
2701 'keyColumn' => 'id',
2702 'labelColumn' => 'name',
2708 * @param CRM_Core_DAO_CustomField $field
2709 * @param 'add|modify|delete' $operation
2713 protected static function prepareCreateParams($field, $operation) {
2714 $tableName = CRM_Core_DAO
::getFieldValue(
2715 'CRM_Core_DAO_CustomGroup',
2716 $field->custom_group_id
,
2721 'table_name' => $tableName,
2722 'operation' => $operation,
2723 'name' => $field->column_name
,
2724 'type' => CRM_Core_BAO_CustomValueTable
::fieldToSQLType(
2728 'required' => $field->is_required
,
2729 'searchable' => $field->is_searchable
,
2732 // For adding/dropping FK constraints
2733 $params['fkName'] = CRM_Core_BAO_SchemaHandler
::getIndexName($tableName, $field->column_name
);
2736 'Country' => 'civicrm_country',
2737 'StateProvince' => 'civicrm_state_province',
2738 'ContactReference' => 'civicrm_contact',
2739 'File' => 'civicrm_file',
2741 if (isset($fkFields[$field->data_type
])) {
2742 // Serialized fields store value-separated strings which are incompatible with FK constraints
2743 if (!$field->serialize
) {
2744 $params['fk_table_name'] = $fkFields[$field->data_type
];
2745 $params['fk_field_name'] = 'id';
2746 $params['fk_attributes'] = 'ON DELETE SET NULL';
2749 if ($field->serialize
) {
2750 $params['type'] = 'varchar(255)';
2752 if (isset($field->default_value
)) {
2753 $params['default'] = "'{$field->default_value}'";