Merge pull request #21910 from eileenmcnaughton/new_validate
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
CommitLineData
6a488035
TO
1<?php
2/*
bc77d7c0
TO
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * Business objects for managing custom data fields.
6a488035
TO
20 */
21class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
22
6a488035
TO
23 /**
24 * Array to hold (formatted) fields for import
25 *
26 * @var array
6a488035
TO
27 */
28 public static $_importFields = NULL;
29
30 /**
fe482240 31 * Build and retrieve the list of data types and descriptions.
6a488035 32 *
a6c01b45
CW
33 * @return array
34 * Data type => Description
6a488035 35 */
5d2f1ed6
CW
36 public static function dataType() {
37 return [
38 'String' => ts('Alphanumeric'),
39 'Int' => ts('Integer'),
40 'Float' => ts('Number'),
41 'Money' => ts('Money'),
42 'Memo' => ts('Note'),
43 'Date' => ts('Date'),
44 'Boolean' => ts('Yes or No'),
45 'StateProvince' => ts('State/Province'),
46 'Country' => ts('Country'),
47 'File' => ts('File'),
48 'Link' => ts('Link'),
49 'ContactReference' => ts('Contact Reference'),
50 ];
6a488035
TO
51 }
52
89d4a22f 53 /**
54 * Build the map of custom field's data types and there respective Util type
55 *
56 * @return array
57 * Data data-type => CRM_Utils_Type
58 */
59 public static function dataToType() {
60 return [
61 'String' => CRM_Utils_Type::T_STRING,
62 'Int' => CRM_Utils_Type::T_INT,
8ad22b15 63 'Money' => CRM_Utils_Type::T_MONEY,
64 'Memo' => CRM_Utils_Type::T_LONGTEXT,
89d4a22f 65 'Float' => CRM_Utils_Type::T_FLOAT,
89d4a22f 66 'Date' => CRM_Utils_Type::T_DATE,
8ad22b15 67 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
89d4a22f 68 'Boolean' => CRM_Utils_Type::T_BOOLEAN,
69 'StateProvince' => CRM_Utils_Type::T_INT,
8ad22b15 70 'File' => CRM_Utils_Type::T_STRING,
89d4a22f 71 'Link' => CRM_Utils_Type::T_STRING,
72 'ContactReference' => CRM_Utils_Type::T_INT,
8ad22b15 73 'Country' => CRM_Utils_Type::T_INT,
89d4a22f 74 ];
75 }
76
6a488035 77 /**
fe482240 78 * Takes an associative array and creates a custom field object.
6a488035
TO
79 *
80 * This function is invoked from within the web form layer and also from the api layer
81 *
6a0b768e
TO
82 * @param array $params
83 * (reference) an assoc array of name/value pairs.
6a488035 84 *
16b10e64 85 * @return CRM_Core_DAO_CustomField
6a488035 86 */
f557467c 87 public static function create($params) {
051f2455 88 $changeSerialize = self::getChangeSerialize($params);
f45a42ba 89 $customField = self::createCustomFieldRecord($params);
051f2455
CW
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));
93 }
e43db21d 94 $op = empty($params['id']) ? 'add' : 'modify';
ead0c08f 95 self::createField($customField, $op);
051f2455
CW
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));
99 }
edd71ef9 100
e43db21d 101 CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField);
54ac54a2 102
6a488035 103 CRM_Utils_System::flushCache();
58159c3b 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();
6a488035
TO
107
108 return $customField;
109 }
110
e5ce15c3 111 /**
bfa91094 112 * Save multiple fields, now deprecated in favor of self::writeRecords.
e5ce15c3 113 * https://lab.civicrm.org/dev/core/issues/1093
bfa91094 114 * @deprecated
e5ce15c3 115 *
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.
0623f992 120 *
121 * @throws \CiviCRM_API3_Exception
e5ce15c3 122 */
d3e1ba37 123 public static function bulkSave($bulkParams, $defaults = []) {
402d501e 124 CRM_Core_Error::deprecatedFunctionWarning(__CLASS__ . '::writeRecords');
d3e1ba37 125 foreach ($bulkParams as $index => $fieldParams) {
bfa91094
CW
126 $bulkParams[$index] = array_merge($defaults, $fieldParams);
127 }
128 self::writeRecords($bulkParams);
129 }
130
131 /**
132 * Create/update several fields at once in a mysql efficient way.
133 *
134 * @param array $records
135 * @return CRM_Core_DAO_CustomField[]
136 * @throws CRM_Core_Exception
bfa91094 137 */
78b7cb89 138 public static function writeRecords(array $records): array {
bfa91094
CW
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);
142
051f2455 143 $changeSerialize = self::getChangeSerialize($params);
e5ce15c3 144 $customField = self::createCustomFieldRecord($params);
051f2455
CW
145 // Serialize/deserialize sql must run after/before the table is altered
146 if ($changeSerialize === 0) {
147 $pre[] = self::getAlterSerializeSQL($customField);
148 }
149 if ($changeSerialize === 1) {
150 $post[] = self::getAlterSerializeSQL($customField);
151 }
d3e1ba37 152 $fieldSQL = self::getAlterFieldSQL($customField, empty($params['id']) ? 'add' : 'modify');
bfa91094 153
03e3a32e
CW
154 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id, 'table_name');
155 $sql[$tableName][] = $fieldSQL;
0cb10002 156 $addedColumns[$tableName][] = $customField->column_name;
d3e1ba37 157 $customFields[$index] = $customField;
e5ce15c3 158 }
47e86ed3 159
051f2455
CW
160 foreach ($pre as $query) {
161 CRM_Core_DAO::executeQuery($query);
162 }
163
e5ce15c3 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);
47e86ed3
CR
167
168 if (CRM_Core_Config::singleton()->logging) {
169 $logging = new CRM_Logging_Schema();
170 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
171 }
172
03e3a32e 173 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
e5ce15c3 174 }
051f2455
CW
175
176 foreach ($post as $query) {
177 CRM_Core_DAO::executeQuery($query);
178 }
179
e5ce15c3 180 CRM_Utils_System::flushCache();
bfa91094
CW
181 Civi::cache('metadata')->clear();
182
d3e1ba37 183 foreach ($customFields as $index => $customField) {
bfa91094 184 CRM_Utils_Hook::post(empty($records[$index]['id']) ? 'create' : 'edit', 'CustomField', $customField->id, $customField);
e5ce15c3 185 }
bfa91094 186 return $customFields;
e5ce15c3 187 }
188
6a488035 189 /**
fe482240 190 * Fetch object based on array of properties.
6a488035 191 *
6a0b768e 192 * @param array $params
9b85cd33 193 * An assoc array of name/value pairs.
6a0b768e
TO
194 * @param array $defaults
195 * (reference ) an assoc array to hold the flattened values.
6a488035 196 *
16b10e64 197 * @return CRM_Core_DAO_CustomField
6a488035 198 */
9b85cd33 199 public static function retrieve($params, &$defaults) {
6a488035
TO
200 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
201 }
202
203 /**
fe482240 204 * Update the is_active flag in the db.
6a488035 205 *
6a0b768e
TO
206 * @param int $id
207 * Id of the database record.
208 * @param bool $is_active
209 * Value we want to set the is_active field.
6a488035 210 *
8a4fede3 211 * @return bool
212 * true if we found and updated the object, else false
6a488035 213 */
00be9182 214 public static function setIsActive($id, $is_active) {
6a488035
TO
215
216 CRM_Utils_System::flushCache();
217
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);
221 }
222
223 /**
224 * Get the field title.
225 *
6a0b768e
TO
226 * @param int $id
227 * Id of field.
6a488035 228 *
a6c01b45
CW
229 * @return string
230 * name
6a488035
TO
231 */
232 public static function getTitle($id) {
233 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
234 }
235
2921fb78
CW
236 /**
237 * @param string $context
238 * @return array|bool
239 */
3b66f502 240 public function getOptions($context = NULL) {
2921fb78 241 CRM_Core_DAO::buildOptionsContext($context);
2921fb78
CW
242
243 if (!$this->id) {
244 return FALSE;
245 }
cec0bbb8 246 $cacheKey = "CRM_Core_BAO_CustomField_getOptions_{$this->id}_$context";
c67c18bb
JG
247 $cache = CRM_Utils_Cache::singleton();
248 $options = $cache->get($cacheKey);
249 if (!isset($options)) {
250 if (!$this->data_type || !$this->custom_group_id) {
251 $this->find(TRUE);
252 }
2921fb78 253
c67c18bb
JG
254 // This will hold the list of options in format key => label
255 $options = [];
6c5d3fc7 256
c67c18bb
JG
257 if (!empty($this->option_group_id)) {
258 $options = CRM_Core_OptionGroup::valuesByID(
1453c6db 259 $this->option_group_id, FALSE, FALSE, FALSE, $context == 'validate' ? 'name' : 'label', !($context == 'validate' || $context == 'get')
c67c18bb
JG
260 );
261 }
262 elseif ($this->data_type === 'StateProvince') {
263 $options = CRM_Core_PseudoConstant::stateProvince();
264 }
265 elseif ($this->data_type === 'Country') {
266 $options = $context == 'validate' ? CRM_Core_PseudoConstant::countryIsoCode() : CRM_Core_PseudoConstant::country();
267 }
268 elseif ($this->data_type === 'Boolean') {
5f627bb5 269 $options = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
c67c18bb
JG
270 }
271 CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
5f627bb5 272 CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, ['context' => $context]);
c67c18bb 273 $cache->set($cacheKey, $options);
3b66f502 274 }
2921fb78
CW
275 return $options;
276 }
277
3221e413
CW
278 /**
279 * @inheritDoc
280 */
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';
a7bcb32d 286 $options['Select Country'] = 'Select Country';
3221e413 287 $options['Multi-Select Country'] = 'Multi-Select Country';
a7bcb32d 288 $options['Select State/Province'] = 'Select State/Province';
3221e413
CW
289 $options['Multi-Select State/Province'] = 'Multi-Select State/Province';
290 }
291 return $options;
292 }
293
6a488035
TO
294 /**
295 * Store and return an array of all active custom fields.
296 *
6a0b768e 297 * @param string $customDataType
1f61a7b1 298 * Type of Custom Data; 'ANY' is a synonym for "all contact data types".
6a0b768e
TO
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.
317103ab
CW
311 * @param bool|int $checkPermission
312 * Either a CRM_Core_Permission constant or FALSE to disable checks
6a488035 313 *
a6c01b45
CW
314 * @return array
315 * an array of active custom fields.
1fbda76b 316 * @throws \CRM_Core_Exception
6a488035 317 */
f9f40af3
TO
318 public static function &getFields(
319 $customDataType = 'Individual',
320 $showAll = FALSE,
321 $inline = FALSE,
322 $customDataSubType = NULL,
323 $customDataSubName = NULL,
324 $onlyParent = FALSE,
325 $onlySubType = FALSE,
317103ab 326 $checkPermission = CRM_Core_Permission::EDIT
6a488035 327 ) {
317103ab
CW
328 if ($checkPermission === TRUE) {
329 CRM_Core_Error::deprecatedWarning('Unexpected TRUE passed to CustomField::getFields $checkPermission param.');
330 $checkPermission = CRM_Core_Permission::EDIT;
331 }
0400dfac 332 if (empty($customDataType)) {
5f627bb5 333 $customDataType = ['Contact', 'Individual', 'Organization', 'Household'];
0400dfac 334 }
1f61a7b1 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;
339 }
645a0241 340 if ($customDataType && !is_array($customDataType)) {
6a488035 341
c3f7ab62 342 if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes(), TRUE)) {
6a488035
TO
343 // This is the case when getFieldsForImport() requires fields
344 // limited strictly to a subtype.
345 $customDataSubType = $customDataType;
645a0241
TO
346 $customDataType = CRM_Contact_BAO_ContactType::getBasicType($customDataType);
347 $onlySubType = TRUE;
6a488035
TO
348 }
349
c3f7ab62 350 if (array_key_exists($customDataType, CRM_Core_SelectValues::customGroupExtends())) {
6a488035
TO
351 // this makes the method flexible to support retrieving fields
352 // for multiple extends value.
5f627bb5 353 $customDataType = [$customDataType];
6a488035
TO
354 }
355 }
356
5439e6fb 357 $customDataSubType = CRM_Utils_Array::explodePadded($customDataSubType);
6a488035
TO
358
359 if (is_array($customDataType)) {
360 $cacheKey = implode('_', $customDataType);
361 }
362 else {
363 $cacheKey = $customDataType;
364 }
365
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_';
317103ab 372 $cacheKey .= $checkPermission ? $checkPermission . CRM_Core_Session::getLoggedInContactID() . '_' : '_0_0_';
f4de748f 373 $cacheKey .= '_' . CRM_Core_Config::domainID() . '_';
6a488035
TO
374
375 $cgTable = CRM_Core_DAO_CustomGroup::getTableName();
376
377 // also get the permission stuff here
378 if ($checkPermission) {
317103ab 379 $permissionClause = CRM_Core_Permission::customGroupClause($checkPermission,
6a488035
TO
380 "{$cgTable}."
381 );
382 }
383 else {
384 $permissionClause = '(1)';
385 }
386
387 // lets md5 permission clause and take first 8 characters
388 $cacheKey .= substr(md5($permissionClause), 0, 8);
389
390 if (strlen($cacheKey) > 40) {
391 $cacheKey = md5($cacheKey);
392 }
393
394 if (!self::$_importFields ||
395 CRM_Utils_Array::value($cacheKey, self::$_importFields) === NULL
396 ) {
397 if (!self::$_importFields) {
5f627bb5 398 self::$_importFields = [];
6a488035
TO
399 }
400
401 // check if we can retrieve from database cache
9cdf85c1 402 $fields = Civi::Cache('fields')->get("custom importableFields $cacheKey");
6a488035
TO
403
404 if ($fields === NULL) {
6a488035
TO
405
406 $extends = '';
407 if (is_array($customDataType)) {
408 $value = NULL;
409 foreach ($customDataType as $dataType) {
c3f7ab62 410 if (array_key_exists($dataType, CRM_Core_SelectValues::customGroupExtends())) {
5f627bb5 411 if (in_array($dataType, ['Individual', 'Household', 'Organization'])) {
6a488035
TO
412 $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' ";
413 }
414 else {
415 $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "'";
416 }
417 $value = $value ? $value . ", {$val}" : $val;
418 }
419 }
420 if ($value) {
421 $extends = "AND $cgTable.extends IN ( $value ) ";
422 }
423 }
424
4f57b83f 425 if (!empty($customDataType) && empty($extends)) {
0400dfac 426 // $customDataType specified a filter, but there is no corresponding SQL ($extends)
5f627bb5 427 self::$_importFields[$cacheKey] = [];
837afcfe
TO
428 return self::$_importFields[$cacheKey];
429 }
430
6a488035
TO
431 if ($onlyParent) {
432 $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL ";
433 }
1fbda76b 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.
84ad04e8 437 $serializeField = CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1') ? "custom_field.serialize," : '';
6a488035 438
84ad04e8 439 $query = "SELECT custom_field.id, custom_field.label,
6a488035 440 $cgTable.title,
84ad04e8 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,
6a488035
TO
449 $cgTable.extends_entity_column_value,
450 $cgTable.extends_entity_column_id,
84ad04e8 451 custom_field.is_view,
452 custom_field.option_group_id,
453 custom_field.date_format,
454 custom_field.time_format,
14f42e31 455 $cgTable.is_multiple,
1fbda76b 456 $serializeField
a1120b3c 457 $cgTable.table_name,
14f42e31 458 og.name as option_group_name
84ad04e8 459 FROM civicrm_custom_field custom_field
6a488035 460 INNER JOIN $cgTable
84ad04e8 461 ON custom_field.custom_group_id = $cgTable.id
14f42e31 462 LEFT JOIN civicrm_option_group og
84ad04e8 463 ON custom_field.option_group_id = og.id
6a488035
TO
464 WHERE ( 1 ) ";
465
466 if (!$showAll) {
84ad04e8 467 $query .= " AND custom_field.is_active = 1 AND $cgTable.is_active = 1 ";
6a488035
TO
468 }
469
470 if ($inline) {
471 $query .= " AND $cgTable.style = 'Inline' ";
472 }
473
474 //get the custom fields for specific type in
475 //combination with fields those support any type.
476 if (!empty($customDataSubType)) {
5f627bb5 477 $subtypeClause = [];
6a488035 478 foreach ($customDataSubType as $subtype) {
9a760581 479 $subtype = CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($subtype, 'String') . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
480 $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'";
481 }
482 if (!$onlySubType) {
483 $subtypeClause[] = "$cgTable.extends_entity_column_value IS NULL";
484 }
485 $query .= " AND ( " . implode(' OR ', $subtypeClause) . " )";
486 }
487
488 if ($customDataSubName) {
489 $query .= " AND ( $cgTable.extends_entity_column_id = $customDataSubName ) ";
490 }
491
492 // also get the permission stuff here
493 if ($checkPermission) {
317103ab 494 $permissionClause = CRM_Core_Permission::customGroupClause($checkPermission,
6a488035
TO
495 "{$cgTable}.", TRUE
496 );
497 }
498 else {
499 $permissionClause = '(1)';
500 }
501
502 $query .= " $extends AND $permissionClause
503 ORDER BY $cgTable.weight, $cgTable.title,
84ad04e8 504 custom_field.weight, custom_field.label";
6a488035
TO
505
506 $dao = CRM_Core_DAO::executeQuery($query);
507
5f627bb5 508 $fields = [];
6a488035 509 while (($dao->fetch()) != NULL) {
cef2e96c 510 $regexp = preg_replace('/[.,;:!?]/', '', NULL);
1f61a7b1 511 $fields[$dao->id]['id'] = $dao->id;
6a488035 512 $fields[$dao->id]['label'] = $dao->label;
cef2e96c 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;
6a488035
TO
518 $fields[$dao->id]['groupTitle'] = $dao->title;
519 $fields[$dao->id]['data_type'] = $dao->data_type;
cef2e96c 520 $fields[$dao->id]['name'] = 'custom_' . $dao->id;
521 $fields[$dao->id]['type'] = CRM_Utils_Array::value($dao->data_type, self::dataToType());
6a488035 522 $fields[$dao->id]['html_type'] = $dao->html_type;
a389ed51 523 $fields[$dao->id]['default_value'] = $dao->default_value;
6a488035
TO
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;
817cf27c 536 $fields[$dao->id]['is_required'] = $dao->is_required;
a1120b3c
CW
537 $fields[$dao->id]['table_name'] = $dao->table_name;
538 $fields[$dao->id]['column_name'] = $dao->column_name;
d51a6a62 539 $fields[$dao->id]['serialize'] = $serializeField ? $dao->serialize : (int) self::isSerialized($dao);
cef2e96c 540 $fields[$dao->id]['where'] = $dao->table_name . '.' . $dao->column_name;
1f61a7b1 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';
546 }
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'];
14f42e31 549 self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
6a488035
TO
550 }
551
9cdf85c1 552 Civi::cache('fields')->set("custom importableFields $cacheKey", $fields);
6a488035
TO
553 }
554 self::$_importFields[$cacheKey] = $fields;
555 }
556
557 return self::$_importFields[$cacheKey];
558 }
559
560 /**
317103ab
CW
561 * Return field ids and names (with groups).
562 *
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.
6a488035 565 *
2a6da8d7 566 * @param int|string $contactType Contact type
6a0b768e
TO
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.
317103ab
CW
573 * @param bool|int $checkPermission
574 * Either a CRM_Core_Permission constant or FALSE to disable checks
2a6da8d7
EM
575 *
576 * @param bool $withMultiple
6a488035 577 *
a6c01b45 578 * @return array
6a488035 579 */
b60efd6a 580 public static function getFieldsForImport(
f9f40af3
TO
581 $contactType = 'Individual',
582 $showAll = FALSE,
583 $onlyParent = FALSE,
584 $search = FALSE,
6a488035 585 $checkPermission = TRUE,
f9f40af3 586 $withMultiple = FALSE
6a488035 587 ) {
317103ab
CW
588 if ($checkPermission === TRUE) {
589 // TODO: Trigger deprecation notice for passing TRUE
590 $checkPermission = CRM_Core_Permission::EDIT;
591 }
6a488035
TO
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,
595 $showAll,
596 FALSE,
597 NULL,
598 NULL,
599 $onlyParent,
600 FALSE,
601 $checkPermission
602 );
603
cef2e96c 604 $importableFields = [];
6a488035
TO
605 foreach ($fields as $id => $values) {
606 // for now we should not allow multiple fields in profile / export etc, hence unsetting
607 if (!$search &&
8cc574cf 608 (!empty($values['is_multiple']) && !$withMultiple)
6a488035
TO
609 ) {
610 continue;
611 }
b7d6b1fa 612 if (!empty($values['text_length'])) {
613 $values['maxlength'] = (int) $values['text_length'];
614 }
6a488035
TO
615
616 /* generate the key for the fields array */
617
618 $key = "custom_$id";
cef2e96c 619 $importableFields[$key] = $values;
620 $importableFields[$key]['import'] = 1;
6a488035
TO
621 }
622
623 return $importableFields;
624 }
625
626 /**
fe482240 627 * Get the field id from an import key.
6a488035 628 *
6a0b768e
TO
629 * @param string $key
630 * The key to parse.
6a488035 631 *
2a6da8d7 632 * @param bool $all
b60efd6a 633 *
72b3a70c
CW
634 * @return int|null
635 * The id (if exists)
6a488035
TO
636 */
637 public static function getKeyID($key, $all = FALSE) {
5f627bb5 638 $match = [];
6a488035
TO
639 if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) {
640 if (!$all) {
641 return $match[1];
642 }
643 else {
5f627bb5 644 return [
6a488035
TO
645 $match[1],
646 CRM_Utils_Array::value(2, $match),
5f627bb5 647 ];
6a488035
TO
648 }
649 }
5f627bb5 650 return $all ? [NULL, NULL] : NULL;
6a488035
TO
651 }
652
653 /**
fe482240 654 * Use the cache to get all values of a specific custom field.
6a488035 655 *
6a0b768e
TO
656 * @param int $fieldID
657 * The custom field ID.
6a488035 658 *
01057d41 659 * @return CRM_Core_BAO_CustomField
5c766a0b 660 * The field object.
ac15829d 661 * @throws CRM_Core_Exception
6a488035 662 */
00be9182 663 public static function getFieldObject($fieldID) {
01057d41 664 $field = new CRM_Core_BAO_CustomField();
6a488035
TO
665
666 // check if we can get the field values from the system cache
f9f40af3
TO
667 $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
668 $cache = CRM_Utils_Cache::singleton();
6a488035
TO
669 $fieldValues = $cache->get($cacheKey);
670 if (empty($fieldValues)) {
671 $field->id = $fieldID;
672 if (!$field->find(TRUE)) {
ac15829d 673 throw new CRM_Core_Exception('Cannot find Custom Field');
6a488035
TO
674 }
675
5f627bb5 676 $fieldValues = [];
6a488035
TO
677 CRM_Core_DAO::storeValues($field, $fieldValues);
678
679 $cache->set($cacheKey, $fieldValues);
680 }
681 else {
682 $field->copyValues($fieldValues);
683 }
684
685 return $field;
686 }
687
688 /**
3a7773be 689 * Add a custom field to an existing form.
6a488035 690 *
6a0b768e
TO
691 * @param CRM_Core_Form $qf
692 * Form object (reference).
693 * @param string $elementName
694 * Name of the custom field.
100fef9d 695 * @param int $fieldId
6a0b768e
TO
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.
3a7773be
CW
702 * @return \HTML_QuickForm_Element|null
703 * @throws \CiviCRM_API3_Exception
6a488035 704 */
f9f40af3 705 public static function addQuickFormElement(
3a7773be 706 $qf, $elementName, $fieldId, $useRequired = TRUE, $search = FALSE, $label = NULL
6a488035 707 ) {
6a488035 708 $field = self::getFieldObject($fieldId);
3130209f 709 $widget = $field->html_type;
2b31bc15 710 $element = NULL;
5f627bb5 711 $customFieldAttributes = [];
2dd1b730 712
a2eb6152
AH
713 if (!isset($label)) {
714 $label = $field->label;
715 }
716
4367e964
AH
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);
720
a7d0519b 721 // Custom field HTML should indicate group+field name
be09038f 722 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id);
4367e964 723 $fieldAttributes['data-crm-custom'] = $groupName . ':' . $field->name;
2dd1b730 724
6a488035 725 // Fixed for Issue CRM-2183
3130209f
CW
726 if ($widget == 'TextArea' && $search) {
727 $widget = 'Text';
728 }
729
a2eb6152 730 $placeholder = $search ? ts('- any %1 -', [1 => $label]) : ts('- select %1 -', [1 => $label]);
6a488035 731
4367e964 732 if (in_array($widget, [
f9f40af3 733 'Select',
f9f40af3 734 'CheckBox',
21dfd5f5 735 'Radio',
4367e964 736 ])) {
01057d41 737 $options = $field->getOptions($search ? 'search' : 'create');
3130209f
CW
738
739 // Consolidate widget types to simplify the below switch statement
4367e964 740 if ($search) {
3130209f
CW
741 $widget = 'Select';
742 }
3ef93345 743
3130209f 744 // Search field is always multi-select
3221e413 745 if ($search || (self::isSerialized($field) && $widget === 'Select')) {
190240f0 746 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' huge');
4367e964
AH
747 $fieldAttributes['multiple'] = 'multiple';
748 $fieldAttributes['placeholder'] = $placeholder;
3130209f 749 }
3ef93345 750
3130209f 751 // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
3ef93345 752 $canEditOptions = CRM_Core_Permission::check('administer CiviCRM');
4367e964 753 if ($field->option_group_id && !$search && $canEditOptions) {
5f627bb5 754 $customFieldAttributes += [
01057d41 755 'data-api-entity' => $field->getEntity(),
3130209f
CW
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),
5f627bb5 758 ];
4367e964 759 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
8a6cfaa9 760 }
8a6cfaa9
CW
761 }
762
0afd6ed2
CW
763 $rangeDataTypes = ['Int', 'Float', 'Money'];
764
b60efd6a 765 // at some point in time we might want to split the below into small functions
6a488035 766
3130209f 767 switch ($widget) {
6a488035 768 case 'Text':
3130209f 769 case 'Link':
0afd6ed2 770 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
4367e964
AH
771 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
772 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
6a488035
TO
773 }
774 else {
a6e63388 775 if ($field->text_length) {
4367e964 776 $fieldAttributes['maxlength'] = $field->text_length;
a6e63388 777 if ($field->text_length < 20) {
4367e964 778 $fieldAttributes['size'] = $field->text_length;
a6e63388 779 }
780 }
2b31bc15 781 $element = $qf->add('text', $elementName, $label,
4367e964 782 $fieldAttributes,
6a488035
TO
783 $useRequired && !$search
784 );
785 }
786 break;
787
788 case 'TextArea':
4367e964
AH
789 $fieldAttributes['rows'] = $field->note_rows ?? 4;
790 $fieldAttributes['cols'] = $field->note_columns ?? 60;
791
2f940a36 792 if ($field->text_length) {
4367e964 793 $fieldAttributes['maxlength'] = $field->text_length;
2f940a36 794 }
2b31bc15 795 $element = $qf->add('textarea',
6a488035
TO
796 $elementName,
797 $label,
4367e964 798 $fieldAttributes,
6a488035
TO
799 $useRequired && !$search
800 );
801 break;
802
803 case 'Select Date':
153b5662
WA
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;
807
5f627bb5 808 $params = [
013ac5df 809 'date' => $field->date_format,
153b5662 810 'minDate' => isset($minYear) ? $minYear . '-01-01' : NULL,
d696827c 811 //CRM-18487 - max date should be the last date of the year.
812 'maxDate' => isset($maxYear) ? $maxYear . '-12-31' : NULL,
013ac5df 813 'time' => $field->time_format ? $field->time_format * 12 : FALSE,
5f627bb5 814 ];
6a488035 815 if ($field->is_search_range && $search) {
4367e964
AH
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);
6a488035
TO
818 }
819 else {
4367e964 820 $element = $qf->add('datepicker', $elementName, $label, $fieldAttributes, $useRequired && !$search, $params);
6a488035
TO
821 }
822 break;
823
824 case 'Radio':
0afd6ed2 825 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
4367e964
AH
826 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
827 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
6a488035 828 }
8a4f27dc 829 else {
4367e964 830 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
44e7c198 831 if ($search || empty($useRequired)) {
4367e964 832 $fieldAttributes['allowClear'] = TRUE;
44e7c198 833 }
4367e964 834 $qf->addRadio($elementName, $label, $options, $fieldAttributes, NULL, $useRequired);
8a4f27dc 835 }
6a488035
TO
836 break;
837
3130209f 838 // For all select elements
6a488035 839 case 'Select':
190240f0 840 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-select2');
e71fb06f
KE
841
842 if (empty($fieldAttributes['multiple'])) {
843 $options = ['' => $placeholder] + $options;
3130209f 844 }
e71fb06f
KE
845 $element = $qf->add('select', $elementName, $label, $options, $useRequired && !$search, $fieldAttributes);
846
847 // Add and/or option for fields that store multiple values
848 if ($search && self::isSerialized($field)) {
849 $qf->addRadio($elementName . '_operator', '', [
850 'or' => ts('Any'),
851 'and' => ts('All'),
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')],
855 ]);
856 $qf->setDefaults([$elementName . '_operator' => 'or']);
6a488035 857 }
3130209f 858 break;
6a488035 859
6a488035 860 case 'CheckBox':
5f627bb5 861 $check = [];
3130209f 862 foreach ($options as $v => $l) {
4367e964
AH
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.
3ef93345 866 $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, $customFieldAttributes);
6a488035 867 }
3ef93345
MD
868
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]);
873 }
874
6a488035 875 if ($useRequired && !$search) {
5f627bb5 876 $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
6a488035
TO
877 }
878 break;
879
880 case 'File':
881 // we should not build upload file in search mode
882 if ($search) {
2b31bc15 883 return NULL;
6a488035 884 }
2b31bc15 885 $element = $qf->add(
6a488035
TO
886 strtolower($field->html_type),
887 $elementName,
888 $label,
4367e964 889 $fieldAttributes,
6a488035
TO
890 $useRequired && !$search
891 );
892 $qf->addUploadElement($elementName);
893 break;
894
6a488035 895 case 'RichTextEditor':
4367e964
AH
896 $fieldAttributes['rows'] = $field->note_rows;
897 $fieldAttributes['cols'] = $field->note_columns;
2f940a36 898 if ($field->text_length) {
4367e964 899 $fieldAttributes['maxlength'] = $field->text_length;
2f940a36 900 }
4367e964 901 $element = $qf->add('wysiwyg', $elementName, $label, $fieldAttributes, $useRequired && !$search);
6a488035
TO
902 break;
903
904 case 'Autocomplete-Select':
5f627bb5 905 static $customUrls = [];
6a488035 906 if ($field->data_type == 'ContactReference') {
f40e966a 907 // break if contact does not have permission to access ContactReference
908 if (!CRM_Core_Permission::check('access contact reference fields')) {
909 break;
910 }
190240f0 911 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-form-contact-reference huge');
4367e964 912 $fieldAttributes['data-api-entity'] = 'Contact';
a61d2ab7
CW
913 if (!empty($field->serialize) || $search) {
914 $fieldAttributes['multiple'] = TRUE;
915 }
4367e964 916 $element = $qf->add('text', $elementName, $label, $fieldAttributes, $useRequired && !$search);
1b4d9e39 917
6a488035 918 $urlParams = "context=customfield&id={$field->id}";
8d90652e
AF
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
c94a15af 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, ']')) {
8d90652e
AF
925 $idOfelement = substr(substr($elementName, (strpos($elementName, '[') + 1)), 0, -1);
926 }
927 $customUrls[$idOfelement] = CRM_Utils_System::url('civicrm/ajax/contactref',
6a488035
TO
928 $urlParams,
929 FALSE, NULL, FALSE
930 );
931
6a488035
TO
932 }
933 else {
2fea9ed9 934 // FIXME: This won't work with customFieldOptions hook
4367e964 935 $fieldAttributes += [
af00ced5 936 'entity' => 'OptionValue',
1b4d9e39 937 'placeholder' => $placeholder,
0bdc1b6d 938 'multiple' => $search ? TRUE : !empty($field->serialize),
5f627bb5
SL
939 'api' => [
940 'params' => ['option_group_id' => $field->option_group_id, 'is_active' => 1],
941 ],
942 ];
4367e964 943 $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
6a488035
TO
944 }
945
946 $qf->assign('customUrls', $customUrls);
947 break;
948 }
949
950 switch ($field->data_type) {
951 case 'Int':
952 // integers will have numeric rule applied to them.
e71fb06f 953 if ($field->is_search_range && $search && $widget != 'Select') {
5f627bb5
SL
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');
6a488035 956 }
3130209f 957 elseif ($widget == 'Text') {
5f627bb5 958 $qf->addRule($elementName, ts('%1 must be an integer (whole number).', [1 => $label]), 'integer');
6a488035
TO
959 }
960 break;
961
962 case 'Float':
963 if ($field->is_search_range && $search) {
5f627bb5
SL
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');
6a488035 966 }
3130209f 967 elseif ($widget == 'Text') {
5f627bb5 968 $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', [1 => $label]), 'numeric');
6a488035
TO
969 }
970 break;
971
972 case 'Money':
973 if ($field->is_search_range && $search) {
5f627bb5
SL
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');
6a488035 976 }
3130209f 977 elseif ($widget == 'Text') {
5f627bb5 978 $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
6a488035
TO
979 }
980 break;
981
982 case 'Link':
3130209f 983 $element->setAttribute('class', "url");
eef9a6da 984 $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
6a488035
TO
985 break;
986 }
987 if ($field->is_view && !$search) {
988 $qf->freeze($elementName);
989 }
2b31bc15 990 return $element;
6a488035
TO
991 }
992
4367e964
AH
993 /**
994 * Take a string of HTML element attributes and turn it into an associative
995 * array.
996 *
997 * @param string $attrString
998 * The attributes as a string, e.g. `rows=3 cols=40`.
999 *
1000 * @return array
1001 * The attributes as an array, e.g. `['rows' => 3, 'cols' => 40]`.
1002 */
1003 public static function attributesFromString($attrString) {
1004 $attributes = [];
1005 foreach (explode(' ', $attrString) as $at) {
1006 if (strpos($at, '=')) {
1007 list($k, $v) = explode('=', $at);
1008 $attributes[$k] = trim($v, ' "');
1009 }
1010 }
1011 return $attributes;
1012 }
1013
6a488035
TO
1014 /**
1015 * Delete the Custom Field.
1016 *
6a0b768e
TO
1017 * @param object $field
1018 * The field object.
6a488035
TO
1019 */
1020 public static function deleteField($field) {
1021 CRM_Utils_System::flushCache();
1022
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);
1028 }
6a488035
TO
1029 // next drop the column from the custom value table
1030 self::createField($field, 'delete');
1031
1032 $field->delete();
1033 CRM_Core_BAO_UFField::delUFField($field->id);
751dec04 1034 CRM_Core_BAO_Mapping::removeFieldFromMapping('custom_' . $field->id);
6a488035 1035 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');
54ac54a2
CW
1036
1037 CRM_Utils_Hook::post('delete', 'CustomField', $field->id, $field);
6a488035
TO
1038 }
1039
1040 /**
3b66f502 1041 * @param string|int|array|null $value
28241a61 1042 * @param CRM_Core_BAO_CustomField|int|array|string $field
cf346323 1043 * @param int $entityId
3b66f502 1044 *
51a3717f 1045 * @return string
9113bb82 1046 *
1047 * @throws \CRM_Core_Exception
3b66f502 1048 */
cf346323 1049 public static function displayValue($value, $field, $entityId = NULL) {
28241a61 1050 $field = is_array($field) ? $field['id'] : $field;
51a3717f
CW
1051 $fieldId = is_object($field) ? $field->id : (int) str_replace('custom_', '', $field);
1052
1053 if (!$fieldId) {
9113bb82 1054 throw new CRM_Core_Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
51a3717f 1055 }
3b66f502 1056
01057d41
CW
1057 if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
1058 $field = self::getFieldObject($fieldId);
3b66f502 1059 }
01057d41 1060
5f627bb5 1061 $fieldInfo = ['options' => $field->getOptions()] + (array) $field;
01057d41 1062
21d40964
PN
1063 $displayValue = self::formatDisplayValue($value, $fieldInfo, $entityId);
1064
1065 // Call hook to alter display value.
1066 CRM_Utils_Hook::alterCustomFieldDisplayValue($displayValue, $value, $entityId, $fieldInfo);
1067
1068 return $displayValue;
3b66f502
CW
1069 }
1070
3b66f502
CW
1071 /**
1072 * Lower-level logic for rendering a custom field value
1073 *
1074 * @param string|array $value
1075 * @param array $field
080d719e 1076 * @param int|null $entityId
3b66f502 1077 *
51a3717f 1078 * @return string
3b66f502 1079 */
080d719e 1080 private static function formatDisplayValue($value, $field, $entityId = NULL) {
51a3717f 1081
3b66f502 1082 if (self::isSerialized($field) && !is_array($value)) {
74d79c61 1083 $value = CRM_Utils_Array::explodePadded($value);
3b66f502
CW
1084 }
1085 // CRM-12989 fix
6ebb4fa8
ML
1086 if ($field['html_type'] == 'CheckBox' && $value) {
1087 $value = CRM_Utils_Array::convertCheckboxFormatToArray($value);
3b66f502 1088 }
51a3717f
CW
1089
1090 $display = is_array($value) ? implode(', ', $value) : (string) $value;
1091
3b66f502
CW
1092 switch ($field['html_type']) {
1093
1094 case 'Select':
1095 case 'Autocomplete-Select':
1096 case 'Radio':
6a488035 1097 case 'CheckBox':
a61d2ab7
CW
1098 if ($field['data_type'] == 'ContactReference' && (is_array($value) || is_numeric($value))) {
1099 $displayNames = [];
1100 foreach ((array) $value as $contactId) {
1101 $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');
69b2206a 1102 }
a61d2ab7
CW
1103 $display = implode(', ', $displayNames);
1104 }
1105 elseif ($field['data_type'] == 'ContactReference') {
1106 $display = $value;
6a488035 1107 }
3b66f502 1108 elseif (is_array($value)) {
5f627bb5 1109 $v = [];
3b66f502 1110 foreach ($value as $key => $val) {
9c1bc317 1111 $v[] = $field['options'][$val] ?? NULL;
6a488035 1112 }
6a488035
TO
1113 $display = implode(', ', $v);
1114 }
3b66f502 1115 else {
51a3717f 1116 $display = CRM_Utils_Array::value($value, $field['options'], '');
bfe563af 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] ?? '';
1130 }
3b66f502 1131 }
6a488035
TO
1132 break;
1133
1134 case 'Select Date':
51a3717f
CW
1135 $customFormat = NULL;
1136
74d79c61
CW
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;
1140
ed0ca248 1141 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
9c1bc317 1142 $format = $field['date_format'] ?? NULL;
51a3717f
CW
1143
1144 if ($format) {
1145 if (array_key_exists($format, $actualPHPFormats)) {
1146 $customTimeFormat = (array) $actualPHPFormats[$format];
1147 switch (CRM_Utils_Array::value('time_format', $field)) {
1148 case 1:
1149 $customTimeFormat[] = 'g:iA';
1150 break;
1151
1152 case 2:
1153 $customTimeFormat[] = 'G:i';
1154 break;
1155
1156 default:
c65ec456
JP
1157 //If time is not selected remove time from value.
1158 $value = $value ? date('Y-m-d', strtotime($value)) : '';
51a3717f
CW
1159 }
1160 $customFormat = implode(" ", $customTimeFormat);
6a488035 1161 }
6a488035 1162 }
51a3717f 1163 $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
6a488035
TO
1164 break;
1165
6a488035 1166 case 'File':
5f7e0d20 1167 // In the context of displaying a profile, show file/image
e34e6979 1168 if ($value) {
1169 if ($entityId) {
1470f70a
CW
1170 if (CRM_Utils_Rule::positiveInteger($value)) {
1171 $fileId = $value;
1172 }
1173 else {
1174 $fileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1175 }
1176 $url = self::getFileURL($entityId, $field['id'], $fileId);
e34e6979 1177 if ($url) {
1178 $display = $url['file_url'];
1179 }
1180 }
1181 else {
1182 // In other contexts show a paperclip icon
5a1bbef4 1183 if (CRM_Utils_Rule::integer($value)) {
1184 $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
419b013b
D
1185
1186 $file_description = '';
1187 try {
1188 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1189 }
1190 catch (CiviCRM_API3_Exception $dontcare) {
1191 // don't care
1192 }
1193
1194 $display = "{$icons[$value]}{$file_description}";
5a1bbef4 1195 }
1196 else {
1197 //CRM-18396, if filename is passed instead
1198 $display = $value;
1199 }
5f7e0d20 1200 }
6a488035
TO
1201 }
1202 break;
1203
8472b684
CW
1204 case 'Link':
1205 $display = $display ? "<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1206 break;
1207
6a488035 1208 case 'TextArea':
51a3717f 1209 $display = nl2br($display);
6a488035 1210 break;
5d4ee51f 1211
1212 case 'Text':
1213 if ($field['data_type'] == 'Money' && isset($value)) {
aa965915 1214 // $value can also be an array(while using IN operator from search builder or api).
5d4ee51f 1215 foreach ((array) $value as $val) {
476a47e9 1216 $disp[] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($val);
5d4ee51f 1217 }
1218 $display = implode(', ', $disp);
1219 }
69e15d73
JJ
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);
1224 }
1225 $display = implode(', ', $disp);
1226 }
5d4ee51f 1227 break;
6a488035 1228 }
51a3717f 1229 return $display;
6a488035
TO
1230 }
1231
1232 /**
fe482240 1233 * Set default values for custom data used in profile.
6a488035 1234 *
6a0b768e
TO
1235 * @param int $customFieldId
1236 * Custom field id.
1237 * @param string $elementName
1238 * Custom field name.
1239 * @param array $defaults
1240 * Associated array of fields.
1241 * @param int $contactId
1242 * Contact id.
1243 * @param int $mode
1244 * Profile mode.
1245 * @param mixed $value
1246 * If passed - dont fetch value from db,.
6a488035 1247 * just format the given value
6a488035 1248 */
2da40d21 1249 public static function setProfileDefaults(
f9f40af3 1250 $customFieldId,
6a488035
TO
1251 $elementName,
1252 &$defaults,
1253 $contactId = NULL,
1254 $mode = NULL,
1255 $value = NULL
1256 ) {
1257 //get the type of custom field
1258 $customField = new CRM_Core_BAO_CustomField();
1259 $customField->id = $customFieldId;
1260 $customField->find(TRUE);
1261
1262 if (!$contactId) {
1263 if ($mode == CRM_Profile_Form::MODE_CREATE) {
1264 $value = $customField->default_value;
1265 }
1266 }
1267 else {
1268 if (!isset($value)) {
f9f40af3
TO
1269 $info = self::getTableColumnGroup($customFieldId);
1270 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
6a488035
TO
1271 $result = CRM_Core_DAO::executeQuery($query);
1272 if ($result->fetch()) {
1273 $value = $result->value;
1274 }
1275 }
1276
1277 if ($customField->data_type == 'Country') {
1278 if (!$value) {
1279 $config = CRM_Core_Config::singleton();
1280 if ($config->defaultContactCountry) {
d14f0a66 1281 $value = CRM_Core_BAO_Country::defaultContactCountry();
6a488035
TO
1282 }
1283 }
1284 }
1285 }
1286
1287 //set defaults if mode is registration
1288 if (!trim($value) &&
1289 ($value !== 0) &&
5f627bb5 1290 (!in_array($mode, [CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH]))
6a488035
TO
1291 ) {
1292 $value = $customField->default_value;
1293 }
1294
1295 if ($customField->data_type == 'Money' && isset($value)) {
1296 $value = number_format($value, 2);
1297 }
e1d699d7 1298 if (self::isSerialized($customField) && $value) {
08cde786
CW
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;
1306 }
1307 else {
1308 $defaults[$elementName][$val['value']] = $val['value'];
6a488035
TO
1309 }
1310 }
08cde786
CW
1311 }
1312 }
1313 else {
1314 $defaults[$elementName] = $value;
6a488035
TO
1315 }
1316 }
1317
b5c2afd0 1318 /**
b60efd6a 1319 * Get file url.
b5c2afd0 1320 *
100fef9d
CW
1321 * @param int $contactID
1322 * @param int $cfID
1323 * @param int $fileID
b5c2afd0
EM
1324 * @param bool $absolute
1325 *
ad37ac8e 1326 * @param string $multiRecordWhereClause
1327 *
b5c2afd0
EM
1328 * @return array
1329 */
00be9182 1330 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
6a488035
TO
1331 if ($contactID) {
1332 if (!$fileID) {
5f627bb5
SL
1333 $params = ['id' => $cfID];
1334 $defaults = [];
6a488035
TO
1335 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1336 $columnName = $defaults['column_name'];
1337
1338 //table name of custom data
1339 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
1340 $defaults['custom_group_id'],
1341 'table_name', 'id'
1342 );
1343
1344 //query to fetch id from civicrm_file
b42d84aa 1345 if ($multiRecordWhereClause) {
1346 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1347 }
1348 else {
1349 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1350 }
6a488035
TO
1351 $fileID = CRM_Core_DAO::singleValueQuery($query);
1352 }
1353
5f627bb5 1354 $result = [];
6a488035
TO
1355 if ($fileID) {
1356 $fileType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1357 $fileID,
1358 'mime_type',
1359 'id'
1360 );
1361 $result['file_id'] = $fileID;
1362
1363 if ($fileType == 'image/jpeg' ||
1364 $fileType == 'image/pjpeg' ||
1365 $fileType == 'image/gif' ||
1366 $fileType == 'image/x-png' ||
1367 $fileType == 'image/png'
1368 ) {
1369 $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile',
1370 $fileID,
1371 'entity_id',
c55199e1 1372 'file_id'
6a488035 1373 );
9bbc34f1 1374 list($path) = CRM_Core_BAO_File::path($fileID, $entityId);
efddd94f 1375 $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileID);
6a488035 1376 $url = CRM_Utils_System::url('civicrm/file',
0ada9416 1377 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
6a488035
TO
1378 $absolute, NULL, TRUE, TRUE
1379 );
f3726153 1380 $result['file_url'] = CRM_Utils_File::getFileURL($path, $fileType, $url);
6a488035 1381 }
f3726153 1382 // for non image files
6a488035
TO
1383 else {
1384 $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1385 $fileID,
1386 'uri'
1387 );
0ada9416 1388 $fileHash = CRM_Core_BAO_File::generateFileHash($contactID, $fileID);
6a488035 1389 $url = CRM_Utils_System::url('civicrm/file',
ff9aeadb 1390 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
6a488035
TO
1391 $absolute, NULL, TRUE, TRUE
1392 );
f3726153 1393 $result['file_url'] = CRM_Utils_File::getFileURL($uri, $fileType, $url);
6a488035
TO
1394 }
1395 }
1396 return $result;
1397 }
1398 }
1399
1400 /**
fe482240 1401 * Format custom fields before inserting.
6a488035 1402 *
6a0b768e
TO
1403 * @param int $customFieldId
1404 * Custom field id.
1405 * @param array $customFormatted
1406 * Formatted array.
bb06e9ed 1407 * @param mixed $value
6a0b768e
TO
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.
6a488035 1421 *
a1a2a83d 1422 * @return array|NULL
a6c01b45 1423 * formatted custom field array
6a488035 1424 */
2da40d21 1425 public static function formatCustomField(
f9f40af3 1426 $customFieldId, &$customFormatted, $value,
6a488035
TO
1427 $customFieldExtend, $customValueId = NULL,
1428 $entityId = NULL,
1429 $inline = FALSE,
211353a8 1430 $checkPermission = TRUE,
f9f40af3 1431 $includeViewOnly = FALSE
6a488035
TO
1432 ) {
1433 //get the custom fields for the entity
1434 //subtype and basic type
1435 $customDataSubType = NULL;
e25c4389 1436 if ($customFieldExtend) {
6a488035
TO
1437 // This is the case when getFieldsForImport() requires fields
1438 // of subtype and its parent.CRM-5143
d9ef38cc 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)) {
e25c4389 1442 $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType);
1443 if (is_array($customFieldExtend)) {
1444 $customFieldExtend = array_unique(array_values($customFieldExtend));
1445 }
d9ef38cc 1446 }
6a488035
TO
1447 }
1448
1449 $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend,
1450 FALSE,
1451 $inline,
1452 $customDataSubType,
1453 NULL,
1454 FALSE,
1455 FALSE,
317103ab 1456 $checkPermission ? CRM_Core_Permission::EDIT : FALSE
6a488035
TO
1457 );
1458
1459 if (!array_key_exists($customFieldId, $customFields)) {
6c552737 1460 return NULL;
6a488035
TO
1461 }
1462
1463 // return if field is a 'code' field
211353a8 1464 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
a1a2a83d 1465 return NULL;
6a488035
TO
1466 }
1467
1468 list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId);
1469
6a488035
TO
1470 if (!$customValueId &&
1471 // we always create new entites for is_multiple unless specified
1472 !$customFields[$customFieldId]['is_multiple'] &&
1473 $entityId
1474 ) {
1475 $query = "
1476SELECT id
1477 FROM $tableName
1478 WHERE entity_id={$entityId}";
1479
1480 $customValueId = CRM_Core_DAO::singleValueQuery($query);
1481 }
1482
1483 //fix checkbox, now check box always submits values
1484 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1485 if ($value) {
1486 // Note that only during merge this is not an array, and you can directly use value
1487 if (is_array($value)) {
5f627bb5 1488 $selectedValues = [];
6a488035
TO
1489 foreach ($value as $selId => $val) {
1490 if ($val) {
1491 $selectedValues[] = $selId;
1492 }
1493 }
1494 if (!empty($selectedValues)) {
1495 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
f9f40af3
TO
1496 $selectedValues
1497 ) . CRM_Core_DAO::VALUE_SEPARATOR;
6a488035
TO
1498 }
1499 else {
1500 $value = '';
1501 }
1502 }
1503 }
1504 }
c54649bf 1505 elseif (self::isSerialized($customFields[$customFieldId])) {
0bdc1b6d
CW
1506 // Select2 v3 returns a comma-separated string.
1507 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1508 $value = explode(',', $value);
1509 }
1510
c54649bf 1511 $value = $value ? CRM_Utils_Array::implodePadded($value) : '';
6a488035
TO
1512 }
1513
c54649bf 1514 if (self::isSerialized($customFields[$customFieldId]) &&
6a488035
TO
1515 $customFields[$customFieldId]['data_type'] == 'String' &&
1516 !empty($customFields[$customFieldId]['text_length']) &&
1517 !empty($value)
1518 ) {
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
1523
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;
1529 }
1530
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);
1534 }
1535 }
1536
1537 $date = NULL;
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);
1542 }
1543 $value = $date;
1544 }
1545
1546 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1547 $customFields[$customFieldId]['data_type'] == 'Money'
1548 ) {
1549 if (!$value) {
1550 $value = 0;
1551 }
1552
1553 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1554 $value = CRM_Utils_Rule::cleanMoney($value);
1555 }
1556 }
1557
1558 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1559 $customFields[$customFieldId]['data_type'] == 'Country'
1560 ) &&
1561 empty($value)
1562 ) {
1563 // CRM-3415
1564 $value = 0;
1565 }
1566
e5077b06 1567 $fileID = NULL;
6a488035
TO
1568
1569 if ($customFields[$customFieldId]['data_type'] == 'File') {
1570 if (empty($value)) {
1571 return;
1572 }
1573
1574 $config = CRM_Core_Config::singleton();
1575
756b5b30 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);
07702f3e 1581 if ($fileDAO->N) {
756b5b30 1582 $fileID = $value;
1583 $fName = $fileDAO->uri;
1584 $mimeType = $fileDAO->mime_type;
1585 }
1586 }
1b8b0c11
ML
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.
1592 return;
1593 }
33d245c8
CW
1594 else {
1595 $fName = $value['name'];
1596 $mimeType = $value['type'];
1597 }
756b5b30 1598
6a488035
TO
1599 $filename = pathinfo($fName, PATHINFO_BASENAME);
1600
756b5b30 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
ecc6bd60 1603 if (empty($fileID) && !rename($fName, $config->customFileUploadDir . $filename)) {
6a488035 1604 CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory'));
6a488035
TO
1605 }
1606
ecc6bd60 1607 if ($customValueId && empty($fileID)) {
6a488035
TO
1608 $query = "
1609SELECT $columnName
1610 FROM $tableName
1611 WHERE id = %1";
5f627bb5 1612 $params = [1 => [$customValueId, 'Integer']];
e5077b06 1613 $fileID = CRM_Core_DAO::singleValueQuery($query, $params);
6a488035
TO
1614 }
1615
1616 $fileDAO = new CRM_Core_DAO_File();
1617
e5077b06
AM
1618 if ($fileID) {
1619 $fileDAO->id = $fileID;
6a488035
TO
1620 }
1621
1622 $fileDAO->uri = $filename;
1623 $fileDAO->mime_type = $mimeType;
c13ff164 1624 $fileDAO->upload_date = date('YmdHis');
6a488035 1625 $fileDAO->save();
e5077b06 1626 $fileID = $fileDAO->id;
6a488035
TO
1627 $value = $filename;
1628 }
1629
1630 if (!is_array($customFormatted)) {
5f627bb5 1631 $customFormatted = [];
6a488035
TO
1632 }
1633
1634 if (!array_key_exists($customFieldId, $customFormatted)) {
5f627bb5 1635 $customFormatted[$customFieldId] = [];
6a488035
TO
1636 }
1637
1638 $index = -1;
1639 if ($customValueId) {
1640 $index = $customValueId;
1641 }
1642
1643 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
5f627bb5 1644 $customFormatted[$customFieldId][$index] = [];
6a488035 1645 }
5f627bb5 1646 $customFormatted[$customFieldId][$index] = [
6a488035
TO
1647 'id' => $customValueId > 0 ? $customValueId : NULL,
1648 'value' => $value,
1649 'type' => $customFields[$customFieldId]['data_type'],
1650 'custom_field_id' => $customFieldId,
1651 'custom_group_id' => $groupID,
1652 'table_name' => $tableName,
1653 'column_name' => $columnName,
e5077b06 1654 'file_id' => $fileID,
b99d7139 1655 // is_multiple refers to the custom group, serialize refers to the field.
6a488035 1656 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
b99d7139 1657 'serialize' => $customFields[$customFieldId]['serialize'],
5f627bb5 1658 ];
6a488035
TO
1659
1660 //we need to sort so that custom fields are created in the order of entry
1661 krsort($customFormatted[$customFieldId]);
1662 return $customFormatted;
1663 }
1664
b5c2afd0 1665 /**
b60efd6a
EM
1666 * Get default custom table schema.
1667 *
c490a46a 1668 * @param array $params
b5c2afd0
EM
1669 *
1670 * @return array
1671 */
b60efd6a 1672 public static function defaultCustomTableSchema($params) {
6a488035 1673 // add the id and extends_id
799f3149
SL
1674 $collation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
1675 $characterSet = 'utf8';
1676 if (stripos($collation, 'utf8mb4') !== FALSE) {
1677 $characterSet = 'utf8mb4';
1678 }
5f627bb5 1679 $table = [
6a488035
TO
1680 'name' => $params['name'],
1681 'is_multiple' => $params['is_multiple'],
799f3149 1682 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
5f627bb5
SL
1683 'fields' => [
1684 [
6a488035
TO
1685 'name' => 'id',
1686 'type' => 'int unsigned',
1687 'primary' => TRUE,
1688 'required' => TRUE,
1689 'attributes' => 'AUTO_INCREMENT',
1690 'comment' => 'Default MySQL primary key',
5f627bb5
SL
1691 ],
1692 [
6a488035
TO
1693 'name' => 'entity_id',
1694 'type' => 'int unsigned',
1695 'required' => TRUE,
1696 'comment' => 'Table that this extends',
1697 'fk_table_name' => $params['extends_name'],
1698 'fk_field_name' => 'id',
1699 'fk_attributes' => 'ON DELETE CASCADE',
5f627bb5
SL
1700 ],
1701 ],
1702 ];
6a488035 1703
80ad1850
SL
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';
1708 }
1709
6a488035 1710 if (!$params['is_multiple']) {
5f627bb5
SL
1711 $table['indexes'] = [
1712 [
6a488035
TO
1713 'unique' => TRUE,
1714 'field_name_1' => 'entity_id',
5f627bb5
SL
1715 ],
1716 ];
6a488035
TO
1717 }
1718 return $table;
1719 }
1720
b5c2afd0 1721 /**
b60efd6a
EM
1722 * Create custom field.
1723 *
1724 * @param CRM_Core_DAO_CustomField $field
1725 * @param string $operation
b5c2afd0 1726 */
ead0c08f 1727 public static function createField($field, $operation) {
3111965c 1728 $sql = str_repeat(' ', 8);
e5ce15c3 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);
3111965c 1732
1733 // CRM-7007: do not i18n-rewrite this query
1734 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1735
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?)
e5ce15c3 1741 if ($operation === 'add' || $operation === 'modify') {
3111965c 1742 $logging = new CRM_Logging_Schema();
e5ce15c3 1743 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name]]);
3111965c 1744 }
1745 }
1746
ead0c08f 1747 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
3111965c 1748 }
1749
1750 /**
1751 * @param CRM_Core_DAO_CustomField $field
a61d2ab7 1752 * @param string $operation add|modify|delete
3111965c 1753 *
1754 * @return bool
1755 */
e5ce15c3 1756 public static function getAlterFieldSQL($field, $operation) {
c914d4dd 1757 $params = self::prepareCreateParams($field, $operation);
e5ce15c3 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...
3111965c 1760 $params['required'] = FALSE;
ba9c74ab 1761 return CRM_Core_BAO_SchemaHandler::getFieldAlterSQL($params);
6a488035
TO
1762 }
1763
721c9da1 1764 /**
051f2455 1765 * Get query to reformat existing values for a field when changing its serialize attribute
721c9da1
CW
1766 *
1767 * @param CRM_Core_DAO_CustomField $field
051f2455 1768 * @return string
721c9da1 1769 */
051f2455 1770 private static function getAlterSerializeSQL($field) {
721c9da1
CW
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) {
051f2455 1775 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
721c9da1
CW
1776 }
1777 else {
051f2455 1778 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
721c9da1 1779 }
721c9da1
CW
1780 }
1781
6a488035 1782 /**
fe482240 1783 * Determine whether it would be safe to move a field.
6a488035 1784 *
6a0b768e
TO
1785 * @param int $fieldID
1786 * FK to civicrm_custom_field.
1787 * @param int $newGroupID
1788 * FK to civicrm_custom_group.
6a488035 1789 *
5c766a0b
TO
1790 * @return array
1791 * array(string) or TRUE
6a488035 1792 */
00be9182 1793 public static function _moveFieldValidate($fieldID, $newGroupID) {
5f627bb5 1794 $errors = [];
6a488035
TO
1795
1796 $field = new CRM_Core_DAO_CustomField();
1797 $field->id = $fieldID;
1798 if (!$field->find(TRUE)) {
1799 $errors['fieldID'] = 'Invalid ID for custom field';
1800 return $errors;
1801 }
1802
1803 $oldGroup = new CRM_Core_DAO_CustomGroup();
1804 $oldGroup->id = $field->custom_group_id;
1805 if (!$oldGroup->find(TRUE)) {
1806 $errors['fieldID'] = 'Invalid ID for old custom group';
1807 return $errors;
1808 }
1809
1810 $newGroup = new CRM_Core_DAO_CustomGroup();
1811 $newGroup->id = $newGroupID;
1812 if (!$newGroup->find(TRUE)) {
1813 $errors['newGroupID'] = 'Invalid ID for new custom group';
1814 return $errors;
1815 }
1816
1817 $query = "
1818SELECT b.id
1819FROM civicrm_custom_field a
1820INNER JOIN civicrm_custom_field b
1821WHERE a.id = %1
1822AND a.label = b.label
1823AND b.custom_group_id = %2
1824";
5f627bb5
SL
1825 $params = [
1826 1 => [$field->id, 'Integer'],
1827 2 => [$newGroup->id, 'Integer'],
1828 ];
6a488035
TO
1829 $count = CRM_Core_DAO::singleValueQuery($query, $params);
1830 if ($count > 0) {
1831 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1832 }
1833
1834 $tableName = $oldGroup->table_name;
1835 $columnName = $field->column_name;
1836
1837 $query = "
1838SELECT count(*)
1839FROM $tableName
1840WHERE $columnName is not null
1841";
e03e1641 1842 $count = CRM_Core_DAO::singleValueQuery($query);
6a488035
TO
1843 if ($count > 0) {
1844 $query = "
1845SELECT extends
1846FROM civicrm_custom_group
1847WHERE id IN ( %1, %2 )
1848";
5f627bb5
SL
1849 $params = [
1850 1 => [$oldGroup->id, 'Integer'],
1851 2 => [$newGroup->id, 'Integer'],
1852 ];
6a488035
TO
1853
1854 $dao = CRM_Core_DAO::executeQuery($query, $params);
5f627bb5 1855 $extends = [];
6a488035
TO
1856 while ($dao->fetch()) {
1857 $extends[] = $dao->extends;
1858 }
1859 if ($extends[0] != $extends[1]) {
1860 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1861 }
1862 }
1863
1864 return empty($errors) ? TRUE : $errors;
1865 }
1866
1867 /**
b60efd6a 1868 * Move a custom data field from one group (table) to another.
6a488035 1869 *
6a0b768e
TO
1870 * @param int $fieldID
1871 * FK to civicrm_custom_field.
1872 * @param int $newGroupID
1873 * FK to civicrm_custom_group.
ac15829d
SL
1874 *
1875 * @throws CRM_Core_Exception
6a488035 1876 */
00be9182 1877 public static function moveField($fieldID, $newGroupID) {
6a488035
TO
1878 $validation = self::_moveFieldValidate($fieldID, $newGroupID);
1879 if (TRUE !== $validation) {
ac15829d 1880 throw new CRM_Core_Exception(implode(' ', $validation));
6a488035
TO
1881 }
1882 $field = new CRM_Core_DAO_CustomField();
1883 $field->id = $fieldID;
1884 $field->find(TRUE);
1885
1886 $newGroup = new CRM_Core_DAO_CustomGroup();
1887 $newGroup->id = $newGroupID;
1888 $newGroup->find(TRUE);
1889
1890 $oldGroup = new CRM_Core_DAO_CustomGroup();
1891 $oldGroup->id = $field->custom_group_id;
1892 $oldGroup->find(TRUE);
1893
1894 $add = clone$field;
1895 $add->custom_group_id = $newGroup->id;
1896 self::createField($add, 'add');
1897
4f426b76
BS
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}`
6a488035
TO
1901 ";
1902 CRM_Core_DAO::executeQuery($sql);
1903
1904 $del = clone$field;
1905 $del->custom_group_id = $oldGroup->id;
1906 self::createField($del, 'delete');
1907
1908 $add->save();
1909
1910 CRM_Utils_System::flushCache();
1911 }
1912
554a91f6 1913 /**
1914 * Create an option value for a custom field option group ID.
1915 *
1916 * @param array $params
1917 * @param string $value
1918 * @param \CRM_Core_DAO_OptionGroup $optionGroup
bb6bfd68 1919 * @param string $index
554a91f6 1920 * @param string $dataType
1921 */
bb6bfd68 1922 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup $optionGroup, $index, $dataType) {
554a91f6 1923 if (strlen(trim($value))) {
1924 $optionValue = new CRM_Core_DAO_OptionValue();
1925 $optionValue->option_group_id = $optionGroup->id;
bb6bfd68
CW
1926 $optionValue->label = $params['option_label'][$index];
1927 $optionValue->name = $params['option_name'][$index] ?? CRM_Utils_String::titleToVar($params['option_label'][$index]);
554a91f6 1928 switch ($dataType) {
1929 case 'Money':
1930 $optionValue->value = CRM_Utils_Rule::cleanMoney($value);
1931 break;
1932
1933 case 'Int':
1934 $optionValue->value = intval($value);
1935 break;
1936
1937 case 'Float':
1938 $optionValue->value = floatval($value);
1939 break;
1940
1941 default:
1942 $optionValue->value = trim($value);
1943 }
1944
bb6bfd68
CW
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;
554a91f6 1950 $optionValue->save();
1951 }
1952 }
1953
4f166aec 1954 /**
1955 * Prepare for the create operation.
1956 *
1957 * Munge params, create the option values if needed.
1958 *
1959 * This could be called by a single create or a batchCreate.
1960 *
1961 * @param array $params
4f166aec 1962 *
1963 * @return array
1964 */
e43db21d 1965 protected static function prepareCreate($params) {
1966 $op = empty($params['id']) ? 'create' : 'edit';
4f166aec 1967 CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params);
b5c280fc 1968 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
4f166aec 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));
1974 }
1975 if (!isset($params['name'])) {
1976 $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64);
1977 }
1978 }
1979 else {
299e3946 1980 $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
4f166aec 1981 }
1982
299e3946
CW
1983 $htmlType = $params['html_type'] ?? NULL;
1984 $dataType = $params['data_type'] ?? NULL;
4f166aec 1985
299e3946
CW
1986 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1987 $params['date_format'] = Civi::settings()->get('dateInputFormat');
1988 }
4f166aec 1989
fdd11b39
CW
1990 // Checkboxes are always serialized in current schema
1991 if ($htmlType == 'CheckBox') {
1992 $params['serialize'] = CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND;
1993 }
1994
1995 if (!empty($params['serialize'])) {
299e3946
CW
1996 if (isset($params['default_checkbox_option'])) {
1997 $defaultArray = [];
1998 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
1999 if ($params['option_value'][$v]) {
2000 $defaultArray[] = $params['option_value'][$v];
4f166aec 2001 }
2002 }
299e3946
CW
2003
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);
4f166aec 2007 }
299e3946
CW
2008 }
2009 else {
2010 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
2011 $params['default_value'] = $params['option_value'][$params['default_option']];
2012 }
2013 }
4f166aec 2014 }
2015
4f166aec 2016 // create any option group & values if required
299e3946 2017 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
9ccfced4 2018 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
4f166aec 2019 //CRM-16659: if option_value then create an option group for this custom field.
9ccfced4
EM
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']))
2986a716 2024 && (empty($params['option_group_id']) && !empty($params['option_value'])
9ccfced4
EM
2025 )
2026 ) {
4f166aec 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);
2040 }
2041 }
2042 }
2043 }
2044
bdb24928
JG
2045 // Remove option group IDs from fields changed to Text html_type.
2046 if ($htmlType == 'Text') {
2047 $params['option_group_id'] = '';
2048 }
2049
4f166aec 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']);
2054 }
2055
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
5967bdb6 2060 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
4f166aec 2061
299e3946 2062 if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) {
4f166aec 2063 $params['default_value'] = $defaultValue;
2064 }
2065 }
2066 }
2067
33482697
CW
2068 // Set default textarea attributes
2069 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
4f166aec 2070 $params['attributes'] = 'rows=4, cols=60';
2071 }
2072 return $params;
2073 }
2074
f45a42ba 2075 /**
2076 * Create database entry for custom field and related option groups.
2077 *
2078 * @param array $params
2079 *
2080 * @return CRM_Core_DAO_CustomField
2081 */
2082 protected static function createCustomFieldRecord($params) {
2083 $transaction = new CRM_Core_Transaction();
2084 $params = self::prepareCreate($params);
2085
2086 $customField = new CRM_Core_DAO_CustomField();
2087 $customField->copyValues($params);
2088 $customField->save();
2089
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}";
2095 }
2096 $customField->column_name = $params['column_name'];
2097 $customField->save();
2098 }
2099
2100 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2101 $transaction->commit();
2102
2103 // make sure all values are present in the object for further processing
2104 $customField->find(TRUE);
721c9da1 2105
f45a42ba 2106 return $customField;
2107 }
2108
051f2455
CW
2109 /**
2110 * @param $params
2111 * @return int|null
2112 */
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'];
2117 }
2118 }
2119 return NULL;
2120 }
2121
1600a9c0 2122 /**
2123 * Move custom data from one contact to another.
2124 *
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.
2127 *
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.
2131 *
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.
2135 *
2136 * @param int $oldContactID
2137 * @param int $newContactID
2138 * @param int[] $fieldIDs
2139 * Optional list field ids to move.
2140 *
2141 * @throws \CiviCRM_API3_Exception
2142 * @throws \Exception
2143 */
2144 public function move($oldContactID, $newContactID, $fieldIDs) {
2145 if (empty($fieldIDs)) {
2146 return;
2147 }
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'];
2149 $return = [];
2150 foreach ($fieldIDs as $fieldID) {
2151 $return[] = 'custom_' . $fieldID;
2152 }
2153 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2154 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2155
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']);
2164 }
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']]}"
2170 );
2171 }
2172 }
2173 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2174 CRM_Core_DAO::executeQuery(
4f426b76 2175 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
1600a9c0 2176 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2177 ON DUPLICATE KEY UPDATE
4f426b76 2178 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
1600a9c0 2179 ");
2180 }
2181 }
2182 }
2183
6a488035 2184 /**
fe482240 2185 * Get the database table name and column name for a custom field.
6a488035 2186 *
6a0b768e
TO
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).
6a488035 2191 *
a6c01b45
CW
2192 * @return array
2193 * fatal is fieldID does not exists, else array of tableName, columnName
ac15829d 2194 * @throws \CRM_Core_Exception
6a488035 2195 */
00be9182 2196 public static function getTableColumnGroup($fieldID, $force = FALSE) {
f9f40af3
TO
2197 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2198 $cache = CRM_Utils_Cache::singleton();
6a488035
TO
2199 $fieldValues = $cache->get($cacheKey);
2200 if (empty($fieldValues) || $force) {
2201 $query = "
2202SELECT cg.table_name, cf.column_name, cg.id
2203FROM civicrm_custom_group cg,
2204 civicrm_custom_field cf
2205WHERE cf.custom_group_id = cg.id
2206AND cf.id = %1";
5f627bb5 2207 $params = [1 => [$fieldID, 'Integer']];
6a488035
TO
2208 $dao = CRM_Core_DAO::executeQuery($query, $params);
2209
2210 if (!$dao->fetch()) {
ac15829d 2211 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
6a488035 2212 }
5f627bb5 2213 $fieldValues = [$dao->table_name, $dao->column_name, $dao->id];
6a488035
TO
2214 $cache->set($cacheKey, $fieldValues);
2215 }
2216 return $fieldValues;
2217 }
2218
2219 /**
fe482240 2220 * Get custom option groups.
6a488035 2221 *
c0fce6be
MW
2222 * @deprecated Use the API OptionGroup.get
2223 *
6a0b768e 2224 * @param array $includeFieldIds
b60efd6a 2225 * Ids of custom fields for which option groups must be included.
6a488035
TO
2226 *
2227 * Currently this is required in the cases where option groups are to be included
2228 * for inactive fields : CRM-5369
2229 *
72b3a70c 2230 * @return mixed
6a488035 2231 */
b60efd6a 2232 public static function customOptionGroup($includeFieldIds = NULL) {
6a488035
TO
2233 static $customOptionGroup = NULL;
2234
2235 $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force';
2236 if ($cacheKey == 'force') {
2237 $customOptionGroup[$cacheKey] = NULL;
2238 }
2239
a7488080 2240 if (empty($customOptionGroup[$cacheKey])) {
6a488035
TO
2241 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2242
2243 //support for single as well as array format.
2244 if (!empty($includeFieldIds)) {
2245 if (is_array($includeFieldIds)) {
2246 $includeFieldIds = implode(',', $includeFieldIds);
2247 }
2248 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2249 }
2250
2251 $query = "
2252 SELECT g.id, g.title
2253 FROM civicrm_option_group g
2254INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2255 WHERE {$whereClause}";
2256
2257 $dao = CRM_Core_DAO::executeQuery($query);
2258 while ($dao->fetch()) {
2259 $customOptionGroup[$cacheKey][$dao->id] = $dao->title;
2260 }
2261 }
2262
2263 return $customOptionGroup[$cacheKey];
2264 }
2265
58c232cb 2266 /**
2267 * Get defaults for new entity.
2268 *
2269 * @return array
2270 */
2271 public static function getDefaults() {
2272 return [
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,
2280 'is_view' => FALSE,
2281 ];
2282 }
2283
6a488035 2284 /**
fe482240 2285 * Fix orphan groups.
6a488035 2286 *
6a0b768e
TO
2287 * @param int $customFieldId
2288 * Custom field id.
2289 * @param int $optionGroupId
2290 * Option group id.
6a488035 2291 */
00be9182 2292 public static function fixOptionGroups($customFieldId, $optionGroupId) {
6a488035
TO
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',
2296 $customFieldId,
2297 'option_group_id'
2298 );
2299 // get the updated option group
2300 // if both are same return
aa4143bb 2301 if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) {
6a488035
TO
2302 return;
2303 }
2304
2305 // check if option group is related to any other field
2306 self::checkOptionGroup($currentOptionGroupId);
2307 }
2308
2309 /**
b60efd6a 2310 * Check if option group is related to more than one custom field.
6a488035 2311 *
6a0b768e
TO
2312 * @param int $optionGroupId
2313 * Option group id.
6a488035 2314 */
00be9182 2315 public static function checkOptionGroup($optionGroupId) {
6a488035
TO
2316 $query = "
2317SELECT count(*)
2318FROM civicrm_custom_field
2319WHERE option_group_id = {$optionGroupId}";
2320
2321 $count = CRM_Core_DAO::singleValueQuery($query);
2322
2323 if ($count < 2) {
2324 //delete the option group
2325 CRM_Core_BAO_OptionGroup::del($optionGroupId);
2326 }
2327 }
2328
b5c2afd0 2329 /**
b60efd6a
EM
2330 * Get option group default.
2331 *
100fef9d 2332 * @param int $optionGroupId
5967bdb6 2333 * @param bool $serialize
b5c2afd0
EM
2334 *
2335 * @return null|string
2336 */
5967bdb6 2337 public static function getOptionGroupDefault($optionGroupId, $serialize) {
6a488035 2338 $query = "
5967bdb6 2339SELECT default_value, serialize
6a488035
TO
2340FROM civicrm_custom_field
2341WHERE option_group_id = {$optionGroupId}
5967bdb6 2342AND default_value IS NOT NULL";
6a488035 2343
f9f40af3 2344 $dao = CRM_Core_DAO::executeQuery($query);
6a488035 2345 while ($dao->fetch()) {
5967bdb6 2346 if ($dao->serialize == $serialize) {
6a488035
TO
2347 return $dao->default_value;
2348 }
5967bdb6 2349 $defaultValue = $dao->default_value;
6a488035
TO
2350 }
2351
5967bdb6
CW
2352 // Convert serialization
2353 if (isset($defaultValue) && $serialize) {
2354 return CRM_Utils_Array::implodePadded([$defaultValue]);
6a488035 2355 }
5967bdb6
CW
2356 elseif (isset($defaultValue)) {
2357 return CRM_Utils_Array::explodePadded($defaultValue)[0];
6a488035 2358 }
5967bdb6 2359 return NULL;
6a488035
TO
2360 }
2361
b5c2afd0 2362 /**
b60efd6a
EM
2363 * Post process function.
2364 *
c490a46a 2365 * @param array $params
100fef9d 2366 * @param int $entityID
b60efd6a 2367 * @param string $customFieldExtends
b5c2afd0 2368 * @param bool $inline
e9ff5391 2369 * @param bool $checkPermissions
b5c2afd0
EM
2370 *
2371 * @return array
2372 */
2da40d21 2373 public static function postProcess(
f9f40af3 2374 &$params,
6a488035
TO
2375 $entityID,
2376 $customFieldExtends,
e9ff5391 2377 $inline = FALSE,
2378 $checkPermissions = TRUE
6a488035 2379 ) {
5f627bb5 2380 $customData = [];
6a488035
TO
2381
2382 foreach ($params as $key => $value) {
2383 if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE)) {
2384
2385 // for autocomplete transfer hidden value instead of label
2386 if ($params[$key] && isset($params[$key . '_id'])) {
2387 $value = $params[$key . '_id'];
2388 }
2389
2390 // we need to append time with date
2391 if ($params[$key] && isset($params[$key . '_time'])) {
2392 $value .= ' ' . $params[$key . '_time'];
2393 }
2394
2395 CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0],
2396 $customData,
2397 $value,
2398 $customFieldExtends,
2399 $customFieldInfo[1],
2400 $entityID,
e9ff5391 2401 $inline,
2402 $checkPermissions
6a488035
TO
2403 );
2404 }
2405 }
2406 return $customData;
2407 }
2408
2921fb78 2409 /**
b906accb 2410 * Get custom field ID from field/group name/title.
2921fb78 2411 *
b906accb 2412 * @param string $fieldName Field name or label
30b6e002 2413 * @param string|null $groupName (Optional) Group name or label
b906accb 2414 * @param bool $fullString Whether to return "custom_123" or "123"
b60efd6a 2415 *
b906accb
MW
2416 * @return string|int|null
2417 * @throws \CiviCRM_API3_Exception
b5c2afd0 2418 */
30b6e002
PF
2419 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2420 $cacheKey = $groupName . '.' . $fieldName;
2421 if (!isset(Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
b906accb
MW
2422 $customFieldParams = [
2423 'name' => $fieldName,
2424 'label' => $fieldName,
2425 'options' => ['or' => [["name", "label"]]],
2426 ];
2427
30b6e002
PF
2428 if ($groupName) {
2429 $customFieldParams['custom_group_id.name'] = $groupName;
2430 $customFieldParams['custom_group_id.title'] = $groupName;
b906accb
MW
2431 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2432 }
6a488035 2433
b906accb
MW
2434 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2435
2436 if (empty($field['id'])) {
30b6e002
PF
2437 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2438 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
b906accb
MW
2439 }
2440 else {
30b6e002
PF
2441 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2442 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
b906accb 2443 }
6a488035 2444 }
b906accb
MW
2445
2446 if ($fullString) {
30b6e002 2447 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
6a488035 2448 }
30b6e002 2449 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
6a488035
TO
2450 }
2451
2452 /**
2453 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
67f947ac
EM
2454 *
2455 * @param array $ids
2456 *
2457 * @return array
6a488035 2458 */
00be9182 2459 public static function getNameFromID($ids) {
6a488035
TO
2460 if (is_array($ids)) {
2461 $ids = implode(',', $ids);
2462 }
2463 $sql = "
2464SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2465FROM civicrm_custom_field f
2466INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2467WHERE f.id IN ($ids)";
2468
6a488035 2469 $dao = CRM_Core_DAO::executeQuery($sql);
5f627bb5 2470 $result = [];
6a488035 2471 while ($dao->fetch()) {
5f627bb5 2472 $result[$dao->id] = [
6a488035
TO
2473 'field_name' => $dao->field_name,
2474 'field_label' => $dao->field_label,
2475 'group_name' => $dao->group_name,
2476 'group_title' => $dao->group_title,
5f627bb5 2477 ];
6a488035
TO
2478 }
2479 return $result;
2480 }
2481
2482 /**
2483 * Validate custom data.
2484 *
6a0b768e
TO
2485 * @param array $params
2486 * Custom data submitted.
16b10e64 2487 * ie array( 'custom_1' => 'validate me' );
6a488035 2488 *
a6c01b45
CW
2489 * @return array
2490 * validation errors.
6a488035 2491 */
00be9182 2492 public static function validateCustomData($params) {
5f627bb5 2493 $errors = [];
6a488035
TO
2494 if (!is_array($params) || empty($params)) {
2495 return $errors;
2496 }
2497
6a488035 2498 //pick up profile fields.
5f627bb5 2499 $profileFields = [];
9c1bc317 2500 $ufGroupId = $params['ufGroupId'] ?? NULL;
6a488035
TO
2501 if ($ufGroupId) {
2502 $profileFields = CRM_Core_BAO_UFGroup::getFields($ufGroupId,
2503 FALSE,
2504 CRM_Core_Action::VIEW
2505 );
2506 }
2507
2508 //lets start w/ params.
2509 foreach ($params as $key => $value) {
2510 $customFieldID = self::getKeyID($key);
2511 if (!$customFieldID) {
2512 continue;
2513 }
2514
2515 //load the structural info for given field.
2516 $field = new CRM_Core_DAO_CustomField();
2517 $field->id = $customFieldID;
2518 if (!$field->find(TRUE)) {
2519 continue;
2520 }
2521 $dataType = $field->data_type;
2522
5f627bb5 2523 $profileField = CRM_Utils_Array::value($key, $profileFields, []);
9c1bc317
CW
2524 $fieldTitle = $profileField['title'] ?? NULL;
2525 $isRequired = $profileField['is_required'] ?? NULL;
6a488035
TO
2526 if (!$fieldTitle) {
2527 $fieldTitle = $field->label;
2528 }
2529
2530 //no need to validate.
2531 if (CRM_Utils_System::isNull($value) && !$isRequired) {
2532 continue;
2533 }
2534
2535 //lets validate first for required field.
2536 if ($isRequired && CRM_Utils_System::isNull($value)) {
5f627bb5 2537 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
6a488035
TO
2538 continue;
2539 }
2540
2541 //now time to take care of custom field form rules.
2542 $ruleName = $errorMsg = NULL;
2543 switch ($dataType) {
2544 case 'Int':
2545 $ruleName = 'integer';
2546 $errorMsg = ts('%1 must be an integer (whole number).',
2547 array(1 => $fieldTitle)
2548 );
2549 break;
2550
2551 case 'Money':
2552 $ruleName = 'money';
2553 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2554 array(1 => $fieldTitle)
2555 );
2556 break;
2557
2558 case 'Float':
2559 $ruleName = 'numeric';
2560 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2561 array(1 => $fieldTitle)
2562 );
2563 break;
2564
2565 case 'Link':
2566 $ruleName = 'wikiURL';
2567 $errorMsg = ts('%1 must be valid Website.',
2568 array(1 => $fieldTitle)
2569 );
2570 break;
2571 }
2572
2573 if ($ruleName && !CRM_Utils_System::isNull($value)) {
2574 $valid = FALSE;
2575 $funName = "CRM_Utils_Rule::{$ruleName}";
2576 if (is_callable($funName)) {
2577 $valid = call_user_func($funName, $value);
2578 }
2579 if (!$valid) {
2580 $errors[$key] = $errorMsg;
2581 }
2582 }
2583 }
2584
2585 return $errors;
2586 }
2587
ffd93213 2588 /**
b60efd6a
EM
2589 * Is this field a multi record field.
2590 *
100fef9d 2591 * @param int $customId
ffd93213
EM
2592 *
2593 * @return bool
2594 */
00be9182 2595 public static function isMultiRecordField($customId) {
6a488035
TO
2596 $isMultipleWithGid = FALSE;
2597 if (!is_numeric($customId)) {
2598 $customId = self::getKeyID($customId);
2599 }
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
2605WHERE cf.id = %1 AND cg.is_multiple = 1";
5f627bb5 2606 $params[1] = [$customId, 'Integer'];
6a488035
TO
2607 $dao = CRM_Core_DAO::executeQuery($sql, $params);
2608 if ($dao->fetch()) {
2609 if ($dao->cgId) {
2610 $isMultipleWithGid = $dao->cgId;
2611 }
2612 }
2613 }
2614
2615 return $isMultipleWithGid;
2616 }
3130209f 2617
1f8ac3d2
CW
2618 /**
2619 * Does this field type have any select options?
2620 *
2621 * @param array $field
2622 *
2623 * @return bool
2624 */
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'])) {
2630 return TRUE;
2631 }
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'])) {
2635 return FALSE;
2636 }
2637 if (strpos($field['html_type'], 'Select') !== FALSE) {
2638 return TRUE;
2639 }
2640 return !empty($field['option_group_id']);
2641 }
2642
3130209f
CW
2643 /**
2644 * Does this field store a serialized string?
b60efd6a 2645 *
5ea026c1 2646 * @param CRM_Core_DAO_CustomField|array $field
b60efd6a 2647 *
3130209f
CW
2648 * @return bool
2649 */
00be9182 2650 public static function isSerialized($field) {
3130209f 2651 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
3766bd36 2652 $html_type = is_object($field) ? $field->html_type : $field['html_type'];
5ea026c1
CW
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) {
2656 return TRUE;
2657 }
d51a6a62 2658 // Otherwise this is the new standard as of 5.27
5ea026c1 2659 return is_object($field) ? !empty($field->serialize) : !empty($field['serialize']);
3130209f 2660 }
96025800 2661
01057d41
CW
2662 /**
2663 * Get api entity for this field
2664 *
2665 * @return string
2666 */
2667 public function getEntity() {
2668 $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
5f627bb5 2669 return in_array($entity, ['Individual', 'Household', 'Organization']) ? 'Contact' : $entity;
01057d41
CW
2670 }
2671
14f42e31 2672 /**
3b66f502 2673 * Set pseudoconstant properties for field metadata.
b60efd6a 2674 *
14f42e31
CW
2675 * @param array $field
2676 * @param string|null $optionGroupName
2677 */
2678 private static function getOptionsForField(&$field, $optionGroupName) {
2679 if ($optionGroupName) {
5f627bb5 2680 $field['pseudoconstant'] = [
14f42e31
CW
2681 'optionGroupName' => $optionGroupName,
2682 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
5f627bb5 2683 ];
14f42e31
CW
2684 }
2685 elseif ($field['data_type'] == 'Boolean') {
5f627bb5 2686 $field['pseudoconstant'] = [
14f42e31 2687 'callback' => 'CRM_Core_SelectValues::boolean',
5f627bb5 2688 ];
14f42e31
CW
2689 }
2690 elseif ($field['data_type'] == 'Country') {
5f627bb5 2691 $field['pseudoconstant'] = [
14f42e31
CW
2692 'table' => 'civicrm_country',
2693 'keyColumn' => 'id',
2694 'labelColumn' => 'name',
2695 'nameColumn' => 'iso_code',
5f627bb5 2696 ];
14f42e31
CW
2697 }
2698 elseif ($field['data_type'] == 'StateProvince') {
5f627bb5 2699 $field['pseudoconstant'] = [
14f42e31
CW
2700 'table' => 'civicrm_state_province',
2701 'keyColumn' => 'id',
2702 'labelColumn' => 'name',
5f627bb5 2703 ];
14f42e31
CW
2704 }
2705 }
2706
c914d4dd 2707 /**
2708 * @param CRM_Core_DAO_CustomField $field
a61d2ab7 2709 * @param 'add|modify|delete' $operation
c914d4dd 2710 *
2711 * @return array
2712 */
2713 protected static function prepareCreateParams($field, $operation) {
2714 $tableName = CRM_Core_DAO::getFieldValue(
2715 'CRM_Core_DAO_CustomGroup',
2716 $field->custom_group_id,
2717 'table_name'
2718 );
2719
2720 $params = [
2721 'table_name' => $tableName,
2722 'operation' => $operation,
2723 'name' => $field->column_name,
2724 'type' => CRM_Core_BAO_CustomValueTable::fieldToSQLType(
2725 $field->data_type,
2726 $field->text_length
2727 ),
2728 'required' => $field->is_required,
2729 'searchable' => $field->is_searchable,
2730 ];
2731
051f2455
CW
2732 // For adding/dropping FK constraints
2733 $params['fkName'] = CRM_Core_BAO_SchemaHandler::getIndexName($tableName, $field->column_name);
2734
a61d2ab7
CW
2735 $fkFields = [
2736 'Country' => 'civicrm_country',
2737 'StateProvince' => 'civicrm_state_province',
2738 'ContactReference' => 'civicrm_contact',
2739 'File' => 'civicrm_file',
2740 ];
2741 if (isset($fkFields[$field->data_type])) {
2742 // Serialized fields store value-separated strings which are incompatible with FK constraints
d532d0cf 2743 if (!$field->serialize) {
a61d2ab7
CW
2744 $params['fk_table_name'] = $fkFields[$field->data_type];
2745 $params['fk_field_name'] = 'id';
2746 $params['fk_attributes'] = 'ON DELETE SET NULL';
2747 }
c914d4dd 2748 }
d532d0cf 2749 if ($field->serialize) {
2750 $params['type'] = 'varchar(255)';
2751 }
c914d4dd 2752 if (isset($field->default_value)) {
2753 $params['default'] = "'{$field->default_value}'";
2754 }
2755 return $params;
2756 }
2757
6a488035 2758}