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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Utils
;
24 use CRM_Utils_Array
as UtilsArray
;
26 require_once 'api/v3/utils.php';
28 class FormattingUtil
{
31 * Massage values into the format the BAO expects for a write operation
36 * @throws \API_Exception
38 public static function formatWriteParams(&$params, $entity, $fields) {
39 foreach ($fields as $name => $field) {
40 if (!empty($params[$name])) {
41 $value =& $params[$name];
42 // Hack for null values -- see comment below
43 if ($value === 'null') {
46 FormattingUtil
::formatValue($value, $field, $entity);
47 // Ensure we have an array for serialized fields
48 if (!empty($field['serialize'] && !is_array($value))) {
49 $value = (array) $value;
53 * Because of the wacky way that database values are saved we need to format
54 * some of the values here. In this strange world the string 'null' is used to
55 * unset values. Hence if we encounter true null we change it to string 'null'.
57 * If we encounter the string 'null' then we assume the user actually wants to
58 * set the value to string null. However since the string null is reserved for
59 * unsetting values we must change it. Another quirk of the DB_DataObject is
60 * that it allows 'Null' to be set, but any other variation of string 'null'
61 * will be converted to true null, e.g. 'nuLL', 'NUlL' etc. so we change it to
64 elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
65 $params[$name] = 'null';
69 \CRM_Utils_API_HTMLInputCoder
::singleton()->encodeRow($params);
73 * Transform raw api input to appropriate format for use in a SQL query.
75 * This is used by read AND write actions (Get, Create, Update, Replace)
79 * @param string $entity
80 * Ex: 'Contact', 'Domain'
81 * @throws \API_Exception
83 public static function formatValue(&$value, $fieldSpec, $entity) {
84 if (is_array($value)) {
85 foreach ($value as &$val) {
86 self
::formatValue($val, $fieldSpec, $entity);
90 $fk = UtilsArray
::value('fk_entity', $fieldSpec);
91 if ($fieldSpec['name'] == 'id') {
94 $dataType = UtilsArray
::value('data_type', $fieldSpec);
96 if ($fk === 'Domain' && $value === 'current_domain') {
97 $value = \CRM_Core_Config
::domainID();
100 if ($fk === 'Contact' && !is_numeric($value)) {
101 $value = \
_civicrm_api3_resolve_contactID($value);
102 if ('unknown-user' === $value) {
103 throw new \
API_Exception("\"{$fieldSpec['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldSpec['name'], "type" => "integer"]);
109 $value = date('Y-m-d H:i:s', strtotime($value));
113 $value = date('Ymd', strtotime($value));
117 $hic = \CRM_Utils_API_HTMLInputCoder
::singleton();
118 if (!$hic->isSkippedField($fieldSpec['name'])) {
119 $value = $hic->encodeValue($value);