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