commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Core / BAO / CustomOption.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 * Fetch object based on array of properties.
44 *
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 * @param array $defaults
48 * (reference ) an assoc array to hold the flattened values.
49 *
50 * @return CRM_Core_BAO_CustomOption
51 */
52 public static function retrieve(&$params, &$defaults) {
53 $customOption = new CRM_Core_DAO_OptionValue();
54 $customOption->copyValues($params);
55 if ($customOption->find(TRUE)) {
56 CRM_Core_DAO::storeValues($customOption, $defaults);
57 return $customOption;
58 }
59 return NULL;
60 }
61
62 /**
63 * Returns all active options ordered by weight for a given field.
64 *
65 * @param int $fieldID
66 * Field whose options are needed.
67 * @param bool $inactiveNeeded Do we need inactive options ?.
68 * Do we need inactive options ?.
69 *
70 * @return array
71 * all active options for fieldId
72 */
73 public static function getCustomOption(
74 $fieldID,
75 $inactiveNeeded = FALSE
76 ) {
77 $options = array();
78 if (!$fieldID) {
79 return $options;
80 }
81
82 $field = CRM_Core_BAO_CustomField::getFieldObject($fieldID);
83
84 // get the option group id
85 $optionGroupID = $field->option_group_id;
86 if (!$optionGroupID) {
87 return $options;
88 }
89
90 $optionValues = CRM_Core_BAO_OptionValue::getOptionValuesArray($optionGroupID);
91
92 foreach ($optionValues as $id => $value) {
93 if (!$inactiveNeeded && empty($value['is_active'])) {
94 continue;
95 }
96
97 $options[$id] = array();
98 $options[$id]['id'] = $id;
99 $options[$id]['label'] = $value['label'];
100 $options[$id]['value'] = $value['value'];
101 }
102
103 CRM_Utils_Hook::customFieldOptions($fieldID, $options, TRUE);
104
105 return $options;
106 }
107
108 /**
109 * wrapper for ajax option selector.
110 *
111 * @param array $params
112 * Associated array for params record id.
113 *
114 * @return array
115 * associated array of option list
116 * -rp = rowcount
117 * -page= offset
118 */
119 static public function getOptionListSelector(&$params) {
120
121 $options = array();
122
123 //get the default value from custom fields
124 $customFieldBAO = new CRM_Core_BAO_CustomField();
125 $customFieldBAO->id = $params['fid'];
126 if ($customFieldBAO->find(TRUE)) {
127 $defaultValue = $customFieldBAO->default_value;
128 $fieldHtmlType = $customFieldBAO->html_type;
129 }
130 else {
131 CRM_Core_Error::fatal();
132 }
133 $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR,
134 substr($defaultValue, 1, -1)
135 );
136
137 // format the params
138 $params['offset'] = ($params['page'] - 1) * $params['rp'];
139 $params['rowCount'] = $params['rp'];
140
141 $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']);
142
143 // get the option group id
144 $optionGroupID = $field->option_group_id;
145 if (!$optionGroupID) {
146 return $options;
147 }
148 $queryParams = array(1 => array($optionGroupID, 'Integer'));
149 $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1";
150 $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams);
151
152 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
153 $orderBy = ' ORDER BY options.weight asc';
154
155 $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}";
156 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
157 $links = CRM_Custom_Page_Option::actionLinks();
158
159 $fields = array('id', 'label', 'value');
160 $config = CRM_Core_Config::singleton();
161 while ($dao->fetch()) {
162 $options[$dao->id] = array();
163 foreach ($fields as $k) {
164 $options[$dao->id][$k] = $dao->$k;
165 }
166 $action = array_sum(array_keys($links));
167 $class = 'crm-entity';
168 // update enable/disable links depending on custom_field properties.
169 if ($dao->is_active) {
170 $action -= CRM_Core_Action::ENABLE;
171 }
172 else {
173 $class .= ' disabled';
174 $action -= CRM_Core_Action::DISABLE;
175 }
176 if ($fieldHtmlType == 'CheckBox' ||
177 $fieldHtmlType == 'AdvMulti-Select' ||
178 $fieldHtmlType == 'Multi-Select'
179 ) {
180 if (in_array($dao->value, $defVal)) {
181 $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
182 }
183 else {
184 $options[$dao->id]['is_default'] = '';
185 }
186 }
187 else {
188 if ($defaultValue == $dao->value) {
189 $options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
190 }
191 else {
192 $options[$dao->id]['is_default'] = '';
193 }
194 }
195
196 $options[$dao->id]['class'] = $dao->id . ',' . $class;
197 $options[$dao->id]['is_active'] = !empty($dao->is_active) ? 'Yes' : 'No';
198 $options[$dao->id]['links'] = CRM_Core_Action::formLink($links,
199 $action,
200 array(
201 'id' => $dao->id,
202 'fid' => $params['fid'],
203 'gid' => $params['gid'],
204 ),
205 ts('more'),
206 FALSE,
207 'customOption.row.actions',
208 'customOption',
209 $dao->id
210 );
211 }
212
213 return $options;
214 }
215
216 /**
217 * Returns the option label for a custom field with a specific value. Handles all
218 * custom field data and html types
219 *
220 * @param int $fieldId
221 * the custom field ID.
222 * @pram $value string the value (typically from the DB) of this custom field
223 * @param $value
224 * @param string $htmlType
225 * the html type of the field (optional).
226 * @param string $dataType
227 * the data type of the field (optional).
228 *
229 * @return string
230 * the label to display for this custom field
231 */
232 public static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
233 if (!$fieldId) {
234 return NULL;
235 }
236
237 if (!$htmlType || !$dataType) {
238 $sql = "
239 SELECT html_type, data_type
240 FROM civicrm_custom_field
241 WHERE id = %1
242 ";
243 $params = array(1 => array($fieldId, 'Integer'));
244 $dao = CRM_Core_DAO::executeQuery($sql, $params);
245 if ($dao->fetch()) {
246 $htmlType = $dao->html_type;
247 $dataType = $dao->data_type;
248 }
249 else {
250 CRM_Core_Error::fatal();
251 }
252 }
253
254 $options = NULL;
255 switch ($htmlType) {
256 case 'CheckBox':
257 case 'Multi-Select':
258 case 'AdvMulti-Select':
259 case 'Select':
260 case 'Radio':
261 case 'Autocomplete-Select':
262 if (!in_array($dataType, array(
263 'Boolean',
264 'ContactReference',
265 ))
266 ) {
267 $options = self::valuesByID($fieldId);
268 }
269 }
270
271 return CRM_Core_BAO_CustomField::getDisplayValueCommon($value,
272 $options,
273 $htmlType,
274 $dataType
275 );
276 }
277
278 /**
279 * Delete Option.
280 *
281 * @param $optionId integer
282 * option id
283 *
284 */
285 public static function del($optionId) {
286 // get the customFieldID
287 $query = "
288 SELECT f.id as id, f.data_type as dataType
289 FROM civicrm_option_value v,
290 civicrm_option_group g,
291 civicrm_custom_field f
292 WHERE v.id = %1
293 AND g.id = f.option_group_id
294 AND g.id = v.option_group_id";
295 $params = array(1 => array($optionId, 'Integer'));
296 $dao = CRM_Core_DAO::executeQuery($query, $params);
297 if ($dao->fetch()) {
298 if (in_array($dao->dataType,
299 array('Int', 'Float', 'Money', 'Boolean')
300 )) {
301 $value = 0;
302 }
303 else {
304 $value = '';
305 }
306 $params = array(
307 'optionId' => $optionId,
308 'fieldId' => $dao->id,
309 'value' => $value,
310 );
311 // delete this value from the tables
312 self::updateCustomValues($params);
313
314 // also delete this option value
315 $query = "
316 DELETE
317 FROM civicrm_option_value
318 WHERE id = %1";
319 $params = array(1 => array($optionId, 'Integer'));
320 CRM_Core_DAO::executeQuery($query, $params);
321 }
322 }
323
324 /**
325 * @param array $params
326 *
327 * @throws Exception
328 */
329 public static function updateCustomValues($params) {
330 $optionDAO = new CRM_Core_DAO_OptionValue();
331 $optionDAO->id = $params['optionId'];
332 $optionDAO->find(TRUE);
333 $oldValue = $optionDAO->value;
334
335 // get the table, column, html_type and data type for this field
336 $query = "
337 SELECT g.table_name as tableName ,
338 f.column_name as columnName,
339 f.data_type as dataType,
340 f.html_type as htmlType
341 FROM civicrm_custom_group g,
342 civicrm_custom_field f
343 WHERE f.custom_group_id = g.id
344 AND f.id = %1";
345 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
346 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
347 if ($dao->fetch()) {
348 if ($dao->dataType == 'Money') {
349 $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']);
350 }
351 switch ($dao->htmlType) {
352 case 'Autocomplete-Select':
353 case 'Select':
354 case 'Radio':
355 $query = "
356 UPDATE {$dao->tableName}
357 SET {$dao->columnName} = %1
358 WHERE id = %2";
359 if ($dao->dataType == 'Auto-complete') {
360 $dataType = "String";
361 }
362 else {
363 $dataType = $dao->dataType;
364 }
365 $queryParams = array(
366 1 => array(
367 $params['value'],
368 $dataType,
369 ),
370 2 => array(
371 $params['optionId'],
372 'Integer',
373 ),
374 );
375 break;
376
377 case 'AdvMulti-Select':
378 case 'Multi-Select':
379 case 'CheckBox':
380 $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR;
381 $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR;
382 $query = "
383 UPDATE {$dao->tableName}
384 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
385 $queryParams = array(
386 1 => array($oldString, 'String'),
387 2 => array($newString, 'String'),
388 );
389 break;
390
391 default:
392 CRM_Core_Error::fatal();
393 }
394 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
395 }
396 }
397
398 /**
399 * @param int $customFieldID
400 * @param int $optionGroupID
401 *
402 * @return array
403 */
404 public static function valuesByID($customFieldID, $optionGroupID = NULL) {
405 if (!$optionGroupID) {
406 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
407 $customFieldID,
408 'option_group_id'
409 );
410 }
411
412 $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array();
413
414 CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE);
415
416 return $options;
417 }
418
419 }