civicrm_api3_create_error, api/v3/Generic/Update - Remove unnecessary indirection
[civicrm-core.git] / api / v3 / Generic / Update.php
CommitLineData
6a488035 1<?php
1c88e578 2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
1c88e578 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*/
6a488035
TO
28
29/**
e2747b59 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 *
6a488035
TO
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 */
45function civicrm_api3_generic_update($apiRequest) {
e2747b59 46 //$key_id = strtolower ($apiRequest['entity'])."_id";
47 $key_id = "id";
48 if (!array_key_exists($key_id, $apiRequest['params'])) {
b1cb6336 49 return civicrm_api3_create_error("Mandatory parameter missing $key_id");
6a488035 50 }
e386a062 51 // @fixme
d085c5eb 52 // tests show that contribution works better with create
53 // this is horrible but to make it work we'll just handle it separately
54 if(strtolower($apiRequest['entity']) == 'contribution'){
55 return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
56 }
e2747b59 57 $seek = array($key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']);
58 $existing = civicrm_api($apiRequest['entity'], 'get', $seek);
59 if ($existing['is_error']) {
60 return $existing;
61 }
62 if ($existing['count'] > 1) {
b1cb6336 63 return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 64 }
65 if ($existing['count'] == 0) {
b1cb6336 66 return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
e2747b59 67 }
68
69 $existing = array_pop($existing['values']);
70 $p = array_merge($existing, $apiRequest['params']);
71 return civicrm_api($apiRequest['entity'], 'create', $p);
6a488035
TO
72}
73