Merge pull request #19095 from nishant-bhorodia/Issue#537-owner-notification-email...
[civicrm-core.git] / CRM / Core / BAO / CustomValueTable.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 class CRM_Core_BAO_CustomValueTable {
18
19 /**
20 * @param array $customParams
21 * @param string $parentOperation Operation being taken on the parent entity.
22 * If we know the parent entity is doing an insert we can skip the
23 * ON DUPLICATE UPDATE - which improves performance and reduces deadlocks.
24 * - edit
25 * - create
26 *
27 * @throws Exception
28 */
29 public static function create($customParams, $parentOperation = NULL) {
30 if (empty($customParams) ||
31 !is_array($customParams)
32 ) {
33 return;
34 }
35
36 $paramFieldsExtendContactForEntities = [];
37
38 foreach ($customParams as $tableName => $tables) {
39 foreach ($tables as $index => $fields) {
40 $sqlOP = NULL;
41 $hookID = NULL;
42 $hookOP = NULL;
43 $entityID = NULL;
44 $isMultiple = FALSE;
45 $set = [];
46 $params = [];
47 $count = 1;
48
49 $firstField = reset($fields);
50 $entityID = $firstField['entity_id'];
51 $hookID = $firstField['custom_group_id'];
52 $isMultiple = $firstField['is_multiple'];
53 if (array_key_exists('id', $firstField)) {
54 $sqlOP = "UPDATE $tableName ";
55 $where = " WHERE id = %{$count}";
56 $params[$count] = [$firstField['id'], 'Integer'];
57 $count++;
58 $hookOP = 'edit';
59 }
60 else {
61 $sqlOP = "INSERT INTO $tableName ";
62 $where = NULL;
63 $hookOP = 'create';
64 }
65
66 CRM_Utils_Hook::customPre($hookOP,
67 $hookID,
68 $entityID,
69 $fields
70 );
71
72 foreach ($fields as $field) {
73 // fix the value before we store it
74 $value = $field['value'];
75 $type = $field['type'];
76 switch ($type) {
77 case 'StateProvince':
78 $type = 'Integer';
79 if (is_array($value)) {
80 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
81 $type = 'String';
82 }
83 elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
84 //fix for multi select state, CRM-3437
85 $mulValues = explode(',', $value);
86 $validStates = [];
87 foreach ($mulValues as $key => $stateVal) {
88 $states = [];
89 $states['state_province'] = trim($stateVal);
90
91 CRM_Utils_Array::lookupValue($states, 'state_province',
92 CRM_Core_PseudoConstant::stateProvince(), TRUE
93 );
94 if (empty($states['state_province_id'])) {
95 CRM_Utils_Array::lookupValue($states, 'state_province',
96 CRM_Core_PseudoConstant::stateProvinceAbbreviation(), TRUE
97 );
98 }
99 $validStates[] = $states['state_province_id'] ?? NULL;
100 }
101 $value = implode(CRM_Core_DAO::VALUE_SEPARATOR,
102 $validStates
103 );
104 $type = 'String';
105 }
106 elseif (!$value) {
107 // CRM-3415
108 // using type of timestamp allows us to sneak in a null into db
109 // gross but effective hack
110 $value = NULL;
111 $type = 'Timestamp';
112 }
113 else {
114 $type = 'String';
115 }
116 break;
117
118 case 'Country':
119 $type = 'Integer';
120 if (is_array($value)) {
121 $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
122 $type = 'String';
123 }
124 elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
125 //fix for multi select country, CRM-3437
126 $mulValues = explode(',', $value);
127 $validCountries = [];
128 foreach ($mulValues as $key => $countryVal) {
129 $countries = [];
130 $countries['country'] = trim($countryVal);
131 CRM_Utils_Array::lookupValue($countries, 'country',
132 CRM_Core_PseudoConstant::country(), TRUE
133 );
134 if (empty($countries['country_id'])) {
135 CRM_Utils_Array::lookupValue($countries, 'country',
136 CRM_Core_PseudoConstant::countryIsoCode(), TRUE
137 );
138 }
139 $validCountries[] = $countries['country_id'] ?? NULL;
140 }
141 $value = implode(CRM_Core_DAO::VALUE_SEPARATOR,
142 $validCountries
143 );
144 $type = 'String';
145 }
146 elseif (!$value) {
147 // CRM-3415
148 // using type of timestamp allows us to sneak in a null into db
149 // gross but effective hack
150 $value = NULL;
151 $type = 'Timestamp';
152 }
153 else {
154 $type = 'String';
155 }
156 break;
157
158 case 'File':
159 if (!$field['file_id']) {
160 throw new CRM_Core_Exception('Missing parameter file_id');
161 }
162
163 // need to add/update civicrm_entity_file
164 $entityFileDAO = new CRM_Core_DAO_EntityFile();
165 $entityFileDAO->file_id = $field['file_id'];
166 $entityFileDAO->find(TRUE);
167
168 $entityFileDAO->entity_table = $field['table_name'];
169 $entityFileDAO->entity_id = $field['entity_id'];
170 $entityFileDAO->file_id = $field['file_id'];
171 $entityFileDAO->save();
172 $value = $field['file_id'];
173 $type = 'String';
174 break;
175
176 case 'Date':
177 $value = CRM_Utils_Date::isoToMysql($value);
178 break;
179
180 case 'Int':
181 if (is_numeric($value)) {
182 $type = 'Integer';
183 }
184 else {
185 $type = 'Timestamp';
186 }
187 break;
188
189 case 'ContactReference':
190 if ($value == NULL) {
191 $type = 'Timestamp';
192 }
193 else {
194 $type = 'Integer';
195 }
196 break;
197
198 case 'RichTextEditor':
199 $type = 'String';
200 break;
201
202 case 'Boolean':
203 //fix for CRM-3290
204 $value = CRM_Utils_String::strtoboolstr($value);
205 if ($value === FALSE) {
206 $type = 'Timestamp';
207 }
208 break;
209
210 default:
211 break;
212 }
213 if ($value === 'null') {
214 // when unsetting a value to null, we don't need to validate the type
215 // https://projectllr.atlassian.net/browse/VGQBMP-20
216 $set[$field['column_name']] = $value;
217 }
218 else {
219 $set[$field['column_name']] = "%{$count}";
220 $params[$count] = [$value, $type];
221 $count++;
222 }
223
224 $fieldExtends = $field['extends'] ?? NULL;
225 if (
226 CRM_Utils_Array::value('entity_table', $field) == 'civicrm_contact'
227 || $fieldExtends == 'Contact'
228 || $fieldExtends == 'Individual'
229 || $fieldExtends == 'Organization'
230 || $fieldExtends == 'Household'
231 ) {
232 $paramFieldsExtendContactForEntities[$entityID]['custom_' . CRM_Utils_Array::value('custom_field_id', $field)] = $field['custom_field_id'] ?? NULL;
233 }
234 }
235
236 if (!empty($set)) {
237 $setClause = [];
238 foreach ($set as $n => $v) {
239 $setClause[] = "`$n` = $v";
240 }
241 $setClause = implode(',', $setClause);
242 if (!$where) {
243 // do this only for insert
244 $set['entity_id'] = "%{$count}";
245 $params[$count] = [$entityID, 'Integer'];
246 $count++;
247
248 $fieldNames = implode(',', CRM_Utils_Type::escapeAll(array_keys($set), 'MysqlColumnNameOrAlias'));
249 $fieldValues = implode(',', array_values($set));
250 $query = "$sqlOP ( $fieldNames ) VALUES ( $fieldValues )";
251 // for multiple values we dont do on duplicate key update
252 if (!$isMultiple && $parentOperation !== 'create') {
253 $query .= " ON DUPLICATE KEY UPDATE $setClause";
254 }
255 }
256 else {
257 $query = "$sqlOP SET $setClause $where";
258 }
259 $dao = CRM_Core_DAO::executeQuery($query, $params);
260
261 CRM_Utils_Hook::custom($hookOP,
262 $hookID,
263 $entityID,
264 $fields
265 );
266 }
267 }
268 }
269
270 if (!empty($paramFieldsExtendContactForEntities)) {
271 CRM_Contact_BAO_Contact::updateGreetingsOnTokenFieldChange($paramFieldsExtendContactForEntities, ['contact_id' => $entityID]);
272 }
273 }
274
275 /**
276 * Given a field return the mysql data type associated with it.
277 *
278 * @param string $type
279 * @param int $maxLength
280 *
281 * @return string
282 * the mysql data store placeholder
283 */
284 public static function fieldToSQLType($type, $maxLength = 255) {
285 if (!isset($maxLength) ||
286 !is_numeric($maxLength) ||
287 $maxLength <= 0
288 ) {
289 $maxLength = 255;
290 }
291
292 switch ($type) {
293 case 'String':
294 case 'Link':
295 return "varchar($maxLength)";
296
297 case 'Boolean':
298 return 'tinyint';
299
300 case 'Int':
301 return 'int';
302
303 // the below three are FK's, and have constraints added to them
304
305 case 'ContactReference':
306 case 'StateProvince':
307 case 'Country':
308 case 'File':
309 return 'int unsigned';
310
311 case 'Float':
312 return 'double';
313
314 case 'Money':
315 return 'decimal(20,2)';
316
317 case 'Memo':
318 case 'RichTextEditor':
319 return 'text';
320
321 case 'Date':
322 return 'datetime';
323
324 default:
325 throw new CRM_Core_Exception('Invalid Field Type');
326 }
327 }
328
329 /**
330 * @param array $params
331 * @param $entityTable
332 * @param int $entityID
333 * @param string $parentOperation Operation being taken on the parent entity.
334 * If we know the parent entity is doing an insert we can skip the
335 * ON DUPLICATE UPDATE - which improves performance and reduces deadlocks.
336 * - edit
337 * - create
338 */
339 public static function store($params, $entityTable, $entityID, $parentOperation = NULL) {
340 $cvParams = [];
341 foreach ($params as $fieldID => $param) {
342 foreach ($param as $index => $customValue) {
343 $cvParam = [
344 'entity_table' => $entityTable,
345 'entity_id' => $entityID,
346 'value' => $customValue['value'],
347 'type' => $customValue['type'],
348 'custom_field_id' => $customValue['custom_field_id'],
349 'custom_group_id' => $customValue['custom_group_id'],
350 'table_name' => $customValue['table_name'],
351 'column_name' => $customValue['column_name'],
352 'is_multiple' => $customValue['is_multiple'] ?? NULL,
353 'file_id' => $customValue['file_id'],
354 ];
355
356 // Fix Date type to be timestamp, since that is how we store in db.
357 if ($cvParam['type'] == 'Date') {
358 $cvParam['type'] = 'Timestamp';
359 }
360
361 if (!empty($customValue['id'])) {
362 $cvParam['id'] = $customValue['id'];
363 }
364 if (!array_key_exists($customValue['table_name'], $cvParams)) {
365 $cvParams[$customValue['table_name']] = [];
366 }
367
368 if (!array_key_exists($index, $cvParams[$customValue['table_name']])) {
369 $cvParams[$customValue['table_name']][$index] = [];
370 }
371
372 $cvParams[$customValue['table_name']][$index][] = $cvParam;
373 }
374 }
375 if (!empty($cvParams)) {
376 self::create($cvParams, $parentOperation);
377 }
378 }
379
380 /**
381 * Post process function.
382 *
383 * @param array $params
384 * @param $entityTable
385 * @param int $entityID
386 * @param $customFieldExtends
387 */
388 public static function postProcess(&$params, $entityTable, $entityID, $customFieldExtends) {
389 $customData = CRM_Core_BAO_CustomField::postProcess($params,
390 $entityID,
391 $customFieldExtends
392 );
393
394 if (!empty($customData)) {
395 self::store($customData, $entityTable, $entityID);
396 }
397 }
398
399 /**
400 * Return an array of all custom values associated with an entity.
401 *
402 * @param int $entityID
403 * Identification number of the entity.
404 * @param string $entityType
405 * Type of entity that the entityID corresponds to, specified.
406 * as a string with format "'<EntityName>'". Comma separated
407 * list may be used to specify OR matches. Allowable values
408 * are enumerated types in civicrm_custom_group.extends field.
409 * Optional. Default value assumes entityID references a
410 * contact entity.
411 * @param array $fieldIDs
412 * Optional list of fieldIDs that we want to retrieve. If this.
413 * is set the entityType is ignored
414 *
415 * @param bool $formatMultiRecordField
416 * @param array $DTparams - CRM-17810 dataTable params for the multiValued custom fields.
417 *
418 * @return array
419 * Array of custom values for the entity with key=>value
420 * pairs specified as civicrm_custom_field.id => custom value.
421 * Empty array if no custom values found.
422 * @throws CRM_Core_Exception
423 */
424 public static function &getEntityValues($entityID, $entityType = NULL, $fieldIDs = NULL, $formatMultiRecordField = FALSE, $DTparams = NULL) {
425 if (!$entityID) {
426 // adding this here since an empty contact id could have serious repurcussions
427 // like looping forever
428 throw new CRM_Core_Exception('Please file an issue with the backtrace');
429 return NULL;
430 }
431
432 $cond = [];
433 if ($entityType) {
434 $cond[] = "cg.extends IN ( '$entityType' )";
435 }
436 if ($fieldIDs &&
437 is_array($fieldIDs)
438 ) {
439 $fieldIDList = implode(',', $fieldIDs);
440 $cond[] = "cf.id IN ( $fieldIDList )";
441 }
442 if (empty($cond)) {
443 $cond[] = "cg.extends IN ( 'Contact', 'Individual', 'Household', 'Organization' )";
444 }
445 $cond = implode(' AND ', $cond);
446
447 $limit = $orderBy = '';
448 if (!empty($DTparams['rowCount']) && $DTparams['rowCount'] > 0) {
449 $limit = " LIMIT " . CRM_Utils_Type::escape($DTparams['offset'], 'Integer') . ", " . CRM_Utils_Type::escape($DTparams['rowCount'], 'Integer');
450 }
451 if (!empty($DTparams['sort'])) {
452 $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($DTparams['sort'], 'String');
453 }
454
455 // First find all the fields that extend this type of entity.
456 $query = "
457 SELECT cg.table_name,
458 cg.id as groupID,
459 cg.is_multiple,
460 cf.column_name,
461 cf.id as fieldID,
462 cf.data_type as fieldDataType
463 FROM civicrm_custom_group cg,
464 civicrm_custom_field cf
465 WHERE cf.custom_group_id = cg.id
466 AND cg.is_active = 1
467 AND cf.is_active = 1
468 AND $cond
469 ";
470 $dao = CRM_Core_DAO::executeQuery($query);
471
472 $select = $fields = $isMultiple = [];
473
474 while ($dao->fetch()) {
475 if (!array_key_exists($dao->table_name, $select)) {
476 $fields[$dao->table_name] = [];
477 $select[$dao->table_name] = [];
478 }
479 $fields[$dao->table_name][] = $dao->fieldID;
480 $select[$dao->table_name][] = "{$dao->column_name} AS custom_{$dao->fieldID}";
481 $isMultiple[$dao->table_name] = (bool) $dao->is_multiple;
482 $file[$dao->table_name][$dao->fieldID] = $dao->fieldDataType;
483 }
484
485 $result = $sortedResult = [];
486 foreach ($select as $tableName => $clauses) {
487 if (!empty($DTparams['sort'])) {
488 $query = CRM_Core_DAO::executeQuery("SELECT id FROM {$tableName} WHERE entity_id = {$entityID}");
489 $count = 1;
490 while ($query->fetch()) {
491 $sortedResult["{$query->id}"] = $count;
492 $count++;
493 }
494 }
495
496 $query = "SELECT SQL_CALC_FOUND_ROWS id, " . implode(', ', $clauses) . " FROM $tableName WHERE entity_id = $entityID {$orderBy} {$limit}";
497 $dao = CRM_Core_DAO::executeQuery($query);
498 if (!empty($DTparams)) {
499 $result['count'] = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()');
500 }
501 while ($dao->fetch()) {
502 foreach ($fields[$tableName] as $fieldID) {
503 $fieldName = "custom_{$fieldID}";
504 if ($isMultiple[$tableName]) {
505 if ($formatMultiRecordField) {
506 $result["{$dao->id}"]["{$fieldID}"] = $dao->$fieldName;
507 }
508 else {
509 $result["{$fieldID}_{$dao->id}"] = $dao->$fieldName;
510 }
511 }
512 else {
513 $result[$fieldID] = $dao->$fieldName;
514 }
515 }
516 }
517 }
518 if (!empty($sortedResult)) {
519 $result['sortedResult'] = $sortedResult;
520 }
521 return $result;
522 }
523
524 /**
525 * Take in an array of entityID, custom_XXX => value
526 * and set the value in the appropriate table. Should also be able
527 * to set the value to null. Follows api parameter/return conventions
528 *
529 * @array $params
530 *
531 * @param array $params
532 *
533 * @throws Exception
534 * @return array
535 */
536 public static function setValues(&$params) {
537 // For legacy reasons, accept this param in either format
538 if (empty($params['entityID']) && !empty($params['entity_id'])) {
539 $params['entityID'] = $params['entity_id'];
540 }
541
542 if (!isset($params['entityID']) || !CRM_Utils_Type::validate($params['entityID'], 'Integer', FALSE)) {
543 throw new CRM_Core_Exception(ts('entity_id needs to be set and of type Integer'));
544 }
545
546 // first collect all the id/value pairs. The format is:
547 // custom_X => value or custom_X_VALUEID => value (for multiple values), VALUEID == -1, -2 etc for new insertions
548 $fieldValues = [];
549 foreach ($params as $n => $v) {
550 if ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($n, TRUE)) {
551 $fieldID = (int ) $customFieldInfo[0];
552 if (CRM_Utils_Type::escape($fieldID, 'Integer', FALSE) === NULL) {
553 throw new CRM_Core_Exception(ts('field ID needs to be of type Integer for index %1',
554 [1 => $fieldID]
555 ));
556 }
557 if (!array_key_exists($fieldID, $fieldValues)) {
558 $fieldValues[$fieldID] = [];
559 }
560 $id = -1;
561 if ($customFieldInfo[1]) {
562 $id = (int ) $customFieldInfo[1];
563 }
564 $fieldValues[$fieldID][] = [
565 'value' => $v,
566 'id' => $id,
567 ];
568 }
569 }
570
571 $fieldIDList = implode(',', array_keys($fieldValues));
572
573 // format it so that we can just use create
574 $sql = "
575 SELECT cg.table_name as table_name ,
576 cg.id as cg_id ,
577 cg.is_multiple as is_multiple,
578 cg.extends as extends,
579 cf.column_name as column_name,
580 cf.id as cf_id ,
581 cf.html_type as html_type ,
582 cf.data_type as data_type
583 FROM civicrm_custom_group cg,
584 civicrm_custom_field cf
585 WHERE cf.custom_group_id = cg.id
586 AND cf.id IN ( $fieldIDList )
587 ";
588
589 $dao = CRM_Core_DAO::executeQuery($sql);
590 $cvParams = [];
591
592 while ($dao->fetch()) {
593 $dataType = $dao->data_type == 'Date' ? 'Timestamp' : $dao->data_type;
594 foreach ($fieldValues[$dao->cf_id] as $fieldValue) {
595 // Serialize array values
596 if (is_array($fieldValue['value']) && CRM_Core_BAO_CustomField::isSerialized($dao)) {
597 $fieldValue['value'] = CRM_Utils_Array::implodePadded($fieldValue['value']);
598 }
599 // Format null values correctly
600 if ($fieldValue['value'] === NULL || $fieldValue['value'] === '') {
601 switch ($dataType) {
602 case 'String':
603 case 'Int':
604 case 'Link':
605 case 'Boolean':
606 $fieldValue['value'] = '';
607 break;
608
609 case 'Timestamp':
610 $fieldValue['value'] = NULL;
611 break;
612
613 case 'StateProvince':
614 case 'Country':
615 case 'Money':
616 case 'Float':
617 $fieldValue['value'] = (int) 0;
618 break;
619 }
620 }
621 // Ensure that value is of the right data type
622 elseif (CRM_Utils_Type::escape($fieldValue['value'], $dataType, FALSE) === NULL) {
623 throw new CRM_Core_Exception(ts('value: %1 is not of the right field data type: %2',
624 [
625 1 => $fieldValue['value'],
626 2 => $dao->data_type,
627 ]
628 ));
629 }
630
631 $cvParam = [
632 'entity_id' => $params['entityID'],
633 'value' => $fieldValue['value'],
634 'type' => $dataType,
635 'custom_field_id' => $dao->cf_id,
636 'custom_group_id' => $dao->cg_id,
637 'table_name' => $dao->table_name,
638 'column_name' => $dao->column_name,
639 'is_multiple' => $dao->is_multiple,
640 'extends' => $dao->extends,
641 ];
642
643 if (!empty($params['id'])) {
644 $cvParam['id'] = $params['id'];
645 }
646
647 if ($cvParam['type'] == 'File') {
648 $cvParam['file_id'] = $fieldValue['value'];
649 }
650
651 if (!array_key_exists($dao->table_name, $cvParams)) {
652 $cvParams[$dao->table_name] = [];
653 }
654
655 if (!array_key_exists($fieldValue['id'], $cvParams[$dao->table_name])) {
656 $cvParams[$dao->table_name][$fieldValue['id']] = [];
657 }
658
659 if ($fieldValue['id'] > 0) {
660 $cvParam['id'] = $fieldValue['id'];
661 }
662 $cvParams[$dao->table_name][$fieldValue['id']][] = $cvParam;
663 }
664 }
665
666 if (!empty($cvParams)) {
667 self::create($cvParams);
668 return ['is_error' => 0, 'result' => 1];
669 }
670
671 throw new CRM_Core_Exception(ts('Unknown error'));
672 }
673
674 /**
675 * Take in an array of entityID, custom_ID
676 * and gets the value from the appropriate table.
677 *
678 * To get the values of custom fields with IDs 13 and 43 for contact ID 1327, use:
679 * $params = array( 'entityID' => 1327, 'custom_13' => 1, 'custom_43' => 1 );
680 *
681 * Entity Type will be inferred by the custom fields you request
682 * Specify $params['entityType'] if you do not supply any custom fields to return
683 * and entity type is other than Contact
684 *
685 * @array $params
686 *
687 * @param array $params
688 *
689 * @throws Exception
690 * @return array
691 */
692 public static function &getValues(&$params) {
693 if (empty($params)) {
694 return NULL;
695 }
696 if (!isset($params['entityID']) ||
697 CRM_Utils_Type::escape($params['entityID'],
698 'Integer', FALSE
699 ) === NULL
700 ) {
701 return CRM_Core_Error::createAPIError(ts('entityID needs to be set and of type Integer'));
702 }
703
704 // first collect all the ids. The format is:
705 // custom_ID
706 $fieldIDs = [];
707 foreach ($params as $n => $v) {
708 $key = $idx = NULL;
709 if (substr($n, 0, 7) == 'custom_') {
710 $idx = substr($n, 7);
711 if (CRM_Utils_Type::escape($idx, 'Integer', FALSE) === NULL) {
712 return CRM_Core_Error::createAPIError(ts('field ID needs to be of type Integer for index %1',
713 [1 => $idx]
714 ));
715 }
716 $fieldIDs[] = (int ) $idx;
717 }
718 }
719
720 $default = ['Contact', 'Individual', 'Household', 'Organization'];
721 if (!($type = CRM_Utils_Array::value('entityType', $params)) ||
722 in_array($params['entityType'], $default)
723 ) {
724 $type = NULL;
725 }
726 else {
727 $entities = CRM_Core_SelectValues::customGroupExtends();
728 if (!array_key_exists($type, $entities)) {
729 if (in_array($type, $entities)) {
730 $type = $entities[$type];
731 if (in_array($type, $default)) {
732 $type = NULL;
733 }
734 }
735 else {
736 return CRM_Core_Error::createAPIError(ts('Invalid entity type') . ': "' . $type . '"');
737 }
738 }
739 }
740
741 $values = self::getEntityValues($params['entityID'],
742 $type,
743 $fieldIDs
744 );
745 if (empty($values)) {
746 // note that this behaviour is undesirable from an API point of view - it should return an empty array
747 // since this is also called by the merger code & not sure the consequences of changing
748 // are just handling undoing this in the api layer. ie. converting the error back into a success
749 $result = [
750 'is_error' => 1,
751 'error_message' => 'No values found for the specified entity ID and custom field(s).',
752 ];
753 return $result;
754 }
755 else {
756 $result = [
757 'is_error' => 0,
758 'entityID' => $params['entityID'],
759 ];
760 foreach ($values as $id => $value) {
761 $result["custom_{$id}"] = $value;
762 }
763 return $result;
764 }
765 }
766
767 }