3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * @package CiviCRM_APIv3
17 * Update function is basically a hack.
19 * We want to remove it but must resolve issues in
20 * http://issues.civicrm.org/jira/browse/CRM-12144
22 * It is not recommended & if update doesn't work & fix does then update will not be fixed
24 * To do this, perform a 'get' action to load the existing values, then merge in the updates
25 * and call 'create' to save the revised entity.
29 * @param array $apiRequest
34 * - function: callback (mixed)
35 * - params: array, varies
37 * @return array|int|mixed
39 function civicrm_api3_generic_update($apiRequest) {
40 //$key_id = strtolower ($apiRequest['entity'])."_id";
42 if (!array_key_exists($key_id, $apiRequest['params'])) {
43 return civicrm_api3_create_error("Mandatory parameter missing $key_id");
46 // tests show that contribution works better with create
47 // this is horrible but to make it work we'll just handle it separately
48 if (strtolower($apiRequest['entity']) == 'contribution') {
49 return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
51 $seek = [$key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']];
52 $existing = civicrm_api($apiRequest['entity'], 'get', $seek);
53 if ($existing['is_error']) {
56 if ($existing['count'] > 1) {
57 return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
59 if ($existing['count'] == 0) {
60 return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
63 $existing = array_pop($existing['values']);
64 // Per Unit test testUpdateHouseholdWithAll we don't want to load these from the DB
65 // if they are not passed in then we'd rather they are calculated.
66 // Note update is not recomended anyway...
67 foreach (['sort_name', 'display_name'] as $fieldToNotSet) {
68 unset($existing[$fieldToNotSet]);
70 $p = array_merge($existing, $apiRequest['params']);
71 return civicrm_api($apiRequest['entity'], 'create', $p);