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