Merge pull request #21965 from civicrm/5.43
[civicrm-core.git] / CRM / Core / BAO / CustomField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Business objects for managing custom data fields.
20 */
21 class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
22
23 /**
24 * Array to hold (formatted) fields for import
25 *
26 * @var array
27 */
28 public static $_importFields = NULL;
29
30 /**
31 * Build and retrieve the list of data types and descriptions.
32 *
33 * @return array
34 * Data type => Description
35 */
36 public static function dataType() {
37 return [
38 'String' => ts('Alphanumeric'),
39 'Int' => ts('Integer'),
40 'Float' => ts('Number'),
41 'Money' => ts('Money'),
42 'Memo' => ts('Note'),
43 'Date' => ts('Date'),
44 'Boolean' => ts('Yes or No'),
45 'StateProvince' => ts('State/Province'),
46 'Country' => ts('Country'),
47 'File' => ts('File'),
48 'Link' => ts('Link'),
49 'ContactReference' => ts('Contact Reference'),
50 ];
51 }
52
53 /**
54 * Build the map of custom field's data types and there respective Util type
55 *
56 * @return array
57 * Data data-type => CRM_Utils_Type
58 */
59 public static function dataToType() {
60 return [
61 'String' => CRM_Utils_Type::T_STRING,
62 'Int' => CRM_Utils_Type::T_INT,
63 'Money' => CRM_Utils_Type::T_MONEY,
64 'Memo' => CRM_Utils_Type::T_LONGTEXT,
65 'Float' => CRM_Utils_Type::T_FLOAT,
66 'Date' => CRM_Utils_Type::T_DATE,
67 'DateTime' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME,
68 'Boolean' => CRM_Utils_Type::T_BOOLEAN,
69 'StateProvince' => CRM_Utils_Type::T_INT,
70 'File' => CRM_Utils_Type::T_STRING,
71 'Link' => CRM_Utils_Type::T_STRING,
72 'ContactReference' => CRM_Utils_Type::T_INT,
73 'Country' => CRM_Utils_Type::T_INT,
74 ];
75 }
76
77 /**
78 * Takes an associative array and creates a custom field object.
79 *
80 * This function is invoked from within the web form layer and also from the api layer
81 *
82 * @param array $params
83 * (reference) an assoc array of name/value pairs.
84 *
85 * @return CRM_Core_DAO_CustomField
86 */
87 public static function create($params) {
88 $changeSerialize = self::getChangeSerialize($params);
89 $customField = self::createCustomFieldRecord($params);
90 // When deserializing a field, the update needs to run before the schema change
91 if ($changeSerialize === 0) {
92 CRM_Core_DAO::singleValueQuery(self::getAlterSerializeSQL($customField));
93 }
94 $op = empty($params['id']) ? 'add' : 'modify';
95 self::createField($customField, $op);
96 // When serializing a field, the update needs to run after the schema change
97 if ($changeSerialize === 1) {
98 CRM_Core_DAO::singleValueQuery(self::getAlterSerializeSQL($customField));
99 }
100
101 CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField);
102
103 CRM_Utils_System::flushCache();
104 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
105 // so do it manually. Ideally we wouldn't need to clear others...
106 Civi::cache('metadata')->clear();
107
108 return $customField;
109 }
110
111 /**
112 * Save multiple fields, now deprecated in favor of self::writeRecords.
113 * https://lab.civicrm.org/dev/core/issues/1093
114 * @deprecated
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.
120 *
121 * @throws \CiviCRM_API3_Exception
122 */
123 public static function bulkSave($bulkParams, $defaults = []) {
124 CRM_Core_Error::deprecatedFunctionWarning(__CLASS__ . '::writeRecords');
125 foreach ($bulkParams as $index => $fieldParams) {
126 $bulkParams[$index] = array_merge($defaults, $fieldParams);
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
137 */
138 public static function writeRecords(array $records): array {
139 $addedColumns = $sql = $customFields = $pre = $post = [];
140 foreach ($records as $index => $params) {
141 CRM_Utils_Hook::pre(empty($params['id']) ? 'create' : 'edit', 'CustomField', $params['id'] ?? NULL, $params);
142
143 $changeSerialize = self::getChangeSerialize($params);
144 $customField = self::createCustomFieldRecord($params);
145 // Serialize/deserialize sql must run after/before the table is altered
146 if ($changeSerialize === 0) {
147 $pre[] = self::getAlterSerializeSQL($customField);
148 }
149 if ($changeSerialize === 1) {
150 $post[] = self::getAlterSerializeSQL($customField);
151 }
152 $fieldSQL = self::getAlterFieldSQL($customField, empty($params['id']) ? 'add' : 'modify');
153
154 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id, 'table_name');
155 $sql[$tableName][] = $fieldSQL;
156 $addedColumns[$tableName][] = $customField->column_name;
157 $customFields[$index] = $customField;
158 }
159
160 foreach ($pre as $query) {
161 CRM_Core_DAO::executeQuery($query);
162 }
163
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);
167
168 if (CRM_Core_Config::singleton()->logging) {
169 $logging = new CRM_Logging_Schema();
170 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
171 }
172
173 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
174 }
175
176 foreach ($post as $query) {
177 CRM_Core_DAO::executeQuery($query);
178 }
179
180 CRM_Utils_System::flushCache();
181 Civi::cache('metadata')->clear();
182
183 foreach ($customFields as $index => $customField) {
184 CRM_Utils_Hook::post(empty($records[$index]['id']) ? 'create' : 'edit', 'CustomField', $customField->id, $customField);
185 }
186 return $customFields;
187 }
188
189 /**
190 * Fetch object based on array of properties.
191 *
192 * @param array $params
193 * An assoc array of name/value pairs.
194 * @param array $defaults
195 * (reference ) an assoc array to hold the flattened values.
196 *
197 * @return CRM_Core_DAO_CustomField
198 */
199 public static function retrieve($params, &$defaults) {
200 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
201 }
202
203 /**
204 * Update the is_active flag in the db.
205 *
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.
210 *
211 * @return bool
212 * true if we found and updated the object, else false
213 */
214 public static function setIsActive($id, $is_active) {
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 *
226 * @param int $id
227 * Id of field.
228 *
229 * @return string
230 * name
231 */
232 public static function getTitle($id) {
233 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $id, 'label');
234 }
235
236 /**
237 * @param string $context
238 * @return array|bool
239 */
240 public function getOptions($context = NULL) {
241 CRM_Core_DAO::buildOptionsContext($context);
242
243 if (!$this->id) {
244 return FALSE;
245 }
246 $cacheKey = "CRM_Core_BAO_CustomField_getOptions_{$this->id}_$context";
247 $cache = CRM_Utils_Cache::singleton();
248 $options = $cache->get($cacheKey);
249 if (!isset($options)) {
250 if (!$this->data_type || !$this->custom_group_id) {
251 $this->find(TRUE);
252 }
253
254 // This will hold the list of options in format key => label
255 $options = [];
256
257 if (!empty($this->option_group_id)) {
258 $options = CRM_Core_OptionGroup::valuesByID(
259 $this->option_group_id, FALSE, FALSE, FALSE, $context == 'validate' ? 'name' : 'label', !($context == 'validate' || $context == 'get')
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') {
269 $options = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
270 }
271 CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
272 CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, ['context' => $context]);
273 $cache->set($cacheKey, $options);
274 }
275 return $options;
276 }
277
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';
286 $options['Select Country'] = 'Select Country';
287 $options['Multi-Select Country'] = 'Multi-Select Country';
288 $options['Select State/Province'] = 'Select State/Province';
289 $options['Multi-Select State/Province'] = 'Multi-Select State/Province';
290 }
291 return $options;
292 }
293
294 /**
295 * Store and return an array of all active custom fields.
296 *
297 * @param string $customDataType
298 * Type of Custom Data; 'ANY' is a synonym for "all contact data types".
299 * @param bool $showAll
300 * If true returns all fields (includes disabled fields).
301 * @param bool $inline
302 * If true returns all inline fields (includes disabled fields).
303 * @param int $customDataSubType
304 * Custom Data sub type value.
305 * @param int $customDataSubName
306 * Custom Data sub name value.
307 * @param bool $onlyParent
308 * Return only top level custom data, for eg, only Participant and ignore subname and subtype.
309 * @param bool $onlySubType
310 * Return only custom data for subtype.
311 * @param bool|int $checkPermission
312 * Either a CRM_Core_Permission constant or FALSE to disable checks
313 *
314 * @return array
315 * an array of active custom fields.
316 * @throws \CRM_Core_Exception
317 */
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,
326 $checkPermission = CRM_Core_Permission::EDIT
327 ) {
328 if ($checkPermission === TRUE) {
329 CRM_Core_Error::deprecatedWarning('Unexpected TRUE passed to CustomField::getFields $checkPermission param.');
330 $checkPermission = CRM_Core_Permission::EDIT;
331 }
332 if (empty($customDataType)) {
333 $customDataType = ['Contact', 'Individual', 'Organization', 'Household'];
334 }
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 }
340 if ($customDataType && !is_array($customDataType)) {
341
342 if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes(), TRUE)) {
343 // This is the case when getFieldsForImport() requires fields
344 // limited strictly to a subtype.
345 $customDataSubType = $customDataType;
346 $customDataType = CRM_Contact_BAO_ContactType::getBasicType($customDataType);
347 $onlySubType = TRUE;
348 }
349
350 if (array_key_exists($customDataType, CRM_Core_SelectValues::customGroupExtends())) {
351 // this makes the method flexible to support retrieving fields
352 // for multiple extends value.
353 $customDataType = [$customDataType];
354 }
355 }
356
357 $customDataSubType = CRM_Utils_Array::explodePadded($customDataSubType);
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_';
372 $cacheKey .= $checkPermission ? $checkPermission . CRM_Core_Session::getLoggedInContactID() . '_' : '_0_0_';
373 $cacheKey .= '_' . CRM_Core_Config::domainID() . '_';
374
375 $cgTable = CRM_Core_DAO_CustomGroup::getTableName();
376
377 // also get the permission stuff here
378 if ($checkPermission) {
379 $permissionClause = CRM_Core_Permission::customGroupClause($checkPermission,
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) {
398 self::$_importFields = [];
399 }
400
401 // check if we can retrieve from database cache
402 $fields = Civi::Cache('fields')->get("custom importableFields $cacheKey");
403
404 if ($fields === NULL) {
405
406 $extends = '';
407 if (is_array($customDataType)) {
408 $value = NULL;
409 foreach ($customDataType as $dataType) {
410 if (array_key_exists($dataType, CRM_Core_SelectValues::customGroupExtends())) {
411 if (in_array($dataType, ['Individual', 'Household', 'Organization'])) {
412 $val = "'" . CRM_Utils_Type::escape($dataType, 'String') . "', 'Contact' ";
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
425 if (!empty($customDataType) && empty($extends)) {
426 // $customDataType specified a filter, but there is no corresponding SQL ($extends)
427 self::$_importFields[$cacheKey] = [];
428 return self::$_importFields[$cacheKey];
429 }
430
431 if ($onlyParent) {
432 $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL ";
433 }
434 // Temporary hack - in 5.27 a new field is added to civicrm_custom_field. There is a high
435 // risk this function is called before the upgrade page can be reached and if
436 // so it will potentially result in fatal error.
437 $serializeField = CRM_Core_BAO_Domain::isDBVersionAtLeast('5.27.alpha1') ? "custom_field.serialize," : '';
438
439 $query = "SELECT custom_field.id, custom_field.label,
440 $cgTable.title,
441 custom_field.data_type,
442 custom_field.html_type,
443 custom_field.default_value,
444 custom_field.options_per_line, custom_field.text_length,
445 custom_field.custom_group_id,
446 custom_field.is_required,
447 custom_field.column_name,
448 $cgTable.extends, custom_field.is_search_range,
449 $cgTable.extends_entity_column_value,
450 $cgTable.extends_entity_column_id,
451 custom_field.is_view,
452 custom_field.option_group_id,
453 custom_field.date_format,
454 custom_field.time_format,
455 $cgTable.is_multiple,
456 $serializeField
457 $cgTable.table_name,
458 og.name as option_group_name
459 FROM civicrm_custom_field custom_field
460 INNER JOIN $cgTable
461 ON custom_field.custom_group_id = $cgTable.id
462 LEFT JOIN civicrm_option_group og
463 ON custom_field.option_group_id = og.id
464 WHERE ( 1 ) ";
465
466 if (!$showAll) {
467 $query .= " AND custom_field.is_active = 1 AND $cgTable.is_active = 1 ";
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)) {
477 $subtypeClause = [];
478 foreach ($customDataSubType as $subtype) {
479 $subtype = CRM_Core_DAO::VALUE_SEPARATOR . CRM_Utils_Type::escape($subtype, 'String') . CRM_Core_DAO::VALUE_SEPARATOR;
480 $subtypeClause[] = "$cgTable.extends_entity_column_value LIKE '%{$subtype}%'";
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) {
494 $permissionClause = CRM_Core_Permission::customGroupClause($checkPermission,
495 "{$cgTable}.", TRUE
496 );
497 }
498 else {
499 $permissionClause = '(1)';
500 }
501
502 $query .= " $extends AND $permissionClause
503 ORDER BY $cgTable.weight, $cgTable.title,
504 custom_field.weight, custom_field.label";
505
506 $dao = CRM_Core_DAO::executeQuery($query);
507
508 $fields = [];
509 while (($dao->fetch()) != NULL) {
510 $regexp = preg_replace('/[.,;:!?]/', '', NULL);
511 $fields[$dao->id]['id'] = $dao->id;
512 $fields[$dao->id]['label'] = $dao->label;
513 // This seems broken, but not in a new way.
514 $fields[$dao->id]['headerPattern'] = '/' . preg_quote($regexp, '/') . '/';
515 // To support the consolidation of various functions & their expectations.
516 $fields[$dao->id]['title'] = $dao->label;
517 $fields[$dao->id]['custom_field_id'] = $dao->id;
518 $fields[$dao->id]['groupTitle'] = $dao->title;
519 $fields[$dao->id]['data_type'] = $dao->data_type;
520 $fields[$dao->id]['name'] = 'custom_' . $dao->id;
521 $fields[$dao->id]['type'] = CRM_Utils_Array::value($dao->data_type, self::dataToType());
522 $fields[$dao->id]['html_type'] = $dao->html_type;
523 $fields[$dao->id]['default_value'] = $dao->default_value;
524 $fields[$dao->id]['text_length'] = $dao->text_length;
525 $fields[$dao->id]['options_per_line'] = $dao->options_per_line;
526 $fields[$dao->id]['custom_group_id'] = $dao->custom_group_id;
527 $fields[$dao->id]['extends'] = $dao->extends;
528 $fields[$dao->id]['is_search_range'] = $dao->is_search_range;
529 $fields[$dao->id]['extends_entity_column_value'] = $dao->extends_entity_column_value;
530 $fields[$dao->id]['extends_entity_column_id'] = $dao->extends_entity_column_id;
531 $fields[$dao->id]['is_view'] = $dao->is_view;
532 $fields[$dao->id]['is_multiple'] = $dao->is_multiple;
533 $fields[$dao->id]['option_group_id'] = $dao->option_group_id;
534 $fields[$dao->id]['date_format'] = $dao->date_format;
535 $fields[$dao->id]['time_format'] = $dao->time_format;
536 $fields[$dao->id]['is_required'] = $dao->is_required;
537 $fields[$dao->id]['table_name'] = $dao->table_name;
538 $fields[$dao->id]['column_name'] = $dao->column_name;
539 $fields[$dao->id]['serialize'] = $serializeField ? $dao->serialize : (int) self::isSerialized($dao);
540 $fields[$dao->id]['where'] = $dao->table_name . '.' . $dao->column_name;
541 // Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
542 $fields[$dao->id]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery::$extendsMap) ? CRM_Core_BAO_CustomQuery::$extendsMap[$dao->extends] : '';
543 if (in_array($dao->extends, CRM_Contact_BAO_ContactType::subTypes())) {
544 // if $extends is a subtype, refer contact table
545 $fields[$dao->id]['extends_table'] = 'civicrm_contact';
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'];
549 self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
550 }
551
552 Civi::cache('fields')->set("custom importableFields $cacheKey", $fields);
553 }
554 self::$_importFields[$cacheKey] = $fields;
555 }
556
557 return self::$_importFields[$cacheKey];
558 }
559
560 /**
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.
565 *
566 * @param int|string $contactType Contact type
567 * @param bool $showAll
568 * If true returns all fields (includes disabled fields).
569 * @param bool $onlyParent
570 * Return fields ONLY related to basic types.
571 * @param bool $search
572 * When called from search and multiple records need to be returned.
573 * @param bool|int $checkPermission
574 * Either a CRM_Core_Permission constant or FALSE to disable checks
575 *
576 * @param bool $withMultiple
577 *
578 * @return array
579 */
580 public static function getFieldsForImport(
581 $contactType = 'Individual',
582 $showAll = FALSE,
583 $onlyParent = FALSE,
584 $search = FALSE,
585 $checkPermission = TRUE,
586 $withMultiple = FALSE
587 ) {
588 if ($checkPermission === TRUE) {
589 // TODO: Trigger deprecation notice for passing TRUE
590 $checkPermission = CRM_Core_Permission::EDIT;
591 }
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
604 $importableFields = [];
605 foreach ($fields as $id => $values) {
606 // for now we should not allow multiple fields in profile / export etc, hence unsetting
607 if (!$search &&
608 (!empty($values['is_multiple']) && !$withMultiple)
609 ) {
610 continue;
611 }
612 if (!empty($values['text_length'])) {
613 $values['maxlength'] = (int) $values['text_length'];
614 }
615
616 /* generate the key for the fields array */
617
618 $key = "custom_$id";
619 $importableFields[$key] = $values;
620 $importableFields[$key]['import'] = 1;
621 }
622
623 return $importableFields;
624 }
625
626 /**
627 * Get the field id from an import key.
628 *
629 * @param string $key
630 * The key to parse.
631 *
632 * @param bool $all
633 *
634 * @return int|null
635 * The id (if exists)
636 */
637 public static function getKeyID($key, $all = FALSE) {
638 $match = [];
639 if (preg_match('/^custom_(\d+)_?(-?\d+)?$/', $key, $match)) {
640 if (!$all) {
641 return $match[1];
642 }
643 else {
644 return [
645 $match[1],
646 CRM_Utils_Array::value(2, $match),
647 ];
648 }
649 }
650 return $all ? [NULL, NULL] : NULL;
651 }
652
653 /**
654 * Use the cache to get all values of a specific custom field.
655 *
656 * @param int $fieldID
657 * The custom field ID.
658 *
659 * @return CRM_Core_BAO_CustomField
660 * The field object.
661 * @throws CRM_Core_Exception
662 */
663 public static function getFieldObject($fieldID) {
664 $field = new CRM_Core_BAO_CustomField();
665
666 // check if we can get the field values from the system cache
667 $cacheKey = "CRM_Core_DAO_CustomField_{$fieldID}";
668 $cache = CRM_Utils_Cache::singleton();
669 $fieldValues = $cache->get($cacheKey);
670 if (empty($fieldValues)) {
671 $field->id = $fieldID;
672 if (!$field->find(TRUE)) {
673 throw new CRM_Core_Exception('Cannot find Custom Field');
674 }
675
676 $fieldValues = [];
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 /**
689 * Add a custom field to an existing form.
690 *
691 * @param CRM_Core_Form $qf
692 * Form object (reference).
693 * @param string $elementName
694 * Name of the custom field.
695 * @param int $fieldId
696 * @param bool $useRequired
697 * True if required else false.
698 * @param bool $search
699 * True if used for search else false.
700 * @param string $label
701 * Label for custom field.
702 * @return \HTML_QuickForm_Element|null
703 * @throws \CiviCRM_API3_Exception
704 */
705 public static function addQuickFormElement(
706 $qf, $elementName, $fieldId, $useRequired = TRUE, $search = FALSE, $label = NULL
707 ) {
708 $field = self::getFieldObject($fieldId);
709 $widget = $field->html_type;
710 $element = NULL;
711 $customFieldAttributes = [];
712
713 if (!isset($label)) {
714 $label = $field->label;
715 }
716
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
721 // Custom field HTML should indicate group+field name
722 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id);
723 $fieldAttributes['data-crm-custom'] = $groupName . ':' . $field->name;
724
725 // Fixed for Issue CRM-2183
726 if ($widget == 'TextArea' && $search) {
727 $widget = 'Text';
728 }
729
730 $placeholder = $search ? ts('- any %1 -', [1 => $label]) : ts('- select %1 -', [1 => $label]);
731
732 if (in_array($widget, [
733 'Select',
734 'CheckBox',
735 'Radio',
736 ])) {
737 $options = $field->getOptions($search ? 'search' : 'create');
738
739 // Consolidate widget types to simplify the below switch statement
740 if ($search) {
741 $widget = 'Select';
742 }
743
744 // Search field is always multi-select
745 if ($search || (self::isSerialized($field) && $widget === 'Select')) {
746 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' huge');
747 $fieldAttributes['multiple'] = 'multiple';
748 $fieldAttributes['placeholder'] = $placeholder;
749 }
750
751 // Add data for popup link. Normally this is handled by CRM_Core_Form->addSelect
752 $canEditOptions = CRM_Core_Permission::check('administer CiviCRM');
753 if ($field->option_group_id && !$search && $canEditOptions) {
754 $customFieldAttributes += [
755 'data-api-entity' => $field->getEntity(),
756 'data-api-field' => 'custom_' . $field->id,
757 'data-option-edit-path' => 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $field->option_group_id),
758 ];
759 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
760 }
761 }
762
763 $rangeDataTypes = ['Int', 'Float', 'Money'];
764
765 // at some point in time we might want to split the below into small functions
766
767 switch ($widget) {
768 case 'Text':
769 case 'Link':
770 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
771 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
772 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
773 }
774 else {
775 if ($field->text_length) {
776 $fieldAttributes['maxlength'] = $field->text_length;
777 if ($field->text_length < 20) {
778 $fieldAttributes['size'] = $field->text_length;
779 }
780 }
781 $element = $qf->add('text', $elementName, $label,
782 $fieldAttributes,
783 $useRequired && !$search
784 );
785 }
786 break;
787
788 case 'TextArea':
789 $fieldAttributes['rows'] = $field->note_rows ?? 4;
790 $fieldAttributes['cols'] = $field->note_columns ?? 60;
791
792 if ($field->text_length) {
793 $fieldAttributes['maxlength'] = $field->text_length;
794 }
795 $element = $qf->add('textarea',
796 $elementName,
797 $label,
798 $fieldAttributes,
799 $useRequired && !$search
800 );
801 break;
802
803 case 'Select Date':
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
808 $params = [
809 'date' => $field->date_format,
810 'minDate' => isset($minYear) ? $minYear . '-01-01' : NULL,
811 //CRM-18487 - max date should be the last date of the year.
812 'maxDate' => isset($maxYear) ? $maxYear . '-12-31' : NULL,
813 'time' => $field->time_format ? $field->time_format * 12 : FALSE,
814 ];
815 if ($field->is_search_range && $search) {
816 $qf->add('datepicker', $elementName . '_from', $label, $fieldAttributes + array('placeholder' => ts('From')), FALSE, $params);
817 $qf->add('datepicker', $elementName . '_to', NULL, $fieldAttributes + array('placeholder' => ts('To')), FALSE, $params);
818 }
819 else {
820 $element = $qf->add('datepicker', $elementName, $label, $fieldAttributes, $useRequired && !$search, $params);
821 }
822 break;
823
824 case 'Radio':
825 if ($field->is_search_range && $search && in_array($field->data_type, $rangeDataTypes)) {
826 $qf->add('text', $elementName . '_from', $label . ' ' . ts('From'), $fieldAttributes);
827 $qf->add('text', $elementName . '_to', ts('To'), $fieldAttributes);
828 }
829 else {
830 $fieldAttributes = array_merge($fieldAttributes, $customFieldAttributes);
831 if ($search || empty($useRequired)) {
832 $fieldAttributes['allowClear'] = TRUE;
833 }
834 $qf->addRadio($elementName, $label, $options, $fieldAttributes, NULL, $useRequired);
835 }
836 break;
837
838 // For all select elements
839 case 'Select':
840 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-select2');
841
842 if (empty($fieldAttributes['multiple'])) {
843 $options = ['' => $placeholder] + $options;
844 }
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']);
857 }
858 break;
859
860 case 'CheckBox':
861 $check = [];
862 foreach ($options as $v => $l) {
863 // TODO: I'm not sure if this is supposed to exclude whatever might be
864 // in $field->attributes (available in array format as
865 // $fieldAttributes). Leaving as-is for now.
866 $check[] = &$qf->addElement('advcheckbox', $v, NULL, $l, $customFieldAttributes);
867 }
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
875 if ($useRequired && !$search) {
876 $qf->addRule($elementName, ts('%1 is a required field.', [1 => $label]), 'required');
877 }
878 break;
879
880 case 'File':
881 // we should not build upload file in search mode
882 if ($search) {
883 return NULL;
884 }
885 $element = $qf->add(
886 strtolower($field->html_type),
887 $elementName,
888 $label,
889 $fieldAttributes,
890 $useRequired && !$search
891 );
892 $qf->addUploadElement($elementName);
893 break;
894
895 case 'RichTextEditor':
896 $fieldAttributes['rows'] = $field->note_rows;
897 $fieldAttributes['cols'] = $field->note_columns;
898 if ($field->text_length) {
899 $fieldAttributes['maxlength'] = $field->text_length;
900 }
901 $element = $qf->add('wysiwyg', $elementName, $label, $fieldAttributes, $useRequired && !$search);
902 break;
903
904 case 'Autocomplete-Select':
905 static $customUrls = [];
906 if ($field->data_type == 'ContactReference') {
907 // break if contact does not have permission to access ContactReference
908 if (!CRM_Core_Permission::check('access contact reference fields')) {
909 break;
910 }
911 $fieldAttributes['class'] = ltrim(($fieldAttributes['class'] ?? '') . ' crm-form-contact-reference huge');
912 $fieldAttributes['data-api-entity'] = 'Contact';
913 if (!empty($field->serialize) || $search) {
914 $fieldAttributes['multiple'] = TRUE;
915 }
916 $element = $qf->add('text', $elementName, $label, $fieldAttributes, $useRequired && !$search);
917
918 $urlParams = "context=customfield&id={$field->id}";
919 $idOfelement = $elementName;
920 // dev/core#362 if in an onbehalf profile clean up the name to get rid of square brackets that break the select 2 js
921 // However this caused regression https://lab.civicrm.org/dev/core/issues/619 so it has been hacked back to
922 // only affecting on behalf - next time someone looks at this code it should be with a view to overhauling it
923 // rather than layering on more hacks.
924 if (substr($elementName, 0, 8) === 'onbehalf' && strpos($elementName, '[') && strpos($elementName, ']')) {
925 $idOfelement = substr(substr($elementName, (strpos($elementName, '[') + 1)), 0, -1);
926 }
927 $customUrls[$idOfelement] = CRM_Utils_System::url('civicrm/ajax/contactref',
928 $urlParams,
929 FALSE, NULL, FALSE
930 );
931
932 }
933 else {
934 // FIXME: This won't work with customFieldOptions hook
935 $fieldAttributes += [
936 'entity' => 'OptionValue',
937 'placeholder' => $placeholder,
938 'multiple' => $search ? TRUE : !empty($field->serialize),
939 'api' => [
940 'params' => ['option_group_id' => $field->option_group_id, 'is_active' => 1],
941 ],
942 ];
943 $element = $qf->addEntityRef($elementName, $label, $fieldAttributes, $useRequired && !$search);
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.
953 if ($field->is_search_range && $search && $widget != 'Select') {
954 $qf->addRule($elementName . '_from', ts('%1 From must be an integer (whole number).', [1 => $label]), 'integer');
955 $qf->addRule($elementName . '_to', ts('%1 To must be an integer (whole number).', [1 => $label]), 'integer');
956 }
957 elseif ($widget == 'Text') {
958 $qf->addRule($elementName, ts('%1 must be an integer (whole number).', [1 => $label]), 'integer');
959 }
960 break;
961
962 case 'Float':
963 if ($field->is_search_range && $search) {
964 $qf->addRule($elementName . '_from', ts('%1 From must be a number (with or without decimal point).', [1 => $label]), 'numeric');
965 $qf->addRule($elementName . '_to', ts('%1 To must be a number (with or without decimal point).', [1 => $label]), 'numeric');
966 }
967 elseif ($widget == 'Text') {
968 $qf->addRule($elementName, ts('%1 must be a number (with or without decimal point).', [1 => $label]), 'numeric');
969 }
970 break;
971
972 case 'Money':
973 if ($field->is_search_range && $search) {
974 $qf->addRule($elementName . '_from', ts('%1 From must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
975 $qf->addRule($elementName . '_to', ts('%1 To must in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
976 }
977 elseif ($widget == 'Text') {
978 $qf->addRule($elementName, ts('%1 must be in proper money format. (decimal point/comma/space is allowed).', [1 => $label]), 'money');
979 }
980 break;
981
982 case 'Link':
983 $element->setAttribute('class', "url");
984 $qf->addRule($elementName, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'wikiURL');
985 break;
986 }
987 if ($field->is_view && !$search) {
988 $qf->freeze($elementName);
989 }
990 return $element;
991 }
992
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
1014 /**
1015 * Delete the Custom Field.
1016 *
1017 * @param object $field
1018 * The field object.
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 }
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);
1034 CRM_Core_BAO_Mapping::removeFieldFromMapping('custom_' . $field->id);
1035 CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');
1036
1037 CRM_Utils_Hook::post('delete', 'CustomField', $field->id, $field);
1038 }
1039
1040 /**
1041 * @param string|int|array|null $value
1042 * @param CRM_Core_BAO_CustomField|int|array|string $field
1043 * @param int $entityId
1044 *
1045 * @return string
1046 *
1047 * @throws \CRM_Core_Exception
1048 */
1049 public static function displayValue($value, $field, $entityId = NULL) {
1050 $field = is_array($field) ? $field['id'] : $field;
1051 $fieldId = is_object($field) ? $field->id : (int) str_replace('custom_', '', $field);
1052
1053 if (!$fieldId) {
1054 throw new CRM_Core_Exception('CRM_Core_BAO_CustomField::displayValue requires a field id');
1055 }
1056
1057 if (!is_a($field, 'CRM_Core_BAO_CustomField')) {
1058 $field = self::getFieldObject($fieldId);
1059 }
1060
1061 $fieldInfo = ['options' => $field->getOptions()] + (array) $field;
1062
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;
1069 }
1070
1071 /**
1072 * Lower-level logic for rendering a custom field value
1073 *
1074 * @param string|array $value
1075 * @param array $field
1076 * @param int|null $entityId
1077 *
1078 * @return string
1079 */
1080 private static function formatDisplayValue($value, $field, $entityId = NULL) {
1081
1082 if (self::isSerialized($field) && !is_array($value)) {
1083 $value = CRM_Utils_Array::explodePadded($value);
1084 }
1085 // CRM-12989 fix
1086 if ($field['html_type'] == 'CheckBox' && $value) {
1087 $value = CRM_Utils_Array::convertCheckboxFormatToArray($value);
1088 }
1089
1090 $display = is_array($value) ? implode(', ', $value) : (string) $value;
1091
1092 switch ($field['html_type']) {
1093
1094 case 'Select':
1095 case 'Autocomplete-Select':
1096 case 'Radio':
1097 case 'CheckBox':
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');
1102 }
1103 $display = implode(', ', $displayNames);
1104 }
1105 elseif ($field['data_type'] == 'ContactReference') {
1106 $display = $value;
1107 }
1108 elseif (is_array($value)) {
1109 $v = [];
1110 foreach ($value as $key => $val) {
1111 $v[] = $field['options'][$val] ?? NULL;
1112 }
1113 $display = implode(', ', $v);
1114 }
1115 else {
1116 $display = CRM_Utils_Array::value($value, $field['options'], '');
1117 // For float type (see Number and Money) $value would be decimal like
1118 // 1.00 (because it is stored in db as decimal), while options array
1119 // key would be integer like 1. In this case expression on line above
1120 // would return empty string (default value), despite the fact that
1121 // matching option exists in the array.
1122 // In such cases we could just get intval($value) and fetch matching
1123 // option again, but this would not work if key is float like 5.6.
1124 // So we need to truncate trailing zeros to make it work as expected.
1125 if ($display === '' && strpos($value, '.') !== FALSE) {
1126 // Use round() to truncate trailing zeros, e.g:
1127 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1128 $value = (string) round($value, 5);
1129 $display = $field['options'][$value] ?? '';
1130 }
1131 }
1132 break;
1133
1134 case 'Select Date':
1135 $customFormat = NULL;
1136
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
1141 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
1142 $format = $field['date_format'] ?? NULL;
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:
1157 //If time is not selected remove time from value.
1158 $value = $value ? date('Y-m-d', strtotime($value)) : '';
1159 }
1160 $customFormat = implode(" ", $customTimeFormat);
1161 }
1162 }
1163 $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
1164 break;
1165
1166 case 'File':
1167 // In the context of displaying a profile, show file/image
1168 if ($value) {
1169 if ($entityId) {
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);
1177 if ($url) {
1178 $display = $url['file_url'];
1179 }
1180 }
1181 else {
1182 // In other contexts show a paperclip icon
1183 if (CRM_Utils_Rule::integer($value)) {
1184 $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
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}";
1195 }
1196 else {
1197 //CRM-18396, if filename is passed instead
1198 $display = $value;
1199 }
1200 }
1201 }
1202 break;
1203
1204 case 'Link':
1205 $display = $display ? "<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1206 break;
1207
1208 case 'TextArea':
1209 $display = nl2br($display);
1210 break;
1211
1212 case 'Text':
1213 if ($field['data_type'] == 'Money' && isset($value)) {
1214 // $value can also be an array(while using IN operator from search builder or api).
1215 foreach ((array) $value as $val) {
1216 $disp[] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($val);
1217 }
1218 $display = implode(', ', $disp);
1219 }
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 }
1227 break;
1228 }
1229 return $display;
1230 }
1231
1232 /**
1233 * Set default values for custom data used in profile.
1234 *
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,.
1247 * just format the given value
1248 */
1249 public static function setProfileDefaults(
1250 $customFieldId,
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)) {
1269 $info = self::getTableColumnGroup($customFieldId);
1270 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1271 $result = CRM_Core_DAO::executeQuery($query);
1272 if ($result->fetch()) {
1273 $value = $result->value;
1274 }
1275 }
1276
1277 if ($customField->data_type == 'Country') {
1278 if (!$value) {
1279 $config = CRM_Core_Config::singleton();
1280 if ($config->defaultContactCountry) {
1281 $value = CRM_Core_BAO_Country::defaultContactCountry();
1282 }
1283 }
1284 }
1285 }
1286
1287 //set defaults if mode is registration
1288 if (!trim($value) &&
1289 ($value !== 0) &&
1290 (!in_array($mode, [CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH]))
1291 ) {
1292 $value = $customField->default_value;
1293 }
1294
1295 if ($customField->data_type == 'Money' && isset($value)) {
1296 $value = number_format($value, 2);
1297 }
1298 if (self::isSerialized($customField) && $value) {
1299 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE);
1300 $defaults[$elementName] = [];
1301 $checkedValue = CRM_Utils_Array::explodePadded($value);
1302 foreach ($customOption as $val) {
1303 if (in_array($val['value'], $checkedValue)) {
1304 if ($customField->html_type == 'CheckBox') {
1305 $defaults[$elementName][$val['value']] = 1;
1306 }
1307 else {
1308 $defaults[$elementName][$val['value']] = $val['value'];
1309 }
1310 }
1311 }
1312 }
1313 else {
1314 $defaults[$elementName] = $value;
1315 }
1316 }
1317
1318 /**
1319 * Get file url.
1320 *
1321 * @param int $contactID
1322 * @param int $cfID
1323 * @param int $fileID
1324 * @param bool $absolute
1325 *
1326 * @param string $multiRecordWhereClause
1327 *
1328 * @return array
1329 */
1330 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1331 if ($contactID) {
1332 if (!$fileID) {
1333 $params = ['id' => $cfID];
1334 $defaults = [];
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
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 }
1351 $fileID = CRM_Core_DAO::singleValueQuery($query);
1352 }
1353
1354 $result = [];
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',
1372 'file_id'
1373 );
1374 list($path) = CRM_Core_BAO_File::path($fileID, $entityId);
1375 $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileID);
1376 $url = CRM_Utils_System::url('civicrm/file',
1377 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1378 $absolute, NULL, TRUE, TRUE
1379 );
1380 $result['file_url'] = CRM_Utils_File::getFileURL($path, $fileType, $url);
1381 }
1382 // for non image files
1383 else {
1384 $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1385 $fileID,
1386 'uri'
1387 );
1388 $fileHash = CRM_Core_BAO_File::generateFileHash($contactID, $fileID);
1389 $url = CRM_Utils_System::url('civicrm/file',
1390 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1391 $absolute, NULL, TRUE, TRUE
1392 );
1393 $result['file_url'] = CRM_Utils_File::getFileURL($uri, $fileType, $url);
1394 }
1395 }
1396 return $result;
1397 }
1398 }
1399
1400 /**
1401 * Format custom fields before inserting.
1402 *
1403 * @param int $customFieldId
1404 * Custom field id.
1405 * @param array $customFormatted
1406 * Formatted array.
1407 * @param mixed $value
1408 * Value of custom field.
1409 * @param string $customFieldExtend
1410 * Custom field extends.
1411 * @param int $customValueId
1412 * Custom option value id.
1413 * @param int $entityId
1414 * Entity id (contribution, membership...).
1415 * @param bool $inline
1416 * Consider inline custom groups only.
1417 * @param bool $checkPermission
1418 * If false, do not include permissioning clause.
1419 * @param bool $includeViewOnly
1420 * If true, fields marked 'View Only' are included. Required for APIv3.
1421 *
1422 * @return array|NULL
1423 * formatted custom field array
1424 */
1425 public static function formatCustomField(
1426 $customFieldId, &$customFormatted, $value,
1427 $customFieldExtend, $customValueId = NULL,
1428 $entityId = NULL,
1429 $inline = FALSE,
1430 $checkPermission = TRUE,
1431 $includeViewOnly = FALSE
1432 ) {
1433 //get the custom fields for the entity
1434 //subtype and basic type
1435 $customDataSubType = NULL;
1436 if ($customFieldExtend) {
1437 // This is the case when getFieldsForImport() requires fields
1438 // of subtype and its parent.CRM-5143
1439 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1440 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend);
1441 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1442 $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType);
1443 if (is_array($customFieldExtend)) {
1444 $customFieldExtend = array_unique(array_values($customFieldExtend));
1445 }
1446 }
1447 }
1448
1449 $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend,
1450 FALSE,
1451 $inline,
1452 $customDataSubType,
1453 NULL,
1454 FALSE,
1455 FALSE,
1456 $checkPermission ? CRM_Core_Permission::EDIT : FALSE
1457 );
1458
1459 if (!array_key_exists($customFieldId, $customFields)) {
1460 return NULL;
1461 }
1462
1463 // return if field is a 'code' field
1464 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1465 return NULL;
1466 }
1467
1468 list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId);
1469
1470 if (!$customValueId &&
1471 // we always create new entites for is_multiple unless specified
1472 !$customFields[$customFieldId]['is_multiple'] &&
1473 $entityId
1474 ) {
1475 $query = "
1476 SELECT 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)) {
1488 $selectedValues = [];
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,
1496 $selectedValues
1497 ) . CRM_Core_DAO::VALUE_SEPARATOR;
1498 }
1499 else {
1500 $value = '';
1501 }
1502 }
1503 }
1504 }
1505 elseif (self::isSerialized($customFields[$customFieldId])) {
1506 // Select2 v3 returns a comma-separated string.
1507 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1508 $value = explode(',', $value);
1509 }
1510
1511 $value = $value ? CRM_Utils_Array::implodePadded($value) : '';
1512 }
1513
1514 if (self::isSerialized($customFields[$customFieldId]) &&
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
1567 $fileID = NULL;
1568
1569 if ($customFields[$customFieldId]['data_type'] == 'File') {
1570 if (empty($value)) {
1571 return;
1572 }
1573
1574 $config = CRM_Core_Config::singleton();
1575
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);
1581 if ($fileDAO->N) {
1582 $fileID = $value;
1583 $fName = $fileDAO->uri;
1584 $mimeType = $fileDAO->mime_type;
1585 }
1586 }
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 }
1594 else {
1595 $fName = $value['name'];
1596 $mimeType = $value['type'];
1597 }
1598
1599 $filename = pathinfo($fName, PATHINFO_BASENAME);
1600
1601 // rename this file to go into the secure directory only if
1602 // user has uploaded new file not existing verfied on the basis of $fileID
1603 if (empty($fileID) && !rename($fName, $config->customFileUploadDir . $filename)) {
1604 CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory'));
1605 }
1606
1607 if ($customValueId && empty($fileID)) {
1608 $query = "
1609 SELECT $columnName
1610 FROM $tableName
1611 WHERE id = %1";
1612 $params = [1 => [$customValueId, 'Integer']];
1613 $fileID = CRM_Core_DAO::singleValueQuery($query, $params);
1614 }
1615
1616 $fileDAO = new CRM_Core_DAO_File();
1617
1618 if ($fileID) {
1619 $fileDAO->id = $fileID;
1620 }
1621
1622 $fileDAO->uri = $filename;
1623 $fileDAO->mime_type = $mimeType;
1624 $fileDAO->upload_date = date('YmdHis');
1625 $fileDAO->save();
1626 $fileID = $fileDAO->id;
1627 $value = $filename;
1628 }
1629
1630 if (!is_array($customFormatted)) {
1631 $customFormatted = [];
1632 }
1633
1634 if (!array_key_exists($customFieldId, $customFormatted)) {
1635 $customFormatted[$customFieldId] = [];
1636 }
1637
1638 $index = -1;
1639 if ($customValueId) {
1640 $index = $customValueId;
1641 }
1642
1643 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1644 $customFormatted[$customFieldId][$index] = [];
1645 }
1646 $customFormatted[$customFieldId][$index] = [
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,
1654 'file_id' => $fileID,
1655 // is_multiple refers to the custom group, serialize refers to the field.
1656 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1657 'serialize' => $customFields[$customFieldId]['serialize'],
1658 ];
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
1665 /**
1666 * Get default custom table schema.
1667 *
1668 * @param array $params
1669 *
1670 * @return array
1671 */
1672 public static function defaultCustomTableSchema($params) {
1673 // add the id and extends_id
1674 $collation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
1675 $characterSet = 'utf8';
1676 if (stripos($collation, 'utf8mb4') !== FALSE) {
1677 $characterSet = 'utf8mb4';
1678 }
1679 $table = [
1680 'name' => $params['name'],
1681 'is_multiple' => $params['is_multiple'],
1682 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1683 'fields' => [
1684 [
1685 'name' => 'id',
1686 'type' => 'int unsigned',
1687 'primary' => TRUE,
1688 'required' => TRUE,
1689 'attributes' => 'AUTO_INCREMENT',
1690 'comment' => 'Default MySQL primary key',
1691 ],
1692 [
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',
1700 ],
1701 ],
1702 ];
1703
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
1710 if (!$params['is_multiple']) {
1711 $table['indexes'] = [
1712 [
1713 'unique' => TRUE,
1714 'field_name_1' => 'entity_id',
1715 ],
1716 ];
1717 }
1718 return $table;
1719 }
1720
1721 /**
1722 * Create custom field.
1723 *
1724 * @param CRM_Core_DAO_CustomField $field
1725 * @param string $operation
1726 */
1727 public static function createField($field, $operation) {
1728 $sql = str_repeat(' ', 8);
1729 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1730 $sql .= "ALTER TABLE " . $tableName;
1731 $sql .= self::getAlterFieldSQL($field, $operation);
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?)
1741 if ($operation === 'add' || $operation === 'modify') {
1742 $logging = new CRM_Logging_Schema();
1743 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name]]);
1744 }
1745 }
1746
1747 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
1748 }
1749
1750 /**
1751 * @param CRM_Core_DAO_CustomField $field
1752 * @param string $operation add|modify|delete
1753 *
1754 * @return bool
1755 */
1756 public static function getAlterFieldSQL($field, $operation) {
1757 $params = self::prepareCreateParams($field, $operation);
1758 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1759 // a function only used by Custom Field creation...
1760 $params['required'] = FALSE;
1761 return CRM_Core_BAO_SchemaHandler::getFieldAlterSQL($params);
1762 }
1763
1764 /**
1765 * Get query to reformat existing values for a field when changing its serialize attribute
1766 *
1767 * @param CRM_Core_DAO_CustomField $field
1768 * @return string
1769 */
1770 private static function getAlterSerializeSQL($field) {
1771 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1772 $col = $field->column_name;
1773 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
1774 if ($field->serialize) {
1775 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1776 }
1777 else {
1778 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1779 }
1780 }
1781
1782 /**
1783 * Determine whether it would be safe to move a field.
1784 *
1785 * @param int $fieldID
1786 * FK to civicrm_custom_field.
1787 * @param int $newGroupID
1788 * FK to civicrm_custom_group.
1789 *
1790 * @return array
1791 * array(string) or TRUE
1792 */
1793 public static function _moveFieldValidate($fieldID, $newGroupID) {
1794 $errors = [];
1795
1796 $field = new CRM_Core_DAO_CustomField();
1797 $field->id = $fieldID;
1798 if (!$field->find(TRUE)) {
1799 $errors['fieldID'] = ts('Invalid ID for custom field');
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'] = ts('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'] = ts('Invalid ID for new custom group');
1814 return $errors;
1815 }
1816
1817 $query = "
1818 SELECT b.id
1819 FROM civicrm_custom_field a
1820 INNER JOIN civicrm_custom_field b
1821 WHERE a.id = %1
1822 AND a.label = b.label
1823 AND b.custom_group_id = %2
1824 ";
1825 $params = [
1826 1 => [$field->id, 'Integer'],
1827 2 => [$newGroup->id, 'Integer'],
1828 ];
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 = "
1838 SELECT count(*)
1839 FROM $tableName
1840 WHERE $columnName is not null
1841 ";
1842 $count = CRM_Core_DAO::singleValueQuery($query);
1843 if ($count > 0) {
1844 $query = "
1845 SELECT extends
1846 FROM civicrm_custom_group
1847 WHERE id IN ( %1, %2 )
1848 ";
1849 $params = [
1850 1 => [$oldGroup->id, 'Integer'],
1851 2 => [$newGroup->id, 'Integer'],
1852 ];
1853
1854 $dao = CRM_Core_DAO::executeQuery($query, $params);
1855 $extends = [];
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 /**
1868 * Move a custom data field from one group (table) to another.
1869 *
1870 * @param int $fieldID
1871 * FK to civicrm_custom_field.
1872 * @param int $newGroupID
1873 * FK to civicrm_custom_group.
1874 *
1875 * @throws CRM_Core_Exception
1876 */
1877 public static function moveField($fieldID, $newGroupID) {
1878 $validation = self::_moveFieldValidate($fieldID, $newGroupID);
1879 if (TRUE !== $validation) {
1880 throw new CRM_Core_Exception(implode(' ', $validation));
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
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}`
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
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
1919 * @param string $index
1920 * @param string $dataType
1921 */
1922 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup $optionGroup, $index, $dataType) {
1923 if (strlen(trim($value))) {
1924 $optionValue = new CRM_Core_DAO_OptionValue();
1925 $optionValue->option_group_id = $optionGroup->id;
1926 $optionValue->label = $params['option_label'][$index];
1927 $optionValue->name = $params['option_name'][$index] ?? CRM_Utils_String::titleToVar($params['option_label'][$index]);
1928 switch ($dataType) {
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
1945 $optionValue->weight = $params['option_weight'][$index];
1946 $optionValue->is_active = $params['option_status'][$index] ?? FALSE;
1947 $optionValue->description = $params['option_description'][$index] ?? NULL;
1948 $optionValue->color = $params['option_color'][$index] ?? NULL;
1949 $optionValue->icon = $params['option_icon'][$index] ?? NULL;
1950 $optionValue->save();
1951 }
1952 }
1953
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
1962 *
1963 * @return array
1964 */
1965 protected static function prepareCreate($params) {
1966 $op = empty($params['id']) ? 'create' : 'edit';
1967 CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params);
1968 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
1969 if ($op === 'create') {
1970 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
1971 if (!isset($params['column_name'])) {
1972 // if add mode & column_name not present, calculate it.
1973 $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32));
1974 }
1975 if (!isset($params['name'])) {
1976 $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64);
1977 }
1978 }
1979 else {
1980 $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
1981 }
1982
1983 $htmlType = $params['html_type'] ?? NULL;
1984 $dataType = $params['data_type'] ?? NULL;
1985
1986 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1987 $params['date_format'] = Civi::settings()->get('dateInputFormat');
1988 }
1989
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'])) {
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];
2001 }
2002 }
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);
2007 }
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 }
2014 }
2015
2016 // create any option group & values if required
2017 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
2018 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
2019 //CRM-16659: if option_value then create an option group for this custom field.
2020 // An option_type of 2 would be a 'message' from the form layer not to handle
2021 // the option_values key. If not set then it is not ignored.
2022 $optionsType = (int) ($params['option_type'] ?? 0);
2023 if (($optionsType !== 2 && empty($params['id']))
2024 && (empty($params['option_group_id']) && !empty($params['option_value'])
2025 )
2026 ) {
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
2045 // Remove option group IDs from fields changed to Text html_type.
2046 if ($htmlType == 'Text') {
2047 $params['option_group_id'] = '';
2048 }
2049
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
2060 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
2061
2062 if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) {
2063 $params['default_value'] = $defaultValue;
2064 }
2065 }
2066 }
2067
2068 // Set default textarea attributes
2069 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
2070 $params['attributes'] = 'rows=4, cols=60';
2071 }
2072 return $params;
2073 }
2074
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);
2105
2106 return $customField;
2107 }
2108
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
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(
2175 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2176 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2177 ON DUPLICATE KEY UPDATE
2178 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2179 ");
2180 }
2181 }
2182 }
2183
2184 /**
2185 * Get the database table name and column name for a custom field.
2186 *
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).
2191 *
2192 * @return array
2193 * fatal is fieldID does not exists, else array of tableName, columnName
2194 * @throws \CRM_Core_Exception
2195 */
2196 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2197 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2198 $cache = CRM_Utils_Cache::singleton();
2199 $fieldValues = $cache->get($cacheKey);
2200 if (empty($fieldValues) || $force) {
2201 $query = "
2202 SELECT cg.table_name, cf.column_name, cg.id
2203 FROM civicrm_custom_group cg,
2204 civicrm_custom_field cf
2205 WHERE cf.custom_group_id = cg.id
2206 AND cf.id = %1";
2207 $params = [1 => [$fieldID, 'Integer']];
2208 $dao = CRM_Core_DAO::executeQuery($query, $params);
2209
2210 if (!$dao->fetch()) {
2211 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2212 }
2213 $fieldValues = [$dao->table_name, $dao->column_name, $dao->id];
2214 $cache->set($cacheKey, $fieldValues);
2215 }
2216 return $fieldValues;
2217 }
2218
2219 /**
2220 * Get custom option groups.
2221 *
2222 * @deprecated Use the API OptionGroup.get
2223 *
2224 * @param array $includeFieldIds
2225 * Ids of custom fields for which option groups must be included.
2226 *
2227 * Currently this is required in the cases where option groups are to be included
2228 * for inactive fields : CRM-5369
2229 *
2230 * @return mixed
2231 */
2232 public static function customOptionGroup($includeFieldIds = NULL) {
2233 static $customOptionGroup = NULL;
2234
2235 $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force';
2236 if ($cacheKey == 'force') {
2237 $customOptionGroup[$cacheKey] = NULL;
2238 }
2239
2240 if (empty($customOptionGroup[$cacheKey])) {
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
2254 INNER 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
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
2284 /**
2285 * Fix orphan groups.
2286 *
2287 * @param int $customFieldId
2288 * Custom field id.
2289 * @param int $optionGroupId
2290 * Option group id.
2291 */
2292 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2293 // check if option group belongs to any custom Field else delete
2294 // get the current option group
2295 $currentOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
2296 $customFieldId,
2297 'option_group_id'
2298 );
2299 // get the updated option group
2300 // if both are same return
2301 if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) {
2302 return;
2303 }
2304
2305 // check if option group is related to any other field
2306 self::checkOptionGroup($currentOptionGroupId);
2307 }
2308
2309 /**
2310 * Check if option group is related to more than one custom field.
2311 *
2312 * @param int $optionGroupId
2313 * Option group id.
2314 */
2315 public static function checkOptionGroup($optionGroupId) {
2316 $query = "
2317 SELECT count(*)
2318 FROM civicrm_custom_field
2319 WHERE 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
2329 /**
2330 * Get option group default.
2331 *
2332 * @param int $optionGroupId
2333 * @param bool $serialize
2334 *
2335 * @return null|string
2336 */
2337 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2338 $query = "
2339 SELECT default_value, serialize
2340 FROM civicrm_custom_field
2341 WHERE option_group_id = {$optionGroupId}
2342 AND default_value IS NOT NULL";
2343
2344 $dao = CRM_Core_DAO::executeQuery($query);
2345 while ($dao->fetch()) {
2346 if ($dao->serialize == $serialize) {
2347 return $dao->default_value;
2348 }
2349 $defaultValue = $dao->default_value;
2350 }
2351
2352 // Convert serialization
2353 if (isset($defaultValue) && $serialize) {
2354 return CRM_Utils_Array::implodePadded([$defaultValue]);
2355 }
2356 elseif (isset($defaultValue)) {
2357 return CRM_Utils_Array::explodePadded($defaultValue)[0];
2358 }
2359 return NULL;
2360 }
2361
2362 /**
2363 * Post process function.
2364 *
2365 * @param array $params
2366 * @param int $entityID
2367 * @param string $customFieldExtends
2368 * @param bool $inline
2369 * @param bool $checkPermissions
2370 *
2371 * @return array
2372 */
2373 public static function postProcess(
2374 &$params,
2375 $entityID,
2376 $customFieldExtends,
2377 $inline = FALSE,
2378 $checkPermissions = TRUE
2379 ) {
2380 $customData = [];
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,
2401 $inline,
2402 $checkPermissions
2403 );
2404 }
2405 }
2406 return $customData;
2407 }
2408
2409 /**
2410 * Get custom field ID from field/group name/title.
2411 *
2412 * @param string $fieldName Field name or label
2413 * @param string|null $groupName (Optional) Group name or label
2414 * @param bool $fullString Whether to return "custom_123" or "123"
2415 *
2416 * @return string|int|null
2417 * @throws \CiviCRM_API3_Exception
2418 */
2419 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2420 $cacheKey = $groupName . '.' . $fieldName;
2421 if (!isset(Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2422 $customFieldParams = [
2423 'name' => $fieldName,
2424 'label' => $fieldName,
2425 'options' => ['or' => [["name", "label"]]],
2426 ];
2427
2428 if ($groupName) {
2429 $customFieldParams['custom_group_id.name'] = $groupName;
2430 $customFieldParams['custom_group_id.title'] = $groupName;
2431 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2432 }
2433
2434 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2435
2436 if (empty($field['id'])) {
2437 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2438 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2439 }
2440 else {
2441 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2442 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2443 }
2444 }
2445
2446 if ($fullString) {
2447 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2448 }
2449 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
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.
2454 *
2455 * @param array $ids
2456 *
2457 * @return array
2458 */
2459 public static function getNameFromID($ids) {
2460 if (is_array($ids)) {
2461 $ids = implode(',', $ids);
2462 }
2463 $sql = "
2464 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2465 FROM civicrm_custom_field f
2466 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2467 WHERE f.id IN ($ids)";
2468
2469 $dao = CRM_Core_DAO::executeQuery($sql);
2470 $result = [];
2471 while ($dao->fetch()) {
2472 $result[$dao->id] = [
2473 'field_name' => $dao->field_name,
2474 'field_label' => $dao->field_label,
2475 'group_name' => $dao->group_name,
2476 'group_title' => $dao->group_title,
2477 ];
2478 }
2479 return $result;
2480 }
2481
2482 /**
2483 * Validate custom data.
2484 *
2485 * @param array $params
2486 * Custom data submitted.
2487 * ie array( 'custom_1' => 'validate me' );
2488 *
2489 * @return array
2490 * validation errors.
2491 */
2492 public static function validateCustomData($params) {
2493 $errors = [];
2494 if (!is_array($params) || empty($params)) {
2495 return $errors;
2496 }
2497
2498 //pick up profile fields.
2499 $profileFields = [];
2500 $ufGroupId = $params['ufGroupId'] ?? NULL;
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
2523 $profileField = CRM_Utils_Array::value($key, $profileFields, []);
2524 $fieldTitle = $profileField['title'] ?? NULL;
2525 $isRequired = $profileField['is_required'] ?? NULL;
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)) {
2537 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
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
2588 /**
2589 * Is this field a multi record field.
2590 *
2591 * @param int $customId
2592 *
2593 * @return bool
2594 */
2595 public static function isMultiRecordField($customId) {
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
2605 WHERE cf.id = %1 AND cg.is_multiple = 1";
2606 $params[1] = [$customId, 'Integer'];
2607 $dao = CRM_Core_DAO::executeQuery($sql, $params);
2608 if ($dao->fetch()) {
2609 if ($dao->cgId) {
2610 $isMultipleWithGid = $dao->cgId;
2611 }
2612 }
2613 }
2614
2615 return $isMultipleWithGid;
2616 }
2617
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
2643 /**
2644 * Does this field store a serialized string?
2645 *
2646 * @param CRM_Core_DAO_CustomField|array $field
2647 *
2648 * @return bool
2649 */
2650 public static function isSerialized($field) {
2651 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2652 $html_type = is_object($field) ? $field->html_type : $field['html_type'];
2653 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2654 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2655 if ($html_type === 'CheckBox' || strpos($html_type, 'Multi') !== FALSE) {
2656 return TRUE;
2657 }
2658 // Otherwise this is the new standard as of 5.27
2659 return is_object($field) ? !empty($field->serialize) : !empty($field['serialize']);
2660 }
2661
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');
2669 return in_array($entity, ['Individual', 'Household', 'Organization']) ? 'Contact' : $entity;
2670 }
2671
2672 /**
2673 * Set pseudoconstant properties for field metadata.
2674 *
2675 * @param array $field
2676 * @param string|null $optionGroupName
2677 */
2678 private static function getOptionsForField(&$field, $optionGroupName) {
2679 if ($optionGroupName) {
2680 $field['pseudoconstant'] = [
2681 'optionGroupName' => $optionGroupName,
2682 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2683 ];
2684 }
2685 elseif ($field['data_type'] == 'Boolean') {
2686 $field['pseudoconstant'] = [
2687 'callback' => 'CRM_Core_SelectValues::boolean',
2688 ];
2689 }
2690 elseif ($field['data_type'] == 'Country') {
2691 $field['pseudoconstant'] = [
2692 'table' => 'civicrm_country',
2693 'keyColumn' => 'id',
2694 'labelColumn' => 'name',
2695 'nameColumn' => 'iso_code',
2696 ];
2697 }
2698 elseif ($field['data_type'] == 'StateProvince') {
2699 $field['pseudoconstant'] = [
2700 'table' => 'civicrm_state_province',
2701 'keyColumn' => 'id',
2702 'labelColumn' => 'name',
2703 ];
2704 }
2705 }
2706
2707 /**
2708 * @param CRM_Core_DAO_CustomField $field
2709 * @param 'add|modify|delete' $operation
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
2732 // For adding/dropping FK constraints
2733 $params['fkName'] = CRM_Core_BAO_SchemaHandler::getIndexName($tableName, $field->column_name);
2734
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
2743 if (!$field->serialize) {
2744 $params['fk_table_name'] = $fkFields[$field->data_type];
2745 $params['fk_field_name'] = 'id';
2746 $params['fk_attributes'] = 'ON DELETE SET NULL';
2747 }
2748 }
2749 if ($field->serialize) {
2750 $params['type'] = 'varchar(255)';
2751 }
2752 if (isset($field->default_value)) {
2753 $params['default'] = "'{$field->default_value}'";
2754 }
2755 return $params;
2756 }
2757
2758 }