Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business objects for managing custom data values. | |
38 | * | |
39 | */ | |
40 | class CRM_Core_BAO_CustomValue extends CRM_Core_DAO { | |
41 | ||
42 | /** | |
43 | * Validate a value against a CustomField type | |
44 | * | |
45 | * @param string $type The type of the data | |
46 | * @param string $value The data to be validated | |
47 | * | |
48 | * @return boolean True if the value is of the specified type | |
49 | * @access public | |
50 | * @static | |
51 | */ | |
52 | public static function typecheck($type, $value) { | |
53 | switch ($type) { | |
54 | case 'Memo': | |
55 | return TRUE; | |
56 | ||
57 | case 'String': | |
58 | return CRM_Utils_Rule::string($value); | |
59 | ||
60 | case 'Int': | |
61 | return CRM_Utils_Rule::integer($value); | |
62 | ||
63 | case 'Float': | |
64 | case 'Money': | |
65 | return CRM_Utils_Rule::numeric($value); | |
66 | ||
67 | case 'Date': | |
68 | if (is_numeric($value)) { | |
69 | return CRM_Utils_Rule::dateTime($value); | |
70 | } | |
71 | else { | |
72 | return CRM_Utils_Rule::date($value); | |
73 | } | |
74 | case 'Boolean': | |
75 | return CRM_Utils_Rule::boolean($value); | |
76 | ||
77 | case 'ContactReference': | |
78 | return CRM_Utils_Rule::validContact($value); | |
79 | ||
80 | case 'StateProvince': | |
81 | ||
82 | //fix for multi select state, CRM-3437 | |
83 | $valid = FALSE; | |
84 | $mulValues = explode(',', $value); | |
85 | foreach ($mulValues as $key => $state) { | |
86 | $valid = array_key_exists(strtolower(trim($state)), | |
87 | array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER) | |
88 | ) || array_key_exists(strtolower(trim($state)), | |
89 | array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER) | |
90 | ); | |
91 | if (!$valid) { | |
92 | break; | |
93 | } | |
94 | } | |
95 | return $valid; | |
96 | ||
97 | case 'Country': | |
98 | ||
99 | //fix multi select country, CRM-3437 | |
100 | $valid = FALSE; | |
101 | $mulValues = explode(',', $value); | |
102 | foreach ($mulValues as $key => $country) { | |
103 | $valid = array_key_exists(strtolower(trim($country)), | |
104 | array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER) | |
105 | ) || array_key_exists(strtolower(trim($country)), | |
106 | array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER) | |
107 | ); | |
108 | if (!$valid) { | |
109 | break; | |
110 | } | |
111 | } | |
112 | return $valid; | |
113 | ||
114 | case 'Link': | |
115 | return CRM_Utils_Rule::url($value); | |
116 | } | |
117 | return FALSE; | |
118 | } | |
119 | ||
120 | /** | |
121 | * given a 'civicrm' type string, return the mysql data store area | |
122 | * | |
123 | * @param string $type the civicrm type string | |
124 | * | |
125 | * @return the mysql data store placeholder | |
126 | * @access public | |
127 | * @static | |
128 | */ | |
129 | public static function typeToField($type) { | |
130 | switch ($type) { | |
131 | case 'String': | |
132 | case 'File': | |
133 | return 'char_data'; | |
134 | ||
135 | case 'Boolean': | |
136 | case 'Int': | |
137 | case 'StateProvince': | |
138 | case 'Country': | |
139 | case 'Auto-complete': | |
140 | return 'int_data'; | |
141 | ||
142 | case 'Float': | |
143 | return 'float_data'; | |
144 | ||
145 | case 'Money': | |
146 | return 'decimal_data'; | |
147 | ||
148 | case 'Memo': | |
149 | return 'memo_data'; | |
150 | ||
151 | case 'Date': | |
152 | return 'date_data'; | |
153 | ||
154 | case 'Link': | |
155 | return 'char_data'; | |
156 | ||
157 | default: | |
158 | return NULL; | |
159 | } | |
160 | } | |
161 | ||
162 | ||
b5c2afd0 EM |
163 | /** |
164 | * @param $formValues | |
165 | */ | |
6a488035 TO |
166 | public static function fixFieldValueOfTypeMemo(&$formValues) { |
167 | if (empty($formValues)) { | |
168 | return NULL; | |
169 | } | |
170 | foreach (array_keys($formValues) as $key) { | |
171 | if (substr($key, 0, 7) != 'custom_') { | |
172 | continue; | |
173 | } | |
174 | elseif (empty($formValues[$key])) { | |
175 | continue; | |
176 | } | |
177 | ||
178 | $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField', | |
179 | substr($key, 7), 'html_type' | |
180 | ); | |
181 | if (($htmlType == 'TextArea') && | |
182 | !((substr($formValues[$key], 0, 1) == '%') || | |
183 | (substr($formValues[$key], -1, 1) == '%') | |
184 | ) | |
185 | ) { | |
186 | $formValues[$key] = '%' . $formValues[$key] . '%'; | |
187 | } | |
188 | ||
6a488035 TO |
189 | } |
190 | } | |
191 | ||
192 | /** | |
193 | * Function to delet option value give an option value and custom group id | |
194 | * | |
195 | * @param int $customValueID custom value ID | |
196 | * @param int $customGroupID custom group ID | |
197 | * | |
198 | * @return void | |
199 | * @static | |
200 | */ | |
201 | static function deleteCustomValue($customValueID, $customGroupID) { | |
202 | // first we need to find custom value table, from custom group ID | |
203 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name'); | |
204 | ||
205 | // delete custom value from corresponding custom value table | |
206 | $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}"; | |
207 | CRM_Core_DAO::executeQuery($sql); | |
208 | ||
209 | CRM_Utils_Hook::custom('delete', | |
210 | $customGroupID, | |
211 | NULL, | |
212 | $customValueID | |
213 | ); | |
214 | } | |
06508628 | 215 | } |