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