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