Merge pull request #16762 from eileenmcnaughton/mem_allan
[civicrm-core.git] / api / v3 / Generic / Setvalue.php
CommitLineData
6a488035 1<?php
b081365f
CW
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
b081365f 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
b081365f
CW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * @package CiviCRM_APIv3
14 */
15
6a488035 16/**
2fb1dd66
EM
17 * Set a single value using the api.
18 *
19 * This function is called when no specific setvalue api exists.
20 * Params must contain at least id=xx & {one of the fields from getfields}=value
9657ccf2 21 *
72b3a70c 22 * @param array $apiRequest
9657ccf2
EM
23 *
24 * @throws API_Exception
25 * @return array
6a488035
TO
26 */
27function civicrm_api3_generic_setValue($apiRequest) {
28 $entity = $apiRequest['entity'];
29 $params = $apiRequest['params'];
6a488035
TO
30 $id = $params['id'];
31 if (!is_numeric($id)) {
cf8f0fff 32 return civicrm_api3_create_error(ts('Please enter a number'), [
2fb1dd66
EM
33 'error_code' => 'NaN',
34 'field' => "id",
cf8f0fff 35 ]);
6a488035
TO
36 }
37
38 $field = CRM_Utils_String::munge($params['field']);
39 $value = $params['value'];
40
cf8f0fff 41 $fields = civicrm_api($entity, 'getFields', [
c1a920f1
EM
42 'version' => 3,
43 'action' => 'create',
7c31ae57 44 ]);
6a488035 45 // getfields error, shouldn't happen.
6c552737
TO
46 if ($fields['is_error']) {
47 return $fields;
48 }
6a488035
TO
49 $fields = $fields['values'];
50
47737104
CW
51 $isCustom = strpos($field, 'custom_') === 0;
52 // Trim off the id portion of a multivalued custom field name
53 $fieldKey = $isCustom && substr_count($field, '_') > 1 ? rtrim(rtrim($field, '1234567890'), '_') : $field;
54 if (!array_key_exists($fieldKey, $fields)) {
cf8f0fff 55 return civicrm_api3_create_error("Param 'field' ($field) is invalid. must be an existing field", ["error_code" => "invalid_field", "fields" => array_keys($fields)]);
6a488035
TO
56 }
57
47737104
CW
58 $def = $fields[$fieldKey];
59 $title = CRM_Utils_Array::value('title', $def, ts('Field'));
5ba3bfc8
CW
60 // Disallow empty values except for the number zero.
61 // TODO: create a utility for this since it's needed in many places
47737104
CW
62 if (!empty($def['required']) || !empty($def['is_required'])) {
63 if ((empty($value) || $value === 'null') && $value !== '0' && $value !== 0) {
cf8f0fff 64 return civicrm_api3_create_error(ts('%1 is a required field.', [1 => $title]), ["error_code" => "required", "field" => $field]);
47737104 65 }
6a488035
TO
66 }
67
68 switch ($def['type']) {
47737104
CW
69 case CRM_Utils_Type::T_FLOAT:
70 if (!is_numeric($value) && !empty($value) && $value !== 'null') {
cf8f0fff 71 return civicrm_api3_create_error(ts('%1 must be a number.', [1 => $title]), ['error_code' => 'NaN']);
47737104 72 }
c866eb5f 73 break;
47737104 74
da54ec85 75 case CRM_Utils_Type::T_INT:
47737104 76 if (!CRM_Utils_Rule::integer($value) && !empty($value) && $value !== 'null') {
cf8f0fff 77 return civicrm_api3_create_error(ts('%1 must be a number.', [1 => $title]), ['error_code' => 'NaN']);
6a488035 78 }
c866eb5f 79 break;
6a488035 80
da54ec85
CW
81 case CRM_Utils_Type::T_STRING:
82 case CRM_Utils_Type::T_TEXT:
6a488035 83 if (!CRM_Utils_Rule::xssString($value)) {
cf8f0fff 84 return civicrm_api3_create_error(ts('Illegal characters in input (potential scripting attack)'), ['error_code' => 'XSS']);
6a488035 85 }
c866eb5f
TO
86 if (array_key_exists('maxlength', $def)) {
87 $value = substr($value, 0, $def['maxlength']);
88 }
89 break;
6a488035 90
da54ec85 91 case CRM_Utils_Type::T_DATE:
be2e0c6a 92 $value = CRM_Utils_Type::escape($value, "Date", FALSE);
6c552737 93 if (!$value) {
6a488035 94 return civicrm_api3_create_error("Param '$field' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS");
6c552737 95 }
6a488035
TO
96 break;
97
da54ec85 98 case CRM_Utils_Type::T_BOOLEAN:
47737104
CW
99 // Allow empty value for non-required fields
100 if ($value === '' || $value === 'null') {
101 $value = '';
102 }
103 else {
104 $value = (boolean) $value;
105 }
6a488035
TO
106 break;
107
108 default:
cf8f0fff 109 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']);
6a488035
TO
110 }
111
abe95f29 112 $dao_name = _civicrm_api3_get_DAO($entity);
cf8f0fff 113 $params = ['id' => $id, $field => $value];
47737104
CW
114
115 if ((!empty($def['pseudoconstant']) || !empty($def['option_group_id'])) && $value !== '' && $value !== 'null') {
99efe93a 116 _civicrm_api3_api_match_pseudoconstant($params[$field], $entity, $field, $def);
47737104
CW
117 }
118
75c9b470
CW
119 CRM_Utils_Hook::pre('edit', $entity, $id, $params);
120
7a8e775a 121 // Custom fields
47737104 122 if ($isCustom) {
7a8e775a 123 CRM_Utils_Array::crmReplaceKey($params, 'id', 'entityID');
47737104
CW
124 // Treat 'null' as empty value. This is awful but the rest of the code supports it.
125 if ($params[$field] === 'null') {
126 $params[$field] = '';
127 }
7a8e775a 128 CRM_Core_BAO_CustomValueTable::setValues($params);
1273d77c 129 CRM_Utils_Hook::post('edit', $entity, $id);
7a8e775a
CW
130 }
131 // Core fields
132 elseif (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $params[$field])) {
abe95f29 133 $entityDAO = new $dao_name();
134 $entityDAO->copyValues($params);
bb0c64a4 135 CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO);
6a488035
TO
136 }
137 else {
138 return civicrm_api3_create_error("error assigning $field=$value for $entity (id=$id)");
139 }
99efe93a
CW
140
141 // Add changelog entry - TODO: Should we do this for other entities as well?
142 if (strtolower($entity) === 'contact') {
143 CRM_Core_BAO_Log::register($id, 'civicrm_contact', $id);
144 }
145
146 return civicrm_api3_create_success($params);
6a488035 147}