eca547815a80b202d5ad08a793d72f43b429ab75
[civicrm-core.git] / CRM / Core / BAO / CustomOption.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 *
69 * @param $fieldID
70 * @param boolean $inactiveNeeded do we need inactive options ?
71 *
72 * @internal param int $fieldId field whose options are needed
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) {
96 if (!$inactiveNeeded && empty($value['is_active'])) {
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
117 * @param $htmlType string the html type of the field (optional)
118 * @param $dataType string the data type of the field (optional)
119 *
120 * @return string the label to display for this custom field
121 * @static
122 * @access public
123 */
124 static function getOptionLabel($fieldId, $value, $htmlType = NULL, $dataType = NULL) {
125 if (!$fieldId) {
126 return NULL;
127 }
128
129 if (!$htmlType || !$dataType) {
130 $sql = "
131 SELECT html_type, data_type
132 FROM civicrm_custom_field
133 WHERE id = %1
134 ";
135 $params = array(1 => array($fieldId, 'Integer'));
136 $dao = CRM_Core_DAO::executeQuery($sql, $params);
137 if ($dao->fetch()) {
138 $htmlType = $dao->html_type;
139 $dataType = $dao->data_type;
140 }
141 else {
142 CRM_Core_Error::fatal();
143 }
144 }
145
146 $options = NULL;
147 switch ($htmlType) {
148 case 'CheckBox':
149 case 'Multi-Select':
150 case 'AdvMulti-Select':
151 case 'Select':
152 case 'Radio':
153 case 'Autocomplete-Select':
154 if (!in_array($dataType, array(
155 'Boolean', 'ContactReference'))) {
156 $options = self::valuesByID($fieldId);
157 }
158 }
159
160 return CRM_Core_BAO_CustomField::getDisplayValueCommon($value,
161 $options,
162 $htmlType,
163 $dataType
164 );
165 }
166
167 /**
168 * Function to delete Option
169 *
170 * param $optionId integer option id
171 *
172 * @static
173 * @access public
174 */
175 static function del($optionId) {
176 // get the customFieldID
177 $query = "
178 SELECT f.id as id, f.data_type as dataType
179 FROM civicrm_option_value v,
180 civicrm_option_group g,
181 civicrm_custom_field f
182 WHERE v.id = %1
183 AND g.id = f.option_group_id
184 AND g.id = v.option_group_id";
185 $params = array(1 => array($optionId, 'Integer'));
186 $dao = CRM_Core_DAO::executeQuery($query, $params);
187 if ($dao->fetch()) {
188 if (in_array($dao->dataType,
189 array('Int', 'Float', 'Money', 'Boolean')
190 )) {
191 $value = 0;
192 }
193 else {
194 $value = '';
195 }
196 $params = array(
197 'optionId' => $optionId,
198 'fieldId' => $dao->id,
199 'value' => $value,
200 );
201 // delete this value from the tables
202 self::updateCustomValues($params);
203
204 // also delete this option value
205 $query = "
206 DELETE
207 FROM civicrm_option_value
208 WHERE id = %1";
209 $params = array(1 => array($optionId, 'Integer'));
210 CRM_Core_DAO::executeQuery($query, $params);
211 }
212 }
213
214 static function updateCustomValues($params) {
215 $optionDAO = new CRM_Core_DAO_OptionValue();
216 $optionDAO->id = $params['optionId'];
217 $optionDAO->find(TRUE);
218 $oldValue = $optionDAO->value;
219
220 // get the table, column, html_type and data type for this field
221 $query = "
222 SELECT g.table_name as tableName ,
223 f.column_name as columnName,
224 f.data_type as dataType,
225 f.html_type as htmlType
226 FROM civicrm_custom_group g,
227 civicrm_custom_field f
228 WHERE f.custom_group_id = g.id
229 AND f.id = %1";
230 $queryParams = array(1 => array($params['fieldId'], 'Integer'));
231 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
232 if ($dao->fetch()) {
233 if ($dao->dataType == 'Money') {
234 $params['value'] = CRM_Utils_Rule::cleanMoney($params['value']);
235 }
236 switch ($dao->htmlType) {
237 case 'Autocomplete-Select':
238 case 'Select':
239 case 'Radio':
240 $query = "
241 UPDATE {$dao->tableName}
242 SET {$dao->columnName} = %1
243 WHERE id = %2";
244 if ($dao->dataType == 'Auto-complete') {
245 $dataType = "String";
246 }
247 else {
248 $dataType = $dao->dataType;
249 }
250 $queryParams = array(
251 1 => array($params['value'],
252 $dataType,
253 ),
254 2 => array(
255 $params['optionId'],
256 'Integer',
257 ),
258 );
259 break;
260
261 case 'AdvMulti-Select':
262 case 'Multi-Select':
263 case 'CheckBox':
264 $oldString = CRM_Core_DAO::VALUE_SEPARATOR . $oldValue . CRM_Core_DAO::VALUE_SEPARATOR;
265 $newString = CRM_Core_DAO::VALUE_SEPARATOR . $params['value'] . CRM_Core_DAO::VALUE_SEPARATOR;
266 $query = "
267 UPDATE {$dao->tableName}
268 SET {$dao->columnName} = REPLACE( {$dao->columnName}, %1, %2 )";
269 $queryParams = array(1 => array($oldString, 'String'),
270 2 => array($newString, 'String'),
271 );
272 break;
273
274 default:
275 CRM_Core_Error::fatal();
276 }
277 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
278 }
279 }
280
281 static function valuesByID($customFieldID, $optionGroupID = NULL) {
282 if (!$optionGroupID) {
283 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
284 $customFieldID,
285 'option_group_id'
286 );
287 }
288
289 $options = $optionGroupID ? CRM_Core_OptionGroup::valuesByID($optionGroupID) : array();
290
291 CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE);
292
293 return $options;
294 }
295 }
296