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