Merge pull request #16714 from christianwach/lab-1638
[civicrm-core.git] / api / v3 / Generic / Update.php
CommitLineData
6a488035 1<?php
1c88e578 2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
1c88e578 5 | |
a30c801b
TO
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 |
1c88e578 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
b081365f
CW
12/**
13 * @package CiviCRM_APIv3
14 */
15
6a488035 16/**
cd5823ae
EM
17 * Update function is basically a hack.
18 *
19 * We want to remove it but must resolve issues in
e2747b59 20 * http://issues.civicrm.org/jira/browse/CRM-12144
21 *
22 * It is not recommended & if update doesn't work & fix does then update will not be fixed
23 *
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.
26 *
c28e1768
CW
27 * @deprecated
28 *
cf470720 29 * @param array $apiRequest
16b10e64
CW
30 * Array with keys:
31 * - entity: string
32 * - action: string
33 * - version: string
34 * - function: callback (mixed)
35 * - params: array, varies
77b97be7
EM
36 *
37 * @return array|int|mixed
6a488035
TO
38 */
39function civicrm_api3_generic_update($apiRequest) {
e2747b59 40 //$key_id = strtolower ($apiRequest['entity'])."_id";
41 $key_id = "id";
42 if (!array_key_exists($key_id, $apiRequest['params'])) {
b1cb6336 43 return civicrm_api3_create_error("Mandatory parameter missing $key_id");
6a488035 44 }
e386a062 45 // @fixme
d085c5eb 46 // tests show that contribution works better with create
47 // this is horrible but to make it work we'll just handle it separately
9b873358 48 if (strtolower($apiRequest['entity']) == 'contribution') {
d085c5eb 49 return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
50 }
cf8f0fff 51 $seek = [$key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']];
e2747b59 52 $existing = civicrm_api($apiRequest['entity'], 'get', $seek);
53 if ($existing['is_error']) {
54 return $existing;
55 }
56 if ($existing['count'] > 1) {
b1cb6336 57 return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 58 }
59 if ($existing['count'] == 0) {
b1cb6336 60 return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 61 }
62
63 $existing = array_pop($existing['values']);
1bdbd4ec 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]);
69 }
e2747b59 70 $p = array_merge($existing, $apiRequest['params']);
71 return civicrm_api($apiRequest['entity'], 'create', $p);
6a488035 72}