4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Utils
;
15 use Civi\Api4\Query\SqlExpression
;
17 require_once 'api/v3/utils.php';
19 class FormattingUtil
{
24 public static $pseudoConstantContexts = [
26 'abbr' => 'abbreviate',
33 public static $pseudoConstantSuffixes = ['name', 'abbr', 'label', 'color', 'description', 'icon'];
36 * Massage values into the format the BAO expects for a write operation
38 * @param array $params
39 * @param array $fields
40 * @throws \API_Exception
42 public static function formatWriteParams(&$params, $fields) {
43 foreach ($fields as $name => $field) {
44 if (!empty($params[$name])) {
45 $value =& $params[$name];
46 // Hack for null values -- see comment below
47 if ($value === 'null') {
50 self
::formatInputValue($value, $name, $field);
51 // Ensure we have an array for serialized fields
52 if (!empty($field['serialize'] && !is_array($value))) {
53 $value = (array) $value;
57 * Because of the wacky way that database values are saved we need to format
58 * some of the values here. In this strange world the string 'null' is used to
59 * unset values. Hence if we encounter true null we change it to string 'null'.
61 * If we encounter the string 'null' then we assume the user actually wants to
62 * set the value to string null. However since the string null is reserved for
63 * unsetting values we must change it. Another quirk of the DB_DataObject is
64 * that it allows 'Null' to be set, but any other variation of string 'null'
65 * will be converted to true null, e.g. 'nuLL', 'NUlL' etc. so we change it to
68 elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
69 $params[$name] = 'null';
73 \CRM_Utils_API_HTMLInputCoder
::singleton()->encodeRow($params);
77 * Transform raw api input to appropriate format for use in a SQL query.
79 * This is used by read AND write actions (Get, Create, Update, Replace)
82 * @param string $fieldName
83 * @param array $fieldSpec
84 * @param string $operator (only for 'get' actions)
85 * @param int $index (for recursive loops)
86 * @throws \API_Exception
87 * @throws \CRM_Core_Exception
89 public static function formatInputValue(&$value, $fieldName, $fieldSpec, &$operator = NULL, $index = NULL) {
90 // Evaluate pseudoconstant suffix
91 $suffix = strpos($fieldName, ':');
93 $options = self
::getPseudoconstantList($fieldSpec, $fieldName, [], $operator ?
'get' : 'create');
94 $value = self
::replacePseudoconstant($options, $value, TRUE);
97 elseif (is_array($value)) {
99 foreach ($value as &$val) {
100 self
::formatInputValue($val, $fieldName, $fieldSpec, $operator, $i++
);
104 $fk = $fieldSpec['name'] == 'id' ?
$fieldSpec['entity'] : $fieldSpec['fk_entity'] ??
NULL;
106 if ($fk === 'Domain' && $value === 'current_domain') {
107 $value = \CRM_Core_Config
::domainID();
110 if ($fk === 'Contact' && !is_numeric($value)) {
111 $value = \
_civicrm_api3_resolve_contactID($value);
112 if ('unknown-user' === $value) {
113 throw new \
API_Exception("\"{$fieldSpec['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldSpec['name'], "type" => "integer"]);
117 switch ($fieldSpec['data_type'] ??
NULL) {
119 $value = self
::formatDateValue('Y-m-d H:i:s', $value, $operator, $index);
123 $value = self
::formatDateValue('Ymd', $value, $operator, $index);
127 $hic = \CRM_Utils_API_HTMLInputCoder
::singleton();
128 if (is_string($value) && !$hic->isSkippedField($fieldSpec['name'])) {
129 $value = $hic->encodeValue($value);
134 * Parse date expressions.
136 * Expands relative date range expressions, modifying the sql operator if necessary
142 * @return array|string
144 private static function formatDateValue($format, $value, &$operator = NULL, $index = NULL) {
145 // Non-relative dates (or if no search operator)
146 if (!$operator ||
!array_key_exists($value, \CRM_Core_OptionGroup
::values('relative_date_filters'))) {
147 return date($format, strtotime($value));
149 if (isset($index) && !strstr($operator, 'BETWEEN')) {
150 throw new \
API_Exception("Relative dates cannot be in an array using the $operator operator.");
152 [$dateFrom, $dateTo] = \CRM_Utils_Date
::getFromTo($value);
154 // Convert relative date filters to use BETWEEN/NOT BETWEEN operator
160 $operator = ($operator === '=' ||
$operator === 'LIKE') ?
'BETWEEN' : 'NOT BETWEEN';
161 return [self
::formatDateValue($format, $dateFrom), self
::formatDateValue($format, $dateTo)];
163 // Less-than or greater-than-equal-to comparisons use the lower value
166 return self
::formatDateValue($format, $dateFrom);
168 // Greater-than or less-than-equal-to comparisons use the higher value
171 return self
::formatDateValue($format, $dateTo);
173 // For BETWEEN expressions, we are already inside a loop of the 2 values, so give the lower value if index=0, higher value if index=1
176 return self
::formatDateValue($format, $index ?
$dateTo : $dateFrom);
179 throw new \
API_Exception("Relative dates cannot be used with the $operator operator.");
184 * Unserialize raw DAO values and convert to correct type
186 * @param array $results
187 * @param array $fields
188 * @param string $action
189 * @param array $selectAliases
190 * @throws \API_Exception
191 * @throws \CRM_Core_Exception
193 public static function formatOutputValues(&$results, $fields, $action = 'get', $selectAliases = []) {
195 foreach ($results as &$result) {
196 $contactTypePaths = [];
197 foreach ($result as $key => $value) {
198 $fieldExpr = SqlExpression
::convert($selectAliases[$key] ??
$key);
199 $fieldName = \CRM_Utils_Array
::first($fieldExpr->getFields());
200 $baseName = $fieldName ? \CRM_Utils_Array
::first(explode(':', $fieldName)) : NULL;
201 $field = $fields[$fieldName] ??
$fields[$baseName] ??
NULL;
202 $dataType = $field['data_type'] ??
($fieldName == 'id' ?
'Integer' : NULL);
203 // Allow Sql Functions to do special formatting and/or alter the $dataType
204 if (method_exists($fieldExpr, 'formatOutputValue') && is_string($value)) {
205 $result[$key] = $value = $fieldExpr->formatOutputValue($value, $dataType);
207 if (!empty($field['output_formatters'])) {
208 self
::applyFormatters($result, $fieldName, $field, $value);
211 // Evaluate pseudoconstant suffixes
212 $suffix = strrpos($fieldName, ':');
214 $fieldOptions[$fieldName] = $fieldOptions[$fieldName] ?? self
::getPseudoconstantList($field, $fieldName, $result, $action);
217 if ($fieldExpr->supportsExpansion
) {
218 if (!empty($field['serialize']) && is_string($value)) {
219 $value = \CRM_Core_DAO
::unSerializeField($value, $field['serialize']);
221 if (isset($fieldOptions[$fieldName])) {
222 $value = self
::replacePseudoconstant($fieldOptions[$fieldName], $value);
225 // Keep track of contact types for self::contactFieldsToRemove
226 if ($value && isset($field['entity']) && $field['entity'] === 'Contact' && $field['name'] === 'contact_type') {
227 $prefix = strrpos($fieldName, '.');
228 $contactTypePaths[$prefix ?
substr($fieldName, 0, $prefix +
1) : ''] = $value;
230 $result[$key] = self
::convertDataType($value, $dataType);
232 // Remove inapplicable contact fields
233 foreach ($contactTypePaths as $prefix => $contactType) {
234 \CRM_Utils_Array
::remove($result, self
::contactFieldsToRemove($contactType, $prefix));
240 * Retrieves pseudoconstant option list for a field.
242 * @param array $field
243 * @param string $fieldAlias
244 * Field path plus pseudoconstant suffix, e.g. 'contact.employer_id.contact_sub_type:label'
245 * @param array $params
246 * Other values for this object
247 * @param string $action
249 * @throws \API_Exception
251 public static function getPseudoconstantList(array $field, string $fieldAlias, $params = [], $action = 'get') {
252 [$fieldPath, $valueType] = explode(':', $fieldAlias);
253 $context = self
::$pseudoConstantContexts[$valueType] ??
NULL;
254 // For create actions, only unique identifiers can be used.
255 // For get actions any valid suffix is ok.
256 if (($action === 'create' && !$context) ||
!in_array($valueType, self
::$pseudoConstantSuffixes, TRUE)) {
257 throw new \
API_Exception('Illegal expression');
259 $baoName = $context ? CoreUtil
::getBAOFromApiName($field['entity']) : NULL;
260 // Use BAO::buildOptions if possible
262 $fieldName = empty($field['custom_field_id']) ?
$field['name'] : 'custom_' . $field['custom_field_id'];
263 $options = $baoName::buildOptions($fieldName, $context, self
::filterByPrefix($params, $fieldPath, $field['name']));
265 // Fallback for option lists that exist in the api but not the BAO
266 if (!isset($options) ||
$options === FALSE) {
267 $options = civicrm_api4($field['entity'], 'getFields', ['action' => $action, 'loadOptions' => ['id', $valueType], 'where' => [['name', '=', $field['name']]]])[0]['options'] ??
NULL;
268 $options = $options ?
array_column($options, $valueType, 'id') : $options;
270 if (is_array($options)) {
273 throw new \
API_Exception("No option list found for '{$field['name']}'");
277 * Replaces value (or an array of values) with options from a pseudoconstant list.
279 * The direction of lookup defaults to transforming ids to option values for api output;
280 * for api input, set $reverse = TRUE to transform option values to ids.
282 * @param array $options
283 * @param string|string[] $value
284 * @param bool $reverse
285 * Is this a reverse lookup (for transforming input instead of output)
286 * @return array|mixed|null
288 public static function replacePseudoconstant($options, $value, $reverse = FALSE) {
290 foreach ((array) $value as $val) {
291 if (!$reverse && isset($options[$val])) {
292 $matches[] = $options[$val];
294 elseif ($reverse && array_search($val, $options) !== FALSE) {
295 $matches[] = array_search($val, $options);
298 return is_array($value) ?
$matches : $matches[0] ??
NULL;
302 * Apply a field's output_formatters callback functions
304 * @param array $result
305 * @param string $fieldPath
306 * @param array $field
307 * @param mixed $value
309 private static function applyFormatters(array $result, string $fieldPath, array $field, &$value) {
310 $row = self
::filterByPrefix($result, $fieldPath, $field['name']);
312 foreach ($field['output_formatters'] as $formatter) {
313 $formatter($value, $row, $field);
318 * @param mixed $value
319 * @param string $dataType
322 public static function convertDataType($value, $dataType) {
323 if (isset($value) && $dataType) {
324 if (is_array($value)) {
325 foreach ($value as $key => $val) {
326 $value[$key] = self
::convertDataType($val, $dataType);
333 return (bool) $value;
340 return (float) $value;
347 * Lists all field names (including suffixed variants) that should be removed for a given contact type.
349 * @param string $contactType
350 * Individual|Organization|Household
351 * @param string $prefix
352 * Path at which these fields are found, e.g. "address.contact."
355 public static function contactFieldsToRemove($contactType, $prefix) {
356 if (!isset(\Civi
::$statics[__CLASS__
][__FUNCTION__
][$contactType])) {
357 \Civi
::$statics[__CLASS__
][__FUNCTION__
][$contactType] = [];
358 foreach (\CRM_Contact_DAO_Contact
::fields() as $field) {
359 if (!empty($field['contactType']) && $field['contactType'] != $contactType) {
360 \Civi
::$statics[__CLASS__
][__FUNCTION__
][$contactType][] = $field['name'];
361 // Include suffixed variants like prefix_id:label
362 if (!empty($field['pseudoconstant'])) {
363 foreach (self
::$pseudoConstantSuffixes as $suffix) {
364 \Civi
::$statics[__CLASS__
][__FUNCTION__
][$contactType][] = $field['name'] . ':' . $suffix;
371 return array_map(function($name) use ($prefix) {
372 return $prefix . $name;
373 }, \Civi
::$statics[__CLASS__
][__FUNCTION__
][$contactType]);
377 * Given a field belonging to either the main entity or a joined entity,
378 * and a values array of [path => value], this returns all values which share the same root path.
380 * Works by filtering array keys to only include those with the same prefix as a given field,
381 * stripping them of that prefix.
386 * 'first_name' => 'a',
387 * 'middle_name' => 'b',
388 * 'related_contact.first_name' => 'c',
389 * 'related_contact.last_name' => 'd',
390 * 'activity.subject' => 'e',
392 * $fieldPath = 'related_contact.id'
395 * filterByPrefix($values, $fieldPath, $fieldName)
397 * 'first_name' => 'c',
398 * 'last_name' => 'd',
402 * @param array $values
403 * @param string $fieldPath
404 * @param string $fieldName
407 public static function filterByPrefix(array $values, string $fieldPath, string $fieldName): array {
409 $prefix = substr($fieldPath, 0, strpos($fieldPath, $fieldName));
410 foreach ($values as $key => $val) {
411 if (!$prefix ||
strpos($key, $prefix) === 0) {
412 $filtered[substr($key, strlen($prefix))] = $val;