Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / Civi / Api4 / Utils / FormattingUtil.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Utils;
23
24 use CRM_Utils_Array as UtilsArray;
25
26 require_once 'api/v3/utils.php';
27
28 class FormattingUtil {
29
30 /**
31 * Massage values into the format the BAO expects for a write operation
32 *
33 * @param $params
34 * @param $entity
35 * @param $fields
36 * @throws \API_Exception
37 */
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') {
44 $value = 'Null';
45 }
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;
50 }
51 }
52 /*
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'.
56 *
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
62 * 'Null'.
63 */
64 elseif (array_key_exists($name, $params) && $params[$name] === NULL) {
65 $params[$name] = 'null';
66 }
67 }
68 }
69
70 /**
71 * Transform raw api input to appropriate format for use in a SQL query.
72 *
73 * This is used by read AND write actions (Get, Create, Update, Replace)
74 *
75 * @param $value
76 * @param $fieldSpec
77 * @param string $entity
78 * Ex: 'Contact', 'Domain'
79 * @throws \API_Exception
80 */
81 public static function formatValue(&$value, $fieldSpec, $entity) {
82 if (is_array($value)) {
83 foreach ($value as &$val) {
84 self::formatValue($val, $fieldSpec, $entity);
85 }
86 return;
87 }
88 $fk = UtilsArray::value('fk_entity', $fieldSpec);
89 if ($fieldSpec['name'] == 'id') {
90 $fk = $entity;
91 }
92 $dataType = UtilsArray::value('data_type', $fieldSpec);
93
94 if ($fk === 'Domain' && $value === 'current_domain') {
95 $value = \CRM_Core_Config::domainID();
96 }
97
98 if ($fk === 'Contact' && !is_numeric($value)) {
99 $value = \_civicrm_api3_resolve_contactID($value);
100 if ('unknown-user' === $value) {
101 throw new \API_Exception("\"{$fieldSpec['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldSpec['name'], "type" => "integer"]);
102 }
103 }
104
105 switch ($dataType) {
106 case 'Timestamp':
107 $value = date('Y-m-d H:i:s', strtotime($value));
108 break;
109
110 case 'Date':
111 $value = date('Ymd', strtotime($value));
112 break;
113 }
114 }
115
116 }