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