Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | class CRM_Core_OptionValue { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Static field for all the option value information that we can potentially export |
6a488035 TO |
39 | * |
40 | * @var array | |
41 | * @static | |
42 | */ | |
43 | static $_exportableFields = NULL; | |
44 | ||
45 | /** | |
100fef9d | 46 | * Static field for all the option value information that we can potentially export |
6a488035 TO |
47 | * |
48 | * @var array | |
49 | * @static | |
50 | */ | |
51 | static $_importableFields = NULL; | |
52 | ||
53 | /** | |
100fef9d | 54 | * Static field for all the option value information that we can potentially export |
6a488035 TO |
55 | * |
56 | * @var array | |
57 | * @static | |
58 | */ | |
59 | static $_fields = NULL; | |
60 | ||
61 | /** | |
100fef9d | 62 | * Return option-values of a particular group |
6a488035 | 63 | * |
6a0b768e TO |
64 | * @param array $groupParams |
65 | * Array containing group fields whose option-values is to retrieved. | |
66 | * @param string $orderBy | |
67 | * For orderBy clause. | |
68 | * @param array $links | |
69 | * Has links like edit, delete, disable ..etc. | |
6a488035 TO |
70 | * |
71 | * @return array of option-values | |
72 | * | |
6a488035 TO |
73 | * @static |
74 | */ | |
00be9182 | 75 | public static function getRows($groupParams, $links, $orderBy = 'weight') { |
6a488035 TO |
76 | $optionValue = array(); |
77 | ||
78 | $optionGroupID = NULL; | |
79 | if (!isset($groupParams['id']) || !$groupParams['id']) { | |
80 | if ($groupParams['name']) { | |
81 | $config = CRM_Core_Config::singleton(); | |
82 | ||
83 | $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc); | |
84 | $optionGroupID = $optionGroup->id; | |
85 | } | |
86 | } | |
87 | else { | |
88 | $optionGroupID = $groupParams['id']; | |
89 | } | |
90 | ||
91 | $groupName = CRM_Utils_Array::value('name', $groupParams); | |
92 | if (!$groupName && $optionGroupID) { | |
93 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
94 | $optionGroupID, 'name', 'id' | |
95 | ); | |
96 | } | |
97 | ||
98 | $dao = new CRM_Core_DAO_OptionValue(); | |
99 | ||
100 | if ($optionGroupID) { | |
101 | $dao->option_group_id = $optionGroupID; | |
102 | ||
103 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
104 | $dao->domain_id = CRM_Core_Config::domainID(); | |
105 | } | |
106 | ||
107 | $dao->orderBy($orderBy); | |
108 | $dao->find(); | |
109 | } | |
110 | ||
111 | if ($groupName == 'case_type') { | |
112 | $caseTypeIds = CRM_Case_BAO_Case::getUsedCaseType(); | |
113 | } | |
114 | elseif ($groupName == 'case_status') { | |
115 | $caseStatusIds = CRM_Case_BAO_Case::getUsedCaseStatuses(); | |
116 | } | |
117 | ||
118 | $componentNames = CRM_Core_Component::getNames(); | |
119 | $visibilityLabels = CRM_Core_PseudoConstant::visibility(); | |
120 | while ($dao->fetch()) { | |
121 | $optionValue[$dao->id] = array(); | |
122 | CRM_Core_DAO::storeValues($dao, $optionValue[$dao->id]); | |
123 | // form all action links | |
124 | $action = array_sum(array_keys($links)); | |
125 | ||
126 | // update enable/disable links depending on if it is is_reserved or is_active | |
127 | if ($dao->is_reserved) { | |
128 | $action = CRM_Core_Action::UPDATE; | |
129 | } | |
130 | else { | |
131 | if ($dao->is_active) { | |
132 | $action -= CRM_Core_Action::ENABLE; | |
133 | } | |
134 | else { | |
135 | $action -= CRM_Core_Action::DISABLE; | |
136 | } | |
137 | if ((($groupName == 'case_type') && in_array($dao->value, $caseTypeIds)) || | |
138 | (($groupName == 'case_status') && in_array($dao->value, $caseStatusIds)) | |
139 | ) { | |
140 | $action -= CRM_Core_Action::DELETE; | |
141 | } | |
142 | } | |
143 | ||
144 | $optionValue[$dao->id]['label'] = htmlspecialchars($optionValue[$dao->id]['label']); | |
145 | $optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight']; | |
146 | $optionValue[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, | |
147 | array( | |
148 | 'id' => $dao->id, | |
149 | 'gid' => $optionGroupID, | |
150 | 'value' => $dao->value, | |
87dab4a4 AH |
151 | ), |
152 | ts('more'), | |
153 | FALSE, | |
154 | 'optionValue.row.actions', | |
155 | 'optionValue', | |
156 | $dao->id | |
6a488035 TO |
157 | ); |
158 | ||
a7488080 | 159 | if (!empty($optionValue[$dao->id]['component_id'])) { |
6a488035 TO |
160 | $optionValue[$dao->id]['component_name'] = $componentNames[$optionValue[$dao->id]['component_id']]; |
161 | } | |
162 | else { | |
163 | $optionValue[$dao->id]['component_name'] = 'Contact'; | |
164 | } | |
165 | ||
a7488080 | 166 | if (!empty($optionValue[$dao->id]['visibility_id'])) { |
6a488035 TO |
167 | $optionValue[$dao->id]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id]['visibility_id']]; |
168 | } | |
169 | } | |
170 | ||
171 | return $optionValue; | |
172 | } | |
173 | ||
174 | /** | |
100fef9d | 175 | * Add/edit option-value of a particular group |
6a488035 | 176 | * |
6a0b768e TO |
177 | * @param array $params |
178 | * Array containing exported values from the invoking form. | |
179 | * @param array $groupParams | |
180 | * Array containing group fields whose option-values is to retrieved/saved. | |
fd31fa4c | 181 | * @param $action |
2aa397bc | 182 | * @param int $optionValueIDHas the id of the optionValue being edited, disabled ..etc. |
6a0b768e | 183 | * Has the id of the optionValue being edited, disabled ..etc. |
6a488035 | 184 | * |
7c285037 | 185 | * @return CRM_Core_DAO_OptionValue |
6a488035 | 186 | * |
6a488035 TO |
187 | * @static |
188 | */ | |
00be9182 | 189 | public static function addOptionValue(&$params, &$groupParams, &$action, &$optionValueID) { |
6a488035 TO |
190 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
191 | // checking if the group name with the given id or name (in $groupParams) exists | |
192 | if (!empty($groupParams)) { | |
193 | $config = CRM_Core_Config::singleton(); | |
194 | $groupParams['is_active'] = 1; | |
195 | $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults); | |
196 | } | |
197 | ||
198 | // if the corresponding group doesn't exist, create one, provided $groupParams has 'name' in it. | |
199 | if (!$optionGroup->id) { | |
200 | if ($groupParams['name']) { | |
201 | $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams, $defaults); | |
202 | $params['weight'] = 1; | |
203 | $optionGroupID = $newOptionGroup->id; | |
204 | } | |
205 | } | |
206 | else { | |
207 | $optionGroupID = $optionGroup->id; | |
208 | $oldWeight = NULL; | |
209 | if ($optionValueID) { | |
210 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id'); | |
211 | } | |
212 | $fieldValues = array('option_group_id' => $optionGroupID); | |
fc161185 | 213 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues); |
6a488035 TO |
214 | } |
215 | $params['option_group_id'] = $optionGroupID; | |
216 | ||
8cc574cf | 217 | if (($action & CRM_Core_Action::ADD) && empty($params['value'])) { |
6a488035 TO |
218 | $fieldValues = array('option_group_id' => $optionGroupID); |
219 | // use the next available value | |
220 | /* CONVERT(value, DECIMAL) is used to convert varchar | |
221 | field 'value' to decimal->integer */ | |
222 | ||
223 | $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
224 | $fieldValues, | |
225 | 'CONVERT(value, DECIMAL)' | |
226 | ); | |
227 | } | |
228 | if (!$params['label'] && $params['name']) { | |
229 | $params['label'] = $params['name']; | |
230 | } | |
231 | ||
232 | // set name to label if it's not set - but *only* for ADD action (CRM-3522) | |
8cc574cf | 233 | if (($action & CRM_Core_Action::ADD) && empty($params['name']) && $params['label']) { |
6a488035 TO |
234 | $params['name'] = $params['label']; |
235 | } | |
236 | if ($action & CRM_Core_Action::UPDATE) { | |
237 | $ids['optionValue'] = $optionValueID; | |
238 | } | |
239 | $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids); | |
240 | return $optionValue; | |
241 | } | |
242 | ||
243 | /** | |
244 | * Check if there is a record with the same name in the db | |
245 | * | |
6a0b768e TO |
246 | * @param string $value |
247 | * The value of the field we are checking. | |
248 | * @param string $daoName | |
249 | * The dao object name. | |
250 | * @param string $daoID | |
251 | * The id of the object being updated. u can change your name. | |
6a488035 | 252 | * as long as there is no conflict |
100fef9d | 253 | * @param int $optionGroupID |
6a0b768e TO |
254 | * @param string $fieldName |
255 | * The name of the field in the DAO. | |
6a488035 TO |
256 | * |
257 | * @return boolean true if object exists | |
6a488035 TO |
258 | * @static |
259 | */ | |
00be9182 | 260 | public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name') { |
4d5c2eb5 | 261 | $object = new $daoName(); |
6a488035 TO |
262 | $object->$fieldName = $value; |
263 | $object->option_group_id = $optionGroupID; | |
264 | ||
265 | if ($object->find(TRUE)) { | |
266 | return ($daoID && $object->id == $daoID) ? TRUE : FALSE; | |
267 | } | |
268 | else { | |
269 | return TRUE; | |
270 | } | |
271 | } | |
272 | ||
273 | /** | |
274 | * Check if there is a record with the same name in the db | |
275 | * | |
da6b46f4 EM |
276 | * @param string $mode |
277 | * @param string $contactType | |
278 | * | |
6a488035 | 279 | * @return boolean true if object exists |
6a488035 TO |
280 | * @static |
281 | */ | |
00be9182 | 282 | public static function getFields($mode = '', $contactType = 'Individual') { |
6a488035 TO |
283 | $key = "$mode $contactType"; |
284 | if (empty(self::$_fields[$key]) || !self::$_fields[$key]) { | |
285 | self::$_fields[$key] = array(); | |
286 | ||
287 | $option = CRM_Core_DAO_OptionValue::import(); | |
288 | ||
289 | foreach (array_keys($option) as $id) { | |
290 | $optionName = $option[$id]; | |
291 | } | |
292 | ||
293 | $nameTitle = array(); | |
294 | if ($mode == 'contribute') { | |
295 | $nameTitle = array( | |
296 | 'payment_instrument' => array( | |
297 | 'name' => 'payment_instrument', | |
298 | 'title' => ts('Payment Instrument'), | |
299 | 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i', | |
300 | ), | |
301 | ); | |
302 | } | |
303 | elseif ($mode == '') { | |
304 | //the fields email greeting and postal greeting are meant only for Individual and Household | |
305 | //the field addressee is meant for all contact types, CRM-4575 | |
306 | if (in_array($contactType, array( | |
307 | 'Individual', 'Household', 'Organization', 'All'))) { | |
308 | $nameTitle = array( | |
1071730c DL |
309 | 'addressee' => array( |
310 | 'name' => 'addressee', | |
6a488035 TO |
311 | 'title' => ts('Addressee'), |
312 | 'headerPattern' => '/^addressee$/i', | |
313 | ), | |
314 | ); | |
315 | $title = array( | |
1071730c DL |
316 | 'email_greeting' => array( |
317 | 'name' => 'email_greeting', | |
6a488035 TO |
318 | 'title' => ts('Email Greeting'), |
319 | 'headerPattern' => '/^email_greeting$/i', | |
320 | ), | |
321 | 'postal_greeting' => array( | |
322 | 'name' => 'postal_greeting', | |
323 | 'title' => ts('Postal Greeting'), | |
324 | 'headerPattern' => '/^postal_greeting$/i', | |
325 | ), | |
326 | ); | |
327 | $nameTitle = array_merge($nameTitle, $title); | |
328 | } | |
0c145cc0 | 329 | } |
6a488035 TO |
330 | |
331 | if (is_array($nameTitle)) { | |
332 | foreach ($nameTitle as $name => $attribs) { | |
333 | self::$_fields[$key][$name] = $optionName; | |
334 | list($tableName, $fieldName) = explode('.', $optionName['where']); | |
6a488035 TO |
335 | self::$_fields[$key][$name]['where'] = "{$name}.label"; |
336 | foreach ($attribs as $k => $val) { | |
337 | self::$_fields[$key][$name][$k] = $val; | |
338 | } | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | return self::$_fields[$key]; | |
344 | } | |
345 | ||
346 | /** | |
100fef9d | 347 | * Build select query in case of option-values |
6a488035 | 348 | * |
2a6da8d7 EM |
349 | * @param $query |
350 | * | |
6a488035 | 351 | * @return void |
6a488035 | 352 | */ |
00be9182 | 353 | public static function select(&$query) { |
6a488035 TO |
354 | if (!empty($query->_params) || !empty($query->_returnProperties)) { |
355 | $field = self::getFields(); | |
314dbef8 | 356 | foreach ($field as $name => $values) { |
a7488080 | 357 | if (!empty($values['pseudoconstant'])) { |
314dbef8 DL |
358 | continue; |
359 | } | |
360 | list($tableName, $fieldName) = explode('.', $values['where']); | |
a7488080 | 361 | if (!empty($query->_returnProperties[$name])) { |
6a488035 TO |
362 | $query->_select["{$name}_id"] = "{$name}.value as {$name}_id"; |
363 | $query->_element["{$name}_id"] = 1; | |
364 | $query->_select[$name] = "{$name}.{$fieldName} as $name"; | |
365 | $query->_tables[$tableName] = 1; | |
366 | $query->_element[$name] = 1; | |
367 | } | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | /** | |
100fef9d | 373 | * Return option-values of a particular group |
6a488035 | 374 | * |
6a0b768e TO |
375 | * @param array $groupParams |
376 | * Array containing group fields. | |
6a488035 | 377 | * whose option-values is to retrieved. |
6a0b768e TO |
378 | * @param array $values |
379 | * (reference) to the array which. | |
6a488035 | 380 | * will have the values for the group |
6a0b768e TO |
381 | * @param string $orderBy |
382 | * For orderBy clause. | |
6a488035 | 383 | * |
2aa397bc | 384 | * @param bool $isActiveDo you want only active option values?. |
6a0b768e | 385 | * Do you want only active option values?. |
6a488035 TO |
386 | * |
387 | * @return array of option-values | |
388 | * | |
6a488035 TO |
389 | * @static |
390 | */ | |
00be9182 | 391 | public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) { |
6a488035 TO |
392 | if (empty($groupParams)) { |
393 | return NULL; | |
394 | } | |
395 | $select = " | |
396 | SELECT | |
397 | option_value.id as id, | |
398 | option_value.label as label, | |
399 | option_value.value as value, | |
400 | option_value.name as name, | |
401 | option_value.description as description, | |
402 | option_value.weight as weight, | |
403 | option_value.is_active as is_active, | |
404 | option_value.is_default as is_default"; | |
405 | ||
406 | $from = " | |
407 | FROM | |
408 | civicrm_option_value as option_value, | |
409 | civicrm_option_group as option_group "; | |
410 | ||
411 | $where = " WHERE option_group.id = option_value.option_group_id "; | |
412 | ||
413 | if ($isActive) { | |
414 | $where .= " AND option_value.is_active = " . $isActive; | |
415 | } | |
416 | ||
417 | $order = " ORDER BY " . $orderBy; | |
418 | ||
419 | $groupId = CRM_Utils_Array::value('id', $groupParams); | |
420 | $groupName = CRM_Utils_Array::value('name', $groupParams); | |
421 | ||
422 | if ($groupId) { | |
423 | $where .= " AND option_group.id = %1"; | |
424 | $params[1] = array($groupId, 'Integer'); | |
425 | if (!$groupName) { | |
426 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
427 | $groupId, 'name', 'id' | |
428 | ); | |
429 | } | |
430 | } | |
431 | ||
432 | if ($groupName) { | |
433 | $where .= " AND option_group.name = %2"; | |
434 | $params[2] = array($groupName, 'String'); | |
435 | } | |
436 | ||
437 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
438 | $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID(); | |
439 | } | |
440 | ||
441 | $query = $select . $from . $where . $order; | |
442 | ||
443 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
444 | ||
445 | while ($dao->fetch()) { | |
446 | $values[$dao->id] = array( | |
447 | 'id' => $dao->id, | |
448 | 'label' => $dao->label, | |
449 | 'value' => $dao->value, | |
450 | 'name' => $dao->name, | |
451 | 'description' => $dao->description, | |
452 | 'weight' => $dao->weight, | |
453 | 'is_active' => $dao->is_active, | |
454 | 'is_default' => $dao->is_default, | |
455 | ); | |
456 | } | |
457 | } | |
458 | } |