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