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 options. | |
38 | * | |
39 | */ | |
40 | class CRM_Core_BAO_CustomOption { | |
41 | ||
42 | /** | |
43 | * Takes a bunch of params that are needed to match certain criteria and | |
44 | * retrieves the relevant objects. Typically the valid params are only | |
45 | * contact_id. We'll tweak this function to be more full featured over a period | |
46 | * of time. This is the inverse function of create. It also stores all the retrieved | |
47 | * values in the default array | |
48 | * | |
49 | * @param array $params (reference ) an assoc array of name/value pairs | |
50 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
51 | * | |
52 | * @return object CRM_Core_BAO_CustomOption object | |
53 | * @access public | |
54 | * @static | |
55 | */ | |
56 | static function retrieve(&$params, &$defaults) { | |
57 | $customOption = new CRM_Core_DAO_OptionValue(); | |
58 | $customOption->copyValues($params); | |
59 | if ($customOption->find(TRUE)) { | |
60 | CRM_Core_DAO::storeValues($customOption, $defaults); | |
61 | return $customOption; | |
62 | } | |
63 | return NULL; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Returns all active options ordered by weight for a given field | |
68 | * | |
77b97be7 EM |
69 | * @param $fieldID |
70 | * @param boolean $inactiveNeeded do we need inactive options ? | |
6a488035 | 71 | * |
77b97be7 | 72 | * @internal param int $fieldId field whose options are needed |
6a488035 TO |
73 | * @return array $customOption all active options for fieldId |
74 | * @static | |
75 | */ | |
76 | static function getCustomOption( | |
77 | $fieldID, | |
78 | $inactiveNeeded = FALSE | |
79 | ) { | |
80 | $options = array(); | |
81 | if (!$fieldID) { | |
82 | return $options; | |
83 | } | |
84 | ||
85 | $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID); | |
86 | ||
87 | // get the option group id | |
88 | $optionGroupID = $field->option_group_id; | |
89 | if (!$optionGroupID) { | |
90 | return $options; | |
91 | } | |
92 | ||
93 | $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID); | |
94 | ||
95 | foreach ($optionValues as $id => $value) { | |
8cc574cf | 96 | if (!$inactiveNeeded && empty($value['is_active'])) { |
6a488035 TO |
97 | continue; |
98 | } | |
99 | ||
100 | $options[$id] = array(); | |
101 | $options[$id]['id'] = $id; | |
102 | $options[$id]['label'] = $value['label']; | |
103 | $options[$id]['value'] = $value['value']; | |
104 | } | |
105 | ||
106 | CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE); | |
107 | ||
108 | return $options; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Returns the option label for a custom field with a specific value. Handles all | |
113 | * custom field data and html types | |
114 | * | |
115 | * @param $fieldId int the custom field ID | |
116 | * @pram $value string the value (typically from the DB) of this custom field | |
dd244018 | 117 | * @param $value |
6a488035 TO |
118 | * @param $htmlType string the html type of the field (optional) |
119 | * @param $dataType string the data type of the field (optional) | |
120 | * | |
121 | * @return string the label to display for this custom field | |
122 | * @static | |
123 | * @access public | |
124 | */ | |
125 | static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) { | |
126 | if (!$fieldId) { | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | if (!$htmlType || !$dataType) { | |
131 | $sql = " | |
132 | SELECT html_type, data_type | |
133 | FROM civicrm_custom_field | |
134 | WHERE id = %1 | |
135 | "; | |
136 | $params = array(1 => array($fieldId, 'Integer')); | |
137 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
138 | if ($dao->fetch()) { | |
139 | $htmlType = $dao->html_type; | |
140 | $dataType = $dao->data_type; | |
141 | } | |
142 | else { | |
143 | CRM_Core_Error::fatal(); | |
144 | } | |
145 | } | |
146 | ||
147 | $options = NULL; | |
148 | switch ($htmlType) { | |
149 | case 'CheckBox': | |
150 | case 'Multi-Select': | |
151 | case 'AdvMulti-Select': | |
152 | case 'Select': | |
153 | case 'Radio': | |
154 | case 'Autocomplete-Select': | |
155 | if (!in_array($dataType, array( | |
156 | 'Boolean', 'ContactReference'))) { | |
157 | $options = self::valuesByID($fieldId); | |
158 | } | |
159 | } | |
160 | ||
161 | return CRM_Core_BAO_CustomField::getDisplayValueCommon($value, | |
162 | $options, | |
163 | $htmlType, | |
164 | $dataType | |
165 | ); | |
166 | } | |
167 | ||
168 | /** | |
169 | * Function to delete Option | |
170 | * | |
171 | * param $optionId integer option id | |
172 | * | |
173 | * @static | |
174 | * @access public | |
175 | */ | |
176 | static function del($optionId) { | |
177 | // get the customFieldID | |
178 | $query = " | |
179 | SELECT f.id as id, f.data_type as dataType | |
180 | FROM civicrm_option_value v, | |
181 | civicrm_option_group g, | |
182 | civicrm_custom_field f | |
183 | WHERE v.id = %1 | |
184 | AND g.id = f.option_group_id | |
185 | AND g.id = v.option_group_id"; | |
186 | $params = array(1 => array($optionId, 'Integer')); | |
187 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
188 | if ($dao->fetch()) { | |
189 | if (in_array($dao->dataType, | |
190 | array('Int', 'Float', 'Money', 'Boolean') | |
191 | )) { | |
192 | $value = 0; | |
193 | } | |
194 | else { | |
195 | $value = ''; | |
196 | } | |
197 | $params = array( | |
198 | 'optionId' => $optionId, | |
199 | 'fieldId' => $dao->id, | |
200 | 'value' => $value, | |
201 | ); | |
202 | // delete this value from the tables | |
203 | self::updateCustomValues($params); | |
204 | ||
205 | // also delete this option value | |
206 | $query = " | |
207 | DELETE | |
208 | FROM civicrm_option_value | |
209 | WHERE id = %1"; | |
210 | $params = array(1 => array($optionId, 'Integer')); | |
211 | CRM_Core_DAO::executeQuery($query, $params); | |
212 | } | |
213 | } | |
214 | ||
b5c2afd0 EM |
215 | /** |
216 | * @param $params | |
217 | * | |
218 | * @throws Exception | |
219 | */ | |
6a488035 TO |
220 | static function updateCustomValues($params) { |
221 | $optionDAO = new CRM_Core_DAO_OptionValue(); | |
222 | $optionDAO->id = $params['optionId']; | |
223 | $optionDAO->find(TRUE); | |
224 | $oldValue = $optionDAO->value; | |
225 | ||
226 | // get the table, column, html_type and data type for this field | |
227 | $query = " | |
228 | SELECT g.table_name as tableName , | |
229 | f.column_name as columnName, | |
230 | f.data_type as dataType, | |
231 | f.html_type as htmlType | |
232 | FROM civicrm_custom_group g, | |
233 | civicrm_custom_field f | |
234 | WHERE f.custom_group_id = g.id | |
235 | AND f.id = %1"; | |
236 | $queryParams = array(1 => array($params['fieldId'], 'Integer')); | |
237 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
238 | if ($dao->fetch()) { | |
239 | if ($dao->dataType == 'Money') { | |
240 | $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']); | |
241 | } | |
242 | switch ($dao->htmlType) { | |
243 | case 'Autocomplete-Select': | |
244 | case 'Select': | |
245 | case 'Radio': | |
246 | $query = " | |
247 | UPDATE {$dao->tableName} | |
248 | SET {$dao->columnName} = %1 | |
249 | WHERE id = %2"; | |
250 | if ($dao->dataType == 'Auto-complete') { | |
251 | $dataType = "String"; | |
252 | } | |
253 | else { | |
254 | $dataType = $dao->dataType; | |
255 | } | |
256 | $queryParams = array( | |
257 | 1 => array($params['value'], | |
258 | $dataType, | |
259 | ), | |
260 | 2 => array( | |
261 | $params['optionId'], | |
262 | 'Integer', | |
263 | ), | |
264 | ); | |
265 | break; | |
266 | ||
267 | case 'AdvMulti-Select': | |
268 | case 'Multi-Select': | |
269 | case 'CheckBox': | |
270 | $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR; | |
271 | $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR; | |
272 | $query = " | |
273 | UPDATE {$dao->tableName} | |
274 | SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )"; | |
275 | $queryParams = array(1 => array($oldString, 'String'), | |
276 | 2 => array($newString, 'String'), | |
277 | ); | |
278 | break; | |
279 | ||
280 | default: | |
281 | CRM_Core_Error::fatal(); | |
282 | } | |
283 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
284 | } | |
285 | } | |
286 | ||
b5c2afd0 EM |
287 | /** |
288 | * @param $customFieldID | |
289 | * @param null $optionGroupID | |
290 | * | |
291 | * @return array | |
292 | */ | |
2fea9ed9 | 293 | static function valuesByID($customFieldID, $optionGroupID = NULL) { |
6a488035 TO |
294 | if (!$optionGroupID) { |
295 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
296 | $customFieldID, | |
297 | 'option_group_id' | |
298 | ); | |
299 | } | |
300 | ||
2fea9ed9 | 301 | $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array(); |
6a488035 TO |
302 | |
303 | CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE); | |
304 | ||
305 | return $options; | |
306 | } | |
307 | } | |
308 |