Merge pull request #4820 from kurund/CRM-15705
[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 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 * @static
50 */
51 public static function typecheck($type, $value) {
52 switch ($type) {
53 case 'Memo':
54 return TRUE;
55
56 case 'String':
57 return CRM_Utils_Rule::string($value);
58
59 case 'Int':
60 return CRM_Utils_Rule::integer($value);
61
62 case 'Float':
63 case 'Money':
64 return CRM_Utils_Rule::numeric($value);
65
66 case 'Date':
67 if (is_numeric($value)) {
68 return CRM_Utils_Rule::dateTime($value);
69 }
70 else {
71 return CRM_Utils_Rule::date($value);
72 }
73 case 'Boolean':
74 return CRM_Utils_Rule::boolean($value);
75
76 case 'ContactReference':
77 return CRM_Utils_Rule::validContact($value);
78
79 case 'StateProvince':
80
81 //fix for multi select state, CRM-3437
82 $valid = FALSE;
83 $mulValues = explode(',', $value);
84 foreach ($mulValues as $key => $state) {
85 $valid = array_key_exists(strtolower(trim($state)),
86 array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)
87 ) || array_key_exists(strtolower(trim($state)),
88 array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER)
89 );
90 if (!$valid) {
91 break;
92 }
93 }
94 return $valid;
95
96 case 'Country':
97
98 //fix multi select country, CRM-3437
99 $valid = FALSE;
100 $mulValues = explode(',', $value);
101 foreach ($mulValues as $key => $country) {
102 $valid = array_key_exists(strtolower(trim($country)),
103 array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)
104 ) || array_key_exists(strtolower(trim($country)),
105 array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER)
106 );
107 if (!$valid) {
108 break;
109 }
110 }
111 return $valid;
112
113 case 'Link':
114 return CRM_Utils_Rule::url($value);
115 }
116 return FALSE;
117 }
118
119 /**
120 * Given a 'civicrm' type string, return the mysql data store area
121 *
122 * @param string $type the civicrm type string
123 *
124 * @return the mysql data store placeholder
125 * @static
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 $formValues
163 */
164 public static function fixFieldValueOfTypeMemo(&$formValues) {
165 if (empty($formValues)) {
166 return NULL;
167 }
168 foreach (array_keys($formValues) as $key) {
169 if (substr($key, 0, 7) != 'custom_') {
170 continue;
171 }
172 elseif (empty($formValues[$key])) {
173 continue;
174 }
175
176 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
177 substr($key, 7), 'html_type'
178 );
179 if (($htmlType == 'TextArea') &&
180 !((substr($formValues[$key], 0, 1) == '%') ||
181 (substr($formValues[$key], -1, 1) == '%')
182 )
183 ) {
184 $formValues[$key] = '%' . $formValues[$key] . '%';
185 }
186
187 }
188 }
189
190 /**
191 * Delet option value give an option value and custom group id
192 *
193 * @param int $customValueID custom value ID
194 * @param int $customGroupID custom group ID
195 *
196 * @return void
197 * @static
198 */
199 public static function deleteCustomValue($customValueID, $customGroupID) {
200 // first we need to find custom value table, from custom group ID
201 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name');
202
203 // delete custom value from corresponding custom value table
204 $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}";
205 CRM_Core_DAO::executeQuery($sql);
206
207 CRM_Utils_Hook::custom('delete',
208 $customGroupID,
209 NULL,
210 $customValueID
211 );
212 }
213 }