[REF] - Deprecate & delegate BAO::retrieve
[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 * Deprecated in favor of writeRecords & APIv4
79 *
80 * @deprecated
81 * @param array $params
82 * @return CRM_Core_DAO_CustomField
83 */
84 public static function create($params) {
85 $changeSerialize = self::getChangeSerialize($params);
86 $customField = self::createCustomFieldRecord($params);
87 // When deserializing a field, the update needs to run before the schema change
88 if ($changeSerialize === 0) {
89 CRM_Core_DAO::singleValueQuery(self::getAlterSerializeSQL($customField));
90 }
91 $op = empty($params['id']) ? 'add' : 'modify';
92 self::createField($customField, $op);
93 // When serializing a field, the update needs to run after the schema change
94 if ($changeSerialize === 1) {
95 CRM_Core_DAO::singleValueQuery(self::getAlterSerializeSQL($customField));
96 }
97
98 CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField);
99
100 CRM_Utils_System::flushCache();
101 // Flush caches is not aggressive about clearing the specific cache we know we want to clear
102 // so do it manually. Ideally we wouldn't need to clear others...
103 Civi::cache('metadata')->clear();
104
105 return $customField;
106 }
107
108 /**
109 * Save multiple fields, now deprecated in favor of self::writeRecords.
110 * https://lab.civicrm.org/dev/core/issues/1093
111 * @deprecated
112 *
113 * @param array $bulkParams
114 * Array of arrays as would be passed into create
115 * @param array $defaults
116 * Default parameters to be be merged into each of the params.
117 *
118 * @throws \CiviCRM_API3_Exception
119 */
120 public static function bulkSave($bulkParams, $defaults = []) {
121 CRM_Core_Error::deprecatedFunctionWarning(__CLASS__ . '::writeRecords');
122 foreach ($bulkParams as $index => $fieldParams) {
123 $bulkParams[$index] = array_merge($defaults, $fieldParams);
124 }
125 self::writeRecords($bulkParams);
126 }
127
128 /**
129 * Create/update several fields at once in a mysql efficient way.
130 *
131 * @param array $records
132 * @return CRM_Core_DAO_CustomField[]
133 * @throws CRM_Core_Exception
134 */
135 public static function writeRecords(array $records): array {
136 $addedColumns = $sql = $customFields = $pre = $post = [];
137 foreach ($records as $index => $params) {
138 CRM_Utils_Hook::pre(empty($params['id']) ? 'create' : 'edit', 'CustomField', $params['id'] ?? NULL, $params);
139
140 $changeSerialize = self::getChangeSerialize($params);
141 $customField = self::createCustomFieldRecord($params);
142 // Serialize/deserialize sql must run after/before the table is altered
143 if ($changeSerialize === 0) {
144 $pre[] = self::getAlterSerializeSQL($customField);
145 }
146 if ($changeSerialize === 1) {
147 $post[] = self::getAlterSerializeSQL($customField);
148 }
149 $fieldSQL = self::getAlterFieldSQL($customField, empty($params['id']) ? 'add' : 'modify');
150
151 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customField->custom_group_id, 'table_name');
152 $sql[$tableName][] = $fieldSQL;
153 $addedColumns[$tableName][] = $customField->column_name;
154 $customFields[$index] = $customField;
155 }
156
157 foreach ($pre as $query) {
158 CRM_Core_DAO::executeQuery($query);
159 }
160
161 foreach ($sql as $tableName => $statements) {
162 // CRM-7007: do not i18n-rewrite this query
163 CRM_Core_DAO::executeQuery("ALTER TABLE $tableName " . implode(', ', $statements), [], TRUE, NULL, FALSE, FALSE);
164
165 if (CRM_Core_Config::singleton()->logging) {
166 $logging = new CRM_Logging_Schema();
167 $logging->fixSchemaDifferencesFor($tableName, ['ADD' => $addedColumns[$tableName]]);
168 }
169
170 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
171 }
172
173 foreach ($post as $query) {
174 CRM_Core_DAO::executeQuery($query);
175 }
176
177 CRM_Utils_System::flushCache();
178 Civi::cache('metadata')->clear();
179
180 foreach ($customFields as $index => $customField) {
181 CRM_Utils_Hook::post(empty($records[$index]['id']) ? 'create' : 'edit', 'CustomField', $customField->id, $customField);
182 }
183 return $customFields;
184 }
185
186 /**
187 * Retrieve DB object and copy to defaults array.
188 *
189 * @param array $params
190 * Array of criteria values.
191 * @param array $defaults
192 * Array to be populated with found values.
193 *
194 * @return self|null
195 * The DAO object, if found.
196 *
197 * @deprecated
198 */
199 public static function retrieve($params, &$defaults) {
200 return self::commonRetrieve(self::class, $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 = array_merge(['Contact'], CRM_Contact_BAO_ContactType::basicTypes());
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, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) {
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 // Issue #2939 - guard against passing empty values to CRM_Core_DAO::getFieldValue(), which would throw an exception
1100 if (empty($value)) {
1101 $display = '';
1102 }
1103 else {
1104 $displayNames = [];
1105 foreach ((array) $value as $contactId) {
1106 $displayNames[] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'display_name');
1107 }
1108 $display = implode(', ', $displayNames);
1109 }
1110 }
1111 elseif ($field['data_type'] == 'ContactReference') {
1112 $display = $value;
1113 }
1114 elseif (is_array($value)) {
1115 $v = [];
1116 foreach ($value as $key => $val) {
1117 $v[] = $field['options'][$val] ?? NULL;
1118 }
1119 $display = implode(', ', $v);
1120 }
1121 else {
1122 $display = CRM_Utils_Array::value($value, $field['options'], '');
1123 // For float type (see Number and Money) $value would be decimal like
1124 // 1.00 (because it is stored in db as decimal), while options array
1125 // key would be integer like 1. In this case expression on line above
1126 // would return empty string (default value), despite the fact that
1127 // matching option exists in the array.
1128 // In such cases we could just get intval($value) and fetch matching
1129 // option again, but this would not work if key is float like 5.6.
1130 // So we need to truncate trailing zeros to make it work as expected.
1131 if ($display === '' && strpos($value, '.') !== FALSE) {
1132 // Use round() to truncate trailing zeros, e.g:
1133 // 10.00 -> 10, 10.60 -> 10.6, 10.69 -> 10.69.
1134 $value = (string) round($value, 5);
1135 $display = $field['options'][$value] ?? '';
1136 }
1137 }
1138 break;
1139
1140 case 'Select Date':
1141 $customFormat = NULL;
1142
1143 // FIXME: Are there any legitimate reasons why $value would be an array?
1144 // Or should we throw an exception here if it is?
1145 $value = is_array($value) ? CRM_Utils_Array::first($value) : $value;
1146
1147 $actualPHPFormats = CRM_Utils_Date::datePluginToPHPFormats();
1148 $format = $field['date_format'] ?? NULL;
1149
1150 if ($format) {
1151 if (array_key_exists($format, $actualPHPFormats)) {
1152 $customTimeFormat = (array) $actualPHPFormats[$format];
1153 switch (CRM_Utils_Array::value('time_format', $field)) {
1154 case 1:
1155 $customTimeFormat[] = 'g:iA';
1156 break;
1157
1158 case 2:
1159 $customTimeFormat[] = 'G:i';
1160 break;
1161
1162 default:
1163 //If time is not selected remove time from value.
1164 $value = $value ? date('Y-m-d', strtotime($value)) : '';
1165 }
1166 $customFormat = implode(" ", $customTimeFormat);
1167 }
1168 }
1169 $display = CRM_Utils_Date::processDate($value, NULL, FALSE, $customFormat);
1170 break;
1171
1172 case 'File':
1173 // In the context of displaying a profile, show file/image
1174 if ($value) {
1175 if ($entityId) {
1176 if (CRM_Utils_Rule::positiveInteger($value)) {
1177 $fileId = $value;
1178 }
1179 else {
1180 $fileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File', $value, 'id', 'uri');
1181 }
1182 $url = self::getFileURL($entityId, $field['id'], $fileId);
1183 if ($url) {
1184 $display = $url['file_url'];
1185 }
1186 }
1187 else {
1188 // In other contexts show a paperclip icon
1189 if (CRM_Utils_Rule::integer($value)) {
1190 $icons = CRM_Core_BAO_File::paperIconAttachment('*', $value);
1191
1192 $file_description = '';
1193 try {
1194 $file_description = civicrm_api3('File', 'getvalue', ['return' => "description", 'id' => $value]);
1195 }
1196 catch (CiviCRM_API3_Exception $dontcare) {
1197 // don't care
1198 }
1199
1200 $display = "{$icons[$value]}{$file_description}";
1201 }
1202 else {
1203 //CRM-18396, if filename is passed instead
1204 $display = $value;
1205 }
1206 }
1207 }
1208 break;
1209
1210 case 'Link':
1211 $display = $display ? "<a href=\"$display\" target=\"_blank\">$display</a>" : $display;
1212 break;
1213
1214 case 'TextArea':
1215 $display = nl2br($display);
1216 break;
1217
1218 case 'Text':
1219 if ($field['data_type'] == 'Money' && isset($value)) {
1220 // $value can also be an array(while using IN operator from search builder or api).
1221 foreach ((array) $value as $val) {
1222 $disp[] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($val);
1223 }
1224 $display = implode(', ', $disp);
1225 }
1226 elseif ($field['data_type'] == 'Float' && isset($value)) {
1227 // $value can also be an array(while using IN operator from search builder or api).
1228 foreach ((array) $value as $val) {
1229 $disp[] = CRM_Utils_Number::formatLocaleNumeric($val);
1230 }
1231 $display = implode(', ', $disp);
1232 }
1233 break;
1234 }
1235 return $display;
1236 }
1237
1238 /**
1239 * Set default values for custom data used in profile.
1240 *
1241 * @param int $customFieldId
1242 * Custom field id.
1243 * @param string $elementName
1244 * Custom field name.
1245 * @param array $defaults
1246 * Associated array of fields.
1247 * @param int $contactId
1248 * Contact id.
1249 * @param int $mode
1250 * Profile mode.
1251 * @param mixed $value
1252 * If passed - dont fetch value from db,.
1253 * just format the given value
1254 */
1255 public static function setProfileDefaults(
1256 $customFieldId,
1257 $elementName,
1258 &$defaults,
1259 $contactId = NULL,
1260 $mode = NULL,
1261 $value = NULL
1262 ) {
1263 //get the type of custom field
1264 $customField = new CRM_Core_BAO_CustomField();
1265 $customField->id = $customFieldId;
1266 $customField->find(TRUE);
1267
1268 if (!$contactId) {
1269 if ($mode == CRM_Profile_Form::MODE_CREATE) {
1270 $value = $customField->default_value;
1271 }
1272 }
1273 else {
1274 if (!isset($value)) {
1275 $info = self::getTableColumnGroup($customFieldId);
1276 $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
1277 $result = CRM_Core_DAO::executeQuery($query);
1278 if ($result->fetch()) {
1279 $value = $result->value;
1280 }
1281 }
1282
1283 if ($customField->data_type == 'Country') {
1284 if (!$value) {
1285 $config = CRM_Core_Config::singleton();
1286 if ($config->defaultContactCountry) {
1287 $value = CRM_Core_BAO_Country::defaultContactCountry();
1288 }
1289 }
1290 }
1291 }
1292
1293 //set defaults if mode is registration
1294 if (!trim($value) &&
1295 ($value !== 0) &&
1296 (!in_array($mode, [CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH]))
1297 ) {
1298 $value = $customField->default_value;
1299 }
1300
1301 if ($customField->data_type == 'Money' && isset($value)) {
1302 $value = number_format($value, 2);
1303 }
1304 if (self::isSerialized($customField) && $value) {
1305 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE);
1306 $defaults[$elementName] = [];
1307 $checkedValue = CRM_Utils_Array::explodePadded($value);
1308 foreach ($customOption as $val) {
1309 if (in_array($val['value'], $checkedValue)) {
1310 if ($customField->html_type == 'CheckBox') {
1311 $defaults[$elementName][$val['value']] = 1;
1312 }
1313 else {
1314 $defaults[$elementName][$val['value']] = $val['value'];
1315 }
1316 }
1317 }
1318 }
1319 else {
1320 $defaults[$elementName] = $value;
1321 }
1322 }
1323
1324 /**
1325 * Get file url.
1326 *
1327 * @param int $contactID
1328 * @param int $cfID
1329 * @param int $fileID
1330 * @param bool $absolute
1331 *
1332 * @param string $multiRecordWhereClause
1333 *
1334 * @return array
1335 */
1336 public static function getFileURL($contactID, $cfID, $fileID = NULL, $absolute = FALSE, $multiRecordWhereClause = NULL) {
1337 if ($contactID) {
1338 if (!$fileID) {
1339 $params = ['id' => $cfID];
1340 $defaults = [];
1341 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
1342 $columnName = $defaults['column_name'];
1343
1344 //table name of custom data
1345 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
1346 $defaults['custom_group_id'],
1347 'table_name', 'id'
1348 );
1349
1350 //query to fetch id from civicrm_file
1351 if ($multiRecordWhereClause) {
1352 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID} AND {$multiRecordWhereClause}";
1353 }
1354 else {
1355 $query = "SELECT {$columnName} FROM {$tableName} where entity_id = {$contactID}";
1356 }
1357 $fileID = CRM_Core_DAO::singleValueQuery($query);
1358 }
1359
1360 $result = [];
1361 if ($fileID) {
1362 $fileType = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1363 $fileID,
1364 'mime_type',
1365 'id'
1366 );
1367 $result['file_id'] = $fileID;
1368
1369 if ($fileType == 'image/jpeg' ||
1370 $fileType == 'image/pjpeg' ||
1371 $fileType == 'image/gif' ||
1372 $fileType == 'image/x-png' ||
1373 $fileType == 'image/png'
1374 ) {
1375 $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile',
1376 $fileID,
1377 'entity_id',
1378 'file_id'
1379 );
1380 list($path) = CRM_Core_BAO_File::path($fileID, $entityId);
1381 $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileID);
1382 $url = CRM_Utils_System::url('civicrm/file',
1383 "reset=1&id=$fileID&eid=$entityId&fcs=$fileHash",
1384 $absolute, NULL, TRUE, TRUE
1385 );
1386 $result['file_url'] = CRM_Utils_File::getFileURL($path, $fileType, $url);
1387 }
1388 // for non image files
1389 else {
1390 $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
1391 $fileID,
1392 'uri'
1393 );
1394 $fileHash = CRM_Core_BAO_File::generateFileHash($contactID, $fileID);
1395 $url = CRM_Utils_System::url('civicrm/file',
1396 "reset=1&id=$fileID&eid=$contactID&fcs=$fileHash",
1397 $absolute, NULL, TRUE, TRUE
1398 );
1399 $result['file_url'] = CRM_Utils_File::getFileURL($uri, $fileType, $url);
1400 }
1401 }
1402 return $result;
1403 }
1404 }
1405
1406 /**
1407 * Format custom fields before inserting.
1408 *
1409 * @param int $customFieldId
1410 * Custom field id.
1411 * @param array $customFormatted
1412 * Formatted array.
1413 * @param mixed $value
1414 * Value of custom field.
1415 * @param string $customFieldExtend
1416 * Custom field extends.
1417 * @param int $customValueId
1418 * Custom option value id.
1419 * @param int $entityId
1420 * Entity id (contribution, membership...).
1421 * @param bool $inline
1422 * Consider inline custom groups only.
1423 * @param bool $checkPermission
1424 * If false, do not include permissioning clause.
1425 * @param bool $includeViewOnly
1426 * If true, fields marked 'View Only' are included. Required for APIv3.
1427 *
1428 * @return array|NULL
1429 * formatted custom field array
1430 */
1431 public static function formatCustomField(
1432 $customFieldId, &$customFormatted, $value,
1433 $customFieldExtend, $customValueId = NULL,
1434 $entityId = NULL,
1435 $inline = FALSE,
1436 $checkPermission = TRUE,
1437 $includeViewOnly = FALSE
1438 ) {
1439 //get the custom fields for the entity
1440 //subtype and basic type
1441 $customDataSubType = NULL;
1442 if ($customFieldExtend) {
1443 // This is the case when getFieldsForImport() requires fields
1444 // of subtype and its parent.CRM-5143
1445 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type
1446 $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend);
1447 if (!empty($customDataSubType) && is_array($customDataSubType)) {
1448 $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType);
1449 if (is_array($customFieldExtend)) {
1450 $customFieldExtend = array_unique(array_values($customFieldExtend));
1451 }
1452 }
1453 }
1454
1455 $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend,
1456 FALSE,
1457 $inline,
1458 $customDataSubType,
1459 NULL,
1460 FALSE,
1461 FALSE,
1462 $checkPermission ? CRM_Core_Permission::EDIT : FALSE
1463 );
1464
1465 if (!array_key_exists($customFieldId, $customFields)) {
1466 return NULL;
1467 }
1468
1469 // return if field is a 'code' field
1470 if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) {
1471 return NULL;
1472 }
1473
1474 list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId);
1475
1476 if (!$customValueId &&
1477 // we always create new entites for is_multiple unless specified
1478 !$customFields[$customFieldId]['is_multiple'] &&
1479 $entityId
1480 ) {
1481 $query = "
1482 SELECT id
1483 FROM $tableName
1484 WHERE entity_id={$entityId}";
1485
1486 $customValueId = CRM_Core_DAO::singleValueQuery($query);
1487 }
1488
1489 //fix checkbox, now check box always submits values
1490 if ($customFields[$customFieldId]['html_type'] == 'CheckBox') {
1491 if ($value) {
1492 // Note that only during merge this is not an array, and you can directly use value
1493 if (is_array($value)) {
1494 $selectedValues = [];
1495 foreach ($value as $selId => $val) {
1496 if ($val) {
1497 $selectedValues[] = $selId;
1498 }
1499 }
1500 if (!empty($selectedValues)) {
1501 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
1502 $selectedValues
1503 ) . CRM_Core_DAO::VALUE_SEPARATOR;
1504 }
1505 else {
1506 $value = '';
1507 }
1508 }
1509 }
1510 }
1511 elseif (self::isSerialized($customFields[$customFieldId])) {
1512 // Select2 v3 returns a comma-separated string.
1513 if ($customFields[$customFieldId]['html_type'] == 'Autocomplete-Select' && is_string($value)) {
1514 $value = explode(',', $value);
1515 }
1516
1517 $value = $value ? CRM_Utils_Array::implodePadded($value) : '';
1518 }
1519
1520 if (self::isSerialized($customFields[$customFieldId]) &&
1521 $customFields[$customFieldId]['data_type'] == 'String' &&
1522 !empty($customFields[$customFieldId]['text_length']) &&
1523 !empty($value)
1524 ) {
1525 // lets make sure that value is less than the length, else we'll
1526 // be losing some data, CRM-7481
1527 if (strlen($value) >= $customFields[$customFieldId]['text_length']) {
1528 // need to do a few things here
1529
1530 // 1. lets find a new length
1531 $newLength = $customFields[$customFieldId]['text_length'];
1532 $minLength = strlen($value);
1533 while ($newLength < $minLength) {
1534 $newLength = $newLength * 2;
1535 }
1536
1537 // set the custom field meta data to have a length larger than value
1538 // alter the custom value table column to match this length
1539 CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength);
1540 }
1541 }
1542
1543 $date = NULL;
1544 if ($customFields[$customFieldId]['data_type'] == 'Date') {
1545 if (!CRM_Utils_System::isNull($value)) {
1546 $format = $customFields[$customFieldId]['date_format'];
1547 $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format);
1548 }
1549 $value = $date;
1550 }
1551
1552 if ($customFields[$customFieldId]['data_type'] == 'Float' ||
1553 $customFields[$customFieldId]['data_type'] == 'Money'
1554 ) {
1555 if (!$value) {
1556 $value = 0;
1557 }
1558
1559 if ($customFields[$customFieldId]['data_type'] == 'Money') {
1560 $value = CRM_Utils_Rule::cleanMoney($value);
1561 }
1562 }
1563
1564 if (($customFields[$customFieldId]['data_type'] == 'StateProvince' ||
1565 $customFields[$customFieldId]['data_type'] == 'Country'
1566 ) &&
1567 empty($value)
1568 ) {
1569 // CRM-3415
1570 $value = 0;
1571 }
1572
1573 $fileID = NULL;
1574
1575 if ($customFields[$customFieldId]['data_type'] == 'File') {
1576 if (empty($value)) {
1577 return;
1578 }
1579
1580 $config = CRM_Core_Config::singleton();
1581
1582 // If we are already passing the file id as a value then retrieve and set the file data
1583 if (CRM_Utils_Rule::integer($value)) {
1584 $fileDAO = new CRM_Core_DAO_File();
1585 $fileDAO->id = $value;
1586 $fileDAO->find(TRUE);
1587 if ($fileDAO->N) {
1588 $fileID = $value;
1589 $fName = $fileDAO->uri;
1590 $mimeType = $fileDAO->mime_type;
1591 }
1592 }
1593 elseif (empty($value['name'])) {
1594 // Happens when calling the API to update custom fields values, but the filename
1595 // is empty, for an existing entity (in a specific case, was from a d7-webform
1596 // that was updating a relationship with a File customfield, so $value['id'] was
1597 // not empty, but the filename was empty.
1598 return;
1599 }
1600 else {
1601 $fName = $value['name'];
1602 $mimeType = $value['type'];
1603 }
1604
1605 $filename = pathinfo($fName, PATHINFO_BASENAME);
1606
1607 // rename this file to go into the secure directory only if
1608 // user has uploaded new file not existing verfied on the basis of $fileID
1609 if (empty($fileID) && !rename($fName, $config->customFileUploadDir . $filename)) {
1610 CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory'));
1611 }
1612
1613 if ($customValueId && empty($fileID)) {
1614 $query = "
1615 SELECT $columnName
1616 FROM $tableName
1617 WHERE id = %1";
1618 $params = [1 => [$customValueId, 'Integer']];
1619 $fileID = CRM_Core_DAO::singleValueQuery($query, $params);
1620 }
1621
1622 $fileDAO = new CRM_Core_DAO_File();
1623
1624 if ($fileID) {
1625 $fileDAO->id = $fileID;
1626 }
1627
1628 $fileDAO->uri = $filename;
1629 $fileDAO->mime_type = $mimeType;
1630 $fileDAO->upload_date = date('YmdHis');
1631 $fileDAO->save();
1632 $fileID = $fileDAO->id;
1633 $value = $filename;
1634 }
1635
1636 if (!is_array($customFormatted)) {
1637 $customFormatted = [];
1638 }
1639
1640 if (!array_key_exists($customFieldId, $customFormatted)) {
1641 $customFormatted[$customFieldId] = [];
1642 }
1643
1644 $index = -1;
1645 if ($customValueId) {
1646 $index = $customValueId;
1647 }
1648
1649 if (!array_key_exists($index, $customFormatted[$customFieldId])) {
1650 $customFormatted[$customFieldId][$index] = [];
1651 }
1652 $customFormatted[$customFieldId][$index] = [
1653 'id' => $customValueId > 0 ? $customValueId : NULL,
1654 'value' => $value,
1655 'type' => $customFields[$customFieldId]['data_type'],
1656 'custom_field_id' => $customFieldId,
1657 'custom_group_id' => $groupID,
1658 'table_name' => $tableName,
1659 'column_name' => $columnName,
1660 'file_id' => $fileID,
1661 // is_multiple refers to the custom group, serialize refers to the field.
1662 'is_multiple' => $customFields[$customFieldId]['is_multiple'],
1663 'serialize' => $customFields[$customFieldId]['serialize'],
1664 ];
1665
1666 //we need to sort so that custom fields are created in the order of entry
1667 krsort($customFormatted[$customFieldId]);
1668 return $customFormatted;
1669 }
1670
1671 /**
1672 * Get default custom table schema.
1673 *
1674 * @param array $params
1675 *
1676 * @return array
1677 */
1678 public static function defaultCustomTableSchema($params) {
1679 // add the id and extends_id
1680 $collation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
1681 $characterSet = 'utf8';
1682 if (stripos($collation, 'utf8mb4') !== FALSE) {
1683 $characterSet = 'utf8mb4';
1684 }
1685 $table = [
1686 'name' => $params['name'],
1687 'is_multiple' => $params['is_multiple'],
1688 'attributes' => "ENGINE=InnoDB DEFAULT CHARACTER SET {$characterSet} COLLATE {$collation}",
1689 'fields' => [
1690 [
1691 'name' => 'id',
1692 'type' => 'int unsigned',
1693 'primary' => TRUE,
1694 'required' => TRUE,
1695 'attributes' => 'AUTO_INCREMENT',
1696 'comment' => 'Default MySQL primary key',
1697 ],
1698 [
1699 'name' => 'entity_id',
1700 'type' => 'int unsigned',
1701 'required' => TRUE,
1702 'comment' => 'Table that this extends',
1703 'fk_table_name' => $params['extends_name'],
1704 'fk_field_name' => 'id',
1705 'fk_attributes' => 'ON DELETE CASCADE',
1706 ],
1707 ],
1708 ];
1709
1710 // If on MySQL 5.6 include ROW_FORMAT=DYNAMIC to fix unit tests
1711 $databaseVersion = CRM_Utils_SQL::getDatabaseVersion();
1712 if (version_compare($databaseVersion, '5.7', '<') && version_compare($databaseVersion, '5.6', '>=')) {
1713 $table['attributes'] = $table['attributes'] . ' ROW_FORMAT=DYNAMIC';
1714 }
1715
1716 if (!$params['is_multiple']) {
1717 $table['indexes'] = [
1718 [
1719 'unique' => TRUE,
1720 'field_name_1' => 'entity_id',
1721 ],
1722 ];
1723 }
1724 return $table;
1725 }
1726
1727 /**
1728 * Create custom field.
1729 *
1730 * @param CRM_Core_DAO_CustomField $field
1731 * @param string $operation
1732 */
1733 public static function createField($field, $operation) {
1734 $sql = str_repeat(' ', 8);
1735 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1736 $sql .= "ALTER TABLE " . $tableName;
1737 $sql .= self::getAlterFieldSQL($field, $operation);
1738
1739 // CRM-7007: do not i18n-rewrite this query
1740 CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, FALSE, FALSE);
1741
1742 $config = CRM_Core_Config::singleton();
1743 if ($config->logging) {
1744 // CRM-16717 not sure why this was originally limited to add.
1745 // For example custom tables can have field length changes - which need to flow through to logging.
1746 // Are there any modifies we DON'T was to call this function for (& shouldn't it be clever enough to cope?)
1747 if ($operation === 'add' || $operation === 'modify') {
1748 $logging = new CRM_Logging_Schema();
1749 $logging->fixSchemaDifferencesFor($tableName, [trim(strtoupper($operation)) => [$field->column_name]]);
1750 }
1751 }
1752
1753 Civi::service('sql_triggers')->rebuild($tableName, TRUE);
1754 }
1755
1756 /**
1757 * @param CRM_Core_DAO_CustomField $field
1758 * @param string $operation add|modify|delete
1759 *
1760 * @return bool
1761 */
1762 public static function getAlterFieldSQL($field, $operation) {
1763 $params = self::prepareCreateParams($field, $operation);
1764 // Let's suppress the required flag, since that can cause an sql issue... for unknown reasons since we are calling
1765 // a function only used by Custom Field creation...
1766 $params['required'] = FALSE;
1767 return CRM_Core_BAO_SchemaHandler::getFieldAlterSQL($params);
1768 }
1769
1770 /**
1771 * Get query to reformat existing values for a field when changing its serialize attribute
1772 *
1773 * @param CRM_Core_DAO_CustomField $field
1774 * @return string
1775 */
1776 private static function getAlterSerializeSQL($field) {
1777 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $field->custom_group_id, 'table_name');
1778 $col = $field->column_name;
1779 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
1780 if ($field->serialize) {
1781 return "UPDATE `$table` SET `$col` = CONCAT('$sp', `$col`, '$sp') WHERE `$col` IS NOT NULL AND `$col` NOT LIKE '$sp%' AND `$col` != ''";
1782 }
1783 else {
1784 return "UPDATE `$table` SET `$col` = SUBSTRING_INDEX(SUBSTRING(`$col`, 2), '$sp', 1) WHERE `$col` LIKE '$sp%'";
1785 }
1786 }
1787
1788 /**
1789 * Determine whether it would be safe to move a field.
1790 *
1791 * @param int $fieldID
1792 * FK to civicrm_custom_field.
1793 * @param int $newGroupID
1794 * FK to civicrm_custom_group.
1795 *
1796 * @return array
1797 * array(string) or TRUE
1798 */
1799 public static function _moveFieldValidate($fieldID, $newGroupID) {
1800 $errors = [];
1801
1802 $field = new CRM_Core_DAO_CustomField();
1803 $field->id = $fieldID;
1804 if (!$field->find(TRUE)) {
1805 $errors['fieldID'] = ts('Invalid ID for custom field');
1806 return $errors;
1807 }
1808
1809 $oldGroup = new CRM_Core_DAO_CustomGroup();
1810 $oldGroup->id = $field->custom_group_id;
1811 if (!$oldGroup->find(TRUE)) {
1812 $errors['fieldID'] = ts('Invalid ID for old custom group');
1813 return $errors;
1814 }
1815
1816 $newGroup = new CRM_Core_DAO_CustomGroup();
1817 $newGroup->id = $newGroupID;
1818 if (!$newGroup->find(TRUE)) {
1819 $errors['newGroupID'] = ts('Invalid ID for new custom group');
1820 return $errors;
1821 }
1822
1823 $query = "
1824 SELECT b.id
1825 FROM civicrm_custom_field a
1826 INNER JOIN civicrm_custom_field b
1827 WHERE a.id = %1
1828 AND a.label = b.label
1829 AND b.custom_group_id = %2
1830 ";
1831 $params = [
1832 1 => [$field->id, 'Integer'],
1833 2 => [$newGroup->id, 'Integer'],
1834 ];
1835 $count = CRM_Core_DAO::singleValueQuery($query, $params);
1836 if ($count > 0) {
1837 $errors['newGroupID'] = ts('A field of the same label exists in the destination group');
1838 }
1839
1840 $tableName = $oldGroup->table_name;
1841 $columnName = $field->column_name;
1842
1843 $query = "
1844 SELECT count(*)
1845 FROM $tableName
1846 WHERE $columnName is not null
1847 ";
1848 $count = CRM_Core_DAO::singleValueQuery($query);
1849 if ($count > 0) {
1850 $query = "
1851 SELECT extends
1852 FROM civicrm_custom_group
1853 WHERE id IN ( %1, %2 )
1854 ";
1855 $params = [
1856 1 => [$oldGroup->id, 'Integer'],
1857 2 => [$newGroup->id, 'Integer'],
1858 ];
1859
1860 $dao = CRM_Core_DAO::executeQuery($query, $params);
1861 $extends = [];
1862 while ($dao->fetch()) {
1863 $extends[] = $dao->extends;
1864 }
1865 if ($extends[0] != $extends[1]) {
1866 $errors['newGroupID'] = ts('The destination group extends a different entity type.');
1867 }
1868 }
1869
1870 return empty($errors) ? TRUE : $errors;
1871 }
1872
1873 /**
1874 * Move a custom data field from one group (table) to another.
1875 *
1876 * @param int $fieldID
1877 * FK to civicrm_custom_field.
1878 * @param int $newGroupID
1879 * FK to civicrm_custom_group.
1880 *
1881 * @throws CRM_Core_Exception
1882 */
1883 public static function moveField($fieldID, $newGroupID) {
1884 $validation = self::_moveFieldValidate($fieldID, $newGroupID);
1885 if (TRUE !== $validation) {
1886 throw new CRM_Core_Exception(implode(' ', $validation));
1887 }
1888 $field = new CRM_Core_DAO_CustomField();
1889 $field->id = $fieldID;
1890 $field->find(TRUE);
1891
1892 $newGroup = new CRM_Core_DAO_CustomGroup();
1893 $newGroup->id = $newGroupID;
1894 $newGroup->find(TRUE);
1895
1896 $oldGroup = new CRM_Core_DAO_CustomGroup();
1897 $oldGroup->id = $field->custom_group_id;
1898 $oldGroup->find(TRUE);
1899
1900 $add = clone$field;
1901 $add->custom_group_id = $newGroup->id;
1902 self::createField($add, 'add');
1903
1904 $sql = "INSERT INTO {$newGroup->table_name} (entity_id, `{$field->column_name}`)
1905 SELECT entity_id, `{$field->column_name}` FROM {$oldGroup->table_name}
1906 ON DUPLICATE KEY UPDATE `{$field->column_name}` = {$oldGroup->table_name}.`{$field->column_name}`
1907 ";
1908 CRM_Core_DAO::executeQuery($sql);
1909
1910 $del = clone$field;
1911 $del->custom_group_id = $oldGroup->id;
1912 self::createField($del, 'delete');
1913
1914 $add->save();
1915
1916 CRM_Utils_System::flushCache();
1917 }
1918
1919 /**
1920 * Create an option value for a custom field option group ID.
1921 *
1922 * @param array $params
1923 * @param string $value
1924 * @param \CRM_Core_DAO_OptionGroup $optionGroup
1925 * @param string $index
1926 * @param string $dataType
1927 */
1928 protected static function createOptionValue(&$params, $value, CRM_Core_DAO_OptionGroup $optionGroup, $index, $dataType) {
1929 if (strlen(trim($value))) {
1930 $optionValue = new CRM_Core_DAO_OptionValue();
1931 $optionValue->option_group_id = $optionGroup->id;
1932 $optionValue->label = $params['option_label'][$index];
1933 $optionValue->name = $params['option_name'][$index] ?? CRM_Utils_String::titleToVar($params['option_label'][$index]);
1934 switch ($dataType) {
1935 case 'Money':
1936 $optionValue->value = CRM_Utils_Rule::cleanMoney($value);
1937 break;
1938
1939 case 'Int':
1940 $optionValue->value = intval($value);
1941 break;
1942
1943 case 'Float':
1944 $optionValue->value = floatval($value);
1945 break;
1946
1947 default:
1948 $optionValue->value = trim($value);
1949 }
1950
1951 $optionValue->weight = $params['option_weight'][$index];
1952 $optionValue->is_active = $params['option_status'][$index] ?? FALSE;
1953 $optionValue->description = $params['option_description'][$index] ?? NULL;
1954 $optionValue->color = $params['option_color'][$index] ?? NULL;
1955 $optionValue->icon = $params['option_icon'][$index] ?? NULL;
1956 $optionValue->save();
1957 }
1958 }
1959
1960 /**
1961 * Prepare for the create operation.
1962 *
1963 * Munge params, create the option values if needed.
1964 *
1965 * This could be called by a single create or a batchCreate.
1966 *
1967 * @param array $params
1968 *
1969 * @return array
1970 */
1971 protected static function prepareCreate($params) {
1972 $op = empty($params['id']) ? 'create' : 'edit';
1973 CRM_Utils_Hook::pre($op, 'CustomField', CRM_Utils_Array::value('id', $params), $params);
1974 $params['is_append_field_id_to_column_name'] = !isset($params['column_name']);
1975 if ($op === 'create') {
1976 CRM_Core_DAO::setCreateDefaults($params, self::getDefaults());
1977 if (!isset($params['column_name'])) {
1978 // if add mode & column_name not present, calculate it.
1979 $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32));
1980 }
1981 if (!isset($params['name'])) {
1982 $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64);
1983 }
1984 }
1985 else {
1986 $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name');
1987 }
1988
1989 $htmlType = $params['html_type'] ?? NULL;
1990 $dataType = $params['data_type'] ?? NULL;
1991
1992 if ($htmlType === 'Select Date' && empty($params['date_format'])) {
1993 $params['date_format'] = Civi::settings()->get('dateInputFormat');
1994 }
1995
1996 // Checkboxes are always serialized in current schema
1997 if ($htmlType == 'CheckBox') {
1998 $params['serialize'] = CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND;
1999 }
2000
2001 if (!empty($params['serialize'])) {
2002 if (isset($params['default_checkbox_option'])) {
2003 $defaultArray = [];
2004 foreach (array_keys($params['default_checkbox_option']) as $k => $v) {
2005 if ($params['option_value'][$v]) {
2006 $defaultArray[] = $params['option_value'][$v];
2007 }
2008 }
2009
2010 if (!empty($defaultArray)) {
2011 // also add the separator before and after the value per new convention (CRM-1604)
2012 $params['default_value'] = CRM_Utils_Array::implodePadded($defaultArray);
2013 }
2014 }
2015 else {
2016 if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) {
2017 $params['default_value'] = $params['option_value'][$params['default_option']];
2018 }
2019 }
2020 }
2021
2022 // create any option group & values if required
2023 $allowedOptionTypes = ['String', 'Int', 'Float', 'Money'];
2024 if ($htmlType !== 'Text' && in_array($dataType, $allowedOptionTypes, TRUE)) {
2025 //CRM-16659: if option_value then create an option group for this custom field.
2026 // An option_type of 2 would be a 'message' from the form layer not to handle
2027 // the option_values key. If not set then it is not ignored.
2028 $optionsType = (int) ($params['option_type'] ?? 0);
2029 if (($optionsType !== 2 && empty($params['id']))
2030 && (empty($params['option_group_id']) && !empty($params['option_value'])
2031 )
2032 ) {
2033 // first create an option group for this custom group
2034 $optionGroup = new CRM_Core_DAO_OptionGroup();
2035 $optionGroup->name = "{$params['column_name']}_" . date('YmdHis');
2036 $optionGroup->title = $params['label'];
2037 $optionGroup->is_active = 1;
2038 // Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
2039 $optionGroup->is_reserved = 0;
2040 $optionGroup->data_type = $dataType;
2041 $optionGroup->save();
2042 $params['option_group_id'] = $optionGroup->id;
2043 if (!empty($params['option_value']) && is_array($params['option_value'])) {
2044 foreach ($params['option_value'] as $k => $v) {
2045 self::createOptionValue($params, $v, $optionGroup, $k, $dataType);
2046 }
2047 }
2048 }
2049 }
2050
2051 // Remove option group IDs from fields changed to Text html_type.
2052 if ($htmlType == 'Text') {
2053 $params['option_group_id'] = '';
2054 }
2055
2056 // check for orphan option groups
2057 if (!empty($params['option_group_id'])) {
2058 if (!empty($params['id'])) {
2059 self::fixOptionGroups($params['id'], $params['option_group_id']);
2060 }
2061
2062 // if we do not have a default value
2063 // retrieve it from one of the other custom fields which use this option group
2064 if (empty($params['default_value'])) {
2065 //don't insert only value separator as default value, CRM-4579
2066 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], !empty($params['serialize']));
2067
2068 if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) {
2069 $params['default_value'] = $defaultValue;
2070 }
2071 }
2072 }
2073
2074 // Set default textarea attributes
2075 if ($op == 'create' && !isset($params['attributes']) && $htmlType == 'TextArea') {
2076 $params['attributes'] = 'rows=4, cols=60';
2077 }
2078 return $params;
2079 }
2080
2081 /**
2082 * Create database entry for custom field and related option groups.
2083 *
2084 * @param array $params
2085 *
2086 * @return CRM_Core_DAO_CustomField
2087 */
2088 protected static function createCustomFieldRecord($params) {
2089 $transaction = new CRM_Core_Transaction();
2090 $params = self::prepareCreate($params);
2091
2092 $customField = new CRM_Core_DAO_CustomField();
2093 $customField->copyValues($params);
2094 $customField->save();
2095
2096 //create/drop the index when we toggle the is_searchable flag
2097 $op = empty($params['id']) ? 'add' : 'modify';
2098 if ($op !== 'modify') {
2099 if ($params['is_append_field_id_to_column_name']) {
2100 $params['column_name'] .= "_{$customField->id}";
2101 }
2102 $customField->column_name = $params['column_name'];
2103 $customField->save();
2104 }
2105
2106 // complete transaction - note that any table alterations include an implicit commit so this is largely meaningless.
2107 $transaction->commit();
2108
2109 // make sure all values are present in the object for further processing
2110 $customField->find(TRUE);
2111
2112 return $customField;
2113 }
2114
2115 /**
2116 * @param $params
2117 * @return int|null
2118 */
2119 protected static function getChangeSerialize($params) {
2120 if (isset($params['serialize']) && !empty($params['id'])) {
2121 if ($params['serialize'] != CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'serialize')) {
2122 return (int) $params['serialize'];
2123 }
2124 }
2125 return NULL;
2126 }
2127
2128 /**
2129 * Move custom data from one contact to another.
2130 *
2131 * This is currently the start of a refactoring. The theory is each
2132 * entity could have a 'move' function with a DAO default one to fall back on.
2133 *
2134 * At the moment this only does a small part of the process - ie deleting a file field that
2135 * is about to be overwritten. However, the goal is the whole process around the move for
2136 * custom data should be in here.
2137 *
2138 * This is currently called by the merge class but it makes sense that api could
2139 * expose move actions as moving (e.g) contributions feels like a common
2140 * ask that should be handled by the form layer.
2141 *
2142 * @param int $oldContactID
2143 * @param int $newContactID
2144 * @param int[] $fieldIDs
2145 * Optional list field ids to move.
2146 *
2147 * @throws \CiviCRM_API3_Exception
2148 * @throws \Exception
2149 */
2150 public function move($oldContactID, $newContactID, $fieldIDs) {
2151 if (empty($fieldIDs)) {
2152 return;
2153 }
2154 $fields = civicrm_api3('CustomField', 'get', ['id' => ['IN' => $fieldIDs], 'return' => ['custom_group_id.is_multiple', 'custom_group_id.table_name', 'column_name', 'data_type'], 'options' => ['limit' => 0]])['values'];
2155 $return = [];
2156 foreach ($fieldIDs as $fieldID) {
2157 $return[] = 'custom_' . $fieldID;
2158 }
2159 $oldContact = civicrm_api3('Contact', 'getsingle', ['id' => $oldContactID, 'return' => $return]);
2160 $newContact = civicrm_api3('Contact', 'getsingle', ['id' => $newContactID, 'return' => $return]);
2161
2162 // The moveAllBelongings function has functionality to move custom fields. It doesn't work very well...
2163 // @todo handle all fields here but more immediately Country since that is broken at the moment.
2164 $fieldTypesNotHandledInMergeAttempt = ['File'];
2165 foreach ($fields as $field) {
2166 $isMultiple = !empty($field['custom_group_id.is_multiple']);
2167 if ($field['data_type'] === 'File' && !$isMultiple) {
2168 if (!empty($oldContact['custom_' . $field['id']]) && !empty($newContact['custom_' . $field['id']])) {
2169 CRM_Core_BAO_File::deleteFileReferences($oldContact['custom_' . $field['id']], $oldContactID, $field['id']);
2170 }
2171 if (!empty($oldContact['custom_' . $field['id']])) {
2172 CRM_Core_DAO::executeQuery("
2173 UPDATE civicrm_entity_file
2174 SET entity_id = $newContactID
2175 WHERE file_id = {$oldContact['custom_' . $field['id']]}"
2176 );
2177 }
2178 }
2179 if (in_array($field['data_type'], $fieldTypesNotHandledInMergeAttempt) && !$isMultiple) {
2180 CRM_Core_DAO::executeQuery(
2181 "INSERT INTO {$field['custom_group_id.table_name']} (entity_id, `{$field['column_name']}`)
2182 VALUES ($newContactID, {$oldContact['custom_' . $field['id']]})
2183 ON DUPLICATE KEY UPDATE
2184 `{$field['column_name']}` = {$oldContact['custom_' . $field['id']]}
2185 ");
2186 }
2187 }
2188 }
2189
2190 /**
2191 * Get the database table name and column name for a custom field.
2192 *
2193 * @param int $fieldID
2194 * The fieldID of the custom field.
2195 * @param bool $force
2196 * Force the sql to be run again (primarily used for tests).
2197 *
2198 * @return array
2199 * fatal is fieldID does not exists, else array of tableName, columnName
2200 * @throws \CRM_Core_Exception
2201 */
2202 public static function getTableColumnGroup($fieldID, $force = FALSE) {
2203 $cacheKey = "CRM_Core_DAO_CustomField_CustomGroup_TableColumn_{$fieldID}";
2204 $cache = CRM_Utils_Cache::singleton();
2205 $fieldValues = $cache->get($cacheKey);
2206 if (empty($fieldValues) || $force) {
2207 $query = "
2208 SELECT cg.table_name, cf.column_name, cg.id
2209 FROM civicrm_custom_group cg,
2210 civicrm_custom_field cf
2211 WHERE cf.custom_group_id = cg.id
2212 AND cf.id = %1";
2213 $params = [1 => [$fieldID, 'Integer']];
2214 $dao = CRM_Core_DAO::executeQuery($query, $params);
2215
2216 if (!$dao->fetch()) {
2217 throw new CRM_Core_Exception("Cannot find table and column information for Custom Field " . $fieldID);
2218 }
2219 $fieldValues = [$dao->table_name, $dao->column_name, $dao->id];
2220 $cache->set($cacheKey, $fieldValues);
2221 }
2222 return $fieldValues;
2223 }
2224
2225 /**
2226 * Get custom option groups.
2227 *
2228 * @deprecated Use the API OptionGroup.get
2229 *
2230 * @param array $includeFieldIds
2231 * Ids of custom fields for which option groups must be included.
2232 *
2233 * Currently this is required in the cases where option groups are to be included
2234 * for inactive fields : CRM-5369
2235 *
2236 * @return mixed
2237 */
2238 public static function customOptionGroup($includeFieldIds = NULL) {
2239 static $customOptionGroup = NULL;
2240
2241 $cacheKey = (empty($includeFieldIds)) ? 'onlyActive' : 'force';
2242 if ($cacheKey == 'force') {
2243 $customOptionGroup[$cacheKey] = NULL;
2244 }
2245
2246 if (empty($customOptionGroup[$cacheKey])) {
2247 $whereClause = '( g.is_active = 1 AND f.is_active = 1 )';
2248
2249 //support for single as well as array format.
2250 if (!empty($includeFieldIds)) {
2251 if (is_array($includeFieldIds)) {
2252 $includeFieldIds = implode(',', $includeFieldIds);
2253 }
2254 $whereClause .= "OR f.id IN ( $includeFieldIds )";
2255 }
2256
2257 $query = "
2258 SELECT g.id, g.title
2259 FROM civicrm_option_group g
2260 INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id )
2261 WHERE {$whereClause}";
2262
2263 $dao = CRM_Core_DAO::executeQuery($query);
2264 while ($dao->fetch()) {
2265 $customOptionGroup[$cacheKey][$dao->id] = $dao->title;
2266 }
2267 }
2268
2269 return $customOptionGroup[$cacheKey];
2270 }
2271
2272 /**
2273 * Get defaults for new entity.
2274 *
2275 * @return array
2276 */
2277 public static function getDefaults() {
2278 return [
2279 'is_required' => FALSE,
2280 'is_searchable' => FALSE,
2281 'in_selector' => FALSE,
2282 'is_search_range' => FALSE,
2283 //CRM-15792 - Custom field gets disabled if is_active not set
2284 // this would ideally be a mysql default.
2285 'is_active' => TRUE,
2286 'is_view' => FALSE,
2287 ];
2288 }
2289
2290 /**
2291 * Fix orphan groups.
2292 *
2293 * @param int $customFieldId
2294 * Custom field id.
2295 * @param int $optionGroupId
2296 * Option group id.
2297 */
2298 public static function fixOptionGroups($customFieldId, $optionGroupId) {
2299 // check if option group belongs to any custom Field else delete
2300 // get the current option group
2301 $currentOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
2302 $customFieldId,
2303 'option_group_id'
2304 );
2305 // get the updated option group
2306 // if both are same return
2307 if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) {
2308 return;
2309 }
2310
2311 // check if option group is related to any other field
2312 self::checkOptionGroup($currentOptionGroupId);
2313 }
2314
2315 /**
2316 * Check if option group is related to more than one custom field.
2317 *
2318 * @param int $optionGroupId
2319 * Option group id.
2320 */
2321 public static function checkOptionGroup($optionGroupId) {
2322 $query = "
2323 SELECT count(*)
2324 FROM civicrm_custom_field
2325 WHERE option_group_id = {$optionGroupId}";
2326
2327 $count = CRM_Core_DAO::singleValueQuery($query);
2328
2329 if ($count < 2) {
2330 //delete the option group
2331 CRM_Core_BAO_OptionGroup::del($optionGroupId);
2332 }
2333 }
2334
2335 /**
2336 * Get option group default.
2337 *
2338 * @param int $optionGroupId
2339 * @param bool $serialize
2340 *
2341 * @return null|string
2342 */
2343 public static function getOptionGroupDefault($optionGroupId, $serialize) {
2344 $query = "
2345 SELECT default_value, serialize
2346 FROM civicrm_custom_field
2347 WHERE option_group_id = {$optionGroupId}
2348 AND default_value IS NOT NULL";
2349
2350 $dao = CRM_Core_DAO::executeQuery($query);
2351 while ($dao->fetch()) {
2352 if ($dao->serialize == $serialize) {
2353 return $dao->default_value;
2354 }
2355 $defaultValue = $dao->default_value;
2356 }
2357
2358 // Convert serialization
2359 if (isset($defaultValue) && $serialize) {
2360 return CRM_Utils_Array::implodePadded([$defaultValue]);
2361 }
2362 elseif (isset($defaultValue)) {
2363 return CRM_Utils_Array::explodePadded($defaultValue)[0];
2364 }
2365 return NULL;
2366 }
2367
2368 /**
2369 * Post process function.
2370 *
2371 * @param array $params
2372 * @param int $entityID
2373 * @param string $customFieldExtends
2374 * @param bool $inline
2375 * @param bool $checkPermissions
2376 *
2377 * @return array
2378 */
2379 public static function postProcess(
2380 &$params,
2381 $entityID,
2382 $customFieldExtends,
2383 $inline = FALSE,
2384 $checkPermissions = TRUE
2385 ) {
2386 $customData = [];
2387
2388 foreach ($params as $key => $value) {
2389 if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE)) {
2390
2391 // for autocomplete transfer hidden value instead of label
2392 if ($params[$key] && isset($params[$key . '_id'])) {
2393 $value = $params[$key . '_id'];
2394 }
2395
2396 // we need to append time with date
2397 if ($params[$key] && isset($params[$key . '_time'])) {
2398 $value .= ' ' . $params[$key . '_time'];
2399 }
2400
2401 CRM_Core_BAO_CustomField::formatCustomField($customFieldInfo[0],
2402 $customData,
2403 $value,
2404 $customFieldExtends,
2405 $customFieldInfo[1],
2406 $entityID,
2407 $inline,
2408 $checkPermissions
2409 );
2410 }
2411 }
2412 return $customData;
2413 }
2414
2415 /**
2416 * Get custom field ID from field/group name/title.
2417 *
2418 * @param string $fieldName Field name or label
2419 * @param string|null $groupName (Optional) Group name or label
2420 * @param bool $fullString Whether to return "custom_123" or "123"
2421 *
2422 * @return string|int|null
2423 * @throws \CiviCRM_API3_Exception
2424 */
2425 public static function getCustomFieldID($fieldName, $groupName = NULL, $fullString = FALSE) {
2426 $cacheKey = $groupName . '.' . $fieldName;
2427 if (!isset(Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey])) {
2428 $customFieldParams = [
2429 'name' => $fieldName,
2430 'label' => $fieldName,
2431 'options' => ['or' => [["name", "label"]]],
2432 ];
2433
2434 if ($groupName) {
2435 $customFieldParams['custom_group_id.name'] = $groupName;
2436 $customFieldParams['custom_group_id.title'] = $groupName;
2437 $customFieldParams['options'] = ['or' => [["name", "label"], ["custom_group_id.name", "custom_group_id.title"]]];
2438 }
2439
2440 $field = civicrm_api3('CustomField', 'get', $customFieldParams);
2441
2442 if (empty($field['id'])) {
2443 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = NULL;
2444 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = NULL;
2445 }
2446 else {
2447 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'] = $field['id'];
2448 Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'] = 'custom_' . $field['id'];
2449 }
2450 }
2451
2452 if ($fullString) {
2453 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['string'];
2454 }
2455 return Civi::$statics['CRM_Core_BAO_CustomField'][$cacheKey]['id'];
2456 }
2457
2458 /**
2459 * Given ID of a custom field, return its name as well as the name of the custom group it belongs to.
2460 *
2461 * @param array $ids
2462 *
2463 * @return array
2464 */
2465 public static function getNameFromID($ids) {
2466 if (is_array($ids)) {
2467 $ids = implode(',', $ids);
2468 }
2469 $sql = "
2470 SELECT f.id, f.name AS field_name, f.label AS field_label, g.name AS group_name, g.title AS group_title
2471 FROM civicrm_custom_field f
2472 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
2473 WHERE f.id IN ($ids)";
2474
2475 $dao = CRM_Core_DAO::executeQuery($sql);
2476 $result = [];
2477 while ($dao->fetch()) {
2478 $result[$dao->id] = [
2479 'field_name' => $dao->field_name,
2480 'field_label' => $dao->field_label,
2481 'group_name' => $dao->group_name,
2482 'group_title' => $dao->group_title,
2483 ];
2484 }
2485 return $result;
2486 }
2487
2488 /**
2489 * Validate custom data.
2490 *
2491 * @param array $params
2492 * Custom data submitted.
2493 * ie array( 'custom_1' => 'validate me' );
2494 *
2495 * @return array
2496 * validation errors.
2497 */
2498 public static function validateCustomData($params) {
2499 $errors = [];
2500 if (!is_array($params) || empty($params)) {
2501 return $errors;
2502 }
2503
2504 //pick up profile fields.
2505 $profileFields = [];
2506 $ufGroupId = $params['ufGroupId'] ?? NULL;
2507 if ($ufGroupId) {
2508 $profileFields = CRM_Core_BAO_UFGroup::getFields($ufGroupId,
2509 FALSE,
2510 CRM_Core_Action::VIEW
2511 );
2512 }
2513
2514 //lets start w/ params.
2515 foreach ($params as $key => $value) {
2516 $customFieldID = self::getKeyID($key);
2517 if (!$customFieldID) {
2518 continue;
2519 }
2520
2521 //load the structural info for given field.
2522 $field = new CRM_Core_DAO_CustomField();
2523 $field->id = $customFieldID;
2524 if (!$field->find(TRUE)) {
2525 continue;
2526 }
2527 $dataType = $field->data_type;
2528
2529 $profileField = CRM_Utils_Array::value($key, $profileFields, []);
2530 $fieldTitle = $profileField['title'] ?? NULL;
2531 $isRequired = $profileField['is_required'] ?? NULL;
2532 if (!$fieldTitle) {
2533 $fieldTitle = $field->label;
2534 }
2535
2536 //no need to validate.
2537 if (CRM_Utils_System::isNull($value) && !$isRequired) {
2538 continue;
2539 }
2540
2541 //lets validate first for required field.
2542 if ($isRequired && CRM_Utils_System::isNull($value)) {
2543 $errors[$key] = ts('%1 is a required field.', [1 => $fieldTitle]);
2544 continue;
2545 }
2546
2547 //now time to take care of custom field form rules.
2548 $ruleName = $errorMsg = NULL;
2549 switch ($dataType) {
2550 case 'Int':
2551 $ruleName = 'integer';
2552 $errorMsg = ts('%1 must be an integer (whole number).',
2553 array(1 => $fieldTitle)
2554 );
2555 break;
2556
2557 case 'Money':
2558 $ruleName = 'money';
2559 $errorMsg = ts('%1 must in proper money format. (decimal point/comma/space is allowed).',
2560 array(1 => $fieldTitle)
2561 );
2562 break;
2563
2564 case 'Float':
2565 $ruleName = 'numeric';
2566 $errorMsg = ts('%1 must be a number (with or without decimal point).',
2567 array(1 => $fieldTitle)
2568 );
2569 break;
2570
2571 case 'Link':
2572 $ruleName = 'wikiURL';
2573 $errorMsg = ts('%1 must be valid Website.',
2574 array(1 => $fieldTitle)
2575 );
2576 break;
2577 }
2578
2579 if ($ruleName && !CRM_Utils_System::isNull($value)) {
2580 $valid = FALSE;
2581 $funName = "CRM_Utils_Rule::{$ruleName}";
2582 if (is_callable($funName)) {
2583 $valid = call_user_func($funName, $value);
2584 }
2585 if (!$valid) {
2586 $errors[$key] = $errorMsg;
2587 }
2588 }
2589 }
2590
2591 return $errors;
2592 }
2593
2594 /**
2595 * Is this field a multi record field.
2596 *
2597 * @param int $customId
2598 *
2599 * @return bool
2600 */
2601 public static function isMultiRecordField($customId) {
2602 $isMultipleWithGid = FALSE;
2603 if (!is_numeric($customId)) {
2604 $customId = self::getKeyID($customId);
2605 }
2606 if (is_numeric($customId)) {
2607 $sql = "SELECT cg.id cgId
2608 FROM civicrm_custom_group cg
2609 INNER JOIN civicrm_custom_field cf
2610 ON cg.id = cf.custom_group_id
2611 WHERE cf.id = %1 AND cg.is_multiple = 1";
2612 $params[1] = [$customId, 'Integer'];
2613 $dao = CRM_Core_DAO::executeQuery($sql, $params);
2614 if ($dao->fetch()) {
2615 if ($dao->cgId) {
2616 $isMultipleWithGid = $dao->cgId;
2617 }
2618 }
2619 }
2620
2621 return $isMultipleWithGid;
2622 }
2623
2624 /**
2625 * Does this field type have any select options?
2626 *
2627 * @param array $field
2628 *
2629 * @return bool
2630 */
2631 public static function hasOptions($field) {
2632 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2633 $field = (array) $field;
2634 // This will include boolean fields with Yes/No options.
2635 if (in_array($field['html_type'], ['Radio', 'CheckBox'])) {
2636 return TRUE;
2637 }
2638 // Do this before the "Select" string search because date fields have a "Select Date" html_type
2639 // and contactRef fields have an "Autocomplete-Select" html_type - contacts are an FK not an option list.
2640 if (in_array($field['data_type'], ['ContactReference', 'Date'])) {
2641 return FALSE;
2642 }
2643 if (strpos($field['html_type'], 'Select') !== FALSE) {
2644 return TRUE;
2645 }
2646 return !empty($field['option_group_id']);
2647 }
2648
2649 /**
2650 * Does this field store a serialized string?
2651 *
2652 * @param CRM_Core_DAO_CustomField|array $field
2653 *
2654 * @return bool
2655 */
2656 public static function isSerialized($field) {
2657 // Fields retrieved via api are an array, or from the dao are an object. We'll accept either.
2658 $html_type = is_object($field) ? $field->html_type : $field['html_type'];
2659 // APIv3 has a "legacy" mode where it returns old-style html_type of "Multi-Select"
2660 // If anyone is using this function in conjunction with legacy api output, we'll accomodate:
2661 if ($html_type === 'CheckBox' || strpos($html_type, 'Multi') !== FALSE) {
2662 return TRUE;
2663 }
2664 // Otherwise this is the new standard as of 5.27
2665 return is_object($field) ? !empty($field->serialize) : !empty($field['serialize']);
2666 }
2667
2668 /**
2669 * Get api entity for this field
2670 *
2671 * @return string
2672 */
2673 public function getEntity() {
2674 $entity = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->custom_group_id, 'extends');
2675 return in_array($entity, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE) ? 'Contact' : $entity;
2676 }
2677
2678 /**
2679 * Set pseudoconstant properties for field metadata.
2680 *
2681 * @param array $field
2682 * @param string|null $optionGroupName
2683 */
2684 private static function getOptionsForField(&$field, $optionGroupName) {
2685 if ($optionGroupName) {
2686 $field['pseudoconstant'] = [
2687 'optionGroupName' => $optionGroupName,
2688 'optionEditPath' => 'civicrm/admin/options/' . $optionGroupName,
2689 ];
2690 }
2691 elseif ($field['data_type'] == 'Boolean') {
2692 $field['pseudoconstant'] = [
2693 'callback' => 'CRM_Core_SelectValues::boolean',
2694 ];
2695 }
2696 elseif ($field['data_type'] == 'Country') {
2697 $field['pseudoconstant'] = [
2698 'table' => 'civicrm_country',
2699 'keyColumn' => 'id',
2700 'labelColumn' => 'name',
2701 'nameColumn' => 'iso_code',
2702 ];
2703 }
2704 elseif ($field['data_type'] == 'StateProvince') {
2705 $field['pseudoconstant'] = [
2706 'table' => 'civicrm_state_province',
2707 'keyColumn' => 'id',
2708 'labelColumn' => 'name',
2709 ];
2710 }
2711 }
2712
2713 /**
2714 * Prepare params for the create operation.
2715 *
2716 * @param CRM_Core_DAO_CustomField $field
2717 * @param string $operation
2718 * add|modify|delete
2719 *
2720 * @return array
2721 * @throws \CRM_Core_Exception
2722 */
2723 protected static function prepareCreateParams($field, $operation): array {
2724 $tableName = CRM_Core_DAO::getFieldValue(
2725 'CRM_Core_DAO_CustomGroup',
2726 $field->custom_group_id,
2727 'table_name'
2728 );
2729
2730 $params = [
2731 'table_name' => $tableName,
2732 'operation' => $operation,
2733 'name' => $field->column_name,
2734 'type' => CRM_Core_BAO_CustomValueTable::fieldToSQLType(
2735 $field->data_type,
2736 $field->text_length
2737 ),
2738 'required' => $field->is_required,
2739 'searchable' => $field->is_searchable,
2740 ];
2741
2742 // For adding/dropping FK constraints
2743 $params['fkName'] = CRM_Core_BAO_SchemaHandler::getIndexName($tableName, $field->column_name);
2744
2745 $fkFields = [
2746 'Country' => 'civicrm_country',
2747 'StateProvince' => 'civicrm_state_province',
2748 'ContactReference' => 'civicrm_contact',
2749 'File' => 'civicrm_file',
2750 ];
2751 if (isset($fkFields[$field->data_type])) {
2752 // Serialized fields store value-separated strings which are incompatible with FK constraints
2753 if (!$field->serialize) {
2754 $params['fk_table_name'] = $fkFields[$field->data_type];
2755 $params['fk_field_name'] = 'id';
2756 $params['fk_attributes'] = 'ON DELETE SET NULL';
2757 }
2758 }
2759 if ($field->serialize) {
2760 $params['type'] = 'varchar(255)';
2761 }
2762 if (isset($field->default_value)) {
2763 $params['default'] = "'{$field->default_value}'";
2764 }
2765 return $params;
2766 }
2767
2768 }