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