Merge pull request #14981 from eileenmcnaughton/load_extract
[civicrm-core.git] / api / v3 / Generic / Update.php
CommitLineData
6a488035 1<?php
1c88e578 2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
1c88e578 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
1c88e578 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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
b081365f
CW
28/**
29 * @package CiviCRM_APIv3
30 */
31
6a488035 32/**
cd5823ae
EM
33 * Update function is basically a hack.
34 *
35 * We want to remove it but must resolve issues in
e2747b59 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 *
c28e1768
CW
43 * @deprecated
44 *
cf470720 45 * @param array $apiRequest
16b10e64
CW
46 * Array with keys:
47 * - entity: string
48 * - action: string
49 * - version: string
50 * - function: callback (mixed)
51 * - params: array, varies
77b97be7
EM
52 *
53 * @return array|int|mixed
6a488035
TO
54 */
55function civicrm_api3_generic_update($apiRequest) {
e2747b59 56 //$key_id = strtolower ($apiRequest['entity'])."_id";
57 $key_id = "id";
58 if (!array_key_exists($key_id, $apiRequest['params'])) {
b1cb6336 59 return civicrm_api3_create_error("Mandatory parameter missing $key_id");
6a488035 60 }
e386a062 61 // @fixme
d085c5eb 62 // tests show that contribution works better with create
63 // this is horrible but to make it work we'll just handle it separately
9b873358 64 if (strtolower($apiRequest['entity']) == 'contribution') {
d085c5eb 65 return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
66 }
cf8f0fff 67 $seek = [$key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']];
e2747b59 68 $existing = civicrm_api($apiRequest['entity'], 'get', $seek);
69 if ($existing['is_error']) {
70 return $existing;
71 }
72 if ($existing['count'] > 1) {
b1cb6336 73 return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 74 }
75 if ($existing['count'] == 0) {
b1cb6336 76 return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 77 }
78
79 $existing = array_pop($existing['values']);
1bdbd4ec 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 }
e2747b59 86 $p = array_merge($existing, $apiRequest['params']);
87 return civicrm_api($apiRequest['entity'], 'create', $p);
6a488035 88}