Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
f299f7db | 7 | | Copyright CiviCRM LLC (c) 2004-2020 | |
380f3545 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
f299f7db | 32 | * @copyright CiviCRM LLC (c) 2004-2020 |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace Civi\Api4\Utils; |
39 | ||
40 | use CRM_Utils_Array as UtilsArray; | |
41 | ||
42 | require_once 'api/v3/utils.php'; | |
43 | ||
44 | class FormattingUtil { | |
45 | ||
46 | /** | |
47 | * Massage values into the format the BAO expects for a write operation | |
48 | * | |
49 | * @param $params | |
50 | * @param $entity | |
51 | * @param $fields | |
52 | * @throws \API_Exception | |
53 | */ | |
54 | public static function formatWriteParams(&$params, $entity, $fields) { | |
55 | foreach ($fields as $name => $field) { | |
56 | if (!empty($params[$name])) { | |
57 | $value =& $params[$name]; | |
58 | // Hack for null values -- see comment below | |
59 | if ($value === 'null') { | |
60 | $value = 'Null'; | |
61 | } | |
62 | FormattingUtil::formatValue($value, $field, $entity); | |
63 | // Ensure we have an array for serialized fields | |
64 | if (!empty($field['serialize'] && !is_array($value))) { | |
65 | $value = (array) $value; | |
66 | } | |
67 | } | |
68 | /* | |
69 | * Because of the wacky way that database values are saved we need to format | |
70 | * some of the values here. In this strange world the string 'null' is used to | |
71 | * unset values. Hence if we encounter true null we change it to string 'null'. | |
72 | * | |
73 | * If we encounter the string 'null' then we assume the user actually wants to | |
74 | * set the value to string null. However since the string null is reserved for | |
75 | * unsetting values we must change it. Another quirk of the DB_DataObject is | |
76 | * that it allows 'Null' to be set, but any other variation of string 'null' | |
77 | * will be converted to true null, e.g. 'nuLL', 'NUlL' etc. so we change it to | |
78 | * 'Null'. | |
79 | */ | |
80 | elseif (array_key_exists($name, $params) && $params[$name] === NULL) { | |
81 | $params[$name] = 'null'; | |
82 | } | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * Transform raw api input to appropriate format for use in a SQL query. | |
88 | * | |
89 | * This is used by read AND write actions (Get, Create, Update, Replace) | |
90 | * | |
91 | * @param $value | |
92 | * @param $fieldSpec | |
93 | * @param string $entity | |
94 | * Ex: 'Contact', 'Domain' | |
95 | * @throws \API_Exception | |
96 | */ | |
97 | public static function formatValue(&$value, $fieldSpec, $entity) { | |
98 | if (is_array($value)) { | |
99 | foreach ($value as &$val) { | |
100 | self::formatValue($val, $fieldSpec, $entity); | |
101 | } | |
102 | return; | |
103 | } | |
104 | $fk = UtilsArray::value('fk_entity', $fieldSpec); | |
105 | if ($fieldSpec['name'] == 'id') { | |
106 | $fk = $entity; | |
107 | } | |
108 | $dataType = UtilsArray::value('data_type', $fieldSpec); | |
109 | ||
110 | if ($fk === 'Domain' && $value === 'current_domain') { | |
111 | $value = \CRM_Core_Config::domainID(); | |
112 | } | |
113 | ||
114 | if ($fk === 'Contact' && !is_numeric($value)) { | |
115 | $value = \_civicrm_api3_resolve_contactID($value); | |
116 | if ('unknown-user' === $value) { | |
117 | throw new \API_Exception("\"{$fieldSpec['name']}\" \"{$value}\" cannot be resolved to a contact ID", 2002, ['error_field' => $fieldSpec['name'], "type" => "integer"]); | |
118 | } | |
119 | } | |
120 | ||
121 | switch ($dataType) { | |
122 | case 'Timestamp': | |
123 | $value = date('Y-m-d H:i:s', strtotime($value)); | |
124 | break; | |
125 | ||
126 | case 'Date': | |
127 | $value = date('Ymd', strtotime($value)); | |
128 | break; | |
129 | } | |
130 | } | |
131 | ||
132 | } |