Api docblock cleanup.
[civicrm-core.git] / api / v3 / Generic / Setvalue.php
CommitLineData
6a488035 1<?php
b081365f
CW
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
6a488035 32/**
2fb1dd66
EM
33 * Set a single value using the api.
34 *
35 * This function is called when no specific setvalue api exists.
36 * Params must contain at least id=xx & {one of the fields from getfields}=value
9657ccf2 37 *
72b3a70c 38 * @param array $apiRequest
9657ccf2
EM
39 *
40 * @throws API_Exception
41 * @return array
6a488035
TO
42 */
43function civicrm_api3_generic_setValue($apiRequest) {
44 $entity = $apiRequest['entity'];
45 $params = $apiRequest['params'];
46 // we can't use _spec, doesn't work with generic
47 civicrm_api3_verify_mandatory($params, NULL, array('id', 'field', 'value'));
48 $id = $params['id'];
49 if (!is_numeric($id)) {
2fb1dd66
EM
50 return civicrm_api3_create_error(ts('Please enter a number'), array(
51 'error_code' => 'NaN',
52 'field' => "id",
53 ));
6a488035
TO
54 }
55
56 $field = CRM_Utils_String::munge($params['field']);
57 $value = $params['value'];
58
c1a920f1
EM
59 $fields = civicrm_api($entity, 'getFields', array(
60 'version' => 3,
61 'action' => 'create',
62 "sequential")
63 );
6a488035 64 // getfields error, shouldn't happen.
6c552737
TO
65 if ($fields['is_error']) {
66 return $fields;
67 }
6a488035
TO
68 $fields = $fields['values'];
69
47737104
CW
70 $isCustom = strpos($field, 'custom_') === 0;
71 // Trim off the id portion of a multivalued custom field name
72 $fieldKey = $isCustom && substr_count($field, '_') > 1 ? rtrim(rtrim($field, '1234567890'), '_') : $field;
73 if (!array_key_exists($fieldKey, $fields)) {
6a488035
TO
74 return civicrm_api3_create_error("Param 'field' ($field) is invalid. must be an existing field", array("error_code" => "invalid_field", "fields" => array_keys($fields)));
75 }
76
47737104
CW
77 $def = $fields[$fieldKey];
78 $title = CRM_Utils_Array::value('title', $def, ts('Field'));
5ba3bfc8
CW
79 // Disallow empty values except for the number zero.
80 // TODO: create a utility for this since it's needed in many places
47737104
CW
81 if (!empty($def['required']) || !empty($def['is_required'])) {
82 if ((empty($value) || $value === 'null') && $value !== '0' && $value !== 0) {
83 return civicrm_api3_create_error(ts('%1 is a required field.', array(1 => $title)), array("error_code" => "required", "field" => $field));
84 }
6a488035
TO
85 }
86
87 switch ($def['type']) {
47737104
CW
88 case CRM_Utils_Type::T_FLOAT:
89 if (!is_numeric($value) && !empty($value) && $value !== 'null') {
90 return civicrm_api3_create_error(ts('%1 must be a number.', array(1 => $title)), array('error_code' => 'NaN'));
91 }
c866eb5f 92 break;
47737104 93
da54ec85 94 case CRM_Utils_Type::T_INT:
47737104
CW
95 if (!CRM_Utils_Rule::integer($value) && !empty($value) && $value !== 'null') {
96 return civicrm_api3_create_error(ts('%1 must be a number.', array(1 => $title)), array('error_code' => 'NaN'));
6a488035 97 }
c866eb5f 98 break;
6a488035 99
da54ec85
CW
100 case CRM_Utils_Type::T_STRING:
101 case CRM_Utils_Type::T_TEXT:
6a488035
TO
102 if (!CRM_Utils_Rule::xssString($value)) {
103 return civicrm_api3_create_error(ts('Illegal characters in input (potential scripting attack)'), array('error_code' => 'XSS'));
104 }
c866eb5f
TO
105 if (array_key_exists('maxlength', $def)) {
106 $value = substr($value, 0, $def['maxlength']);
107 }
108 break;
6a488035 109
da54ec85 110 case CRM_Utils_Type::T_DATE:
be2e0c6a 111 $value = CRM_Utils_Type::escape($value, "Date", FALSE);
6c552737 112 if (!$value) {
6a488035 113 return civicrm_api3_create_error("Param '$field' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS");
6c552737 114 }
6a488035
TO
115 break;
116
da54ec85 117 case CRM_Utils_Type::T_BOOLEAN:
47737104
CW
118 // Allow empty value for non-required fields
119 if ($value === '' || $value === 'null') {
120 $value = '';
121 }
122 else {
123 $value = (boolean) $value;
124 }
6a488035
TO
125 break;
126
127 default:
92fcb95f 128 return civicrm_api3_create_error("Param '$field' is of a type not managed yet (" . $def['type'] . "). Join the API team and help us implement it", array('error_code' => 'NOT_IMPLEMENTED'));
6a488035
TO
129 }
130
abe95f29 131 $dao_name = _civicrm_api3_get_DAO($entity);
75c9b470 132 $params = array('id' => $id, $field => $value);
47737104
CW
133
134 if ((!empty($def['pseudoconstant']) || !empty($def['option_group_id'])) && $value !== '' && $value !== 'null') {
99efe93a 135 _civicrm_api3_api_match_pseudoconstant($params[$field], $entity, $field, $def);
47737104
CW
136 }
137
75c9b470
CW
138 CRM_Utils_Hook::pre('edit', $entity, $id, $params);
139
7a8e775a 140 // Custom fields
47737104 141 if ($isCustom) {
7a8e775a 142 CRM_Utils_Array::crmReplaceKey($params, 'id', 'entityID');
47737104
CW
143 // Treat 'null' as empty value. This is awful but the rest of the code supports it.
144 if ($params[$field] === 'null') {
145 $params[$field] = '';
146 }
7a8e775a
CW
147 CRM_Core_BAO_CustomValueTable::setValues($params);
148 CRM_Utils_Hook::post('edit', $entity, $id, CRM_Core_DAO::$_nullObject);
7a8e775a
CW
149 }
150 // Core fields
151 elseif (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $params[$field])) {
abe95f29 152 $entityDAO = new $dao_name();
153 $entityDAO->copyValues($params);
bb0c64a4 154 CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO);
6a488035
TO
155 }
156 else {
157 return civicrm_api3_create_error("error assigning $field=$value for $entity (id=$id)");
158 }
99efe93a
CW
159
160 // Add changelog entry - TODO: Should we do this for other entities as well?
161 if (strtolower($entity) === 'contact') {
162 CRM_Core_BAO_Log::register($id, 'civicrm_contact', $id);
163 }
164
165 return civicrm_api3_create_success($params);
6a488035 166}