5 * Update function is basically a hack to get around issues listed in
6 * http://issues.civicrm.org/jira/browse/CRM-12144
8 * It is not recommended & if update doesn't work & fix does then update will not be fixed
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.
13 * @param $apiRequest an array with keys:
17 * - function: callback (mixed)
18 * - params: array, varies
20 function civicrm_api3_generic_update($apiRequest) {
21 $errorFnName = 'civicrm_api3_create_error';
23 //$key_id = strtolower ($apiRequest['entity'])."_id";
25 if (!array_key_exists($key_id, $apiRequest['params'])) {
26 return $errorFnName("Mandatory parameter missing $key_id");
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']);
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']) {
39 if ($existing['count'] > 1) {
40 return $errorFnName("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
42 if ($existing['count'] == 0) {
43 return $errorFnName("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
46 $existing = array_pop($existing['values']);
47 $p = array_merge($existing, $apiRequest['params']);
48 return civicrm_api($apiRequest['entity'], 'create', $p);