Afform - add entityRef widget support.
[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:
c866eb5f
TO
83 if (array_key_exists('maxlength', $def)) {
84 $value = substr($value, 0, $def['maxlength']);
85 }
86 break;
6a488035 87
da54ec85 88 case CRM_Utils_Type::T_DATE:
be2e0c6a 89 $value = CRM_Utils_Type::escape($value, "Date", FALSE);
6c552737 90 if (!$value) {
6a488035 91 return civicrm_api3_create_error("Param '$field' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS");
6c552737 92 }
6a488035
TO
93 break;
94
da54ec85 95 case CRM_Utils_Type::T_BOOLEAN:
47737104
CW
96 // Allow empty value for non-required fields
97 if ($value === '' || $value === 'null') {
98 $value = '';
99 }
100 else {
101 $value = (boolean) $value;
102 }
6a488035
TO
103 break;
104
105 default:
cf8f0fff 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']);
6a488035
TO
107 }
108
abe95f29 109 $dao_name = _civicrm_api3_get_DAO($entity);
cf8f0fff 110 $params = ['id' => $id, $field => $value];
47737104
CW
111
112 if ((!empty($def['pseudoconstant']) || !empty($def['option_group_id'])) && $value !== '' && $value !== 'null') {
99efe93a 113 _civicrm_api3_api_match_pseudoconstant($params[$field], $entity, $field, $def);
47737104
CW
114 }
115
75c9b470
CW
116 CRM_Utils_Hook::pre('edit', $entity, $id, $params);
117
7a8e775a 118 // Custom fields
47737104 119 if ($isCustom) {
7a8e775a 120 CRM_Utils_Array::crmReplaceKey($params, 'id', 'entityID');
47737104
CW
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 }
7a8e775a 125 CRM_Core_BAO_CustomValueTable::setValues($params);
1273d77c 126 CRM_Utils_Hook::post('edit', $entity, $id);
7a8e775a
CW
127 }
128 // Core fields
129 elseif (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $params[$field])) {
abe95f29 130 $entityDAO = new $dao_name();
131 $entityDAO->copyValues($params);
bb0c64a4 132 CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO);
6a488035
TO
133 }
134 else {
135 return civicrm_api3_create_error("error assigning $field=$value for $entity (id=$id)");
136 }
99efe93a
CW
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);
6a488035 144}