Merge pull request #13736 from agileware/CIVICRM-1149
[civicrm-core.git] / CRM / Core / BAO / CustomValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * Business objects for managing custom data values.
6a488035
TO
36 */
37class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
38
39 /**
fe482240 40 * Validate a value against a CustomField type.
6a488035 41 *
6a0b768e
TO
42 * @param string $type
43 * The type of the data.
44 * @param string $value
45 * The data to be validated.
6a488035 46 *
8d7a9d07 47 * @return bool
a6c01b45 48 * True if the value is of the specified type
6a488035
TO
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)),
353ffa53
TO
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 );
6a488035
TO
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)),
353ffa53
TO
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 );
6a488035
TO
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 /**
100fef9d 119 * Given a 'civicrm' type string, return the mysql data store area
6a488035 120 *
6a0b768e
TO
121 * @param string $type
122 * The civicrm type string.
6a488035 123 *
72b3a70c
CW
124 * @return string|null
125 * the mysql data store placeholder
6a488035
TO
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
b5c2afd0 161 /**
72b3a70c
CW
162 * @param array $formValues
163 * @return null
b5c2afd0 164 */
c94d39fd 165 public static function fixCustomFieldValue(&$formValues) {
6a488035
TO
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 );
c94d39fd 180 $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
181 substr($key, 7), 'data_type'
182 );
183
184 if (is_array($formValues[$key])) {
d7e0081e 185 if (!in_array(key($formValues[$key]), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
c94d39fd 186 $formValues[$key] = array('IN' => $formValues[$key]);
187 }
188 }
189 elseif (($htmlType == 'TextArea' ||
190 ($htmlType == 'Text' && $dataType == 'String')
2b6760c5 191 ) && strstr($formValues[$key], '%')
6a488035 192 ) {
c995043c 193 $formValues[$key] = array('LIKE' => $formValues[$key]);
6a488035 194 }
6a488035
TO
195 }
196 }
197
198 /**
fe482240 199 * Delete option value give an option value and custom group id.
6a488035 200 *
6a0b768e
TO
201 * @param int $customValueID
202 * Custom value ID.
203 * @param int $customGroupID
204 * Custom group ID.
6a488035 205 */
00be9182 206 public static function deleteCustomValue($customValueID, $customGroupID) {
6a488035
TO
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
2c57d73b
JJ
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
6a488035
TO
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,
2c57d73b 221 $entityID,
6a488035
TO
222 $customValueID
223 );
224 }
96025800 225
06508628 226}