Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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. | |
91484e0a | 65 | * @param bool $skipEmptyComponents |
4d669903 | 66 | * Whether to skip OptionValue rows with empty Component name |
67 | * (i.e. when Extension providing the Component is disabled) | |
6a488035 | 68 | * |
a6c01b45 | 69 | * @return array |
16b10e64 | 70 | * Array of option-values |
6a488035 | 71 | * |
6a488035 | 72 | */ |
4d669903 | 73 | public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) { |
6a488035 | 74 | $optionValue = array(); |
6a488035 | 75 | $optionGroupID = NULL; |
b249a148 MD |
76 | $isGroupLocked = FALSE; |
77 | ||
6a488035 TO |
78 | if (!isset($groupParams['id']) || !$groupParams['id']) { |
79 | if ($groupParams['name']) { | |
6a488035 TO |
80 | $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc); |
81 | $optionGroupID = $optionGroup->id; | |
b249a148 | 82 | $isGroupLocked = (bool) $optionGroup->is_locked; |
6a488035 TO |
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]); | |
4d669903 | 121 | if (!empty($optionValue[$dao->id]['component_id']) && |
122 | empty($componentNames[$optionValue[$dao->id]['component_id']]) && | |
123 | $skipEmptyComponents | |
124 | ) { | |
125 | unset($optionValue[$dao->id]); | |
126 | continue; | |
127 | } | |
6a488035 TO |
128 | // form all action links |
129 | $action = array_sum(array_keys($links)); | |
130 | ||
131 | // update enable/disable links depending on if it is is_reserved or is_active | |
132 | if ($dao->is_reserved) { | |
133 | $action = CRM_Core_Action::UPDATE; | |
134 | } | |
135 | else { | |
136 | if ($dao->is_active) { | |
137 | $action -= CRM_Core_Action::ENABLE; | |
138 | } | |
139 | else { | |
140 | $action -= CRM_Core_Action::DISABLE; | |
141 | } | |
142 | if ((($groupName == 'case_type') && in_array($dao->value, $caseTypeIds)) || | |
143 | (($groupName == 'case_status') && in_array($dao->value, $caseStatusIds)) | |
144 | ) { | |
145 | $action -= CRM_Core_Action::DELETE; | |
146 | } | |
147 | } | |
148 | ||
b249a148 | 149 | // disallow deletion of option values for locked groups |
218d9fd2 | 150 | if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) { |
b249a148 MD |
151 | $action -= CRM_Core_Action::DELETE; |
152 | } | |
153 | ||
6a488035 TO |
154 | $optionValue[$dao->id]['label'] = htmlspecialchars($optionValue[$dao->id]['label']); |
155 | $optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight']; | |
00c2ff9e | 156 | $optionValue[$dao->id]['icon'] = CRM_Utils_Array::value('icon', $optionValue[$dao->id], ''); |
6a488035 TO |
157 | $optionValue[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action, |
158 | array( | |
159 | 'id' => $dao->id, | |
160 | 'gid' => $optionGroupID, | |
161 | 'value' => $dao->value, | |
87dab4a4 AH |
162 | ), |
163 | ts('more'), | |
164 | FALSE, | |
165 | 'optionValue.row.actions', | |
166 | 'optionValue', | |
167 | $dao->id | |
6a488035 TO |
168 | ); |
169 | ||
a7488080 | 170 | if (!empty($optionValue[$dao->id]['component_id'])) { |
6a488035 TO |
171 | $optionValue[$dao->id]['component_name'] = $componentNames[$optionValue[$dao->id]['component_id']]; |
172 | } | |
173 | else { | |
174 | $optionValue[$dao->id]['component_name'] = 'Contact'; | |
175 | } | |
176 | ||
a7488080 | 177 | if (!empty($optionValue[$dao->id]['visibility_id'])) { |
6a488035 TO |
178 | $optionValue[$dao->id]['visibility_label'] = $visibilityLabels[$optionValue[$dao->id]['visibility_id']]; |
179 | } | |
180 | } | |
181 | ||
182 | return $optionValue; | |
183 | } | |
184 | ||
185 | /** | |
100fef9d | 186 | * Add/edit option-value of a particular group |
6a488035 | 187 | * |
6a0b768e TO |
188 | * @param array $params |
189 | * Array containing exported values from the invoking form. | |
ff625280 | 190 | * @param string $optionGroupName |
6a0b768e | 191 | * Array containing group fields whose option-values is to retrieved/saved. |
fd31fa4c | 192 | * @param $action |
8d7a9d07 | 193 | * @param int $optionValueID Has the id of the optionValue being edited, disabled ..etc. |
6a0b768e | 194 | * Has the id of the optionValue being edited, disabled ..etc. |
6a488035 | 195 | * |
7c285037 | 196 | * @return CRM_Core_DAO_OptionValue |
6a488035 | 197 | * |
6a488035 | 198 | */ |
ff625280 | 199 | public static function addOptionValue(&$params, $optionGroupName, $action, $optionValueID) { |
6a488035 TO |
200 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
201 | // checking if the group name with the given id or name (in $groupParams) exists | |
ff625280 | 202 | $groupParams = ['name' => $optionGroupName, 'is_active' => 1]; |
203 | $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults); | |
6a488035 | 204 | |
ff625280 | 205 | // if the corresponding group doesn't exist, create one. |
6a488035 | 206 | if (!$optionGroup->id) { |
ff625280 | 207 | $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams); |
208 | $params['weight'] = 1; | |
209 | $optionGroupID = $newOptionGroup->id; | |
6a488035 TO |
210 | } |
211 | else { | |
212 | $optionGroupID = $optionGroup->id; | |
213 | $oldWeight = NULL; | |
214 | if ($optionValueID) { | |
215 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id'); | |
216 | } | |
217 | $fieldValues = array('option_group_id' => $optionGroupID); | |
fc161185 | 218 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues); |
6a488035 TO |
219 | } |
220 | $params['option_group_id'] = $optionGroupID; | |
221 | ||
ce7ccb1e | 222 | if (($action & CRM_Core_Action::ADD) && !isset($params['value'])) { |
6a488035 TO |
223 | $fieldValues = array('option_group_id' => $optionGroupID); |
224 | // use the next available value | |
225 | /* CONVERT(value, DECIMAL) is used to convert varchar | |
e70a7fc0 | 226 | field 'value' to decimal->integer */ |
6a488035 TO |
227 | |
228 | $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', | |
229 | $fieldValues, | |
230 | 'CONVERT(value, DECIMAL)' | |
231 | ); | |
232 | } | |
233 | if (!$params['label'] && $params['name']) { | |
234 | $params['label'] = $params['name']; | |
235 | } | |
236 | ||
237 | // set name to label if it's not set - but *only* for ADD action (CRM-3522) | |
8cc574cf | 238 | if (($action & CRM_Core_Action::ADD) && empty($params['name']) && $params['label']) { |
6a488035 TO |
239 | $params['name'] = $params['label']; |
240 | } | |
241 | if ($action & CRM_Core_Action::UPDATE) { | |
7b3b2c95 | 242 | $params['id'] = $optionValueID; |
6a488035 | 243 | } |
7b3b2c95 | 244 | $optionValue = CRM_Core_BAO_OptionValue::add($params); |
6a488035 TO |
245 | return $optionValue; |
246 | } | |
247 | ||
248 | /** | |
d09edf64 | 249 | * Check if there is a record with the same name in the db. |
6a488035 | 250 | * |
6a0b768e TO |
251 | * @param string $value |
252 | * The value of the field we are checking. | |
253 | * @param string $daoName | |
254 | * The dao object name. | |
255 | * @param string $daoID | |
256 | * The id of the object being updated. u can change your name. | |
6a488035 | 257 | * as long as there is no conflict |
100fef9d | 258 | * @param int $optionGroupID |
6a0b768e TO |
259 | * @param string $fieldName |
260 | * The name of the field in the DAO. | |
e6101f17 | 261 | * @param bool $domainSpecific |
0c1e1321 SL |
262 | * Filter this check to the current domain. |
263 | * Some optionGroups allow for same labels or same names but | |
264 | * they must be in different domains, so filter the check to | |
265 | * the current domain. | |
6a488035 | 266 | * |
8d7a9d07 | 267 | * @return bool |
a6c01b45 | 268 | * true if object exists |
6a488035 | 269 | */ |
e6101f17 | 270 | public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name', $domainSpecific) { |
4d5c2eb5 | 271 | $object = new $daoName(); |
6a488035 TO |
272 | $object->$fieldName = $value; |
273 | $object->option_group_id = $optionGroupID; | |
274 | ||
e6101f17 SL |
275 | if ($domainSpecific) { |
276 | $object->domain_id = CRM_Core_Config::domainID(); | |
277 | } | |
278 | ||
6a488035 TO |
279 | if ($object->find(TRUE)) { |
280 | return ($daoID && $object->id == $daoID) ? TRUE : FALSE; | |
281 | } | |
282 | else { | |
283 | return TRUE; | |
284 | } | |
285 | } | |
286 | ||
287 | /** | |
d09edf64 | 288 | * Check if there is a record with the same name in the db. |
6a488035 | 289 | * |
da6b46f4 EM |
290 | * @param string $mode |
291 | * @param string $contactType | |
292 | * | |
3058f4d9 | 293 | * @return array |
6a488035 | 294 | */ |
00be9182 | 295 | public static function getFields($mode = '', $contactType = 'Individual') { |
6a488035 TO |
296 | $key = "$mode $contactType"; |
297 | if (empty(self::$_fields[$key]) || !self::$_fields[$key]) { | |
298 | self::$_fields[$key] = array(); | |
299 | ||
300 | $option = CRM_Core_DAO_OptionValue::import(); | |
301 | ||
302 | foreach (array_keys($option) as $id) { | |
303 | $optionName = $option[$id]; | |
304 | } | |
305 | ||
306 | $nameTitle = array(); | |
307 | if ($mode == 'contribute') { | |
8f165fa5 | 308 | // This is part of a move towards standardising option values but we |
309 | // should derive them from the fields array so am deprecating it again... | |
310 | // note that the reason this was needed was that payment_instrument_id was | |
311 | // not set to exportable. | |
6a488035 TO |
312 | $nameTitle = array( |
313 | 'payment_instrument' => array( | |
314 | 'name' => 'payment_instrument', | |
536f0e02 | 315 | 'title' => ts('Payment Method'), |
6a488035 TO |
316 | 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i', |
317 | ), | |
318 | ); | |
319 | } | |
320 | elseif ($mode == '') { | |
321 | //the fields email greeting and postal greeting are meant only for Individual and Household | |
322 | //the field addressee is meant for all contact types, CRM-4575 | |
323 | if (in_array($contactType, array( | |
353ffa53 TO |
324 | 'Individual', |
325 | 'Household', | |
326 | 'Organization', | |
8d7a9d07 | 327 | 'All', |
353ffa53 | 328 | ))) { |
6a488035 | 329 | $nameTitle = array( |
1071730c DL |
330 | 'addressee' => array( |
331 | 'name' => 'addressee', | |
6a488035 TO |
332 | 'title' => ts('Addressee'), |
333 | 'headerPattern' => '/^addressee$/i', | |
334 | ), | |
335 | ); | |
336 | $title = array( | |
1071730c DL |
337 | 'email_greeting' => array( |
338 | 'name' => 'email_greeting', | |
6a488035 TO |
339 | 'title' => ts('Email Greeting'), |
340 | 'headerPattern' => '/^email_greeting$/i', | |
341 | ), | |
342 | 'postal_greeting' => array( | |
343 | 'name' => 'postal_greeting', | |
344 | 'title' => ts('Postal Greeting'), | |
345 | 'headerPattern' => '/^postal_greeting$/i', | |
346 | ), | |
347 | ); | |
348 | $nameTitle = array_merge($nameTitle, $title); | |
349 | } | |
0c145cc0 | 350 | } |
6a488035 TO |
351 | |
352 | if (is_array($nameTitle)) { | |
353 | foreach ($nameTitle as $name => $attribs) { | |
354 | self::$_fields[$key][$name] = $optionName; | |
355 | list($tableName, $fieldName) = explode('.', $optionName['where']); | |
6a488035 TO |
356 | self::$_fields[$key][$name]['where'] = "{$name}.label"; |
357 | foreach ($attribs as $k => $val) { | |
358 | self::$_fields[$key][$name][$k] = $val; | |
359 | } | |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | return self::$_fields[$key]; | |
365 | } | |
366 | ||
367 | /** | |
100fef9d | 368 | * Build select query in case of option-values |
6a488035 | 369 | * |
2a6da8d7 | 370 | * @param $query |
6a488035 | 371 | */ |
00be9182 | 372 | public static function select(&$query) { |
6a488035 TO |
373 | if (!empty($query->_params) || !empty($query->_returnProperties)) { |
374 | $field = self::getFields(); | |
314dbef8 | 375 | foreach ($field as $name => $values) { |
a7488080 | 376 | if (!empty($values['pseudoconstant'])) { |
314dbef8 DL |
377 | continue; |
378 | } | |
379 | list($tableName, $fieldName) = explode('.', $values['where']); | |
a7488080 | 380 | if (!empty($query->_returnProperties[$name])) { |
6a488035 TO |
381 | $query->_select["{$name}_id"] = "{$name}.value as {$name}_id"; |
382 | $query->_element["{$name}_id"] = 1; | |
383 | $query->_select[$name] = "{$name}.{$fieldName} as $name"; | |
384 | $query->_tables[$tableName] = 1; | |
385 | $query->_element[$name] = 1; | |
386 | } | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | /** | |
100fef9d | 392 | * Return option-values of a particular group |
6a488035 | 393 | * |
6a0b768e TO |
394 | * @param array $groupParams |
395 | * Array containing group fields. | |
6a488035 | 396 | * whose option-values is to retrieved. |
6a0b768e TO |
397 | * @param array $values |
398 | * (reference) to the array which. | |
6a488035 | 399 | * will have the values for the group |
6a0b768e TO |
400 | * @param string $orderBy |
401 | * For orderBy clause. | |
6a488035 | 402 | * |
8d7a9d07 | 403 | * @param bool $isActive Do you want only active option values? |
6a488035 | 404 | * |
a6c01b45 | 405 | * @return array |
16b10e64 | 406 | * Array of option-values |
6a488035 | 407 | * |
6a488035 | 408 | */ |
00be9182 | 409 | public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) { |
6a488035 TO |
410 | if (empty($groupParams)) { |
411 | return NULL; | |
412 | } | |
413 | $select = " | |
414 | SELECT | |
415 | option_value.id as id, | |
416 | option_value.label as label, | |
417 | option_value.value as value, | |
418 | option_value.name as name, | |
419 | option_value.description as description, | |
420 | option_value.weight as weight, | |
421 | option_value.is_active as is_active, | |
422 | option_value.is_default as is_default"; | |
423 | ||
424 | $from = " | |
425 | FROM | |
426 | civicrm_option_value as option_value, | |
427 | civicrm_option_group as option_group "; | |
428 | ||
429 | $where = " WHERE option_group.id = option_value.option_group_id "; | |
430 | ||
431 | if ($isActive) { | |
432 | $where .= " AND option_value.is_active = " . $isActive; | |
433 | } | |
434 | ||
435 | $order = " ORDER BY " . $orderBy; | |
436 | ||
437 | $groupId = CRM_Utils_Array::value('id', $groupParams); | |
438 | $groupName = CRM_Utils_Array::value('name', $groupParams); | |
439 | ||
440 | if ($groupId) { | |
441 | $where .= " AND option_group.id = %1"; | |
442 | $params[1] = array($groupId, 'Integer'); | |
443 | if (!$groupName) { | |
444 | $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', | |
445 | $groupId, 'name', 'id' | |
446 | ); | |
447 | } | |
448 | } | |
449 | ||
450 | if ($groupName) { | |
451 | $where .= " AND option_group.name = %2"; | |
452 | $params[2] = array($groupName, 'String'); | |
453 | } | |
454 | ||
455 | if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
456 | $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID(); | |
457 | } | |
458 | ||
459 | $query = $select . $from . $where . $order; | |
460 | ||
461 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
462 | ||
463 | while ($dao->fetch()) { | |
464 | $values[$dao->id] = array( | |
465 | 'id' => $dao->id, | |
466 | 'label' => $dao->label, | |
467 | 'value' => $dao->value, | |
468 | 'name' => $dao->name, | |
469 | 'description' => $dao->description, | |
470 | 'weight' => $dao->weight, | |
471 | 'is_active' => $dao->is_active, | |
472 | 'is_default' => $dao->is_default, | |
473 | ); | |
474 | } | |
475 | } | |
96025800 | 476 | |
6a488035 | 477 | } |