Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /** | |
e2747b59 | 5 | * Update function is basically a hack to get around issues listed in |
6 | * http://issues.civicrm.org/jira/browse/CRM-12144 | |
7 | * | |
8 | * It is not recommended & if update doesn't work & fix does then update will not be fixed | |
9 | * | |
10 | * To do this, perform a 'get' action to load the existing values, then merge in the updates | |
11 | * and call 'create' to save the revised entity. | |
12 | * | |
6a488035 TO |
13 | * @param $apiRequest an array with keys: |
14 | * - entity: string | |
15 | * - action: string | |
16 | * - version: string | |
17 | * - function: callback (mixed) | |
18 | * - params: array, varies | |
19 | */ | |
20 | function civicrm_api3_generic_update($apiRequest) { | |
e2747b59 | 21 | $errorFnName = 'civicrm_api3_create_error'; |
6a488035 | 22 | |
e2747b59 | 23 | //$key_id = strtolower ($apiRequest['entity'])."_id"; |
24 | $key_id = "id"; | |
25 | if (!array_key_exists($key_id, $apiRequest['params'])) { | |
26 | return $errorFnName("Mandatory parameter missing $key_id"); | |
6a488035 | 27 | } |
e386a062 | 28 | // @fixme |
d085c5eb | 29 | // tests show that contribution works better with create |
30 | // this is horrible but to make it work we'll just handle it separately | |
31 | if(strtolower($apiRequest['entity']) == 'contribution'){ | |
32 | return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']); | |
33 | } | |
e2747b59 | 34 | $seek = array($key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']); |
35 | $existing = civicrm_api($apiRequest['entity'], 'get', $seek); | |
36 | if ($existing['is_error']) { | |
37 | return $existing; | |
38 | } | |
39 | if ($existing['count'] > 1) { | |
40 | return $errorFnName("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]); | |
41 | } | |
42 | if ($existing['count'] == 0) { | |
43 | return $errorFnName("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]); | |
44 | } | |
45 | ||
46 | $existing = array_pop($existing['values']); | |
47 | $p = array_merge($existing, $apiRequest['params']); | |
48 | return civicrm_api($apiRequest['entity'], 'create', $p); | |
6a488035 TO |
49 | } |
50 |