Merge pull request #19965 from eileenmcnaughton/mem_trxn
[civicrm-core.git] / api / v3 / Generic / Setvalue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CiviCRM_APIv3
14 */
15
16 /**
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
21 *
22 * @param array $apiRequest
23 *
24 * @throws API_Exception
25 * @return array
26 */
27 function civicrm_api3_generic_setValue($apiRequest) {
28 $entity = $apiRequest['entity'];
29 $params = $apiRequest['params'];
30 $id = $params['id'];
31 if (!is_numeric($id)) {
32 return civicrm_api3_create_error(ts('Please enter a number'), [
33 'error_code' => 'NaN',
34 'field' => "id",
35 ]);
36 }
37
38 $field = CRM_Utils_String::munge($params['field']);
39 $value = $params['value'];
40
41 $fields = civicrm_api($entity, 'getFields', [
42 'version' => 3,
43 'action' => 'create',
44 ]);
45 // getfields error, shouldn't happen.
46 if ($fields['is_error']) {
47 return $fields;
48 }
49 $fields = $fields['values'];
50
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)) {
55 return civicrm_api3_create_error("Param 'field' ($field) is invalid. must be an existing field", ["error_code" => "invalid_field", "fields" => array_keys($fields)]);
56 }
57
58 $def = $fields[$fieldKey];
59 $title = CRM_Utils_Array::value('title', $def, ts('Field'));
60 // Disallow empty values except for the number zero.
61 // TODO: create a utility for this since it's needed in many places
62 if (!empty($def['required']) || !empty($def['is_required'])) {
63 if ((empty($value) || $value === 'null') && $value !== '0' && $value !== 0) {
64 return civicrm_api3_create_error(ts('%1 is a required field.', [1 => $title]), ["error_code" => "required", "field" => $field]);
65 }
66 }
67
68 switch ($def['type']) {
69 case CRM_Utils_Type::T_FLOAT:
70 if (!is_numeric($value) && !empty($value) && $value !== 'null') {
71 return civicrm_api3_create_error(ts('%1 must be a number.', [1 => $title]), ['error_code' => 'NaN']);
72 }
73 break;
74
75 case CRM_Utils_Type::T_INT:
76 if (!CRM_Utils_Rule::integer($value) && !empty($value) && $value !== 'null') {
77 return civicrm_api3_create_error(ts('%1 must be a number.', [1 => $title]), ['error_code' => 'NaN']);
78 }
79 break;
80
81 case CRM_Utils_Type::T_STRING:
82 case CRM_Utils_Type::T_TEXT:
83 if (array_key_exists('maxlength', $def)) {
84 $value = substr($value, 0, $def['maxlength']);
85 }
86 break;
87
88 case CRM_Utils_Type::T_DATE:
89 $value = CRM_Utils_Type::escape($value, "Date", FALSE);
90 if (!$value) {
91 return civicrm_api3_create_error("Param '$field' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS");
92 }
93 break;
94
95 case CRM_Utils_Type::T_BOOLEAN:
96 // Allow empty value for non-required fields
97 if ($value === '' || $value === 'null') {
98 $value = '';
99 }
100 else {
101 $value = (boolean) $value;
102 }
103 break;
104
105 default:
106 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']);
107 }
108
109 $dao_name = _civicrm_api3_get_DAO($entity);
110 $params = ['id' => $id, $field => $value];
111
112 if ((!empty($def['pseudoconstant']) || !empty($def['option_group_id'])) && $value !== '' && $value !== 'null') {
113 _civicrm_api3_api_match_pseudoconstant($params[$field], $entity, $field, $def);
114 }
115
116 CRM_Utils_Hook::pre('edit', $entity, $id, $params);
117
118 // Custom fields
119 if ($isCustom) {
120 CRM_Utils_Array::crmReplaceKey($params, 'id', 'entityID');
121 // Treat 'null' as empty value. This is awful but the rest of the code supports it.
122 if ($params[$field] === 'null') {
123 $params[$field] = '';
124 }
125 CRM_Core_BAO_CustomValueTable::setValues($params);
126 CRM_Utils_Hook::post('edit', $entity, $id);
127 }
128 // Core fields
129 elseif (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $params[$field])) {
130 $entityDAO = new $dao_name();
131 $entityDAO->copyValues($params);
132 CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO);
133 }
134 else {
135 return civicrm_api3_create_error("error assigning $field=$value for $entity (id=$id)");
136 }
137
138 // Add changelog entry - TODO: Should we do this for other entities as well?
139 if (strtolower($entity) === 'contact') {
140 CRM_Core_BAO_Log::register($id, 'civicrm_contact', $id);
141 }
142
143 return civicrm_api3_create_success($params);
144 }