3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * CiviCRM APIv3 utility functions.
31 * @package CiviCRM_APIv3
35 * Initialize CiviCRM - should be run at the start of each API function.
37 function _civicrm_api3_initialize() {
38 require_once 'CRM/Core/ClassLoader.php';
39 CRM_Core_ClassLoader
::singleton()->register();
40 CRM_Core_Config
::singleton();
44 * Wrapper Function for civicrm_verify_mandatory to make it simple to pass either / or fields for checking.
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.
53 function civicrm_api3_verify_one_mandatory($params, $daoName = NULL, $keyoptions = array()) {
54 $keys = array(array());
55 foreach ($keyoptions as $key) {
58 civicrm_api3_verify_mandatory($params, $daoName, $keys);
62 * Check mandatory fields are included.
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).
69 * List of required fields. A value can be an array denoting that either this or that is required.
70 * @param bool $verifyDAO
72 * @throws \API_Exception
74 function civicrm_api3_verify_mandatory($params, $daoName = NULL, $keys = array(), $verifyDAO = TRUE) {
77 if ($daoName != NULL && $verifyDAO && empty($params['id'])) {
78 $unmatched = _civicrm_api3_check_required_fields($params, $daoName, TRUE);
79 if (!is_array($unmatched)) {
84 if (!empty($params['id'])) {
85 $keys = array('version');
88 if (!in_array('version', $keys)) {
89 // required from v3 onwards
93 foreach ($keys as $key) {
97 foreach ($key as $subkey) {
98 if (!array_key_exists($subkey, $params) ||
empty($params[$subkey])) {
99 $optionset[] = $subkey;
102 // as long as there is one match then we don't need to rtn anything
106 if (empty($match) && !empty($optionset)) {
107 $unmatched[] = "one of (" . implode(", ", $optionset) . ")";
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')) {
118 if (!empty($unmatched)) {
119 throw new API_Exception("Mandatory key(s) missing from params array: " . implode(", ", $unmatched), "mandatory_missing", array("fields" => $unmatched));
124 * Create error array.
131 function civicrm_api3_create_error($msg, $data = array()) {
132 $data['is_error'] = 1;
133 $data['error_message'] = $msg;
135 // we will show sql to privileged user only (not sure of a specific
136 // security hole here but seems sensible - perhaps should apply to the trace as well?)
137 if (isset($data['sql']) && (CRM_Core_Permission
::check('Administer CiviCRM') || CIVICRM_UF
== 'UnitTests')) {
138 // Isn't this redundant?
139 $data['debug_information'] = $data['sql'];
148 * Format array in result output style.
150 * @param array|int $values values generated by API operation (the result)
151 * @param array $params
152 * Parameters passed into API call.
153 * @param string $entity
154 * The entity being acted on.
155 * @param string $action
156 * The action passed to the API.
158 * DAO object to be freed here.
159 * @param array $extraReturnValues
160 * Additional values to be added to top level of result array(.
161 * - this param is currently used for legacy behaviour support
165 function civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL, &$dao = NULL, $extraReturnValues = array()) {
167 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
168 // TODO: This shouldn't be necessary but this fn sometimes gets called with lowercase entity
169 $entity = _civicrm_api_get_camel_name($entity);
170 $result['is_error'] = 0;
171 //lets set the ['id'] field if it's not set & we know what the entity is
172 if (is_array($values) && $entity && $action != 'getfields') {
173 foreach ($values as $key => $item) {
174 if (empty($item['id']) && !empty($item[$lowercase_entity . "_id"])) {
175 $values[$key]['id'] = $item[$lowercase_entity . "_id"];
177 if (!empty($item['financial_type_id'])) {
178 //4.3 legacy handling
179 $values[$key]['contribution_type_id'] = $item['financial_type_id'];
181 if (!empty($item['next_sched_contribution_date'])) {
182 // 4.4 legacy handling
183 $values[$key]['next_sched_contribution'] = $item['next_sched_contribution_date'];
188 if (is_array($params) && !empty($params['debug'])) {
189 if (is_string($action) && $action != 'getfields') {
190 $apiFields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => $action) +
$params);
192 elseif ($action != 'getfields') {
193 $apiFields = civicrm_api($entity, 'getfields', array('version' => 3) +
$params);
199 $allFields = array();
200 if ($action != 'getfields' && is_array($apiFields) && is_array(CRM_Utils_Array
::value('values', $apiFields))) {
201 $allFields = array_keys($apiFields['values']);
203 $paramFields = array_keys($params);
204 $undefined = array_diff($paramFields, $allFields, array_keys($_COOKIE), array(
223 $result['undefined_fields'] = array_merge($undefined);
226 if (is_object($dao)) {
230 $result['version'] = 3;
231 if (is_array($values)) {
232 $result['count'] = (int) count($values);
234 // Convert value-separated strings to array
235 if ($action != 'getfields') {
236 _civicrm_api3_separate_values($values);
239 if ($result['count'] == 1) {
240 list($result['id']) = array_keys($values);
242 elseif (!empty($values['id']) && is_int($values['id'])) {
243 $result['id'] = $values['id'];
247 $result['count'] = !empty($values) ?
1 : 0;
250 if (is_array($values) && isset($params['sequential']) &&
251 $params['sequential'] == 1
253 $result['values'] = array_values($values);
256 $result['values'] = $values;
258 if (!empty($params['options']['metadata'])) {
259 // We've made metadata an array but only supporting 'fields' atm.
260 if (in_array('fields', (array) $params['options']['metadata']) && $action !== 'getfields') {
261 $fields = civicrm_api3($entity, 'getfields', array(
262 'action' => substr($action, 0, 3) == 'get' ?
'get' : 'create',
264 $result['metadata']['fields'] = $fields['values'];
267 // Report deprecations.
268 $deprecated = _civicrm_api3_deprecation_check($entity, $result);
269 // Always report "setvalue" action as deprecated.
270 if (!is_string($deprecated) && ($action == 'getactions' ||
$action == 'setvalue')) {
271 $deprecated = ((array) $deprecated) +
array('setvalue' => 'The "setvalue" action is deprecated. Use "create" with an id instead.');
273 // Always report "update" action as deprecated.
274 if (!is_string($deprecated) && ($action == 'getactions' ||
$action == 'update')) {
275 $deprecated = ((array) $deprecated) +
array('update' => 'The "update" action is deprecated. Use "create" with an id instead.');
278 // Metadata-level deprecations or wholesale entity deprecations.
279 if ($entity == 'Entity' ||
$action == 'getactions' ||
is_string($deprecated)) {
280 $result['deprecated'] = $deprecated;
282 // Action-specific deprecations
283 elseif (!empty($deprecated[$action])) {
284 $result['deprecated'] = $deprecated[$action];
287 return array_merge($result, $extraReturnValues);
291 * Load the DAO of the entity.
297 function _civicrm_api3_load_DAO($entity) {
298 $dao = _civicrm_api3_get_DAO($entity);
307 * Return the DAO of the function or Entity.
309 * @param string $name
310 * Either a function of the api (civicrm_{entity}_create or the entity name.
311 * return the DAO name to manipulate this function
312 * eg. "civicrm_api3_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
314 * @return mixed|string
316 function _civicrm_api3_get_DAO($name) {
317 if (strpos($name, 'civicrm_api3') !== FALSE) {
318 $last = strrpos($name, '_');
319 // len ('civicrm_api3_') == 13
320 $name = substr($name, 13, $last - 13);
323 $name = _civicrm_api_get_camel_name($name);
325 if ($name == 'Individual' ||
$name == 'Household' ||
$name == 'Organization') {
329 // hack to deal with incorrectly named BAO/DAO - see CRM-10859
331 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingEventQueue
332 if ($name == 'MailingEventQueue') {
333 return 'CRM_Mailing_Event_DAO_Queue';
335 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingRecipients
336 // but am not confident mailing_recipients is tested so have not tackled.
337 if ($name == 'MailingRecipients') {
338 return 'CRM_Mailing_DAO_Recipients';
340 // FIXME: DAO should be renamed CRM_Mailing_DAO_MailingComponent
341 if ($name == 'MailingComponent') {
342 return 'CRM_Mailing_DAO_Component';
344 // FIXME: DAO should be renamed CRM_ACL_DAO_AclRole
345 if ($name == 'AclRole') {
346 return 'CRM_ACL_DAO_EntityRole';
348 // FIXME: DAO should be renamed CRM_SMS_DAO_SmsProvider
349 // But this would impact SMS extensions so need to coordinate
350 // Probably best approach is to migrate them to use the api and decouple them from core BAOs
351 if ($name == 'SmsProvider') {
352 return 'CRM_SMS_DAO_Provider';
354 // FIXME: DAO names should follow CamelCase convention
355 if ($name == 'Im' ||
$name == 'Acl') {
356 $name = strtoupper($name);
358 $dao = CRM_Core_DAO_AllCoreTables
::getFullName($name);
359 if ($dao ||
!$name) {
363 // Really weird apis can declare their own DAO name. Not sure if this is a good idea...
364 if (file_exists("api/v3/$name.php")) {
365 include_once "api/v3/$name.php";
368 $daoFn = "_civicrm_api3_" . _civicrm_api_get_entity_name_from_camel($name) . "_DAO";
369 if (function_exists($daoFn)) {
377 * Return the BAO name of the function or Entity.
379 * @param string $name
380 * Is either a function of the api (civicrm_{entity}_create or the entity name.
381 * return the DAO name to manipulate this function
382 * eg. "civicrm_contact_create" or "Contact" will return "CRM_Contact_BAO_Contact"
386 function _civicrm_api3_get_BAO($name) {
387 // FIXME: DAO should be renamed CRM_Badge_DAO_BadgeLayout
388 if ($name == 'PrintLabel') {
389 return 'CRM_Badge_BAO_Layout';
391 $dao = _civicrm_api3_get_DAO($name);
395 $bao = str_replace("DAO", "BAO", $dao);
396 $file = strtr($bao, '_', '/') . '.php';
397 // Check if this entity actually has a BAO. Fall back on the DAO if not.
398 return stream_resolve_include_path($file) ?
$bao : $dao;
402 * Recursive function to explode value-separated strings into arrays.
406 function _civicrm_api3_separate_values(&$values) {
407 $sp = CRM_Core_DAO
::VALUE_SEPARATOR
;
408 foreach ($values as $key => & $value) {
409 if (is_array($value)) {
410 _civicrm_api3_separate_values($value);
412 elseif (is_string($value)) {
413 // This is to honor the way case API was originally written.
414 if ($key == 'case_type_id') {
415 $value = trim(str_replace($sp, ',', $value), ',');
417 elseif (strpos($value, $sp) !== FALSE) {
418 $value = explode($sp, trim($value, $sp));
425 * This is a legacy wrapper for api_store_values.
427 * It checks suitable fields using getfields rather than DAO->fields.
429 * Getfields has handling for how to deal with unique names which dao->fields doesn't
431 * Note this is used by BAO type create functions - eg. contribution
433 * @param string $entity
434 * @param array $params
435 * @param array $values
437 function _civicrm_api3_filter_fields_for_bao($entity, &$params, &$values) {
438 $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'create'));
439 $fields = $fields['values'];
440 _civicrm_api3_store_values($fields, $params, $values);
445 * @param array $fields
446 * @param array $params
447 * @param array $values
451 function _civicrm_api3_store_values(&$fields, &$params, &$values) {
454 $keys = array_intersect_key($params, $fields);
455 foreach ($keys as $name => $value) {
456 if ($name !== 'id') {
457 $values[$name] = $value;
465 * Returns field names of the given entity fields.
467 * @param array $fields
468 * Fields array to retrieve the field names for.
471 function _civicrm_api3_field_names($fields) {
473 foreach ($fields as $key => $value) {
474 if (!empty($value['name'])) {
475 $result[] = $value['name'];
482 * Returns an array with database information for the custom fields of an
485 * Something similar might already exist in CiviCRM. But I was not
488 * @param string $entity
491 * an array that maps the custom field ID's to table name and
495 * 'table_name' => 'table_name_1',
496 * 'column_name' => 'column_name_1',
497 * 'data_type' => 'data_type_1',
501 function _civicrm_api3_custom_fields_for_entity($entity) {
505 SELECT f.id, f.label, f.data_type,
506 f.html_type, f.is_search_range,
507 f.option_group_id, f.custom_group_id,
508 f.column_name, g.table_name,
509 f.date_format,f.time_format
510 FROM civicrm_custom_field f
511 JOIN civicrm_custom_group g ON f.custom_group_id = g.id
512 WHERE g.is_active = 1
517 '1' => array($entity, 'String'),
520 $dao = CRM_Core_DAO
::executeQuery($query, $params);
521 while ($dao->fetch()) {
522 $result[$dao->id
] = array(
523 'table_name' => $dao->table_name
,
524 'column_name' => $dao->column_name
,
525 'data_type' => $dao->data_type
,
534 * Get function for query object api.
536 * The API supports 2 types of get request. The more complex uses the BAO query object.
537 * This is a generic function for those functions that call it
539 * At the moment only called by contact we should extend to contribution &
540 * others that use the query object. Note that this function passes permission information in.
543 * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the
547 * @param array $params
548 * As passed into api get or getcount function.
549 * @param array $additional_options
550 * Array of options (so we can modify the filter).
551 * @param bool $getCount
552 * Are we just after the count.
554 * This basically correlates to the component.
555 * @param null|array $defaultReturnProperties
556 * Default return properties for the entity
557 * (used if return not set - but don't do that - set return!).
560 * @throws API_Exception
562 function _civicrm_api3_get_using_query_object($entity, $params, $additional_options = array(), $getCount = NULL, $mode = 1, $defaultReturnProperties = NULL) {
563 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
564 // Convert id to e.g. contact_id
565 if (empty($params[$lowercase_entity . '_id']) && isset($params['id'])) {
566 $params[$lowercase_entity . '_id'] = $params['id'];
568 unset($params['id']);
570 $options = _civicrm_api3_get_options_from_params($params, TRUE);
572 $inputParams = array_merge(
573 CRM_Utils_Array
::value('input_params', $options, array()),
574 CRM_Utils_Array
::value('input_params', $additional_options, array())
576 $returnProperties = array_merge(
577 CRM_Utils_Array
::value('return', $options, array()),
578 CRM_Utils_Array
::value('return', $additional_options, array())
580 if (empty($returnProperties)) {
581 $returnProperties = $defaultReturnProperties;
583 if (!empty($params['check_permissions'])) {
584 // we will filter query object against getfields
585 $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'get'));
586 // we need to add this in as earlier in this function 'id' was unset in favour of $entity_id
587 $fields['values'][$lowercase_entity . '_id'] = array();
588 $varsToFilter = array('returnProperties', 'inputParams');
589 foreach ($varsToFilter as $varToFilter) {
590 if (!is_array($
$varToFilter)) {
593 //I was going to throw an exception rather than silently filter out - but
594 //would need to diff out of exceptions arr other keys like 'options', 'return', 'api. etcetc
595 //so we are silently ignoring parts of their request
596 //$exceptionsArr = array_diff(array_keys($$varToFilter), array_keys($fields['values']));
597 $
$varToFilter = array_intersect_key($
$varToFilter, $fields['values']);
600 $options = array_merge($options, $additional_options);
601 $sort = CRM_Utils_Array
::value('sort', $options, NULL);
602 $offset = CRM_Utils_Array
::value('offset', $options, NULL);
603 $limit = CRM_Utils_Array
::value('limit', $options, NULL);
604 $smartGroupCache = CRM_Utils_Array
::value('smartGroupCache', $params);
608 $returnProperties = NULL;
611 if (substr($sort, 0, 2) == 'id') {
612 $sort = $lowercase_entity . "_" . $sort;
615 $newParams = CRM_Contact_BAO_Query
::convertFormValues($inputParams);
617 $skipPermissions = !empty($params['check_permissions']) ?
0 : 1;
619 list($entities) = CRM_Contact_BAO_Query
::apiQuery(
636 * Get dao query object based on input params.
638 * Ideally this would be merged with _civicrm_get_using_query_object but we need to resolve differences in what the
641 * @param array $params
642 * @param string $mode
643 * @param string $entity
646 * [CRM_Core_DAO|CRM_Contact_BAO_Query]
648 function _civicrm_api3_get_query_object($params, $mode, $entity) {
649 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
650 $sort = CRM_Utils_Array
::value('sort', $options, NULL);
651 $offset = CRM_Utils_Array
::value('offset', $options);
652 $rowCount = CRM_Utils_Array
::value('limit', $options);
653 $inputParams = CRM_Utils_Array
::value('input_params', $options, array());
654 $returnProperties = CRM_Utils_Array
::value('return', $options, NULL);
655 if (empty($returnProperties)) {
656 $returnProperties = CRM_Contribute_BAO_Query
::defaultReturnProperties($mode);
659 $newParams = CRM_Contact_BAO_Query
::convertFormValues($inputParams, 0, FALSE, $entity);
660 $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL,
662 empty($params['check_permissions'])
664 list($select, $from, $where, $having) = $query->query();
666 $sql = "$select $from $where $having";
669 $sql .= " ORDER BY $sort ";
671 if (!empty($rowCount)) {
672 $sql .= " LIMIT $offset, $rowCount ";
674 $dao = CRM_Core_DAO
::executeQuery($sql);
675 return array($dao, $query);
679 * Function transfers the filters being passed into the DAO onto the params object.
681 * @deprecated DAO based retrieval is being phased out.
683 * @param CRM_Core_DAO $dao
684 * @param array $params
685 * @param bool $unique
686 * @param array $extraSql
687 * API specific queries eg for event isCurrent would be converted to
688 * $extraSql['where'] = array('civicrm_event' => array('(start_date >= CURDATE() || end_date >= CURDATE())'));
690 * @throws API_Exception
693 function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $extraSql = array()) {
694 $entity = _civicrm_api_get_entity_name_from_dao($dao);
695 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
696 if (!empty($params[$lowercase_entity . "_id"]) && empty($params['id'])) {
697 //if entity_id is set then treat it as ID (will be overridden by id if set)
698 $params['id'] = $params[$lowercase_entity . "_id"];
700 $allfields = _civicrm_api3_build_fields_array($dao, $unique);
701 $fields = array_intersect(array_keys($allfields), array_keys($params));
703 $options = _civicrm_api3_get_options_from_params($params);
704 //apply options like sort
705 _civicrm_api3_apply_options_to_dao($params, $dao, $entity);
707 //accept filters like filter.activity_date_time_high
708 // std is now 'filters' => ..
709 if (strstr(implode(',', array_keys($params)), 'filter')) {
710 if (isset($params['filters']) && is_array($params['filters'])) {
711 foreach ($params['filters'] as $paramkey => $paramvalue) {
712 _civicrm_api3_apply_filters_to_dao($paramkey, $paramvalue, $dao);
716 foreach ($params as $paramkey => $paramvalue) {
717 if (strstr($paramkey, 'filter')) {
718 _civicrm_api3_apply_filters_to_dao(substr($paramkey, 7), $paramvalue, $dao);
727 foreach ($fields as $field) {
728 if (is_array($params[$field])) {
729 //get the actual fieldname from db
730 $fieldName = $allfields[$field]['name'];
731 $where = CRM_Core_DAO
::createSqlFilter($fieldName, $params[$field], 'String');
732 if (!empty($where)) {
733 $dao->whereAdd($where);
738 $daoFieldName = $allfields[$field]['name'];
739 if (empty($daoFieldName)) {
740 throw new API_Exception("Failed to determine field name for \"$field\"");
742 $dao->{$daoFieldName} = $params[$field];
745 $dao->$field = $params[$field];
749 if (!empty($extraSql['where'])) {
750 foreach ($extraSql['where'] as $table => $sqlWhere) {
751 foreach ($sqlWhere as $where) {
752 $dao->whereAdd($where);
756 if (!empty($options['return']) && is_array($options['return']) && empty($options['is_count'])) {
758 // Ensure 'id' is included.
759 $options['return']['id'] = TRUE;
760 $allfields = _civicrm_api3_get_unique_name_array($dao);
761 $returnMatched = array_intersect(array_keys($options['return']), $allfields);
762 foreach ($returnMatched as $returnValue) {
763 $dao->selectAdd($returnValue);
766 // Not already matched on the field names.
767 $unmatchedFields = array_diff(
768 array_keys($options['return']),
772 $returnUniqueMatched = array_intersect(
774 // But a match for the field keys.
775 array_flip($allfields)
777 foreach ($returnUniqueMatched as $uniqueVal) {
778 $dao->selectAdd($allfields[$uniqueVal]);
781 $dao->setApiFilter($params);
785 * Apply filters (e.g. high, low) to DAO object (prior to find).
787 * @param string $filterField
788 * Field name of filter.
789 * @param string $filterValue
790 * Field value of filter.
794 function _civicrm_api3_apply_filters_to_dao($filterField, $filterValue, &$dao) {
795 if (strstr($filterField, 'high')) {
796 $fieldName = substr($filterField, 0, -5);
797 $dao->whereAdd("($fieldName <= $filterValue )");
799 if (strstr($filterField, 'low')) {
800 $fieldName = substr($filterField, 0, -4);
801 $dao->whereAdd("($fieldName >= $filterValue )");
803 if ($filterField == 'is_current' && $filterValue == 1) {
804 $todayStart = date('Ymd000000', strtotime('now'));
805 $todayEnd = date('Ymd235959', strtotime('now'));
806 $dao->whereAdd("(start_date <= '$todayStart' OR start_date IS NULL) AND (end_date >= '$todayEnd' OR end_date IS NULL)");
807 if (property_exists($dao, 'is_active')) {
808 $dao->whereAdd('is_active = 1');
814 * Get sort, limit etc options from the params - supporting old & new formats.
816 * Get returnProperties for legacy
818 * @param array $params
819 * Params array as passed into civicrm_api.
820 * @param bool $queryObject
821 * Is this supporting a queryObject api (e.g contact) - if so we support more options.
822 * for legacy report & return a unique fields array
824 * @param string $entity
825 * @param string $action
827 * @throws API_Exception
829 * options extracted from params
831 function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $entity = '', $action = '') {
832 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
834 $sort = CRM_Utils_Array
::value('sort', $params, 0);
835 $sort = CRM_Utils_Array
::value('option.sort', $params, $sort);
836 $sort = CRM_Utils_Array
::value('option_sort', $params, $sort);
838 $offset = CRM_Utils_Array
::value('offset', $params, 0);
839 $offset = CRM_Utils_Array
::value('option.offset', $params, $offset);
840 // dear PHP thought it would be a good idea to transform a.b into a_b in the get/post
841 $offset = CRM_Utils_Array
::value('option_offset', $params, $offset);
843 $limit = CRM_Utils_Array
::value('rowCount', $params, 25);
844 $limit = CRM_Utils_Array
::value('option.limit', $params, $limit);
845 $limit = CRM_Utils_Array
::value('option_limit', $params, $limit);
847 if (is_array(CRM_Utils_Array
::value('options', $params))) {
848 // is count is set by generic getcount not user
849 $is_count = CRM_Utils_Array
::value('is_count', $params['options']);
850 $offset = CRM_Utils_Array
::value('offset', $params['options'], $offset);
851 $limit = CRM_Utils_Array
::value('limit', $params['options'], $limit);
852 $sort = CRM_Utils_Array
::value('sort', $params['options'], $sort);
855 $returnProperties = array();
856 // handle the format return =sort_name,display_name...
857 if (array_key_exists('return', $params)) {
858 if (is_array($params['return'])) {
859 $returnProperties = array_fill_keys($params['return'], 1);
862 $returnProperties = explode(',', str_replace(' ', '', $params['return']));
863 $returnProperties = array_fill_keys($returnProperties, 1);
866 if ($entity && $action == 'get') {
867 if (!empty($returnProperties['id'])) {
868 $returnProperties[$lowercase_entity . '_id'] = 1;
869 unset($returnProperties['id']);
871 switch (trim(strtolower($sort))) {
875 $sort = str_replace('id', $lowercase_entity . '_id', $sort);
880 'offset' => CRM_Utils_Rule
::integer($offset) ?
$offset : NULL,
881 'sort' => CRM_Utils_Rule
::string($sort) ?
$sort : NULL,
882 'limit' => CRM_Utils_Rule
::integer($limit) ?
$limit : NULL,
883 'is_count' => $is_count,
884 'return' => !empty($returnProperties) ?
$returnProperties : array(),
887 if ($options['sort'] && stristr($options['sort'], 'SELECT')) {
888 throw new API_Exception('invalid string in sort options');
894 //here comes the legacy support for $returnProperties, $inputParams e.g for contat_get
895 // if the query object is being used this should be used
896 $inputParams = array();
897 $legacyreturnProperties = array();
899 'sort', 'offset', 'rowCount', 'options', 'return',
900 'version', 'prettyprint', 'check_permissions', 'sequential',
902 foreach ($params as $n => $v) {
903 if (substr($n, 0, 7) == 'return.') {
904 $legacyreturnProperties[substr($n, 7)] = $v;
906 elseif ($n == 'id') {
907 $inputParams[$lowercase_entity . '_id'] = $v;
909 elseif (in_array($n, $otherVars)) {
912 $inputParams[$n] = $v;
913 if ($v && !is_array($v) && stristr($v, 'SELECT')) {
914 throw new API_Exception('invalid string');
918 $options['return'] = array_merge($returnProperties, $legacyreturnProperties);
919 $options['input_params'] = $inputParams;
924 * Apply options (e.g. sort, limit, order by) to DAO object (prior to find).
926 * @param array $params
927 * Params array as passed into civicrm_api.
932 function _civicrm_api3_apply_options_to_dao(&$params, &$dao, $entity) {
934 $options = _civicrm_api3_get_options_from_params($params, FALSE, $entity);
935 if (!$options['is_count']) {
936 if (!empty($options['limit'])) {
937 $dao->limit((int) $options['offset'], (int) $options['limit']);
939 if (!empty($options['sort'])) {
940 $dao->orderBy($options['sort']);
946 * Build fields array.
948 * This is the array of fields as it relates to the given DAO
949 * returns unique fields as keys by default but if set but can return by DB fields
951 * @param CRM_Core_DAO $bao
952 * @param bool $unique
956 function _civicrm_api3_build_fields_array(&$bao, $unique = TRUE) {
957 $fields = $bao->fields();
959 if (empty($fields['id'])) {
960 $lowercase_entity = _civicrm_api_get_entity_name_from_camel(_civicrm_api_get_entity_name_from_dao($bao));
961 $fields['id'] = $fields[$lowercase_entity . '_id'];
962 unset($fields[$lowercase_entity . '_id']);
967 foreach ($fields as $field) {
968 $dbFields[$field['name']] = $field;
974 * Build fields array.
976 * This is the array of fields as it relates to the given DAO
977 * returns unique fields as keys by default but if set but can return by DB fields
979 * @param CRM_Core_DAO $bao
983 function _civicrm_api3_get_unique_name_array(&$bao) {
984 $fields = $bao->fields();
985 foreach ($fields as $field => $values) {
986 $uniqueFields[$field] = CRM_Utils_Array
::value('name', $values, $field);
988 return $uniqueFields;
992 * Converts an DAO object to an array.
994 * @deprecated - DAO based retrieval is being phased out.
996 * @param CRM_Core_DAO $dao
998 * @param array $params
999 * @param bool $uniqueFields
1000 * @param string $entity
1001 * @param bool $autoFind
1005 function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE, $entity = "", $autoFind = TRUE) {
1007 if (isset($params['options']) && !empty($params['options']['is_count'])) {
1008 return $dao->count();
1013 if ($autoFind && !$dao->find()) {
1017 if (isset($dao->count
)) {
1021 $fields = array_keys(_civicrm_api3_build_fields_array($dao, FALSE));
1022 while ($dao->fetch()) {
1024 foreach ($fields as $key) {
1025 if (array_key_exists($key, $dao)) {
1026 // not sure on that one
1027 if ($dao->$key !== NULL) {
1028 $tmp[$key] = $dao->$key;
1032 $result[$dao->id
] = $tmp;
1034 if (_civicrm_api3_custom_fields_are_required($entity, $params)) {
1035 _civicrm_api3_custom_data_get($result[$dao->id
], $params['check_permissions'], $entity, $dao->id
);
1043 * Determine if custom fields need to be retrieved.
1045 * We currently retrieve all custom fields or none at this level so if we know the entity
1046 * && 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
1047 * @todo filter so only required fields are queried
1049 * @param string $entity
1050 * Entity name in CamelCase.
1051 * @param array $params
1055 function _civicrm_api3_custom_fields_are_required($entity, $params) {
1056 if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery
::$extendsMap)) {
1059 $options = _civicrm_api3_get_options_from_params($params);
1060 // We check for possibility of 'custom' => 1 as well as specific custom fields.
1061 $returnString = implode('', $options['return']) . implode('', array_keys($options['return']));
1062 if (stristr($returnString, 'custom')) {
1068 * Converts an object to an array.
1070 * @param object $dao
1071 * (reference) object to convert.
1072 * @param array $values
1073 * (reference) array.
1074 * @param array|bool $uniqueFields
1076 function _civicrm_api3_object_to_array(&$dao, &$values, $uniqueFields = FALSE) {
1078 $fields = _civicrm_api3_build_fields_array($dao, $uniqueFields);
1079 foreach ($fields as $key => $value) {
1080 if (array_key_exists($key, $dao)) {
1081 $values[$key] = $dao->$key;
1087 * Wrapper for _civicrm_object_to_array when api supports unique fields.
1094 function _civicrm_api3_object_to_array_unique_fields(&$dao, &$values) {
1095 return _civicrm_api3_object_to_array($dao, $values, TRUE);
1099 * Format custom parameters.
1101 * @param array $params
1102 * @param array $values
1103 * @param string $extends
1104 * Entity that this custom field extends (e.g. contribution, event, contact).
1105 * @param string $entityId
1106 * ID of entity per $extends.
1108 function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) {
1109 $values['custom'] = array();
1110 $checkCheckBoxField = FALSE;
1112 if (in_array($extends, array('Household', 'Individual', 'Organization'))) {
1113 $entity = 'Contact';
1116 $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'create'));
1117 if (!$fields['is_error']) {
1118 // not sure if fields could be error - maybe change to using civicrm_api3 wrapper later - this is conservative
1119 $fields = $fields['values'];
1120 $checkCheckBoxField = TRUE;
1123 foreach ($params as $key => $value) {
1124 list($customFieldID, $customValueID) = CRM_Core_BAO_CustomField
::getKeyID($key, TRUE);
1125 if ($customFieldID && (!is_null($value))) {
1126 if ($checkCheckBoxField && !empty($fields['custom_' . $customFieldID]) && $fields['custom_' . $customFieldID]['html_type'] == 'CheckBox') {
1127 formatCheckBoxField($value, 'custom_' . $customFieldID, $entity);
1130 CRM_Core_BAO_CustomField
::formatCustomField($customFieldID, $values['custom'],
1131 $value, $extends, $customValueID, $entityId, FALSE, FALSE, TRUE
1138 * Format parameters for create action.
1140 * @param array $params
1143 function _civicrm_api3_format_params_for_create(&$params, $entity) {
1144 $nonGenericEntities = array('Contact', 'Individual', 'Household', 'Organization');
1146 $customFieldEntities = array_diff_key(CRM_Core_BAO_CustomQuery
::$extendsMap, array_fill_keys($nonGenericEntities, 1));
1147 if (!array_key_exists($entity, $customFieldEntities)) {
1151 _civicrm_api3_custom_format_params($params, $values, $entity);
1152 $params = array_merge($params, $values);
1156 * We can't rely on downstream to add separators to checkboxes so we'll check here.
1158 * We should look at pushing to BAO function
1159 * 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
1160 * note that this is specifically tested in the GRANT api test case so later refactoring should use that as a checking point
1162 * 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
1163 * don't touch - lots of very cautious code in here
1165 * The resulting array should look like
1171 * OR one or more keys wrapped in a CRM_Core_DAO::VALUE_SEPARATOR - either it accepted by the receiving function
1173 * @todo - we are probably skipping handling disabled options as presumably getoptions is not giving us them. This should be non-regressive but might
1174 * be fixed in future
1176 * @param mixed $checkboxFieldValue
1177 * @param string $customFieldLabel
1178 * @param string $entity
1180 function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) {
1182 if (is_string($checkboxFieldValue) && stristr($checkboxFieldValue, CRM_Core_DAO
::VALUE_SEPARATOR
)) {
1183 // We can assume it's pre-formatted.
1186 $options = civicrm_api($entity, 'getoptions', array('field' => $customFieldLabel, 'version' => 3));
1187 if (!empty($options['is_error'])) {
1188 // The check is precautionary - can probably be removed later.
1192 $options = $options['values'];
1194 if (is_array($checkboxFieldValue)) {
1195 foreach ($checkboxFieldValue as $key => $value) {
1196 if (!array_key_exists($key, $options)) {
1197 $validValue = FALSE;
1201 // we have been passed an array that is already in the 'odd' custom field format
1206 // so we either have an array that is not keyed by the value or we have a string that doesn't hold separators
1207 // if the array only has one item we'll treat it like any other string
1208 if (is_array($checkboxFieldValue) && count($checkboxFieldValue) == 1) {
1209 $possibleValue = reset($checkboxFieldValue);
1211 if (is_string($checkboxFieldValue)) {
1212 $possibleValue = $checkboxFieldValue;
1214 if (isset($possibleValue) && array_key_exists($possibleValue, $options)) {
1215 $checkboxFieldValue = CRM_Core_DAO
::VALUE_SEPARATOR
. $possibleValue . CRM_Core_DAO
::VALUE_SEPARATOR
;
1218 elseif (is_array($checkboxFieldValue)) {
1219 // so this time around we are considering the values in the array
1220 $possibleValues = $checkboxFieldValue;
1221 $formatValue = TRUE;
1223 elseif (stristr($checkboxFieldValue, ',')) {
1224 $formatValue = TRUE;
1225 //lets see if we should separate it - we do this near the end so we
1226 // ensure we have already checked that the comma is not part of a legitimate match
1227 // and of course, we don't make any changes if we don't now have matches
1228 $possibleValues = explode(',', $checkboxFieldValue);
1231 // run out of ideas as to what the format might be - if it's a string it doesn't match with or without the ','
1235 foreach ($possibleValues as $index => $possibleValue) {
1236 if (array_key_exists($possibleValue, $options)) {
1237 // 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)
1239 elseif (array_key_exists(trim($possibleValue), $options)) {
1240 $possibleValues[$index] = trim($possibleValue);
1243 $formatValue = FALSE;
1247 $checkboxFieldValue = CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $possibleValues) . CRM_Core_DAO
::VALUE_SEPARATOR
;
1252 * This function ensures that we have the right input parameters.
1256 * This function is only called when $dao is passed into verify_mandatory.
1257 * The practice of passing $dao into verify_mandatory turned out to be
1258 * unsatisfactory as the required fields @ the dao level is so different to the abstract
1259 * api level. Hence the intention is to remove this function
1260 * & the associated param from verify_mandatory
1262 * @param array $params
1263 * Associative array of property name/value.
1264 * pairs to insert in new history.
1265 * @param string $daoName
1266 * @param bool $return
1268 * @daoName string DAO to check params against
1271 * Should the missing fields be returned as an array (core error created as default)
1272 * true if all fields present, depending on $result a core error is created of an array of missing fields is returned
1274 function _civicrm_api3_check_required_fields($params, $daoName, $return = FALSE) {
1275 //@deprecated - see notes
1276 if (isset($params['extends'])) {
1277 if (($params['extends'] == 'Activity' ||
1278 $params['extends'] == 'Phonecall' ||
1279 $params['extends'] == 'Meeting' ||
1280 $params['extends'] == 'Group' ||
1281 $params['extends'] == 'Contribution'
1283 ($params['style'] == 'Tab')
1285 return civicrm_api3_create_error(ts("Can not create Custom Group in Tab for " . $params['extends']));
1289 $dao = new $daoName();
1290 $fields = $dao->fields();
1293 foreach ($fields as $k => $v) {
1294 if ($v['name'] == 'id') {
1298 if (!empty($v['required'])) {
1299 // 0 is a valid input for numbers, CRM-8122
1300 if (!isset($params[$k]) ||
(empty($params[$k]) && !($params[$k] === 0))) {
1306 if (!empty($missing)) {
1307 if (!empty($return)) {
1311 return civicrm_api3_create_error(ts("Required fields " . implode(',', $missing) . " for $daoName are not present"));
1319 * Function to do a 'standard' api get - when the api is only doing a $bao->find then use this.
1321 * @param string $bao_name
1323 * @param array $params
1325 * @param bool $returnAsSuccess
1326 * Return in api success format.
1327 * @param string $entity
1328 * @param CRM_Utils_SQL_Select|NULL $sql
1329 * Extra SQL bits to add to the query. For filtering current events, this might be:
1330 * CRM_Utils_SQL_Select::fragment()->where('(start_date >= CURDATE() || end_date >= CURDATE())');
1331 * @param bool $uniqueFields
1332 * Should unique field names be returned (for backward compatibility)
1336 function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $entity = "", $sql = NULL, $uniqueFields = FALSE) {
1337 $query = new \Civi\API\
SelectQuery($bao_name, $params, $uniqueFields);
1338 $query->merge($sql);
1339 $result = $query->run();
1340 if ($returnAsSuccess) {
1341 return civicrm_api3_create_success($result, $params, $entity, 'get');
1347 * Function to do a 'standard' api create - when the api is only doing a $bao::create then use this.
1349 * @param string $bao_name
1350 * Name of BAO Class.
1351 * @param array $params
1352 * Parameters passed into the api call.
1353 * @param string $entity
1354 * Entity - pass in if entity is non-standard & required $ids array.
1356 * @throws API_Exception
1357 * @throws \Civi\API\Exception\UnauthorizedException
1360 function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
1361 _civicrm_api3_check_edit_permissions($bao_name, $params);
1362 _civicrm_api3_format_params_for_create($params, $entity);
1363 $args = array(&$params);
1365 $ids = array($entity => CRM_Utils_Array
::value('id', $params));
1369 if (method_exists($bao_name, 'create')) {
1371 $fct_name = $bao_name . '::' . $fct;
1372 $bao = call_user_func_array(array($bao_name, $fct), $args);
1374 elseif (method_exists($bao_name, 'add')) {
1376 $fct_name = $bao_name . '::' . $fct;
1377 $bao = call_user_func_array(array($bao_name, $fct), $args);
1380 $fct_name = '_civicrm_api3_basic_create_fallback';
1381 $bao = _civicrm_api3_basic_create_fallback($bao_name, $params);
1384 if (is_null($bao)) {
1385 return civicrm_api3_create_error('Entity not created (' . $fct_name . ')');
1387 elseif (is_a($bao, 'CRM_Core_Error')) {
1388 //some weird circular thing means the error takes itself as an argument
1389 $msg = $bao->getMessages($bao);
1390 // the api deals with entities on a one-by-one basis. However, the contribution bao pushes entities
1391 // onto the error object - presumably because the contribution import is not handling multiple errors correctly
1392 // so we need to reset the error object here to avoid getting concatenated errors
1393 //@todo - the mulitple error handling should be moved out of the contribution object to the import / multiple entity processes
1394 CRM_Core_Error
::singleton()->reset();
1395 throw new API_Exception($msg);
1399 _civicrm_api3_object_to_array($bao, $values[$bao->id
]);
1400 return civicrm_api3_create_success($values, $params, $entity, 'create', $bao);
1405 * For BAO's which don't have a create() or add() functions, use this fallback implementation.
1407 * @fixme There's an intuitive sense that this behavior should be defined somehow in the BAO/DAO class
1408 * structure. In practice, that requires a fair amount of refactoring and/or kludgery.
1410 * @param string $bao_name
1411 * @param array $params
1413 * @throws API_Exception
1415 * @return CRM_Core_DAO|NULL
1416 * An instance of the BAO
1418 function _civicrm_api3_basic_create_fallback($bao_name, &$params) {
1419 $dao_name = get_parent_class($bao_name);
1420 if ($dao_name === 'CRM_Core_DAO' ||
!$dao_name) {
1421 $dao_name = $bao_name;
1423 $entityName = CRM_Core_DAO_AllCoreTables
::getBriefName($dao_name);
1424 if (empty($entityName)) {
1425 throw new API_Exception("Class \"$bao_name\" does not map to an entity name", "unmapped_class_to_entity", array(
1426 'class_name' => $bao_name,
1429 $hook = empty($params['id']) ?
'create' : 'edit';
1431 CRM_Utils_Hook
::pre($hook, $entityName, CRM_Utils_Array
::value('id', $params), $params);
1432 $instance = new $dao_name();
1433 $instance->copyValues($params);
1435 CRM_Utils_Hook
::post($hook, $entityName, $instance->id
, $instance);
1441 * Function to do a 'standard' api del.
1443 * When the api is only doing a $bao::del then use this if api::del doesn't exist it will try DAO delete method.
1445 * @param string $bao_name
1446 * @param array $params
1450 * @throws API_Exception
1451 * @throws \Civi\API\Exception\UnauthorizedException
1453 function _civicrm_api3_basic_delete($bao_name, &$params) {
1454 civicrm_api3_verify_mandatory($params, NULL, array('id'));
1455 _civicrm_api3_check_edit_permissions($bao_name, array('id' => $params['id']));
1456 $args = array(&$params['id']);
1457 if (method_exists($bao_name, 'del')) {
1458 $bao = call_user_func_array(array($bao_name, 'del'), $args);
1459 if ($bao !== FALSE) {
1460 return civicrm_api3_create_success(TRUE);
1462 throw new API_Exception('Could not delete entity id ' . $params['id']);
1464 elseif (method_exists($bao_name, 'delete')) {
1465 $dao = new $bao_name();
1466 $dao->id
= $params['id'];
1468 while ($dao->fetch()) {
1470 return civicrm_api3_create_success();
1474 throw new API_Exception('Could not delete entity id ' . $params['id']);
1478 throw new API_Exception('no delete method found');
1482 * Get custom data for the given entity & Add it to the returnArray.
1484 * This looks like 'custom_123' = 'custom string' AND
1485 * 'custom_123_1' = 'custom string'
1486 * Where 123 is field value & 1 is the id within the custom group data table (value ID)
1488 * @param array $returnArray
1489 * Array to append custom data too - generally $result[4] where 4 is the entity id.
1490 * @param string $entity
1491 * E.g membership, event.
1492 * @param int $entity_id
1493 * @param int $groupID
1494 * Per CRM_Core_BAO_CustomGroup::getTree.
1495 * @param int $subType
1496 * E.g. membership_type_id where custom data doesn't apply to all membership types.
1497 * @param string $subName
1498 * Subtype of entity.
1500 function _civicrm_api3_custom_data_get(&$returnArray, $checkPermission, $entity, $entity_id, $groupID = NULL, $subType = NULL, $subName = NULL) {
1501 $groupTree = CRM_Core_BAO_CustomGroup
::getTree($entity,
1502 CRM_Core_DAO
::$_nullObject,
1512 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, CRM_Core_DAO
::$_nullObject);
1513 $customValues = array();
1514 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $customValues);
1515 $fieldInfo = array();
1516 foreach ($groupTree as $set) {
1517 $fieldInfo +
= $set['fields'];
1519 if (!empty($customValues)) {
1520 foreach ($customValues as $key => $val) {
1521 // per standard - return custom_fieldID
1522 $id = CRM_Core_BAO_CustomField
::getKeyID($key);
1523 $returnArray['custom_' . $id] = $val;
1525 //not standard - but some api did this so guess we should keep - cheap as chips
1526 $returnArray[$key] = $val;
1528 // Shim to restore legacy behavior of ContactReference custom fields
1529 if (!empty($fieldInfo[$id]) && $fieldInfo[$id]['data_type'] == 'ContactReference') {
1530 $returnArray['custom_' . $id . '_id'] = $returnArray[$key . '_id'] = $val;
1531 $returnArray['custom_' . $id] = $returnArray[$key] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $val, 'sort_name');
1538 * Validate fields being passed into API.
1540 * This function relies on the getFields function working accurately
1541 * for the given API. If error mode is set to TRUE then it will also check
1544 * As of writing only date was implemented.
1546 * @param string $entity
1547 * @param string $action
1548 * @param array $params
1550 * @param array $fields
1551 * Response from getfields all variables are the same as per civicrm_api.
1552 * @param bool $errorMode
1553 * ErrorMode do intensive post fail checks?.
1557 function _civicrm_api3_validate_fields($entity, $action, &$params, $fields, $errorMode = FALSE) {
1558 //CRM-15792 handle datetime for custom fields below code handles chain api call
1559 $chainApikeys = array_flip(preg_grep("/^api./", array_keys($params)));
1560 if (!empty($chainApikeys) && is_array($chainApikeys)) {
1561 foreach ($chainApikeys as $key => $value) {
1562 if (is_array($params[$key])) {
1563 $chainApiParams = array_intersect_key($fields, $params[$key]);
1564 $customFields = array_fill_keys(array_keys($params[$key]), $key);
1568 $fields = array_intersect_key($fields, $params);
1569 if (!empty($chainApiParams)) {
1570 $fields = array_merge($fields, $chainApiParams);
1572 foreach ($fields as $fieldName => $fieldInfo) {
1573 switch (CRM_Utils_Array
::value('type', $fieldInfo)) {
1574 case CRM_Utils_Type
::T_INT
:
1575 //field is of type integer
1576 _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity);
1579 case CRM_Utils_Type
::T_DATE
:
1580 case CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
:
1581 case CRM_Utils_Type
::T_TIMESTAMP
:
1582 //field is of type date or datetime
1583 if (!empty($customFields) && array_key_exists($fieldName, $customFields)) {
1584 $dateParams = &$params[$customFields[$fieldName]];
1587 $dateParams = &$params;
1589 _civicrm_api3_validate_date($dateParams, $fieldName, $fieldInfo);
1594 _civicrm_api3_validate_html($params, $fieldName, $fieldInfo);
1597 case CRM_Utils_Type
::T_STRING
:
1598 _civicrm_api3_validate_string($params, $fieldName, $fieldInfo, $entity);
1601 case CRM_Utils_Type
::T_MONEY
:
1602 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1603 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
1606 foreach ((array) $fieldValue as $fieldvalue) {
1607 if (!CRM_Utils_Rule
::money($fieldvalue) && !empty($fieldvalue)) {
1608 throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]);
1614 // intensive checks - usually only called after DB level fail
1615 if (!empty($errorMode) && strtolower($action) == 'create') {
1616 if (!empty($fieldInfo['FKClassName'])) {
1617 if (!empty($fieldValue)) {
1618 _civicrm_api3_validate_constraint($params, $fieldName, $fieldInfo);
1620 elseif (!empty($fieldInfo['required'])) {
1621 throw new Exception("DB Constraint Violation - possibly $fieldName should possibly be marked as mandatory for this API. If so, please raise a bug report.");
1624 if (!empty($fieldInfo['api.unique'])) {
1625 $params['entity'] = $entity;
1626 _civicrm_api3_validate_unique_key($params, $fieldName);
1633 * Validate date fields being passed into API.
1635 * It currently converts both unique fields and DB field names to a mysql date.
1636 * @todo - probably the unique field handling & the if exists handling is now done before this
1637 * function is reached in the wrapper - can reduce this code down to assume we
1638 * are only checking the passed in field
1640 * It also checks against the RULE:date function. This is a centralisation of code that was scattered and
1641 * may not be the best thing to do. There is no code level documentation on the existing functions to work off
1643 * @param array $params
1644 * Params from civicrm_api.
1645 * @param string $fieldName
1646 * Uniquename of field being checked.
1647 * @param array $fieldInfo
1648 * Array of fields from getfields function.
1652 function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) {
1653 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1654 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
1657 //should we check first to prevent it from being copied if they have passed in sql friendly format?
1658 if (!empty($params[$fieldInfo['name']])) {
1659 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldInfo['name'], $fieldInfo['type']);
1661 if ((CRM_Utils_Array
::value('name', $fieldInfo) != $fieldName) && !empty($fieldValue)) {
1662 $fieldValue = _civicrm_api3_getValidDate($fieldValue, $fieldName, $fieldInfo['type']);
1666 $params[$fieldName][$op] = $fieldValue;
1669 $params[$fieldName] = $fieldValue;
1674 * Convert date into BAO friendly date.
1676 * We accept 'whatever strtotime accepts'
1678 * @param string $dateValue
1679 * @param string $fieldName
1685 function _civicrm_api3_getValidDate($dateValue, $fieldName, $fieldType) {
1686 if (is_array($dateValue)) {
1687 foreach ($dateValue as $key => $value) {
1688 $dateValue[$key] = _civicrm_api3_getValidDate($value, $fieldName, $fieldType);
1692 if (strtotime($dateValue) === FALSE) {
1693 throw new Exception($fieldName . " is not a valid date: " . $dateValue);
1695 $format = ($fieldType == CRM_Utils_Type
::T_DATE
) ?
'Ymd000000' : 'YmdHis';
1696 return CRM_Utils_Date
::processDate($dateValue, NULL, FALSE, $format);
1700 * Validate foreign constraint fields being passed into API.
1702 * @param mixed $fieldValue
1703 * @param string $fieldName
1704 * Uniquename of field being checked.
1705 * @param array $fieldInfo
1706 * Array of fields from getfields function.
1708 * @throws \API_Exception
1710 function _civicrm_api3_validate_constraint(&$fieldValue, &$fieldName, &$fieldInfo) {
1711 $daoName = $fieldInfo['FKClassName'];
1712 $dao = new $daoName();
1713 $dao->id
= $fieldValue;
1715 $dao->selectAdd('id');
1716 if (!$dao->find()) {
1717 throw new API_Exception("$fieldName is not valid : " . $fieldValue);
1722 * Validate foreign constraint fields being passed into API.
1724 * @param array $params
1725 * Params from civicrm_api.
1726 * @param string $fieldName
1727 * Uniquename of field being checked.
1731 function _civicrm_api3_validate_unique_key(&$params, &$fieldName) {
1732 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
1733 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
1736 $existing = civicrm_api($params['entity'], 'get', array(
1737 'version' => $params['version'],
1738 $fieldName => $fieldValue,
1740 // an entry already exists for this unique field
1741 if ($existing['count'] == 1) {
1742 // question - could this ever be a security issue?
1743 throw new API_Exception("Field: `$fieldName` must be unique. An conflicting entity already exists - id: " . $existing['id']);
1748 * Generic implementation of the "replace" action.
1750 * Replace the old set of entities (matching some given keys) with a new set of
1751 * entities (matching the same keys).
1753 * @note This will verify that 'values' is present, but it does not directly verify
1754 * any other parameters.
1756 * @param string $entity
1758 * @param array $params
1759 * Params from civicrm_api, including:.
1760 * - 'values': an array of records to save
1761 * - all other items: keys which identify new/pre-existing records.
1765 function _civicrm_api3_generic_replace($entity, $params) {
1767 $transaction = new CRM_Core_Transaction();
1769 if (!is_array($params['values'])) {
1770 throw new Exception("Mandatory key(s) missing from params array: values");
1773 // Extract the keys -- somewhat scary, don't think too hard about it
1774 $baseParams = _civicrm_api3_generic_replace_base_params($params);
1776 // Lookup pre-existing records
1777 $preexisting = civicrm_api($entity, 'get', $baseParams, $params);
1778 if (civicrm_error($preexisting)) {
1779 $transaction->rollback();
1780 return $preexisting;
1783 // Save the new/updated records
1785 foreach ($params['values'] as $replacement) {
1786 // Sugar: Don't force clients to duplicate the 'key' data
1787 $replacement = array_merge($baseParams, $replacement);
1788 $action = (isset($replacement['id']) ||
isset($replacement[$entity . '_id'])) ?
'update' : 'create';
1789 $create = civicrm_api($entity, $action, $replacement);
1790 if (civicrm_error($create)) {
1791 $transaction->rollback();
1794 foreach ($create['values'] as $entity_id => $entity_value) {
1795 $creates[$entity_id] = $entity_value;
1799 // Remove stale records
1800 $staleIDs = array_diff(
1801 array_keys($preexisting['values']),
1802 array_keys($creates)
1804 foreach ($staleIDs as $staleID) {
1805 $delete = civicrm_api($entity, 'delete', array(
1806 'version' => $params['version'],
1809 if (civicrm_error($delete)) {
1810 $transaction->rollback();
1815 return civicrm_api3_create_success($creates, $params);
1817 catch(PEAR_Exception
$e) {
1818 $transaction->rollback();
1819 return civicrm_api3_create_error($e->getMessage());
1821 catch(Exception
$e) {
1822 $transaction->rollback();
1823 return civicrm_api3_create_error($e->getMessage());
1828 * Replace base parameters.
1830 * @param array $params
1834 function _civicrm_api3_generic_replace_base_params($params) {
1835 $baseParams = $params;
1836 unset($baseParams['values']);
1837 unset($baseParams['sequential']);
1838 unset($baseParams['options']);
1843 * Returns fields allowable by api.
1846 * String Entity to query.
1847 * @param bool $unique
1848 * Index by unique fields?.
1849 * @param array $params
1853 function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = array()) {
1854 $unsetIfEmpty = array(
1861 $dao = _civicrm_api3_get_DAO($entity);
1866 $fields = $d->fields();
1868 // Set html attributes for text fields
1869 foreach ($fields as $name => &$field) {
1870 if (isset($field['html'])) {
1871 $field['html'] +
= (array) $d::makeAttribute($field);
1875 // replace uniqueNames by the normal names as the key
1876 if (empty($unique)) {
1877 foreach ($fields as $name => &$field) {
1878 //getting rid of unused attributes
1879 foreach ($unsetIfEmpty as $attr) {
1880 if (empty($field[$attr])) {
1881 unset($field[$attr]);
1884 if ($name == $field['name']) {
1887 if (array_key_exists($field['name'], $fields)) {
1888 $field['error'] = 'name conflict';
1889 // it should never happen, but better safe than sorry
1892 $fields[$field['name']] = $field;
1893 $fields[$field['name']]['uniqueName'] = $name;
1894 unset($fields[$name]);
1897 // Translate FKClassName to the corresponding api
1898 foreach ($fields as $name => &$field) {
1899 if (!empty($field['FKClassName'])) {
1900 $FKApi = CRM_Core_DAO_AllCoreTables
::getBriefName($field['FKClassName']);
1902 $field['FKApiName'] = $FKApi;
1906 $fields +
= _civicrm_api_get_custom_fields($entity, $params);
1911 * Return an array of fields for a given entity.
1913 * This is the same as the BAO function but fields are prefixed with 'custom_' to represent api params.
1916 * @param array $params
1920 function _civicrm_api_get_custom_fields($entity, &$params) {
1921 $entity = _civicrm_api_get_camel_name($entity);
1922 if ($entity == 'Contact') {
1923 // Use sub-type if available, otherwise "NULL" to fetch from all contact types
1924 $entity = CRM_Utils_Array
::value('contact_type', $params);
1926 $customfields = CRM_Core_BAO_CustomField
::getFields($entity,
1929 // we could / should probably test for other subtypes here - e.g. activity_type_id
1930 CRM_Utils_Array
::value('contact_sub_type', $params),
1939 foreach ($customfields as $key => $value) {
1940 // Regular fields have a 'name' property
1941 $value['name'] = 'custom_' . $key;
1942 $value['title'] = $value['label'];
1943 $value['type'] = _getStandardTypeFromCustomDataType($value);
1944 $ret['custom_' . $key] = $value;
1950 * Translate the custom field data_type attribute into a std 'type'.
1952 * @param array $value
1956 function _getStandardTypeFromCustomDataType($value) {
1957 $dataType = $value['data_type'];
1958 //CRM-15792 - If date custom field contains timeformat change type to DateTime
1959 if ($value['data_type'] == 'Date' && isset($value['time_format']) && $value['time_format'] > 0) {
1960 $dataType = 'DateTime';
1963 'String' => CRM_Utils_Type
::T_STRING
,
1964 'Int' => CRM_Utils_Type
::T_INT
,
1965 'Money' => CRM_Utils_Type
::T_MONEY
,
1966 'Memo' => CRM_Utils_Type
::T_LONGTEXT
,
1967 'Float' => CRM_Utils_Type
::T_FLOAT
,
1968 'Date' => CRM_Utils_Type
::T_DATE
,
1969 'DateTime' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
1970 'Boolean' => CRM_Utils_Type
::T_BOOLEAN
,
1971 'StateProvince' => CRM_Utils_Type
::T_INT
,
1972 'File' => CRM_Utils_Type
::T_STRING
,
1973 'Link' => CRM_Utils_Type
::T_STRING
,
1974 'ContactReference' => CRM_Utils_Type
::T_INT
,
1975 'Country' => CRM_Utils_Type
::T_INT
,
1977 return $mapping[$dataType];
1982 * Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't.
1984 * If multiple aliases the last takes precedence
1986 * Function also swaps unique fields for non-unique fields & vice versa.
1988 * @param $apiRequest
1991 function _civicrm_api3_swap_out_aliases(&$apiRequest, $fields) {
1992 foreach ($fields as $field => $values) {
1993 $uniqueName = CRM_Utils_Array
::value('uniqueName', $values);
1994 if (!empty($values['api.aliases'])) {
1995 // if aliased field is not set we try to use field alias
1996 if (!isset($apiRequest['params'][$field])) {
1997 foreach ($values['api.aliases'] as $alias) {
1998 if (isset($apiRequest['params'][$alias])) {
1999 $apiRequest['params'][$field] = $apiRequest['params'][$alias];
2001 //unset original field nb - need to be careful with this as it may bring inconsistencies
2002 // out of the woodwork but will be implementing only as _spec function extended
2003 unset($apiRequest['params'][$alias]);
2007 if (!isset($apiRequest['params'][$field]) && !empty($values['name']) && $field != $values['name']
2008 && isset($apiRequest['params'][$values['name']])
2010 $apiRequest['params'][$field] = $apiRequest['params'][$values['name']];
2011 // note that it would make sense to unset the original field here but tests need to be in place first
2012 if ($field != 'domain_version') {
2013 unset($apiRequest['params'][$values['name']]);
2016 if (!isset($apiRequest['params'][$field])
2018 && $field != $uniqueName
2019 && array_key_exists($uniqueName, $apiRequest['params'])
2021 $apiRequest['params'][$field] = CRM_Utils_Array
::value($values['uniqueName'], $apiRequest['params']);
2022 // note that it would make sense to unset the original field here but tests need to be in place first
2029 * Validate integer fields being passed into API.
2031 * It currently converts the incoming value 'user_contact_id' into the id of the currently logged in user.
2033 * @param array $params
2034 * Params from civicrm_api.
2035 * @param string $fieldName
2036 * Uniquename of field being checked.
2037 * @param array $fieldInfo
2038 * Array of fields from getfields function.
2039 * @param string $entity
2041 * @throws API_Exception
2043 function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity) {
2044 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2045 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE) {
2049 if (!empty($fieldValue)) {
2050 // if value = 'user_contact_id' (or similar), replace value with contact id
2051 if (!is_numeric($fieldValue) && is_scalar($fieldValue)) {
2052 $realContactId = _civicrm_api3_resolve_contactID($fieldValue);
2053 if ('unknown-user' === $realContactId) {
2054 throw new API_Exception("\"$fieldName\" \"{$fieldValue}\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldName, "type" => "integer"));
2056 elseif (is_numeric($realContactId)) {
2057 $fieldValue = $realContactId;
2060 if (!empty($fieldInfo['pseudoconstant']) ||
!empty($fieldInfo['options'])) {
2061 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo);
2064 // After swapping options, ensure we have an integer(s)
2065 foreach ((array) ($fieldValue) as $value) {
2066 if ($value && !is_numeric($value) && $value !== 'null' && !is_array($value)) {
2067 throw new API_Exception("$fieldName is not a valid integer", 2001, array('error_field' => $fieldName, "type" => "integer"));
2071 // Check our field length
2072 if (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen($fieldValue) > $fieldInfo['maxlength']
2074 throw new API_Exception($fieldValue . " is " . strlen($fieldValue) . " characters - longer than $fieldName length" . $fieldInfo['maxlength'] . ' characters',
2075 2100, array('field' => $fieldName, "max_length" => $fieldInfo['maxlength'])
2081 $params[$fieldName][$op] = $fieldValue;
2084 $params[$fieldName] = $fieldValue;
2089 * Determine a contact ID using a string expression.
2091 * @param string $contactIdExpr
2092 * E.g. "user_contact_id" or "@user:username".
2094 * @return int|NULL|'unknown-user'
2096 function _civicrm_api3_resolve_contactID($contactIdExpr) {
2097 // If value = 'user_contact_id' replace value with logged in user id.
2098 if ($contactIdExpr == "user_contact_id") {
2099 return CRM_Core_Session
::getLoggedInContactID();
2101 elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) {
2102 $config = CRM_Core_Config
::singleton();
2104 $ufID = $config->userSystem
->getUfId($matches[1]);
2106 return 'unknown-user';
2109 $contactID = CRM_Core_BAO_UFMatch
::getContactId($ufID);
2111 return 'unknown-user';
2120 * Validate html (check for scripting attack).
2122 * @param array $params
2123 * @param string $fieldName
2124 * @param array $fieldInfo
2126 * @throws API_Exception
2128 function _civicrm_api3_validate_html(&$params, &$fieldName, $fieldInfo) {
2129 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName);
2130 if (strpos($op, 'NULL') ||
strpos($op, 'EMPTY')) {
2134 if (!CRM_Utils_Rule
::xssString($fieldValue)) {
2135 throw new API_Exception('Input contains illegal SCRIPT tag.', array("field" => $fieldName, "error_code" => "xss"));
2141 * Validate string fields being passed into API.
2143 * @param array $params
2144 * Params from civicrm_api.
2145 * @param string $fieldName
2146 * Uniquename of field being checked.
2147 * @param array $fieldInfo
2148 * Array of fields from getfields function.
2149 * @param string $entity
2151 * @throws API_Exception
2154 function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $entity) {
2155 list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName, 'String');
2156 if (strpos($op, 'NULL') !== FALSE ||
strpos($op, 'EMPTY') !== FALSE || CRM_Utils_System
::isNull($fieldValue)) {
2160 if (!is_array($fieldValue)) {
2161 $fieldValue = (string) $fieldValue;
2164 //@todo what do we do about passed in arrays. For many of these fields
2165 // the missing piece of functionality is separating them to a separated string
2166 // & many save incorrectly. But can we change them wholesale?
2169 foreach ((array) $fieldValue as $value) {
2170 if (!CRM_Utils_Rule
::xssString($fieldValue)) {
2171 throw new Exception('Input contains illegal SCRIPT tag.');
2173 if ($fieldName == 'currency') {
2174 //When using IN operator $fieldValue is a array of currency codes
2175 if (!CRM_Utils_Rule
::currencyCode($value)) {
2176 throw new Exception("Currency not a valid code: $currency");
2181 if (!empty($fieldInfo['pseudoconstant']) ||
!empty($fieldInfo['options'])) {
2182 _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo);
2184 // Check our field length
2185 elseif (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen(utf8_decode($fieldValue)) > $fieldInfo['maxlength']) {
2186 throw new API_Exception("Value for $fieldName is " . strlen(utf8_decode($value)) . " characters - This field has a maxlength of {$fieldInfo['maxlength']} characters.",
2187 2100, array('field' => $fieldName)
2192 $params[$fieldName][$op] = $fieldValue;
2195 $params[$fieldName] = $fieldValue;
2200 * Validate & swap out any pseudoconstants / options.
2202 * @param mixed $fieldValue
2203 * @param string $entity : api entity name
2204 * @param string $fieldName : field name used in api call (not necessarily the canonical name)
2205 * @param array $fieldInfo : getfields meta-data
2207 * @throws \API_Exception
2209 function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo) {
2210 $options = CRM_Utils_Array
::value('options', $fieldInfo);
2213 if (strtolower($entity) == 'profile' && !empty($fieldInfo['entity'])) {
2214 // We need to get the options from the entity the field relates to.
2215 $entity = $fieldInfo['entity'];
2217 $options = civicrm_api($entity, 'getoptions', array(
2219 'field' => $fieldInfo['name'],
2220 'context' => 'validate',
2222 $options = CRM_Utils_Array
::value('values', $options, array());
2225 // If passed a value-separated string, explode to an array, then re-implode after matching values.
2227 if (is_string($fieldValue) && strpos($fieldValue, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
2228 $fieldValue = CRM_Utils_Array
::explodePadded($fieldValue);
2231 // If passed multiple options, validate each.
2232 if (is_array($fieldValue)) {
2233 foreach ($fieldValue as &$value) {
2234 if (!is_array($value)) {
2235 _civicrm_api3_api_match_pseudoconstant_value($value, $options, $fieldName);
2238 // TODO: unwrap the call to implodePadded from the conditional and do it always
2239 // need to verify that this is safe and doesn't break anything though.
2240 // Better yet would be to leave it as an array and ensure that every dao/bao can handle array input
2242 CRM_Utils_Array
::implodePadded($fieldValue);
2246 _civicrm_api3_api_match_pseudoconstant_value($fieldValue, $options, $fieldName);
2251 * Validate & swap a single option value for a field.
2253 * @param string $value field value
2254 * @param array $options array of options for this field
2255 * @param string $fieldName field name used in api call (not necessarily the canonical name)
2257 * @throws API_Exception
2259 function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName) {
2260 // If option is a key, no need to translate
2261 // or if no options are avaiable for pseudoconstant 'table' property
2262 if (array_key_exists($value, $options) ||
!$options) {
2266 // Translate value into key
2267 $newValue = array_search($value, $options);
2268 if ($newValue !== FALSE) {
2272 // Case-insensitive matching
2273 $newValue = strtolower($value);
2274 $options = array_map("strtolower", $options);
2275 $newValue = array_search($newValue, $options);
2276 if ($newValue === FALSE) {
2277 throw new API_Exception("'$value' is not a valid option for field $fieldName", 2001, array('error_field' => $fieldName));
2283 * Returns the canonical name of a field.
2286 * api entity name (string should already be standardized - no camelCase).
2288 * any variation of a field's name (name, unique_name, api.alias).
2290 * @param string $action
2292 * @return bool|string
2293 * FieldName or FALSE if the field does not exist
2295 function _civicrm_api3_api_resolve_alias($entity, $fieldName, $action = 'create') {
2299 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
2302 if ($fieldName == _civicrm_api_get_entity_name_from_camel($entity) . '_id') {
2305 $result = civicrm_api($entity, 'getfields', array(
2307 'action' => $action,
2309 $meta = $result['values'];
2310 if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
2311 $fieldName = $fieldName . '_id';
2313 if (isset($meta[$fieldName])) {
2314 return $meta[$fieldName]['name'];
2316 foreach ($meta as $info) {
2317 if ($fieldName == $info['name'] ||
$fieldName == CRM_Utils_Array
::value('uniqueName', $info)) {
2318 return $info['name'];
2320 if (array_search($fieldName, CRM_Utils_Array
::value('api.aliases', $info, array())) !== FALSE) {
2321 return $info['name'];
2324 // Create didn't work, try with get
2325 if ($action == 'create') {
2326 return _civicrm_api3_api_resolve_alias($entity, $fieldName, 'get');
2332 * Check if the function is deprecated.
2334 * @param string $entity
2335 * @param array $result
2337 * @return string|array|null
2339 function _civicrm_api3_deprecation_check($entity, $result = array()) {
2341 $apiFile = "api/v3/$entity.php";
2342 if (CRM_Utils_File
::isIncludable($apiFile)) {
2343 require_once $apiFile;
2345 $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
2346 $fnName = "_civicrm_api3_{$lowercase_entity}_deprecation";
2347 if (function_exists($fnName)) {
2348 return $fnName($result);
2354 * Get the actual field value.
2356 * In some case $params[$fieldName] holds Array value in this format Array([operator] => [value])
2357 * So this function returns the actual field value.
2359 * @param array $params
2360 * @param string $fieldName
2361 * @param string $type
2365 function _civicrm_api3_field_value_check(&$params, $fieldName, $type = NULL) {
2366 $fieldValue = CRM_Utils_Array
::value($fieldName, $params);
2369 if (!empty($fieldValue) && is_array($fieldValue) &&
2370 (array_search(key($fieldValue), CRM_Core_DAO
::acceptedSQLOperators()) ||
2371 $type == 'String' && strstr(key($fieldValue), 'EMPTY'))
2373 $op = key($fieldValue);
2374 $fieldValue = CRM_Utils_Array
::value($op, $fieldValue);
2376 return array($fieldValue, $op);
2380 * A generic "get" API based on simple array data. This is comparable to
2381 * _civicrm_api3_basic_get but does not use DAO/BAO. This is useful for
2382 * small/mid-size data loaded from external JSON or XML documents.
2385 * @param array $params
2387 * @param array $records
2388 * List of all records.
2389 * @param string $idCol
2390 * The property which defines the ID of a record
2391 * @param array $fields
2392 * List of filterable fields.
2395 * @throws \API_Exception
2397 function _civicrm_api3_basic_array_get($entity, $params, $records, $idCol, $fields) {
2398 $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get');
2399 // TODO // $sort = CRM_Utils_Array::value('sort', $options, NULL);
2400 $offset = CRM_Utils_Array
::value('offset', $options);
2401 $limit = CRM_Utils_Array
::value('limit', $options);
2406 foreach ($records as $record) {
2407 if ($idCol != 'id') {
2408 $record['id'] = $record[$idCol];
2411 foreach ($params as $k => $v) {
2415 if (in_array($k, $fields) && $record[$k] != $v) {
2421 if ($currentOffset >= $offset) {
2422 $matches[$record[$idCol]] = $record;
2424 if ($limit && count($matches) >= $limit) {
2431 $return = CRM_Utils_Array
::value('return', $options, array());
2432 if (!empty($return)) {
2434 $matches = CRM_Utils_Array
::filterColumns($matches, array_keys($return));
2437 return civicrm_api3_create_success($matches, $params);
2441 * @param string $bao_name
2442 * @param array $params
2443 * @throws \Civi\API\Exception\UnauthorizedException
2445 function _civicrm_api3_check_edit_permissions($bao_name, $params) {
2446 // For lack of something more clever, here's a whitelist of entities whos permissions
2447 // are inherited from a contact record.
2448 // Note, when adding here, also remember to modify _civicrm_api3_permissions()
2449 $contactEntities = array(
2450 'CRM_Core_BAO_Email',
2451 'CRM_Core_BAO_Phone',
2452 'CRM_Core_BAO_Address',
2454 'CRM_Core_BAO_Website',
2456 if (!empty($params['check_permissions']) && in_array($bao_name, $contactEntities)) {
2457 $cid = !empty($params['contact_id']) ?
$params['contact_id'] : CRM_Core_DAO
::getFieldValue($bao_name, $params['id'], 'contact_id');
2458 if (!CRM_Contact_BAO_Contact_Permission
::allow($cid, CRM_Core_Permission
::EDIT
)) {
2459 throw new \Civi\API\Exception\
UnauthorizedException('Permission denied to modify contact record');