Cleanup is_array(CRM_Utils_Array::value()) pattern
[civicrm-core.git] / api / v3 / utils.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 * CiviCRM APIv3 utility functions.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Initialize CiviCRM - should be run at the start of each API function.
20 */
21 function _civicrm_api3_initialize() {
22 require_once 'CRM/Core/ClassLoader.php';
23 CRM_Core_ClassLoader::singleton()->register();
24 CRM_Core_Config::singleton();
25 }
26
27 /**
28 * Wrapper Function for civicrm_verify_mandatory to make it simple to pass either / or fields for checking.
29 *
30 * @param array $params
31 * Array of fields to check.
32 * @param array $daoName
33 * String DAO to check for required fields (create functions only).
34 * @param array $keyoptions
35 * List of required fields options. One of the options is required.
36 *
37 * @throws \API_Exception
38 */
39 function civicrm_api3_verify_one_mandatory($params, $daoName = NULL, $keyoptions = []) {
40 $keys = [[]];
41 foreach ($keyoptions as $key) {
42 $keys[0][] = $key;
43 }
44 civicrm_api3_verify_mandatory($params, $daoName, $keys);
45 }
46
47 /**
48 * Check mandatory fields are included.
49 *
50 * @param array $params
51 * Array of fields to check.
52 * @param array $daoName
53 * String DAO to check for required fields (create functions only).
54 * @param array $keys
55 * List of required fields. A value can be an array denoting that either this or that is required.
56 * @param bool $verifyDAO
57 *
58 * @throws \API_Exception
59 */
60 function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = [], $verifyDAO = TRUE) {
61 $unmatched = [];
62
63 if (!empty($params['id'])) {
64 $keys = ['version'];
65 }
66 else {
67 if (!in_array('version', $keys)) {
68 // required from v3 onwards
69 $keys[] = 'version';
70 }
71 }
72 foreach ($keys as $key) {
73 if (is_array($key)) {
74 $match = 0;
75 $optionset = [];
76 foreach ($key as $subkey) {
77 if (!array_key_exists($subkey, $params) || empty($params[$subkey])) {
78 $optionset[] = $subkey;
79 }
80 else {
81 // As long as there is one match we don't need to return anything.
82 $match = 1;
83 }
84 }
85 if (empty($match) && !empty($optionset)) {
86 $unmatched[] = "one of (" . implode(", ", $optionset) . ")";
87 }
88 }
89 else {
90 // Disallow empty values except for the number zero.
91 // TODO: create a utility for this since it's needed in many places.
92 if (!array_key_exists($key, $params) || (empty($params[$key]) && $params[$key] !== 0 && $params[$key] !== '0')) {
93 $unmatched[] = $key;
94 }
95 }
96 }
97 if (!empty($unmatched)) {
98 throw new API_Exception('Mandatory key(s) missing from params array: ' . implode(", ", $unmatched), 'mandatory_missing', ["fields" => $unmatched]);
99 }
100 }
101
102 /**
103 * Create error array.
104 *
105 * @param string $msg
106 * @param array $data
107 *
108 * @return array
109 */
110 function civicrm_api3_create_error($msg, $data = []) {
111 $data['is_error'] = 1;
112 $data['error_message'] = $msg;
113
114 // we will show sql to privileged user only (not sure of a specific
115 // security hole here but seems sensible - perhaps should apply to the trace as well?)
116 if (isset($data['sql'])) {
117 if (CRM_Core_Permission::check('Administer CiviCRM') || CIVICRM_UF === 'UnitTests') {
118 // Isn't this redundant?
119 $data['debug_information'] = $data['sql'];
120 }
121 else {
122 unset($data['sql']);
123 }
124 }
125 return $data;
126 }
127
128 /**
129 * Format array in result output style.
130 *
131 * @param array|int $values values generated by API operation (the result)
132 * @param array $params
133 * Parameters passed into API call.
134 * @param string $entity
135 * The entity being acted on.
136 * @param string $action
137 * The action passed to the API.
138 * @param object $dao
139 * DAO object to be freed here.
140 * @param array $extraReturnValues
141 * Additional values to be added to top level of result array(.
142 * - this param is currently used for legacy behaviour support
143 *
144 * @return array
145 * @throws \CiviCRM_API3_Exception
146 */
147 function civicrm_api3_create_success($values = 1, $params = [], $entity = NULL, $action = NULL, &$dao = NULL, $extraReturnValues = []) {
148 $result = [];
149 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
150 // TODO: This shouldn't be necessary but this fn sometimes gets called with lowercase entity
151 $entity = _civicrm_api_get_camel_name($entity);
152 $result['is_error'] = 0;
153 //lets set the ['id'] field if it's not set & we know what the entity is
154 if (is_array($values) && $entity && $action !== 'getfields') {
155 foreach ($values as $key => $item) {
156 if (empty($item['id']) && !empty($item[$lowercase_entity . '_id'])) {
157 $values[$key]['id'] = $item[$lowercase_entity . '_id'];
158 }
159 if (!empty($item['financial_type_id'])) {
160 // 4.3 legacy handling.
161 $values[$key]['contribution_type_id'] = $item['financial_type_id'];
162 }
163 if (!empty($item['contribution_cancel_date'])) {
164 // 5.16 legacy handling.
165 $values[$key]['cancel_date'] = $item['contribution_cancel_date'];
166 }
167 if (!empty($item['next_sched_contribution_date'])) {
168 // 4.4 legacy handling
169 $values[$key]['next_sched_contribution'] = $item['next_sched_contribution_date'];
170 }
171 }
172 }
173
174 if (is_array($params) && $entity && !empty($params['debug'])) {
175 if (is_string($action) && $action !== 'getfields') {
176 $apiFields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => $action] + $params);
177 }
178 elseif ($action !== 'getfields') {
179 $apiFields = civicrm_api($entity, 'getfields', ['version' => 3] + $params);
180 }
181 else {
182 $apiFields = FALSE;
183 }
184
185 $allFields = [];
186 if ($action !== 'getfields' && isset($apiFields['values']) && is_array($apiFields['values'])) {
187 $allFields = array_keys($apiFields['values']);
188 }
189 $paramFields = array_keys($params);
190 $undefined = array_diff($paramFields, $allFields, array_keys($_COOKIE), [
191 'action',
192 'entity',
193 'debug',
194 'version',
195 'check_permissions',
196 'IDS_request_uri',
197 'IDS_user_agent',
198 'return',
199 'sequential',
200 'rowCount',
201 'option_offset',
202 'option_limit',
203 'custom',
204 'option_sort',
205 'options',
206 'prettyprint',
207 ]);
208 if ($undefined) {
209 $result['undefined_fields'] = array_merge($undefined);
210 }
211 }
212
213 $result['version'] = 3;
214 if (is_array($values)) {
215 $result['count'] = (int) count($values);
216
217 // Convert value-separated strings to array
218 if ($action !== 'getfields') {
219 _civicrm_api3_separate_values($values);
220 }
221
222 if ($result['count'] == 1) {
223 list($result['id']) = array_keys($values);
224 }
225 elseif (!empty($values['id']) && is_int($values['id'])) {
226 $result['id'] = $values['id'];
227 }
228 }
229 else {
230 $result['count'] = !empty($values) ? 1 : 0;
231 }
232
233 if (is_array($values) && isset($params['sequential']) &&
234 $params['sequential'] == 1
235 ) {
236 $result['values'] = array_values($values);
237 }
238 else {
239 $result['values'] = $values;
240 }
241 if (!empty($params['options']['metadata'])) {
242 // We've made metadata an array but only supporting 'fields' atm.
243 if (in_array('fields', (array) $params['options']['metadata']) && $action !== 'getfields') {
244 $fields = civicrm_api3($entity, 'getfields', [
245 'action' => substr($action, 0, 3) == 'get' ? 'get' : 'create',
246 ]);
247 $result['metadata']['fields'] = $fields['values'];
248 }
249 }
250 // Report deprecations.
251 $deprecated = _civicrm_api3_deprecation_check($entity, $result);
252 // Always report "setvalue" action as deprecated.
253 if (!is_string($deprecated) && ($action === 'getactions' || $action === 'setvalue')) {
254 $deprecated = ((array) $deprecated) + ['setvalue' => 'The "setvalue" action is deprecated. Use "create" with an id instead.'];
255 }
256 // Always report "update" action as deprecated.
257 if (!is_string($deprecated) && ($action === 'getactions' || $action === 'update')) {
258 $deprecated = ((array) $deprecated) + ['update' => 'The "update" action is deprecated. Use "create" with an id instead.'];
259 }
260 if ($deprecated) {
261 // Metadata-level deprecations or wholesale entity deprecations.
262 if ($entity === 'Entity' || $action === 'getactions' || is_string($deprecated)) {
263 $result['deprecated'] = $deprecated;
264 }
265 // Action-specific deprecations
266 elseif (!empty($deprecated[$action])) {
267 $result['deprecated'] = $deprecated[$action];
268 }
269 }
270 return array_merge($result, $extraReturnValues);
271 }
272
273 /**
274 * Load the DAO of the entity.
275 *
276 * @param $entity
277 *
278 * @return bool
279 */
280 function _civicrm_api3_load_DAO($entity) {
281 $dao = _civicrm_api3_get_DAO($entity);
282 if (empty($dao)) {
283 return FALSE;
284 }
285 $d = new $dao();
286 return $d;
287 }
288
289 /**
290 * Return the DAO of the function or Entity.
291 *
292 * @param string $name
293 * Either a function of the api (civicrm_{entity}_create or the entity name.
294 * return the DAO name to manipulate this function
295 * eg. "civicrm_api3_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
296 *
297 * @return mixed|string
298 */
299 function _civicrm_api3_get_DAO($name) {
300 if (strpos($name, 'civicrm_api3') !== FALSE) {
301 $last = strrpos($name, '_');
302 // len ('civicrm_api3_') == 13
303 $name = substr($name, 13, $last - 13);
304 }
305
306 $name = _civicrm_api_get_camel_name($name);
307
308 if ($name === 'Individual' || $name === 'Household' || $name === 'Organization') {
309 $name = 'Contact';
310 }
311
312 // hack to deal with incorrectly named BAO/DAO - see CRM-10859
313
314 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingEventQueue
315 if ($name === 'MailingEventQueue') {
316 return 'CRM_Mailing_Event_DAO_Queue';
317 }
318 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingRecipients
319 // but am not confident mailing_recipients is tested so have not tackled.
320 if ($name === 'MailingRecipients') {
321 return 'CRM_Mailing_DAO_Recipients';
322 }
323 // FIXME: DAO should be renamed CRM_ACL_DAO_AclRole
324 if ($name === 'AclRole') {
325 return 'CRM_ACL_DAO_EntityRole';
326 }
327 // FIXME: DAO should be renamed CRM_SMS_DAO_SmsProvider
328 // But this would impact SMS extensions so need to coordinate
329 // Probably best approach is to migrate them to use the api and decouple them from core BAOs
330 if ($name === 'SmsProvider') {
331 return 'CRM_SMS_DAO_Provider';
332 }
333 // FIXME: DAO names should follow CamelCase convention
334 if ($name === 'Im' || $name === 'Acl' || $name === 'Pcp') {
335 $name = strtoupper($name);
336 }
337 $dao = CRM_Core_DAO_AllCoreTables::getFullName($name);
338 if ($dao || !$name) {
339 return $dao;
340 }
341
342 // Really weird apis can declare their own DAO name. Not sure if this is a good idea...
343 if (file_exists("api/v3/$name.php")) {
344 include_once "api/v3/$name.php";
345 }
346
347 $daoFn = '_civicrm_api3_' . _civicrm_api_get_entity_name_from_camel($name) . '_DAO';
348 if (function_exists($daoFn)) {
349 return $daoFn();
350 }
351
352 return NULL;
353 }
354
355 /**
356 * Return the BAO name of the function or Entity.
357 *
358 * @param string $name
359 * Is either a function of the api (civicrm_{entity}_create or the entity name.
360 * return the DAO name to manipulate this function
361 * eg. "civicrm_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
362 *
363 * @return string|null
364 */
365 function _civicrm_api3_get_BAO($name) {
366 // FIXME: DAO should be renamed CRM_Badge_DAO_BadgeLayout
367 if ($name === 'PrintLabel') {
368 return 'CRM_Badge_BAO_Layout';
369 }
370 if ($name === 'Order') {
371 // Order basically maps to contribution at the top level but
372 // has enhanced access to other entities.
373 $name = 'Contribution';
374 }
375 if ($name === 'Dedupe') {
376 // Dedupe is a pseudoentity for PrevNextCache - but accessing dedupe related info
377 // not the other cache info like search results (which could in fact be in Redis or another cache engine)
378 $name = 'PrevNextCache';
379 }
380 if ($name === 'Payment') {
381 $name = 'FinancialTrxn';
382 }
383 $dao = _civicrm_api3_get_DAO($name);
384 if (!$dao) {
385 return NULL;
386 }
387 $bao = str_replace("DAO", "BAO", $dao);
388 $file = strtr($bao, '_', '/') . '.php';
389 // Check if this entity actually has a BAO. Fall back on the DAO if not.
390 return stream_resolve_include_path($file) ? $bao : $dao;
391 }
392
393 /**
394 * Recursive function to explode value-separated strings into arrays.
395 *
396 * @param $values
397 */
398 function _civicrm_api3_separate_values(&$values) {
399 $sp = CRM_Core_DAO::VALUE_SEPARATOR;
400 foreach ($values as $key => & $value) {
401 if (is_array($value)) {
402 _civicrm_api3_separate_values($value);
403 }
404 elseif (is_string($value)) {
405 // This is to honor the way case API was originally written.
406 if ($key === 'case_type_id') {
407 $value = trim(str_replace($sp, ',', $value), ',');
408 }
409 elseif (strpos($value, $sp) !== FALSE) {
410 $value = explode($sp, trim($value, $sp));
411 }
412 }
413 }
414 }
415
416 /**
417 * This is a legacy wrapper for api_store_values.
418 *
419 * It checks suitable fields using getfields rather than DAO->fields.
420 *
421 * Getfields has handling for how to deal with unique names which dao->fields doesn't
422 *
423 * Note this is used by BAO type create functions - eg. contribution
424 *
425 * @param string $entity
426 * @param array $params
427 * @param array $values
428 */
429 function _civicrm_api3_filter_fields_for_bao($entity, &$params, &$values) {
430 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'create']);
431 $fields = $fields['values'];
432 _civicrm_api3_store_values($fields, $params, $values);
433 }
434
435 /**
436 * Store values.
437 *
438 * @param array $fields
439 * @param array $params
440 * @param array $values
441 *
442 * @return Bool
443 */
444 function _civicrm_api3_store_values(&$fields, &$params, &$values) {
445 $valueFound = FALSE;
446
447 $keys = array_intersect_key($params, $fields);
448 foreach ($keys as $name => $value) {
449 if ($name !== 'id') {
450 $values[$name] = $value;
451 $valueFound = TRUE;
452 }
453 }
454 return $valueFound;
455 }
456
457 /**
458 * Returns field names of the given entity fields.
459 *
460 * @param array $fields
461 * Fields array to retrieve the field names for.
462 * @return array
463 */
464 function _civicrm_api3_field_names($fields) {
465 $result = [];
466 foreach ($fields as $key => $value) {
467 if (!empty($value['name'])) {
468 $result[] = $value['name'];
469 }
470 }
471 return $result;
472 }
473
474 /**
475 * Get function for query object api.
476 *
477 * The API supports 2 types of get request. The more complex uses the BAO query object.
478 * This is a generic function for those functions that call it
479 *
480 * At the moment only called by contact we should extend to contribution &
481 * others that use the query object. Note that this function passes permission information in.
482 * The others don't
483 *
484 * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the
485 * 2 variants call
486 *
487 * @param $entity
488 * @param array $params
489 * As passed into api get or getcount function.
490 * @param array $additional_options
491 * Array of options (so we can modify the filter).
492 * @param bool $getCount
493 * Are we just after the count.
494 * @param int $mode
495 * This basically correlates to the component.
496 * @param null|array $defaultReturnProperties
497 * Default return properties for the entity
498 * (used if return not set - but don't do that - set return!).
499 *
500 * @return array
501 * @throws API_Exception
502 */
503 function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = [], $getCount = NULL, $mode = 1, $defaultReturnProperties = NULL) {
504 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
505 // Convert id to e.g. contact_id
506 if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) {
507 $params[$lowercase_entity . '_id'] = $params['id'];
508 }
509 unset($params['id']);
510
511 $options = _civicrm_api3_get_options_from_params($params, TRUE);
512
513 $inputParams = array_merge(
514 CRM_Utils_Array::value('input_params', $options, []),
515 CRM_Utils_Array::value('input_params', $additional_options, [])
516 );
517 $returnProperties = array_merge(
518 CRM_Utils_Array::value('return', $options, []),
519 CRM_Utils_Array::value('return', $additional_options, [])
520 );
521 if (empty($returnProperties)) {
522 $returnProperties = $defaultReturnProperties;
523 }
524 if (!empty($params['check_permissions'])) {
525 // we will filter query object against getfields
526 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'get']);
527 // we need to add this in as earlier in this function 'id' was unset in favour of $entity_id
528 $fields['values'][$lowercase_entity . '_id'] = [];
529 $varsToFilter = ['returnProperties', 'inputParams'];
530 foreach ($varsToFilter as $varToFilter) {
531 if (!is_array($$varToFilter)) {
532 continue;
533 }
534 //I was going to throw an exception rather than silently filter out - but
535 //would need to diff out of exceptions arr other keys like 'options', 'return', 'api. etcetc
536 //so we are silently ignoring parts of their request
537 //$exceptionsArr = array_diff(array_keys($$varToFilter), array_keys($fields['values']));
538 $$varToFilter = array_intersect_key($$varToFilter, $fields['values']);
539 }
540 }
541 $options = array_merge($options, $additional_options);
542 $sort = $options['sort'] ?? NULL;
543 $offset = $options['offset'] ?? NULL;
544 $limit = $options['limit'] ?? NULL;
545 $smartGroupCache = $params['smartGroupCache'] ?? NULL;
546
547 if ($getCount) {
548 $limit = NULL;
549 $returnProperties = NULL;
550 }
551
552 if (substr($sort, 0, 2) == 'id') {
553 $sort = $lowercase_entity . "_" . $sort;
554 }
555
556 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
557
558 $skipPermissions = !empty($params['check_permissions']) ? 0 : 1;
559
560 list($entities) = CRM_Contact_BAO_Query::apiQuery(
561 $newParams,
562 $returnProperties,
563 NULL,
564 $sort,
565 $offset,
566 $limit,
567 $smartGroupCache,
568 $getCount,
569 $skipPermissions,
570 $mode,
571 $entity,
572 TRUE
573 );
574
575 return $entities;
576 }
577
578 /**
579 * Get dao query object based on input params.
580 *
581 * Ideally this would be merged with _civicrm_get_using_query_object but we need to resolve differences in what the
582 * 2 variants call
583 *
584 * @param array $params
585 * @param string $mode
586 * @param string $entity
587 *
588 * @return array
589 * [CRM_Core_DAO|CRM_Contact_BAO_Query]
590 */
591 function _civicrm_api3_get_query_object($params, $mode, $entity) {
592 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
593 $sort = $options['sort'] ?? NULL;
594 $offset = $options['offset'] ?? NULL;
595 $rowCount = $options['limit'] ?? NULL;
596 $inputParams = CRM_Utils_Array::value('input_params', $options, []);
597 $returnProperties = $options['return'] ?? NULL;
598 if (empty($returnProperties)) {
599 $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode);
600 }
601
602 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams, 0, FALSE, $entity);
603 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
604 FALSE, FALSE, $mode,
605 empty($params['check_permissions']),
606 TRUE, TRUE, NULL, 'AND', 'NULL', TRUE
607 );
608 list($select, $from, $where, $having) = $query->query();
609
610 $sql = "$select $from $where $having";
611
612 if (!empty($sort)) {
613 $sort = CRM_Utils_Type::escape($sort, 'MysqlOrderBy');
614 $sql .= " ORDER BY $sort ";
615 }
616 if (!empty($rowCount)) {
617 $sql .= " LIMIT $offset, $rowCount ";
618 }
619 $dao = CRM_Core_DAO::executeQuery($sql);
620 return [$dao, $query];
621 }
622
623 /**
624 * Function transfers the filters being passed into the DAO onto the params object.
625 *
626 * @deprecated DAO based retrieval is being phased out.
627 *
628 * @param CRM_Core_DAO $dao
629 * @param array $params
630 * @param bool $unique
631 * @param array $extraSql
632 * API specific queries eg for event isCurrent would be converted to
633 * $extraSql['where'] = array('civicrm_event' => array('(start_date >= CURDATE() || end_date >= CURDATE())'));
634 *
635 * @throws API_Exception
636 * @throws Exception
637 */
638 function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $extraSql = []) {
639 $entity = _civicrm_api_get_entity_name_from_dao($dao);
640 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
641 if (!empty($params[$lowercase_entity . "_id"]) && empty($params['id'])) {
642 //if entity_id is set then treat it as ID (will be overridden by id if set)
643 $params['id'] = $params[$lowercase_entity . "_id"];
644 }
645 $allfields = _civicrm_api3_build_fields_array($dao, $unique);
646 $fields = array_intersect(array_keys($allfields), array_keys($params));
647
648 $options = _civicrm_api3_get_options_from_params($params);
649 //apply options like sort
650 _civicrm_api3_apply_options_to_dao($params, $dao, $entity);
651
652 //accept filters like filter.activity_date_time_high
653 // std is now 'filters' => ..
654 if (strstr(implode(',', array_keys($params)), 'filter')) {
655 if (isset($params['filters']) && is_array($params['filters'])) {
656 foreach ($params['filters'] as $paramkey => $paramvalue) {
657 _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao);
658 }
659 }
660 else {
661 foreach ($params as $paramkey => $paramvalue) {
662 if (strstr($paramkey, 'filter')) {
663 _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao);
664 }
665 }
666 }
667 }
668 if (!$fields) {
669 $fields = [];
670 }
671
672 foreach ($fields as $field) {
673 if (is_array($params[$field])) {
674 //get the actual fieldname from db
675 $fieldName = $allfields[$field]['name'];
676 $where = CRM_Core_DAO::createSqlFilter($fieldName, $params[$field], 'String');
677 if (!empty($where)) {
678 $dao->whereAdd($where);
679 }
680 }
681 else {
682 if ($unique) {
683 $daoFieldName = $allfields[$field]['name'];
684 if (empty($daoFieldName)) {
685 throw new API_Exception("Failed to determine field name for \"$field\"");
686 }
687 $dao->{$daoFieldName} = $params[$field];
688 }
689 else {
690 $dao->$field = $params[$field];
691 }
692 }
693 }
694 if (!empty($extraSql['where'])) {
695 foreach ($extraSql['where'] as $table => $sqlWhere) {
696 foreach ($sqlWhere as $where) {
697 $dao->whereAdd($where);
698 }
699 }
700 }
701 if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) {
702 $dao->selectAdd();
703 // Ensure 'id' is included.
704 $options['return']['id'] = TRUE;
705 $allfields = _civicrm_api3_get_unique_name_array($dao);
706 $returnMatched = array_intersect(array_keys($options['return']), $allfields);
707 foreach ($returnMatched as $returnValue) {
708 $dao->selectAdd($returnValue);
709 }
710
711 // Not already matched on the field names.
712 $unmatchedFields = array_diff(
713 array_keys($options['return']),
714 $returnMatched
715 );
716
717 $returnUniqueMatched = array_intersect(
718 $unmatchedFields,
719 // But a match for the field keys.
720 array_flip($allfields)
721 );
722 foreach ($returnUniqueMatched as $uniqueVal) {
723 $dao->selectAdd($allfields[$uniqueVal]);
724 }
725 }
726 $dao->setApiFilter($params);
727 }
728
729 /**
730 * Apply filters (e.g. high, low) to DAO object (prior to find).
731 *
732 * @param string $filterField
733 * Field name of filter.
734 * @param string $filterValue
735 * Field value of filter.
736 * @param object $dao
737 * DAO object.
738 */
739 function _civicrm_api3_apply_filters_to_dao($filterField, $filterValue, &$dao) {
740 if (strstr($filterField, 'high')) {
741 $fieldName = substr($filterField, 0, -5);
742 $dao->whereAdd("($fieldName <= $filterValue )");
743 }
744 if (strstr($filterField, 'low')) {
745 $fieldName = substr($filterField, 0, -4);
746 $dao->whereAdd("($fieldName >= $filterValue )");
747 }
748 if ($filterField == 'is_current' && $filterValue == 1) {
749 $todayStart = date('Ymd000000', strtotime('now'));
750 $todayEnd = date('Ymd235959', strtotime('now'));
751 $dao->whereAdd("(start_date <= '$todayStart' OR start_date IS NULL) AND (end_date >= '$todayEnd' OR end_date IS NULL)");
752 if (property_exists($dao, 'is_active')) {
753 $dao->whereAdd('is_active = 1');
754 }
755 }
756 }
757
758 /**
759 * Get sort, limit etc options from the params - supporting old & new formats.
760 *
761 * Get returnProperties for legacy
762 *
763 * @param array $params
764 * Params array as passed into civicrm_api.
765 * @param bool $queryObject
766 * Is this supporting a queryObject api (e.g contact) - if so we support more options.
767 * for legacy report & return a unique fields array
768 *
769 * @param string $entity
770 * @param string $action
771 *
772 * @throws API_Exception
773 * @return array
774 * options extracted from params
775 */
776 function _civicrm_api3_get_options_from_params($params, $queryObject = FALSE, $entity = '', $action = '') {
777 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
778 $is_count = FALSE;
779
780 // dear PHP thought it would be a good idea to transform a.b into a_b in the get/post
781 $sort = $params['option_sort'] ?? $params['option.sort'] ?? $params['sort'] ?? 0;
782 $offset = $params['option_offset'] ?? $params['option.offset'] ?? $params['offset'] ?? 0;
783
784 $limit = CRM_Utils_Array::value('rowCount', $params, 25);
785 $limit = CRM_Utils_Array::value('option.limit', $params, $limit);
786 $limit = CRM_Utils_Array::value('option_limit', $params, $limit);
787
788 if (isset($params['options']) && is_array($params['options'])) {
789 // is count is set by generic getcount not user
790 $is_count = $params['options']['is_count'] ?? FALSE;
791 $offset = $params['options']['offset'] ?? $offset;
792 $limit = CRM_Utils_Array::value('limit', $params['options'], $limit);
793 $sort = $params['options']['sort'] ?? $sort;
794 }
795
796 $returnProperties = [];
797 // handle the format return =sort_name,display_name...
798 if (array_key_exists('return', $params)) {
799 if (is_array($params['return'])) {
800 $returnProperties = array_fill_keys($params['return'], 1);
801 }
802 else {
803 $returnProperties = explode(',', str_replace(' ', '', $params['return']));
804 $returnProperties = array_fill_keys($returnProperties, 1);
805 }
806 }
807 if ($entity && $action == 'get') {
808 if (!empty($returnProperties['id'])) {
809 $returnProperties[$lowercase_entity . '_id'] = 1;
810 unset($returnProperties['id']);
811 }
812 }
813
814 $options = [
815 'offset' => CRM_Utils_Rule::integer($offset) ? $offset : NULL,
816 'limit' => (!$is_count && CRM_Utils_Rule::integer($limit)) ? $limit : NULL,
817 'is_count' => $is_count,
818 'return' => !empty($returnProperties) ? $returnProperties : [],
819 ];
820
821 $finalSort = [];
822 $options['sort'] = NULL;
823 if (!empty($sort)) {
824 if (!is_array($sort)) {
825 $sort = array_map('trim', explode(',', $sort));
826 }
827 foreach ($sort as $s) {
828 if ($s === '(1)' || CRM_Utils_Rule::mysqlOrderBy($s)) {
829 if ($entity && $action === 'get') {
830 switch (trim(strtolower($s))) {
831 case 'id':
832 case 'id desc':
833 case 'id asc':
834 $s = str_replace('id', $lowercase_entity . '_id', $s);
835 }
836 }
837 $finalSort[] = $s;
838 }
839 else {
840 throw new API_Exception("Unknown field specified for sort. Cannot order by '$s'");
841 }
842 }
843 $options['sort'] = implode(', ', $finalSort);
844 }
845
846 if ($options['sort'] && stristr($options['sort'], 'SELECT')) {
847 throw new API_Exception('invalid string in sort options');
848 }
849
850 if (!$queryObject) {
851 return $options;
852 }
853 //here comes the legacy support for $returnProperties, $inputParams e.g for contat_get
854 // if the query object is being used this should be used
855 $inputParams = [];
856 $legacyreturnProperties = [];
857 $otherVars = [
858 'sort', 'offset', 'rowCount', 'options', 'return',
859 'version', 'prettyprint', 'check_permissions', 'sequential',
860 ];
861 foreach ($params as $n => $v) {
862 if (substr($n, 0, 7) === 'return.') {
863 $legacyreturnProperties[substr($n, 7)] = $v;
864 }
865 elseif ($n === 'id') {
866 $inputParams[$lowercase_entity . '_id'] = $v;
867 }
868 elseif (!in_array($n, $otherVars)) {
869 $inputParams[$n] = $v;
870 if ($v && !is_array($v) && stristr($v, 'SELECT')) {
871 throw new API_Exception('invalid string');
872 }
873 }
874 }
875 $options['return'] = array_merge($returnProperties, $legacyreturnProperties);
876 $options['input_params'] = $inputParams;
877 return $options;
878 }
879
880 /**
881 * Apply options (e.g. sort, limit, order by) to DAO object (prior to find).
882 *
883 * @param array $params
884 * Params array as passed into civicrm_api.
885 * @param object $dao
886 * DAO object.
887 * @param $entity
888 *
889 * @throws \API_Exception
890 * @throws \CRM_Core_Exception
891 */
892 function _civicrm_api3_apply_options_to_dao(&$params, &$dao, $entity) {
893
894 $options = _civicrm_api3_get_options_from_params($params, FALSE, $entity);
895 if (!$options['is_count']) {
896 if (!empty($options['limit'])) {
897 $dao->limit((int) $options['offset'], (int) $options['limit']);
898 }
899 if (!empty($options['sort'])) {
900 $options['sort'] = CRM_Utils_Type::escape($options['sort'], 'MysqlOrderBy');
901 $dao->orderBy($options['sort']);
902 }
903 }
904 }
905
906 /**
907 * Build fields array.
908 *
909 * This is the array of fields as it relates to the given DAO
910 * returns unique fields as keys by default but if set but can return by DB fields
911 *
912 * @param CRM_Core_DAO $bao
913 * @param bool $unique
914 *
915 * @return array
916 */
917 function _civicrm_api3_build_fields_array(&$bao, $unique = TRUE) {
918 $fields = $bao->fields();
919 if ($unique) {
920 if (empty($fields['id'])) {
921 $lowercase_entity = _civicrm_api_get_entity_name_from_camel(_civicrm_api_get_entity_name_from_dao($bao));
922 if (isset($fields[$lowercase_entity . '_id'])) {
923 $fields['id'] = $fields[$lowercase_entity . '_id'];
924 unset($fields[$lowercase_entity . '_id']);
925 }
926 }
927 return $fields;
928 }
929
930 foreach ($fields as $field) {
931 $dbFields[$field['name']] = $field;
932 }
933 return $dbFields;
934 }
935
936 /**
937 * Build fields array.
938 *
939 * This is the array of fields as it relates to the given DAO
940 * returns unique fields as keys by default but if set but can return by DB fields
941 *
942 * @param CRM_Core_DAO $bao
943 *
944 * @return array
945 */
946 function _civicrm_api3_get_unique_name_array(&$bao) {
947 $fields = $bao->fields();
948 foreach ($fields as $field => $values) {
949 $uniqueFields[$field] = CRM_Utils_Array::value('name', $values, $field);
950 }
951 return $uniqueFields;
952 }
953
954 /**
955 * Converts an DAO object to an array.
956 *
957 * @param CRM_Core_DAO $dao
958 * Object to convert.
959 * @param array $params
960 * @param bool $uniqueFields
961 * @param string $entity
962 * @param bool $autoFind
963 *
964 * @return array
965 *
966 * @throws \API_Exception
967 *
968 * @deprecated - DAO based retrieval is being phased out.
969 *
970 */
971 function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, $entity = "", $autoFind = TRUE) {
972 $result = [];
973 if (isset($params['options']) && !empty($params['options']['is_count'])) {
974 return $dao->count();
975 }
976 if (empty($dao)) {
977 return [];
978 }
979 if ($autoFind && !$dao->find()) {
980 return [];
981 }
982
983 if (isset($dao->count)) {
984 return $dao->count;
985 }
986
987 $fields = array_keys(_civicrm_api3_build_fields_array($dao, FALSE));
988 while ($dao->fetch()) {
989 $tmp = [];
990 foreach ($fields as $key) {
991 if (array_key_exists($key, $dao)) {
992 // not sure on that one
993 if ($dao->$key !== NULL) {
994 $tmp[$key] = $dao->$key;
995 }
996 }
997 }
998 $result[$dao->id] = $tmp;
999
1000 if (_civicrm_api3_custom_fields_are_required($entity, $params)) {
1001 _civicrm_api3_custom_data_get($result[$dao->id], $params['check_permissions'], $entity, $dao->id);
1002 }
1003 }
1004
1005 return $result;
1006 }
1007
1008 /**
1009 * Determine if custom fields need to be retrieved.
1010 *
1011 * We currently retrieve all custom fields or none at this level so if we know the entity
1012 * && it can take custom fields & there is the string 'custom' in their return request we get them all, they are filtered on the way out
1013 *
1014 * @param string $entity
1015 * Entity name in CamelCase.
1016 * @param array $params
1017 *
1018 * @return bool
1019 * @throws \API_Exception
1020 *
1021 * @todo filter so only required fields are queried
1022 */
1023 function _civicrm_api3_custom_fields_are_required($entity, $params) {
1024 if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery::$extendsMap)) {
1025 return FALSE;
1026 }
1027 $options = _civicrm_api3_get_options_from_params($params);
1028 // We check for possibility of 'custom' => 1 as well as specific custom fields.
1029 $returnString = implode('', $options['return']) . implode('', array_keys($options['return']));
1030 if (stristr($returnString, 'custom')) {
1031 return TRUE;
1032 }
1033 }
1034
1035 /**
1036 * Converts an object to an array.
1037 *
1038 * @param object $dao
1039 * (reference) object to convert.
1040 * @param array $values
1041 * (reference) array.
1042 * @param array|bool $uniqueFields
1043 */
1044 function _civicrm_api3_object_to_array(&$dao, &$values, $uniqueFields = FALSE) {
1045
1046 $fields = _civicrm_api3_build_fields_array($dao, $uniqueFields);
1047 foreach ($fields as $key => $value) {
1048 if (array_key_exists($key, $dao)) {
1049 $values[$key] = $dao->$key;
1050 }
1051 }
1052 }
1053
1054 /**
1055 * Wrapper for _civicrm_object_to_array when api supports unique fields.
1056 *
1057 * @param $dao
1058 * @param $values
1059 *
1060 * @return array
1061 */
1062 function _civicrm_api3_object_to_array_unique_fields(&$dao, &$values) {
1063 return _civicrm_api3_object_to_array($dao, $values, TRUE);
1064 }
1065
1066 /**
1067 * Format custom parameters.
1068 *
1069 * @param array $params
1070 * @param array $values
1071 * @param string $extends
1072 * Entity that this custom field extends (e.g. contribution, event, contact).
1073 * @param string $entityId
1074 * ID of entity per $extends.
1075 */
1076 function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) {
1077 $values['custom'] = [];
1078 $checkCheckBoxField = FALSE;
1079 $entity = $extends;
1080 if (in_array($extends, ['Household', 'Individual', 'Organization'])) {
1081 $entity = 'Contact';
1082 }
1083
1084 $fields = civicrm_api($entity, 'getfields', ['version' => 3, 'action' => 'create']);
1085 if (!$fields['is_error']) {
1086 // not sure if fields could be error - maybe change to using civicrm_api3 wrapper later - this is conservative
1087 $fields = $fields['values'];
1088 $checkCheckBoxField = TRUE;
1089 }
1090
1091 foreach ($params as $key => $value) {
1092 list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField::getKeyID($key, TRUE);
1093 if ($customFieldID && (!is_null($value))) {
1094 if ($checkCheckBoxField && !empty($fields['custom_' . $customFieldID]) && $fields['custom_' . $customFieldID]['html_type'] == 'CheckBox') {
1095 formatCheckBoxField($value, 'custom_' . $customFieldID, $entity);
1096 }
1097
1098 CRM_Core_BAO_CustomField::formatCustomField($customFieldID, $values['custom'],
1099 $value, $extends, $customValueID, $entityId, FALSE, FALSE, TRUE
1100 );
1101 }
1102 }
1103 }
1104
1105 /**
1106 * Format parameters for create action.
1107 *
1108 * @param array $params
1109 * @param $entity
1110 */
1111 function _civicrm_api3_format_params_for_create(&$params, $entity) {
1112 $nonGenericEntities = ['Contact', 'Individual', 'Household', 'Organization'];
1113
1114 $customFieldEntities = array_diff_key(CRM_Core_SelectValues::customGroupExtends(), array_fill_keys($nonGenericEntities, 1));
1115 if (!array_key_exists($entity, $customFieldEntities)) {
1116 return;
1117 }
1118 $values = [];
1119 _civicrm_api3_custom_format_params($params, $values, $entity);
1120 $params = array_merge($params, $values);
1121 }
1122
1123 /**
1124 * We can't rely on downstream to add separators to checkboxes so we'll check here.
1125 *
1126 * We should look at pushing to BAO function
1127 * and / or validate function but this is a safe place for now as it has massive test coverage & we can keep the change very specific
1128 * note that this is specifically tested in the GRANT api test case so later refactoring should use that as a checking point
1129 *
1130 * We will only alter the value if we are sure that changing it will make it correct - if it appears wrong but does not appear to have a clear fix we
1131 * don't touch - lots of very cautious code in here
1132 *
1133 * The resulting array should look like
1134 * array(
1135 * 'key' => 1,
1136 * 'key1' => 1,
1137 * );
1138 *
1139 * OR one or more keys wrapped in a CRM_Core_DAO::VALUE_SEPARATOR - either it accepted by the receiving function
1140 *
1141 * @todo - we are probably skipping handling disabled options as presumably getoptions is not giving us them. This should be non-regressive but might
1142 * be fixed in future
1143 *
1144 * @param mixed $checkboxFieldValue
1145 * @param string $customFieldLabel
1146 * @param string $entity
1147 */
1148 function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) {
1149
1150 if (is_string($checkboxFieldValue) && stristr($checkboxFieldValue, CRM_Core_DAO::VALUE_SEPARATOR)) {
1151 // We can assume it's pre-formatted.
1152 return;
1153 }
1154 $options = civicrm_api($entity, 'getoptions', ['field' => $customFieldLabel, 'version' => 3]);
1155 if (!empty($options['is_error'])) {
1156 // The check is precautionary - can probably be removed later.
1157 return;
1158 }
1159
1160 $options = $options['values'];
1161 $validValue = TRUE;
1162 if (is_array($checkboxFieldValue)) {
1163 foreach ($checkboxFieldValue as $key => $value) {
1164 if (!array_key_exists($key, $options)) {
1165 $validValue = FALSE;
1166 }
1167 }
1168 if ($validValue) {
1169 // we have been passed an array that is already in the 'odd' custom field format
1170 return;
1171 }
1172 }
1173
1174 // so we either have an array that is not keyed by the value or we have a string that doesn't hold separators
1175 // if the array only has one item we'll treat it like any other string
1176 if (is_array($checkboxFieldValue) && count($checkboxFieldValue) == 1) {
1177 $possibleValue = reset($checkboxFieldValue);
1178 }
1179 if (is_string($checkboxFieldValue)) {
1180 $possibleValue = $checkboxFieldValue;
1181 }
1182 if (isset($possibleValue) && array_key_exists($possibleValue, $options)) {
1183 $checkboxFieldValue = CRM_Core_DAO::VALUE_SEPARATOR . $possibleValue . CRM_Core_DAO::VALUE_SEPARATOR;
1184 return;
1185 }
1186 elseif (is_array($checkboxFieldValue)) {
1187 // so this time around we are considering the values in the array
1188 $possibleValues = $checkboxFieldValue;
1189 $formatValue = TRUE;
1190 }
1191 elseif (stristr($checkboxFieldValue, ',')) {
1192 $formatValue = TRUE;
1193 //lets see if we should separate it - we do this near the end so we
1194 // ensure we have already checked that the comma is not part of a legitimate match
1195 // and of course, we don't make any changes if we don't now have matches
1196 $possibleValues = explode(',', $checkboxFieldValue);
1197 }
1198 else {
1199 // run out of ideas as to what the format might be - if it's a string it doesn't match with or without the ','
1200 return;
1201 }
1202
1203 foreach ($possibleValues as $index => $possibleValue) {
1204 if (array_key_exists($possibleValue, $options)) {
1205 // do nothing - we will leave formatValue set to true unless another value is not found (which would cause us to ignore the whole value set)
1206 }
1207 elseif (array_key_exists(trim($possibleValue), $options)) {
1208 $possibleValues[$index] = trim($possibleValue);
1209 }
1210 else {
1211 $formatValue = FALSE;
1212 }
1213 }
1214 if ($formatValue) {
1215 $checkboxFieldValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $possibleValues) . CRM_Core_DAO::VALUE_SEPARATOR;
1216 }
1217 }
1218
1219 /**
1220 * Function to do a 'standard' api get - when the api is only doing a $bao->find then use this.
1221 *
1222 * @param string $bao_name
1223 * Name of BAO.
1224 * @param array $params
1225 * Params from api.
1226 * @param bool $returnAsSuccess
1227 * Return in api success format.
1228 * @param string $entity
1229 * @param CRM_Utils_SQL_Select|NULL $sql
1230 * Extra SQL bits to add to the query. For filtering current events, this might be:
1231 * CRM_Utils_SQL_Select::fragment()->where('(start_date >= CURDATE() || end_date >= CURDATE())');
1232 * @param bool $uniqueFields
1233 * Should unique field names be returned (for backward compatibility)
1234 *
1235 * @return array
1236 */
1237 function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) {
1238 $entity = $entity ?: CRM_Core_DAO_AllCoreTables::getBriefName(str_replace('_BAO_', '_DAO_', $bao_name));
1239 $options = _civicrm_api3_get_options_from_params($params);
1240
1241 $query = new \Civi\API\Api3SelectQuery($entity, CRM_Utils_Array::value('check_permissions', $params, FALSE));
1242 $query->where = $params;
1243 if ($options['is_count']) {
1244 $query->select = ['count_rows'];
1245 }
1246 else {
1247 $query->select = array_keys(array_filter($options['return']));
1248 $query->orderBy = $options['sort'];
1249 $query->isFillUniqueFields = $uniqueFields;
1250 }
1251 $query->limit = $options['limit'];
1252 $query->offset = $options['offset'];
1253 $query->merge($sql);
1254 $result = $query->run();
1255
1256 if ($returnAsSuccess) {
1257 return civicrm_api3_create_success($result, $params, $entity, 'get');
1258 }
1259 return $result;
1260 }
1261
1262 /**
1263 * Function to do a 'standard' api create - when the api is only doing a $bao::create then use this.
1264 *
1265 * @param string $bao_name
1266 * Name of BAO Class.
1267 * @param array $params
1268 * Parameters passed into the api call.
1269 * @param string $entity
1270 * Entity - pass in if entity is non-standard & required $ids array.
1271 *
1272 * @throws API_Exception
1273 * @throws \Civi\API\Exception\UnauthorizedException
1274 * @return array
1275 */
1276 function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
1277 _civicrm_api3_check_edit_permissions($bao_name, $params);
1278 _civicrm_api3_format_params_for_create($params, $entity);
1279 $args = array(&$params);
1280 if ($entity) {
1281 $ids = [$entity => CRM_Utils_Array::value('id', $params)];
1282 $args[] = &$ids;
1283 }
1284
1285 if (method_exists($bao_name, 'create')) {
1286 $fct = 'create';
1287 $fct_name = $bao_name . '::' . $fct;
1288 $bao = call_user_func_array([$bao_name, $fct], $args);
1289 }
1290 elseif (method_exists($bao_name, 'add')) {
1291 $fct = 'add';
1292 $fct_name = $bao_name . '::' . $fct;
1293 $bao = call_user_func_array([$bao_name, $fct], $args);
1294 }
1295 else {
1296 $fct_name = '_civicrm_api3_basic_create_fallback';
1297 $bao = _civicrm_api3_basic_create_fallback($bao_name, $params);
1298 }
1299
1300 if (is_null($bao)) {
1301 return civicrm_api3_create_error('Entity not created (' . $fct_name . ')');
1302 }
1303 elseif (is_a($bao, 'CRM_Core_Error')) {
1304 //some weird circular thing means the error takes itself as an argument
1305 $msg = $bao->getMessages($bao);
1306 // the api deals with entities on a one-by-one basis. However, the contribution bao pushes entities
1307 // onto the error object - presumably because the contribution import is not handling multiple errors correctly
1308 // so we need to reset the error object here to avoid getting concatenated errors
1309 //@todo - the mulitple error handling should be moved out of the contribution object to the import / multiple entity processes
1310 CRM_Core_Error::singleton()->reset();
1311 throw new API_Exception($msg);
1312 }
1313 else {
1314 // If we have custom fields the BAO may have taken care of it or we may have to.
1315 // $extendsMap provides a pretty good hard-coded list of BAOs that take care of the custom data.
1316 if (isset($params['custom']) && empty(CRM_Core_BAO_CustomQuery::$extendsMap[$entity])) {
1317 CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id);
1318 }
1319 $values = [];
1320 _civicrm_api3_object_to_array($bao, $values[$bao->id]);
1321 return civicrm_api3_create_success($values, $params, $entity, 'create', $bao);
1322 }
1323 }
1324
1325 /**
1326 * For BAO's which don't have a create() or add() functions, use this fallback implementation.
1327 *
1328 * @param string|CRM_Core_DAO $bao_name
1329 * @param array $params
1330 *
1331 * @throws API_Exception
1332 *
1333 * @return CRM_Core_DAO|NULL
1334 * An instance of the BAO
1335 */
1336 function _civicrm_api3_basic_create_fallback($bao_name, $params) {
1337 return $bao_name::writeRecord($params);
1338 }
1339
1340 /**
1341 * Function to do a 'standard' api del.
1342 *
1343 * When the api is only doing a $bao::del then use this if api::del doesn't exist it will try DAO delete method.
1344 *
1345 * @param string $bao_name
1346 * @param array $params
1347 *
1348 * @return array
1349 * API result array
1350 *
1351 * @throws API_Exception
1352 * @throws \Civi\API\Exception\UnauthorizedException
1353 * @throws \CiviCRM_API3_Exception
1354 */
1355 function _civicrm_api3_basic_delete($bao_name, &$params) {
1356 civicrm_api3_verify_mandatory($params, NULL, ['id']);
1357 _civicrm_api3_check_edit_permissions($bao_name, ['id' => $params['id']]);
1358 $args = [&$params['id']];
1359 if (method_exists($bao_name, 'del')) {
1360 $dao = new $bao_name();
1361 $dao->id = $params['id'];
1362 if ($dao->find()) {
1363 $bao = call_user_func_array([$bao_name, 'del'], $args);
1364 if ($bao !== FALSE) {
1365 return civicrm_api3_create_success();
1366 }
1367 throw new API_Exception('Could not delete entity id ' . $params['id']);
1368 }
1369 throw new API_Exception('Could not delete entity id ' . $params['id']);
1370 }
1371 elseif (method_exists($bao_name, 'delete')) {
1372 $dao = new $bao_name();
1373 $dao->id = $params['id'];
1374 if ($dao->find()) {
1375 while ($dao->fetch()) {
1376 $dao->delete();
1377 return civicrm_api3_create_success();
1378 }
1379 }
1380 else {
1381 throw new API_Exception('Could not delete entity id ' . $params['id']);
1382 }
1383 }
1384
1385 throw new API_Exception('no delete method found');
1386 }
1387
1388 /**
1389 * Get custom data for the given entity & Add it to the returnArray.
1390 *
1391 * This looks like 'custom_123' = 'custom string' AND
1392 * 'custom_123_1' = 'custom string'
1393 * Where 123 is field value & 1 is the id within the custom group data table (value ID)
1394 *
1395 * @param array $returnArray
1396 * Array to append custom data too - generally $result[4] where 4 is the entity id.
1397 * @param $checkPermission
1398 * @param string $entity
1399 * E.g membership, event.
1400 * @param int $entity_id
1401 * @param int $groupID
1402 * Per CRM_Core_BAO_CustomGroup::getTree.
1403 * @param int $subType
1404 * E.g. membership_type_id where custom data doesn't apply to all membership types.
1405 * @param string $subName
1406 * Subtype of entity.
1407 *
1408 * @throws \CRM_Core_Exception
1409 */
1410 function _civicrm_api3_custom_data_get(&$returnArray, $checkPermission, $entity, $entity_id, $groupID = NULL, $subType = NULL, $subName = NULL) {
1411 $groupTree = CRM_Core_BAO_CustomGroup::getTree($entity,
1412 NULL,
1413 $entity_id,
1414 $groupID,
1415 NULL,
1416 $subName,
1417 TRUE,
1418 NULL,
1419 TRUE,
1420 $checkPermission
1421 );
1422 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1);
1423 $customValues = [];
1424 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $customValues);
1425 $fieldInfo = [];
1426 foreach ($groupTree as $set) {
1427 $fieldInfo += $set['fields'];
1428 }
1429 if (!empty($customValues)) {
1430 foreach ($customValues as $key => $val) {
1431 // per standard - return custom_fieldID
1432 $id = CRM_Core_BAO_CustomField::getKeyID($key);
1433 $returnArray['custom_' . $id] = $val;
1434
1435 //not standard - but some api did this so guess we should keep - cheap as chips
1436 $returnArray[$key] = $val;
1437
1438 // Shim to restore legacy behavior of ContactReference custom fields
1439 if (!empty($fieldInfo[$id]) && $fieldInfo[$id]['data_type'] === 'ContactReference') {
1440 $returnArray['custom_' . $id . '_id'] = $returnArray[$key . '_id'] = $val;
1441 $returnArray['custom_' . $id] = $returnArray[$key] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $val, 'sort_name');
1442 }
1443 }
1444 }
1445 }
1446
1447 /**
1448 * Used by the Validate API.
1449 *
1450 * @param string $entity
1451 * @param string $action
1452 * @param array $params
1453 *
1454 * @return array $errors
1455 * @throws \CiviCRM_API3_Exception
1456 */
1457 function _civicrm_api3_validate($entity, $action, $params) {
1458 $errors = [];
1459 $fields = civicrm_api3($entity, 'getfields', ['sequential' => 1, 'api_action' => $action]);
1460 $fields = $fields['values'];
1461
1462 // Check for required fields.
1463 foreach ($fields as $values) {
1464 if (!empty($values['api.required']) && empty($params[$values['name']])) {
1465 $errors[$values['name']] = [
1466 'message' => 'Mandatory key(s) missing from params array: ' . $values['name'],
1467 'code' => 'mandatory_missing',
1468 ];
1469 }
1470 }
1471
1472 // Select only the fields which have been input as a param.
1473 $finalfields = [];
1474 foreach ($fields as $values) {
1475 if (array_key_exists($values['name'], $params)) {
1476 $finalfields[] = $values;
1477 }
1478 }
1479
1480 // This derives heavily from the function "_civicrm_api3_validate_fields".
1481 // However, the difference is that try-catch blocks are nested in the loop, making it
1482 // possible for us to get all errors in one go.
1483 foreach ($finalfields as $fieldInfo) {
1484 $fieldName = $fieldInfo['name'];
1485 try {
1486 _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params);
1487 }
1488 catch (Exception $e) {
1489 $errors[$fieldName] = [
1490 'message' => $e->getMessage(),
1491 'code' => 'incorrect_value',
1492 ];
1493 }
1494 }
1495
1496 return [$errors];
1497 }
1498
1499 /**
1500 * Used by the Validate API.
1501 * @param $fieldName
1502 * @param array $fieldInfo
1503 * @param string $entity
1504 * @param array $params
1505 *
1506 * @throws API_Exception
1507 * @throws Exception
1508 */
1509 function _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params) {
1510 switch (CRM_Utils_Array::value('type', $fieldInfo)) {
1511 case CRM_Utils_Type::T_INT:
1512 _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
1513 break;
1514
1515 case CRM_Utils_Type::T_DATE:
1516 case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
1517 case CRM_Utils_Type::T_TIMESTAMP:
1518 //field is of type date or datetime
1519 _civicrm_api3_validate_date($params, $fieldName, $fieldInfo);
1520 break;
1521
1522 case CRM_Utils_Type::T_TEXT:
1523 case CRM_Utils_Type::T_STRING:
1524 _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity);
1525 break;
1526
1527 case CRM_Utils_Type::T_MONEY:
1528 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1529
1530 foreach ((array) $fieldValue as $fieldvalue) {
1531 if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
1532 throw new Exception($fieldName . ' is not a valid amount: ' . $params[$fieldName]);
1533 }
1534 }
1535 break;
1536 }
1537 }
1538
1539 /**
1540 * Validate fields being passed into API.
1541 *
1542 * This function relies on the getFields function working accurately
1543 * for the given API.
1544 *
1545 * As of writing only date was implemented.
1546 *
1547 * @param string $entity
1548 * @param string $action
1549 * @param array $params
1550 * -.
1551 * @param array $fields
1552 * Response from getfields all variables are the same as per civicrm_api.
1553 *
1554 * @throws Exception
1555 */
1556 function _civicrm_api3_validate_fields($entity, $action, &$params, $fields) {
1557 //CRM-15792 handle datetime for custom fields below code handles chain api call
1558 $chainApikeys = array_flip(preg_grep("/^api./", array_keys($params)));
1559 if (!empty($chainApikeys) && is_array($chainApikeys)) {
1560 foreach ($chainApikeys as $key => $value) {
1561 if (is_array($params[$key])) {
1562 $chainApiParams = array_intersect_key($fields, $params[$key]);
1563 $customFields = array_fill_keys(array_keys($params[$key]), $key);
1564 }
1565 }
1566 }
1567 $fields = array_intersect_key($fields, $params);
1568 if (!empty($chainApiParams)) {
1569 $fields = array_merge($fields, $chainApiParams);
1570 }
1571 foreach ($fields as $fieldName => $fieldInfo) {
1572 switch (CRM_Utils_Array::value('type', $fieldInfo)) {
1573 case CRM_Utils_Type::T_INT:
1574 //field is of type integer
1575 _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
1576 break;
1577
1578 case CRM_Utils_Type::T_DATE:
1579 case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
1580 case CRM_Utils_Type::T_TIMESTAMP:
1581 //field is of type date or datetime
1582 if (!empty($customFields) && array_key_exists($fieldName, $customFields)) {
1583 $dateParams = &$params[$customFields[$fieldName]];
1584 }
1585 else {
1586 $dateParams = &$params;
1587 }
1588 _civicrm_api3_validate_date($dateParams, $fieldName, $fieldInfo);
1589 break;
1590
1591 case CRM_Utils_Type::T_TEXT:
1592 case CRM_Utils_Type::T_STRING:
1593 _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity);
1594 break;
1595
1596 case CRM_Utils_Type::T_MONEY:
1597 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1598 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1599 break;
1600 }
1601 foreach ((array) $fieldValue as $fieldvalue) {
1602 if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) {
1603 throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]);
1604 }
1605 }
1606 break;
1607 }
1608 }
1609 }
1610
1611 /**
1612 * Validate foreign key values of fields being passed into API.
1613 *
1614 * This function relies on the getFields function working accurately
1615 * for the given API.
1616 *
1617 * @param string $entity
1618 * @param string $action
1619 * @param array $params
1620 *
1621 * @param array $fields
1622 * Response from getfields all variables are the same as per civicrm_api.
1623 *
1624 * @throws Exception
1625 */
1626 function _civicrm_api3_validate_foreign_keys($entity, $action, &$params, $fields) {
1627 // intensive checks - usually only called after DB level fail
1628 foreach ($fields as $fieldName => $fieldInfo) {
1629 if (!empty($fieldInfo['FKClassName'])) {
1630 if (!empty($params[$fieldName])) {
1631 foreach ((array) $params[$fieldName] as $fieldValue) {
1632 _civicrm_api3_validate_constraint($fieldValue, $fieldName, $fieldInfo, $entity);
1633 }
1634 }
1635 elseif (!empty($fieldInfo['required'])) {
1636 throw new Exception("DB Constraint Violation - $fieldName should possibly be marked as mandatory for $entity,$action API. If so, please raise a bug report.");
1637 }
1638 }
1639 if (!empty($fieldInfo['api.unique'])) {
1640 $params['entity'] = $entity;
1641 _civicrm_api3_validate_unique_key($params, $fieldName);
1642 }
1643 }
1644 }
1645
1646 /**
1647 * Validate date fields being passed into API.
1648 *
1649 * It currently converts both unique fields and DB field names to a mysql date.
1650 * @todo - probably the unique field handling & the if exists handling is now done before this
1651 * function is reached in the wrapper - can reduce this code down to assume we
1652 * are only checking the passed in field
1653 *
1654 * It also checks against the RULE:date function. This is a centralisation of code that was scattered and
1655 * may not be the best thing to do. There is no code level documentation on the existing functions to work off
1656 *
1657 * @param array $params
1658 * Params from civicrm_api.
1659 * @param string $fieldName
1660 * Uniquename of field being checked.
1661 * @param array $fieldInfo
1662 * Array of fields from getfields function.
1663 *
1664 * @throws Exception
1665 */
1666 function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) {
1667 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1668 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1669 return;
1670 }
1671
1672 if ($fieldValue === 'null' && empty($fieldInfo['api.required'])) {
1673 // This is the wierd & wonderful way PEAR sets null.
1674 return;
1675 }
1676
1677 //should we check first to prevent it from being copied if they have passed in sql friendly format?
1678 if (!empty($params[$fieldInfo['name']])) {
1679 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldInfo['name'], $fieldInfo['type']);
1680 }
1681 if ((CRM_Utils_Array::value('name', $fieldInfo) != $fieldName) && !empty($fieldValue)) {
1682 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldName, $fieldInfo['type']);
1683 }
1684
1685 if (!empty($op)) {
1686 $params[$fieldName][$op] = $fieldValue;
1687 }
1688 else {
1689 $params[$fieldName] = $fieldValue;
1690 }
1691 }
1692
1693 /**
1694 * Convert date into BAO friendly date.
1695 *
1696 * We accept 'whatever strtotime accepts'
1697 *
1698 * @param string $dateValue
1699 * @param string $fieldName
1700 * @param $fieldType
1701 *
1702 * @throws Exception
1703 * @return mixed
1704 */
1705 function _civicrm_api3_getValidDate($dateValue, $fieldName, $fieldType) {
1706 if (is_array($dateValue)) {
1707 foreach ($dateValue as $key => $value) {
1708 $dateValue[$key] = _civicrm_api3_getValidDate($value, $fieldName, $fieldType);
1709 }
1710 return $dateValue;
1711 }
1712 if (strtotime($dateValue) === FALSE) {
1713 throw new Exception($fieldName . " is not a valid date: " . $dateValue);
1714 }
1715 $format = ($fieldType == CRM_Utils_Type::T_DATE) ? 'Ymd000000' : 'YmdHis';
1716 return CRM_Utils_Date::processDate($dateValue, NULL, FALSE, $format);
1717 }
1718
1719 /**
1720 * Validate foreign constraint fields being passed into API.
1721 *
1722 * @param mixed $fieldValue
1723 * @param string $fieldName
1724 * Unique name of field being checked.
1725 * @param array $fieldInfo
1726 * Array of fields from getfields function.
1727 * @param string $entity
1728 *
1729 * @throws \API_Exception
1730 */
1731 function _civicrm_api3_validate_constraint($fieldValue, $fieldName, $fieldInfo, $entity) {
1732 $daoName = $fieldInfo['FKClassName'];
1733 $fieldInfo = [$fieldName => $fieldInfo];
1734 $params = [$fieldName => $fieldValue];
1735 _civicrm_api3_validate_fields($entity, NULL, $params, $fieldInfo);
1736 /* @var CRM_Core_DAO $dao*/
1737 $dao = new $daoName();
1738 $dao->id = $params[$fieldName];
1739 $dao->selectAdd();
1740 $dao->selectAdd('id');
1741 if (!$dao->find()) {
1742 throw new API_Exception("$fieldName is not valid : " . $fieldValue);
1743 }
1744 }
1745
1746 /**
1747 * Validate foreign constraint fields being passed into API.
1748 *
1749 * @param array $params
1750 * Params from civicrm_api.
1751 * @param string $fieldName
1752 * Uniquename of field being checked.
1753 *
1754 * @throws Exception
1755 */
1756 function _civicrm_api3_validate_unique_key(&$params, &$fieldName) {
1757 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1758 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
1759 return;
1760 }
1761 $existing = civicrm_api($params['entity'], 'get', [
1762 'version' => $params['version'],
1763 $fieldName => $fieldValue,
1764 ]);
1765 // an entry already exists for this unique field
1766 if ($existing['count'] == 1) {
1767 // question - could this ever be a security issue?
1768 throw new API_Exception("Field: `$fieldName` must be unique. An conflicting entity already exists - id: " . $existing['id']);
1769 }
1770 }
1771
1772 /**
1773 * Generic implementation of the "replace" action.
1774 *
1775 * Replace the old set of entities (matching some given keys) with a new set of
1776 * entities (matching the same keys).
1777 *
1778 * @note This will verify that 'values' is present, but it does not directly verify
1779 * any other parameters.
1780 *
1781 * @param string $entity
1782 * Entity name.
1783 * @param array $params
1784 * Params from civicrm_api, including:.
1785 * - 'values': an array of records to save
1786 * - all other items: keys which identify new/pre-existing records.
1787 *
1788 * @return array|int
1789 */
1790 function _civicrm_api3_generic_replace($entity, $params) {
1791
1792 $transaction = new CRM_Core_Transaction();
1793 try {
1794 if (!is_array($params['values'])) {
1795 throw new Exception("Mandatory key(s) missing from params array: values");
1796 }
1797
1798 // Extract the keys -- somewhat scary, don't think too hard about it
1799 $baseParams = _civicrm_api3_generic_replace_base_params($params);
1800
1801 // Lookup pre-existing records
1802 $preexisting = civicrm_api($entity, 'get', $baseParams, $params);
1803 if (civicrm_error($preexisting)) {
1804 $transaction->rollback();
1805 return $preexisting;
1806 }
1807
1808 // Save the new/updated records
1809 $creates = [];
1810 foreach ($params['values'] as $replacement) {
1811 // Sugar: Don't force clients to duplicate the 'key' data
1812 $replacement = array_merge($baseParams, $replacement);
1813 $action = (isset($replacement['id']) || isset($replacement[$entity . '_id'])) ? 'update' : 'create';
1814 $create = civicrm_api($entity, $action, $replacement);
1815 if (civicrm_error($create)) {
1816 $transaction->rollback();
1817 return $create;
1818 }
1819 foreach ($create['values'] as $entity_id => $entity_value) {
1820 $creates[$entity_id] = $entity_value;
1821 }
1822 }
1823
1824 // Remove stale records
1825 $staleIDs = array_diff(
1826 array_keys($preexisting['values']),
1827 array_keys($creates)
1828 );
1829 foreach ($staleIDs as $staleID) {
1830 $delete = civicrm_api($entity, 'delete', [
1831 'version' => $params['version'],
1832 'id' => $staleID,
1833 ]);
1834 if (civicrm_error($delete)) {
1835 $transaction->rollback();
1836 return $delete;
1837 }
1838 }
1839
1840 return civicrm_api3_create_success($creates, $params);
1841 }
1842 catch (PEAR_Exception $e) {
1843 $transaction->rollback();
1844 return civicrm_api3_create_error($e->getMessage());
1845 }
1846 catch (Exception $e) {
1847 $transaction->rollback();
1848 return civicrm_api3_create_error($e->getMessage());
1849 }
1850 }
1851
1852 /**
1853 * Replace base parameters.
1854 *
1855 * @param array $params
1856 *
1857 * @return array
1858 */
1859 function _civicrm_api3_generic_replace_base_params($params) {
1860 $baseParams = $params;
1861 unset($baseParams['values']);
1862 unset($baseParams['sequential']);
1863 unset($baseParams['options']);
1864 return $baseParams;
1865 }
1866
1867 /**
1868 * Returns fields allowable by api.
1869 *
1870 * @param $entity
1871 * String Entity to query.
1872 * @param bool $unique
1873 * Index by unique fields?.
1874 * @param array $params
1875 *
1876 * @return array
1877 */
1878 function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = []) {
1879 $unsetIfEmpty = [
1880 'dataPattern',
1881 'headerPattern',
1882 'default',
1883 'export',
1884 'import',
1885 ];
1886 $dao = _civicrm_api3_get_DAO($entity);
1887 if (empty($dao)) {
1888 return [];
1889 }
1890 $d = new $dao();
1891 $fields = $d->fields();
1892
1893 foreach ($fields as $name => &$field) {
1894 // Denote as core field
1895 $field['is_core_field'] = TRUE;
1896 // Set html attributes for text fields
1897 if (isset($field['html'])) {
1898 $field['html'] += (array) $d::makeAttribute($field);
1899 }
1900 }
1901
1902 // replace uniqueNames by the normal names as the key
1903 if (empty($unique)) {
1904 foreach ($fields as $name => &$field) {
1905 //getting rid of unused attributes
1906 foreach ($unsetIfEmpty as $attr) {
1907 if (empty($field[$attr])) {
1908 unset($field[$attr]);
1909 }
1910 }
1911 if ($name == $field['name']) {
1912 continue;
1913 }
1914 if (array_key_exists($field['name'], $fields)) {
1915 $field['error'] = 'name conflict';
1916 // it should never happen, but better safe than sorry
1917 continue;
1918 }
1919 $fields[$field['name']] = $field;
1920 $fields[$field['name']]['uniqueName'] = $name;
1921 unset($fields[$name]);
1922 }
1923 }
1924 // Translate FKClassName to the corresponding api
1925 foreach ($fields as $name => &$field) {
1926 if (!empty($field['FKClassName'])) {
1927 $FKApi = CRM_Core_DAO_AllCoreTables::getBriefName($field['FKClassName']);
1928 if ($FKApi) {
1929 $field['FKApiName'] = $FKApi;
1930 }
1931 }
1932 }
1933 $fields += _civicrm_api_get_custom_fields($entity, $params);
1934 return $fields;
1935 }
1936
1937 /**
1938 * Return an array of fields for a given entity.
1939 *
1940 * This is the same as the BAO function but fields are prefixed with 'custom_' to represent api params.
1941 *
1942 * @param $entity
1943 * @param array $params
1944 *
1945 * @return array
1946 */
1947 function _civicrm_api_get_custom_fields($entity, &$params) {
1948 $entity = _civicrm_api_get_camel_name($entity);
1949 if ($entity == 'Contact') {
1950 // Use sub-type if available, otherwise "NULL" to fetch from all contact types
1951 $entity = $params['contact_type'] ?? NULL;
1952 }
1953 $customfields = CRM_Core_BAO_CustomField::getFields($entity,
1954 FALSE,
1955 FALSE,
1956 // we could / should probably test for other subtypes here - e.g. activity_type_id
1957 CRM_Utils_Array::value('contact_sub_type', $params),
1958 NULL,
1959 FALSE,
1960 FALSE,
1961 FALSE
1962 );
1963
1964 $ret = [];
1965
1966 foreach ($customfields as $key => $value) {
1967 // Regular fields have a 'name' property
1968 $value['name'] = 'custom_' . $key;
1969 $value['title'] = $value['label'];
1970 if ($value['data_type'] == 'Date' && CRM_Utils_Array::value('time_format', $value, 0) > 0) {
1971 $value['data_type'] = 'DateTime';
1972 }
1973 $value['type'] = CRM_Utils_Array::value($value['data_type'], CRM_Core_BAO_CustomField::dataToType());
1974 $ret['custom_' . $key] = $value;
1975 }
1976 return $ret;
1977 }
1978
1979 /**
1980 * Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't.
1981 *
1982 * If multiple aliases the last takes precedence
1983 *
1984 * Function also swaps unique fields for non-unique fields & vice versa.
1985 *
1986 * @param $apiRequest
1987 * @param $fields
1988 */
1989 function _civicrm_api3_swap_out_aliases(&$apiRequest, $fields) {
1990 foreach ($fields as $field => $values) {
1991 $uniqueName = $values['uniqueName'] ?? NULL;
1992 if (!empty($values['api.aliases'])) {
1993 // if aliased field is not set we try to use field alias
1994 if (!isset($apiRequest['params'][$field])) {
1995 foreach ($values['api.aliases'] as $alias) {
1996 if (isset($apiRequest['params'][$alias])) {
1997 $apiRequest['params'][$field] = $apiRequest['params'][$alias];
1998 }
1999 //unset original field nb - need to be careful with this as it may bring inconsistencies
2000 // out of the woodwork but will be implementing only as _spec function extended
2001 unset($apiRequest['params'][$alias]);
2002 }
2003 }
2004 }
2005 if (!isset($apiRequest['params'][$field]) && !empty($values['name']) && $field != $values['name']
2006 && isset($apiRequest['params'][$values['name']])
2007 ) {
2008 $apiRequest['params'][$field] = $apiRequest['params'][$values['name']];
2009 // note that it would make sense to unset the original field here but tests need to be in place first
2010 if ($field != 'domain_version') {
2011 unset($apiRequest['params'][$values['name']]);
2012 }
2013 }
2014 if (!isset($apiRequest['params'][$field])
2015 && $uniqueName
2016 && $field != $uniqueName
2017 && array_key_exists($uniqueName, $apiRequest['params'])
2018 ) {
2019 $apiRequest['params'][$field] = $apiRequest['params'][$values['uniqueName']] ?? NULL;
2020 // note that it would make sense to unset the original field here but tests need to be in place first
2021 }
2022 }
2023
2024 }
2025
2026 /**
2027 * Validate integer fields being passed into API.
2028 *
2029 * It currently converts the incoming value 'user_contact_id' into the id of the currently logged in user.
2030 *
2031 * @param array $params
2032 * Params from civicrm_api.
2033 * @param string $fieldName
2034 * Uniquename of field being checked.
2035 * @param array $fieldInfo
2036 * Array of fields from getfields function.
2037 * @param string $entity
2038 *
2039 * @throws API_Exception
2040 */
2041 function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $entity) {
2042 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2043 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) {
2044 return;
2045 }
2046
2047 if (!empty($fieldValue) || $fieldValue === '0' || $fieldValue === 0) {
2048 // if value = 'user_contact_id' (or similar), replace value with contact id
2049 if (!is_numeric($fieldValue) && is_scalar($fieldValue)) {
2050 $realContactId = _civicrm_api3_resolve_contactID($fieldValue);
2051 if ('unknown-user' === $realContactId) {
2052 throw new API_Exception("\"$fieldName\" \"{$fieldValue}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldName, "type" => "integer"]);
2053 }
2054 elseif (is_numeric($realContactId)) {
2055 $fieldValue = $realContactId;
2056 }
2057 elseif (is_null($realContactId) && empty($fieldInfo['api.required']) && $fieldValue === 'user_contact_id') {
2058 // If not mandatory this will be OK. If mandatory it should fail.
2059 $fieldValue = NULL;
2060 }
2061 }
2062 if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
2063 $additional_lookup_params = [];
2064 if (strtolower($entity) == 'address' && $fieldName == 'state_province_id') {
2065 $country_id = _civicrm_api3_resolve_country_id($params);
2066 if (!empty($country_id)) {
2067 $additional_lookup_params = ['country_id' => $country_id];
2068 }
2069 }
2070 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op, $additional_lookup_params);
2071 }
2072
2073 // After swapping options, ensure we have an integer(s)
2074 foreach ((array) ($fieldValue) as $value) {
2075 if ($value && !is_numeric($value) && $value !== 'null' && $value !== NULL && !is_array($value)) {
2076 throw new API_Exception("$fieldName is not a valid integer", 2001, ['error_field' => $fieldName, "type" => "integer"]);
2077 }
2078 }
2079
2080 // Check our field length
2081 if (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen($fieldValue) > $fieldInfo['maxlength']
2082 ) {
2083 throw new API_Exception($fieldValue . " is " . strlen($fieldValue) . " characters - longer than $fieldName length" . $fieldInfo['maxlength'] . ' characters',
2084 2100, ['field' => $fieldName, "max_length" => $fieldInfo['maxlength']]
2085 );
2086 }
2087 }
2088
2089 if (!empty($op)) {
2090 $params[$fieldName][$op] = $fieldValue;
2091 }
2092 else {
2093 $params[$fieldName] = $fieldValue;
2094 }
2095 }
2096
2097 /**
2098 * Helper function to determine country_id given the myriad of values for country_id or country that are supported
2099 * @param $params
2100 *
2101 * @return int|null
2102 */
2103 function _civicrm_api3_resolve_country_id($params) {
2104 if (!empty($params['country_id'])) {
2105 if (is_numeric($params['country_id'])) {
2106 $country_id = $params['country_id'];
2107 }
2108 else {
2109 $country = new CRM_Core_DAO_Country();
2110 $country->name = $params['country_id'];
2111 if (!$country->find(TRUE)) {
2112 $country->name = NULL;
2113 $country->iso_code = $params['country_id'];
2114 $country->find(TRUE);
2115 }
2116 if (!empty($country->id)) {
2117 $country_id = $country->id;
2118 }
2119 }
2120 }
2121 elseif (!empty($params['country'])) {
2122 if (is_numeric($params['country'])) {
2123 $country_id = $params['country'];
2124 }
2125 else {
2126 $country = new CRM_Core_DAO_Country();
2127 $country->name = $params['country'];
2128 if (!$country->find(TRUE)) {
2129 $country->name = NULL;
2130 $country->iso_code = $params['country'];
2131 $country->find(TRUE);
2132 }
2133 if (!empty($country->id)) {
2134 $country_id = $country->id;
2135 }
2136 }
2137 }
2138 return !empty($country_id) ? $country_id : NULL;
2139 }
2140
2141 /**
2142 * Determine a contact ID using a string expression.
2143 *
2144 * @param string $contactIdExpr
2145 * E.g. "user_contact_id" or "@user:username".
2146 *
2147 * @return int|null|'unknown-user'
2148 * @throws \CRM_Core_Exception
2149 */
2150 function _civicrm_api3_resolve_contactID($contactIdExpr) {
2151 // If value = 'user_contact_id' replace value with logged in user id.
2152 if ($contactIdExpr == "user_contact_id") {
2153 return CRM_Core_Session::getLoggedInContactID();
2154 }
2155 elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) {
2156 $config = CRM_Core_Config::singleton();
2157
2158 $ufID = $config->userSystem->getUfId($matches[1]);
2159 if (!$ufID) {
2160 return 'unknown-user';
2161 }
2162
2163 $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID);
2164 if (!$contactID) {
2165 return 'unknown-user';
2166 }
2167
2168 return $contactID;
2169 }
2170 return NULL;
2171 }
2172
2173 /**
2174 * Validate html (check for scripting attack).
2175 *
2176 * @param array $params
2177 * @param string $fieldName
2178 * @param array $fieldInfo
2179 *
2180 * @throws API_Exception
2181 */
2182 function _civicrm_api3_validate_html(&$params, &$fieldName, $fieldInfo) {
2183 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2184 if (strpos($op, 'NULL') || strpos($op, 'EMPTY')) {
2185 return;
2186 }
2187 if ($fieldValue) {
2188 if (!CRM_Utils_Rule::xssString($fieldValue)) {
2189 throw new API_Exception('Input contains illegal SCRIPT tag.', ["field" => $fieldName, "error_code" => "xss"]);
2190 }
2191 }
2192 }
2193
2194 /**
2195 * Validate string fields being passed into API.
2196 *
2197 * @param array $params
2198 * Params from civicrm_api.
2199 * @param string $fieldName
2200 * Uniquename of field being checked.
2201 * @param array $fieldInfo
2202 * Array of fields from getfields function.
2203 * @param string $entity
2204 *
2205 * @throws API_Exception
2206 * @throws Exception
2207 */
2208 function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $entity) {
2209 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName, 'String');
2210 if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE || CRM_Utils_System::isNull($fieldValue)) {
2211 return;
2212 }
2213
2214 if (!is_array($fieldValue)) {
2215 $fieldValue = (string) $fieldValue;
2216 }
2217
2218 if ($fieldValue) {
2219 foreach ((array) $fieldValue as $key => $value) {
2220 foreach ([$fieldValue, $key, $value] as $input) {
2221 if (!CRM_Utils_Rule::xssString($input)) {
2222 throw new Exception('Input contains illegal SCRIPT tag.');
2223 }
2224 }
2225 if ($fieldName == 'currency') {
2226 //When using IN operator $fieldValue is a array of currency codes
2227 if (!CRM_Utils_Rule::currencyCode($value)) {
2228 throw new Exception("Currency not a valid code: $currency");
2229 }
2230 }
2231 }
2232 }
2233 if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
2234 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op);
2235 }
2236 // Check our field length
2237 elseif (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen(utf8_decode($fieldValue)) > $fieldInfo['maxlength']) {
2238 throw new API_Exception("Value for $fieldName is " . strlen(utf8_decode($value)) . " characters - This field has a maxlength of {$fieldInfo['maxlength']} characters.",
2239 2100, ['field' => $fieldName]
2240 );
2241 }
2242
2243 if (!empty($op)) {
2244 $params[$fieldName][$op] = $fieldValue;
2245 }
2246 else {
2247 $params[$fieldName] = $fieldValue;
2248 }
2249 }
2250
2251 /**
2252 * Validate & swap out any pseudoconstants / options.
2253 *
2254 * @param mixed $fieldValue
2255 * @param string $entity : api entity name
2256 * @param string $fieldName : field name used in api call (not necessarily the canonical name)
2257 * @param array $fieldInfo : getfields meta-data
2258 * @param string $op
2259 * @param array $additional_lookup_params
2260 *
2261 * @throws \API_Exception
2262 */
2263 function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo, $op = '=', $additional_lookup_params = []) {
2264 if (in_array($op, ['>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'])) {
2265 return;
2266 }
2267
2268 $options = $fieldInfo['options'] ?? NULL;
2269
2270 if (!$options) {
2271 if (strtolower($entity) == 'profile' && !empty($fieldInfo['entity'])) {
2272 // We need to get the options from the entity the field relates to.
2273 $entity = $fieldInfo['entity'];
2274 }
2275 $options_lookup_params = [
2276 'version' => 3,
2277 'field' => $fieldInfo['name'],
2278 'context' => 'validate',
2279 ];
2280 if (!empty($additional_lookup_params)) {
2281 $options_lookup_params = array_merge($additional_lookup_params, $options_lookup_params);
2282 }
2283 $options = civicrm_api($entity, 'getoptions', $options_lookup_params);
2284
2285 $options = CRM_Utils_Array::value('values', $options, []);
2286 }
2287
2288 // If passed a value-separated string, explode to an array, then re-implode after matching values.
2289 $implode = FALSE;
2290 if (is_string($fieldValue) && strpos($fieldValue, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
2291 $fieldValue = CRM_Utils_Array::explodePadded($fieldValue);
2292 $implode = TRUE;
2293 }
2294 // If passed multiple options, validate each.
2295 if (is_array($fieldValue)) {
2296 foreach ($fieldValue as &$value) {
2297 if (!is_array($value)) {
2298 _civicrm_api3_api_match_pseudoconstant_value($value, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
2299 }
2300 }
2301 // TODO: unwrap the call to implodePadded from the conditional and do it always
2302 // need to verify that this is safe and doesn't break anything though.
2303 // Better yet would be to leave it as an array and ensure that every dao/bao can handle array input
2304 if ($implode) {
2305 CRM_Utils_Array::implodePadded($fieldValue);
2306 }
2307 }
2308 else {
2309 _civicrm_api3_api_match_pseudoconstant_value($fieldValue, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
2310 }
2311 }
2312
2313 /**
2314 * Validate & swap a single option value for a field.
2315 *
2316 * @param string $value field value
2317 * @param array $options array of options for this field
2318 * @param string $fieldName field name used in api call (not necessarily the canonical name)
2319 * @param bool $isRequired
2320 * Is this a required field or is 'null' an acceptable option. We allow 'null' last
2321 * in case we have the weird situation of it being a valid option in which case our
2322 * brains will probably explode.
2323 *
2324 * @throws API_Exception
2325 */
2326 function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName, $isRequired) {
2327 // If option is a key, no need to translate
2328 // or if no options are avaiable for pseudoconstant 'table' property
2329 if (array_key_exists($value, $options) || !$options) {
2330 return;
2331 }
2332
2333 // Hack for Profile formatting fields
2334 if ($fieldName === 'field_name' && (strpos($value, 'formatting') === 0)) {
2335 return;
2336 }
2337
2338 // Translate value into key
2339 // Cast $value to string to avoid a bug in array_search
2340 $newValue = array_search((string) $value, $options);
2341 if ($newValue !== FALSE) {
2342 $value = $newValue;
2343 return;
2344 }
2345 // Case-insensitive matching
2346 $newValue = strtolower($value);
2347 $options = array_map("strtolower", $options);
2348 $newValue = array_search($newValue, $options);
2349 if ($newValue === FALSE) {
2350 if ($value === 'null' && !$isRequired) {
2351 // CiviMagic syntax for Nulling out the field - let it through.
2352 return;
2353 }
2354 throw new API_Exception("'$value' is not a valid option for field $fieldName", 2001, ['error_field' => $fieldName]);
2355 }
2356 $value = $newValue;
2357 }
2358
2359 /**
2360 * Returns the canonical name of a field.
2361 *
2362 * @param $entity
2363 * api entity name (string should already be standardized - no camelCase).
2364 * @param $fieldName
2365 * any variation of a field's name (name, unique_name, api.alias).
2366 *
2367 * @param string $action
2368 *
2369 * @return bool|string
2370 * FieldName or FALSE if the field does not exist
2371 */
2372 function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create') {
2373 if (!$fieldName) {
2374 return FALSE;
2375 }
2376 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
2377 return $fieldName;
2378 }
2379 if ($fieldName == _civicrm_api_get_entity_name_from_camel($entity) . '_id') {
2380 return 'id';
2381 }
2382 $result = civicrm_api($entity, 'getfields', [
2383 'version' => 3,
2384 'action' => $action,
2385 ]);
2386 $meta = $result['values'];
2387 if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
2388 $fieldName = $fieldName . '_id';
2389 }
2390 if (isset($meta[$fieldName])) {
2391 return $meta[$fieldName]['name'];
2392 }
2393 foreach ($meta as $info) {
2394 if ($fieldName == $info['name'] || $fieldName == CRM_Utils_Array::value('uniqueName', $info)) {
2395 return $info['name'];
2396 }
2397 if (array_search($fieldName, CRM_Utils_Array::value('api.aliases', $info, [])) !== FALSE) {
2398 return $info['name'];
2399 }
2400 }
2401 // Create didn't work, try with get
2402 if ($action == 'create') {
2403 return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get');
2404 }
2405 return FALSE;
2406 }
2407
2408 /**
2409 * Check if the function is deprecated.
2410 *
2411 * @param string $entity
2412 * @param array $result
2413 *
2414 * @return string|array|null
2415 */
2416 function _civicrm_api3_deprecation_check($entity, $result = []) {
2417 if ($entity) {
2418 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
2419 $fnName = "_civicrm_api3_{$lowercase_entity}_deprecation";
2420 if (function_exists($fnName)) {
2421 return $fnName($result);
2422 }
2423 }
2424 }
2425
2426 /**
2427 * Get the actual field value.
2428 *
2429 * In some case $params[$fieldName] holds Array value in this format Array([operator] => [value])
2430 * So this function returns the actual field value.
2431 *
2432 * @param array $params
2433 * @param string $fieldName
2434 * @param string $type
2435 *
2436 * @return mixed
2437 */
2438 function _civicrm_api3_field_value_check(&$params, $fieldName, $type = NULL) {
2439 $fieldValue = $params[$fieldName] ?? NULL;
2440 $op = NULL;
2441
2442 if (!empty($fieldValue) && is_array($fieldValue) &&
2443 (array_search(key($fieldValue), CRM_Core_DAO::acceptedSQLOperators()) ||
2444 $type == 'String' && strstr(key($fieldValue), 'EMPTY'))
2445 ) {
2446 $op = key($fieldValue);
2447 $fieldValue = $fieldValue[$op] ?? NULL;
2448 }
2449 return [$fieldValue, $op];
2450 }
2451
2452 /**
2453 * A generic "get" API based on simple array data. This is comparable to
2454 * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for
2455 * small/mid-size data loaded from external JSON or XML documents.
2456 *
2457 * @param $entity
2458 * @param array $params
2459 * API parameters.
2460 * @param array $records
2461 * List of all records.
2462 * @param string $idCol
2463 * The property which defines the ID of a record
2464 * @param array $filterableFields
2465 * List of filterable fields.
2466 *
2467 * @return array
2468 * @throws \API_Exception
2469 */
2470 function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $filterableFields) {
2471 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
2472 // TODO // $sort = CRM_Utils_Array::value('sort', $options, NULL);
2473 $offset = $options['offset'] ?? NULL;
2474 $limit = $options['limit'] ?? NULL;
2475
2476 $matches = [];
2477
2478 $currentOffset = 0;
2479 foreach ($records as $record) {
2480 if ($idCol != 'id') {
2481 $record['id'] = $record[$idCol];
2482 }
2483 $match = TRUE;
2484 foreach ($params as $k => $v) {
2485 if ($k == 'id') {
2486 $k = $idCol;
2487 }
2488 if (in_array($k, $filterableFields) && $record[$k] != $v) {
2489 $match = FALSE;
2490 break;
2491 }
2492 }
2493 if ($match) {
2494 if ($currentOffset >= $offset) {
2495 $matches[$record[$idCol]] = $record;
2496 }
2497 if ($limit && count($matches) >= $limit) {
2498 break;
2499 }
2500 $currentOffset++;
2501 }
2502 }
2503
2504 $return = CRM_Utils_Array::value('return', $options, []);
2505 if (!empty($return)) {
2506 $return['id'] = 1;
2507 $matches = CRM_Utils_Array::filterColumns($matches, array_keys($return));
2508 }
2509
2510 return civicrm_api3_create_success($matches, $params);
2511 }
2512
2513 /**
2514 * @param string $bao_name
2515 * @param array $params
2516 * @throws \Civi\API\Exception\UnauthorizedException
2517 */
2518 function _civicrm_api3_check_edit_permissions($bao_name, $params) {
2519 // For lack of something more clever, here's a whitelist of entities whos permissions
2520 // are inherited from a contact record.
2521 // Note, when adding here, also remember to modify _civicrm_api3_permissions()
2522 $contactEntities = [
2523 'CRM_Core_BAO_Email',
2524 'CRM_Core_BAO_Phone',
2525 'CRM_Core_BAO_Address',
2526 'CRM_Core_BAO_IM',
2527 'CRM_Core_BAO_Website',
2528 'CRM_Core_BAO_OpenID',
2529 ];
2530 if (!empty($params['check_permissions']) && in_array($bao_name, $contactEntities)) {
2531 $cid = !empty($params['contact_id']) ? $params['contact_id'] : CRM_Core_DAO::getFieldValue($bao_name, $params['id'], 'contact_id');
2532 if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) {
2533 throw new \Civi\API\Exception\UnauthorizedException('Permission denied to modify contact record');
2534 }
2535 }
2536 }
2537
2538 /**
2539 * Check if an entity has been modified since the last known modified_date
2540 *
2541 * @param string $modifiedDate Last knowm modified_date
2542 * @param int $id Id of record to check
2543 * @param string $entity API Entity
2544 *
2545 * @return bool
2546 * @throws \CiviCRM_API3_Exception
2547 */
2548 function _civicrm_api3_compare_timestamps($modifiedDate, $id, $entity) {
2549 $currentDbInfo = civicrm_api3($entity, 'getsingle', ['id' => $id]);
2550 if (strtotime($currentDbInfo['modified_date']) <= strtotime($modifiedDate)) {
2551 return TRUE;
2552 }
2553 return FALSE;
2554 }