Merge pull request #12426 from totten/master-container-cache
[civicrm-core.git] / CRM / Core / BAO / CustomValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * Business objects for managing custom data values.
36 */
37 class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
38
39 /**
40 * Validate a value against a CustomField type.
41 *
42 * @param string $type
43 * The type of the data.
44 * @param string $value
45 * The data to be validated.
46 *
47 * @return bool
48 * True if the value is of the specified type
49 */
50 public static function typecheck($type, $value) {
51 switch ($type) {
52 case 'Memo':
53 return TRUE;
54
55 case 'String':
56 return CRM_Utils_Rule::string($value);
57
58 case 'Int':
59 return CRM_Utils_Rule::integer($value);
60
61 case 'Float':
62 case 'Money':
63 return CRM_Utils_Rule::numeric($value);
64
65 case 'Date':
66 if (is_numeric($value)) {
67 return CRM_Utils_Rule::dateTime($value);
68 }
69 else {
70 return CRM_Utils_Rule::date($value);
71 }
72 case 'Boolean':
73 return CRM_Utils_Rule::boolean($value);
74
75 case 'ContactReference':
76 return CRM_Utils_Rule::validContact($value);
77
78 case 'StateProvince':
79
80 //fix for multi select state, CRM-3437
81 $valid = FALSE;
82 $mulValues = explode(',', $value);
83 foreach ($mulValues as $key => $state) {
84 $valid = array_key_exists(strtolower(trim($state)),
85 array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)
86 ) || array_key_exists(strtolower(trim($state)),
87 array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER)
88 );
89 if (!$valid) {
90 break;
91 }
92 }
93 return $valid;
94
95 case 'Country':
96
97 //fix multi select country, CRM-3437
98 $valid = FALSE;
99 $mulValues = explode(',', $value);
100 foreach ($mulValues as $key => $country) {
101 $valid = array_key_exists(strtolower(trim($country)),
102 array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)
103 ) || array_key_exists(strtolower(trim($country)),
104 array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER)
105 );
106 if (!$valid) {
107 break;
108 }
109 }
110 return $valid;
111
112 case 'Link':
113 return CRM_Utils_Rule::url($value);
114 }
115 return FALSE;
116 }
117
118 /**
119 * Given a 'civicrm' type string, return the mysql data store area
120 *
121 * @param string $type
122 * The civicrm type string.
123 *
124 * @return string|null
125 * the mysql data store placeholder
126 */
127 public static function typeToField($type) {
128 switch ($type) {
129 case 'String':
130 case 'File':
131 return 'char_data';
132
133 case 'Boolean':
134 case 'Int':
135 case 'StateProvince':
136 case 'Country':
137 case 'Auto-complete':
138 return 'int_data';
139
140 case 'Float':
141 return 'float_data';
142
143 case 'Money':
144 return 'decimal_data';
145
146 case 'Memo':
147 return 'memo_data';
148
149 case 'Date':
150 return 'date_data';
151
152 case 'Link':
153 return 'char_data';
154
155 default:
156 return NULL;
157 }
158 }
159
160
161 /**
162 * @param array $formValues
163 * @return null
164 */
165 public static function fixCustomFieldValue(&$formValues) {
166 if (empty($formValues)) {
167 return NULL;
168 }
169 foreach (array_keys($formValues) as $key) {
170 if (substr($key, 0, 7) != 'custom_') {
171 continue;
172 }
173 elseif (empty($formValues[$key])) {
174 continue;
175 }
176
177 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
178 substr($key, 7), 'html_type'
179 );
180 $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
181 substr($key, 7), 'data_type'
182 );
183
184 if (is_array($formValues[$key])) {
185 if (!in_array(key($formValues[$key]), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
186 $formValues[$key] = array('IN' => $formValues[$key]);
187 }
188 }
189 elseif (($htmlType == 'TextArea' ||
190 ($htmlType == 'Text' && $dataType == 'String')
191 ) && strstr($formValues[$key], '%')
192 ) {
193 $formValues[$key] = array('LIKE' => $formValues[$key]);
194 }
195 }
196 }
197
198 /**
199 * Delete option value give an option value and custom group id.
200 *
201 * @param int $customValueID
202 * Custom value ID.
203 * @param int $customGroupID
204 * Custom group ID.
205 */
206 public static function deleteCustomValue($customValueID, $customGroupID) {
207 // first we need to find custom value table, from custom group ID
208 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name');
209
210 // Retrieve the $entityId so we can pass that to the hook.
211 $entityID = CRM_Core_DAO::singleValueQuery("SELECT entity_id FROM {$tableName} WHERE id = %1", array(
212 1 => array($customValueID, 'Integer'),
213 ));
214
215 // delete custom value from corresponding custom value table
216 $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}";
217 CRM_Core_DAO::executeQuery($sql);
218
219 CRM_Utils_Hook::custom('delete',
220 $customGroupID,
221 $entityID,
222 $customValueID
223 );
224 }
225
226 }