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