Short array syntax - auto-convert api dir
[civicrm-core.git] / api / v3 / Generic / Update.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CiviCRM_APIv3
30 */
31
32 /**
33 * Update function is basically a hack.
34 *
35 * We want to remove it but must resolve issues in
36 * http://issues.civicrm.org/jira/browse/CRM-12144
37 *
38 * It is not recommended & if update doesn't work & fix does then update will not be fixed
39 *
40 * To do this, perform a 'get' action to load the existing values, then merge in the updates
41 * and call 'create' to save the revised entity.
42 *
43 * @deprecated
44 *
45 * @param array $apiRequest
46 * Array with keys:
47 * - entity: string
48 * - action: string
49 * - version: string
50 * - function: callback (mixed)
51 * - params: array, varies
52 *
53 * @return array|int|mixed
54 */
55 function civicrm_api3_generic_update($apiRequest) {
56 //$key_id = strtolower ($apiRequest['entity'])."_id";
57 $key_id = "id";
58 if (!array_key_exists($key_id, $apiRequest['params'])) {
59 return civicrm_api3_create_error("Mandatory parameter missing $key_id");
60 }
61 // @fixme
62 // tests show that contribution works better with create
63 // this is horrible but to make it work we'll just handle it separately
64 if (strtolower($apiRequest['entity']) == 'contribution') {
65 return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
66 }
67 $seek = [$key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']];
68 $existing = civicrm_api($apiRequest['entity'], 'get', $seek);
69 if ($existing['is_error']) {
70 return $existing;
71 }
72 if ($existing['count'] > 1) {
73 return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
74 }
75 if ($existing['count'] == 0) {
76 return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
77 }
78
79 $existing = array_pop($existing['values']);
80 // Per Unit test testUpdateHouseholdWithAll we don't want to load these from the DB
81 // if they are not passed in then we'd rather they are calculated.
82 // Note update is not recomended anyway...
83 foreach (['sort_name', 'display_name'] as $fieldToNotSet) {
84 unset($existing[$fieldToNotSet]);
85 }
86 $p = array_merge($existing, $apiRequest['params']);
87 return civicrm_api($apiRequest['entity'], 'create', $p);
88 }