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