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