CRM-17646 - Refactor out CRM_Report_Form::formatCustomValues
[civicrm-core.git] / CRM / Core / BAO / CustomOption.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Business objects for managing custom data options.
38 *
39 */
40class CRM_Core_BAO_CustomOption {
41
42 /**
fe482240 43 * Fetch object based on array of properties.
6a488035 44 *
6a0b768e
TO
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.
6a488035 49 *
16b10e64 50 * @return CRM_Core_BAO_CustomOption
6a488035 51 */
00be9182 52 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
fe482240 63 * Returns all active options ordered by weight for a given field.
6a488035 64 *
6a0b768e
TO
65 * @param int $fieldID
66 * Field whose options are needed.
317fceb4 67 * @param bool $inactiveNeeded Do we need inactive options ?.
6a0b768e 68 * Do we need inactive options ?.
6a488035 69 *
a6c01b45
CW
70 * @return array
71 * all active options for fieldId
6a488035 72 */
317fceb4 73 public static function getCustomOption(
6a488035
TO
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) {
8cc574cf 93 if (!$inactiveNeeded && empty($value['is_active'])) {
6a488035
TO
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
57c9c217 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'];
57c9c217 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']} ";
57c9c217 153 $orderBy = ' ORDER BY options.weight asc';
57c9c217 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
46227b8d 159 $fields = array('id', 'label', 'value');
57c9c217 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 }
57c9c217 212
213 return $options;
214 }
215
6a488035 216 /**
fe482240 217 * Delete Option.
6a488035 218 *
16b10e64
CW
219 * @param $optionId integer
220 * option id
6a488035 221 *
6a488035 222 */
00be9182 223 public static function del($optionId) {
6a488035
TO
224 // get the customFieldID
225 $query = "
226SELECT f.id as id, f.data_type as dataType
227FROM civicrm_option_value v,
228 civicrm_option_group g,
229 civicrm_custom_field f
230WHERE v.id = %1
231AND g.id = f.option_group_id
232AND g.id = v.option_group_id";
233 $params = array(1 => array($optionId, 'Integer'));
234 $dao = CRM_Core_DAO::executeQuery($query, $params);
235 if ($dao->fetch()) {
236 if (in_array($dao->dataType,
353ffa53
TO
237 array('Int', 'Float', 'Money', 'Boolean')
238 )) {
6a488035
TO
239 $value = 0;
240 }
241 else {
242 $value = '';
243 }
244 $params = array(
245 'optionId' => $optionId,
246 'fieldId' => $dao->id,
247 'value' => $value,
248 );
249 // delete this value from the tables
250 self::updateCustomValues($params);
251
252 // also delete this option value
253 $query = "
254DELETE
255FROM civicrm_option_value
256WHERE id = %1";
257 $params = array(1 => array($optionId, 'Integer'));
258 CRM_Core_DAO::executeQuery($query, $params);
259 }
260 }
261
b5c2afd0 262 /**
c490a46a 263 * @param array $params
b5c2afd0
EM
264 *
265 * @throws Exception
266 */
00be9182 267 public static function updateCustomValues($params) {
6a488035
TO
268 $optionDAO = new CRM_Core_DAO_OptionValue();
269 $optionDAO->id = $params['optionId'];
270 $optionDAO->find(TRUE);
271 $oldValue = $optionDAO->value;
272
273 // get the table, column, html_type and data type for this field
274 $query = "
275SELECT g.table_name as tableName ,
276 f.column_name as columnName,
277 f.data_type as dataType,
278 f.html_type as htmlType
279FROM civicrm_custom_group g,
280 civicrm_custom_field f
281WHERE f.custom_group_id = g.id
282 AND f.id = %1";
283 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
284 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
285 if ($dao->fetch()) {
286 if ($dao->dataType == 'Money') {
287 $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']);
288 }
289 switch ($dao->htmlType) {
290 case 'Autocomplete-Select':
291 case 'Select':
292 case 'Radio':
293 $query = "
294UPDATE {$dao->tableName}
295SET {$dao->columnName} = %1
296WHERE id = %2";
297 if ($dao->dataType == 'Auto-complete') {
298 $dataType = "String";
299 }
300 else {
301 $dataType = $dao->dataType;
302 }
303 $queryParams = array(
353ffa53
TO
304 1 => array(
305 $params['value'],
6a488035
TO
306 $dataType,
307 ),
308 2 => array(
309 $params['optionId'],
310 'Integer',
311 ),
312 );
313 break;
314
315 case 'AdvMulti-Select':
316 case 'Multi-Select':
317 case 'CheckBox':
318 $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR;
319 $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR;
353ffa53 320 $query = "
6a488035
TO
321UPDATE {$dao->tableName}
322SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
2aa397bc 323 $queryParams = array(
353ffa53 324 1 => array($oldString, 'String'),
6a488035
TO
325 2 => array($newString, 'String'),
326 );
327 break;
328
329 default:
330 CRM_Core_Error::fatal();
331 }
332 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
333 }
334 }
335
58eaa092
CW
336 /**
337 * When changing the value of an option this is called to update all corresponding custom data
338 *
339 * @param int $optionId
340 * @param string $newValue
341 */
342 public static function updateValue($optionId, $newValue) {
343 $optionValue = new CRM_Core_DAO_OptionValue();
344 $optionValue->id = $optionId;
345 $optionValue->find(TRUE);
346 $oldValue = $optionValue->value;
347 if ($oldValue == $newValue) {
348 return;
349 }
350
351 $customField = new CRM_Core_DAO_CustomField();
352 $customField->option_group_id = $optionValue->option_group_id;
353 $customField->find();
354 while ($customField->fetch()) {
355 $customGroup = new CRM_Core_DAO_CustomGroup();
356 $customGroup->id = $customField->custom_group_id;
357 $customGroup->find(TRUE);
358 if (CRM_Core_BAO_CustomField::isSerialized($customField)) {
359 $params = array(
360 1 => array(CRM_Utils_Array::implodePadded($oldValue), 'String'),
361 2 => array(CRM_Utils_Array::implodePadded($newValue), 'String'),
362 3 => array('%' . CRM_Utils_Array::implodePadded($oldValue) . '%', 'String'),
363 );
364 }
365 else {
366 $params = array(
367 1 => array($oldValue, 'String'),
368 2 => array($newValue, 'String'),
369 3 => array($oldValue, 'String'),
370 );
371 }
372 $sql = "UPDATE `{$customGroup->table_name}` SET `{$customField->column_name}` = REPLACE(`{$customField->column_name}`, %1, %2) WHERE `{$customField->column_name}` LIKE %3";
373 $customGroup->free();
374 CRM_Core_DAO::executeQuery($sql, $params);
375 }
376 $customField->free();
377 }
378
6a488035 379}