Merge pull request #15972 from aydun/report#24
[civicrm-core.git] / CRM / Core / OptionValue.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Core_OptionValue {
18
19 /**
d09edf64 20 * Static field for all the option value information that we can potentially export.
6a488035
TO
21 *
22 * @var array
6a488035 23 */
518fa0ee 24 public static $_exportableFields = NULL;
6a488035
TO
25
26 /**
d09edf64 27 * Static field for all the option value information that we can potentially export.
6a488035
TO
28 *
29 * @var array
6a488035 30 */
518fa0ee 31 public static $_importableFields = NULL;
6a488035
TO
32
33 /**
d09edf64 34 * Static field for all the option value information that we can potentially export.
6a488035
TO
35 *
36 * @var array
6a488035 37 */
518fa0ee 38 public static $_fields = NULL;
6a488035
TO
39
40 /**
100fef9d 41 * Return option-values of a particular group
6a488035 42 *
6a0b768e
TO
43 * @param array $groupParams
44 * Array containing group fields whose option-values is to retrieved.
6a0b768e
TO
45 * @param array $links
46 * Has links like edit, delete, disable ..etc.
8d7a9d07
CB
47 * @param string $orderBy
48 * For orderBy clause.
91484e0a 49 * @param bool $skipEmptyComponents
4d669903 50 * Whether to skip OptionValue rows with empty Component name
51 * (i.e. when Extension providing the Component is disabled)
6a488035 52 *
a6c01b45 53 * @return array
16b10e64 54 * Array of option-values
6a488035 55 *
6a488035 56 */
4d669903 57 public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) {
be2fb01f 58 $optionValue = [];
6a488035 59 $optionGroupID = NULL;
b249a148
MD
60 $isGroupLocked = FALSE;
61
6a488035
TO
62 if (!isset($groupParams['id']) || !$groupParams['id']) {
63 if ($groupParams['name']) {
6a488035
TO
64 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc);
65 $optionGroupID = $optionGroup->id;
b249a148 66 $isGroupLocked = (bool) $optionGroup->is_locked;
6a488035
TO
67 }
68 }
69 else {
70 $optionGroupID = $groupParams['id'];
71 }
72
73 $groupName = CRM_Utils_Array::value('name', $groupParams);
74 if (!$groupName && $optionGroupID) {
75 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
76 $optionGroupID, 'name', 'id'
77 );
78 }
79
80 $dao = new CRM_Core_DAO_OptionValue();
81
82 if ($optionGroupID) {
83 $dao->option_group_id = $optionGroupID;
84
85 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
86 $dao->domain_id = CRM_Core_Config::domainID();
87 }
88
89 $dao->orderBy($orderBy);
90 $dao->find();
91 }
92
93 if ($groupName == 'case_type') {
94 $caseTypeIds = CRM_Case_BAO_Case::getUsedCaseType();
95 }
96 elseif ($groupName == 'case_status') {
97 $caseStatusIds = CRM_Case_BAO_Case::getUsedCaseStatuses();
98 }
99
100 $componentNames = CRM_Core_Component::getNames();
101 $visibilityLabels = CRM_Core_PseudoConstant::visibility();
102 while ($dao->fetch()) {
be2fb01f 103 $optionValue[$dao->id] = [];
6a488035 104 CRM_Core_DAO::storeValues($dao, $optionValue[$dao->id]);
4d669903 105 if (!empty($optionValue[$dao->id]['component_id']) &&
106 empty($componentNames[$optionValue[$dao->id]['component_id']]) &&
107 $skipEmptyComponents
108 ) {
109 unset($optionValue[$dao->id]);
110 continue;
111 }
6a488035
TO
112 // form all action links
113 $action = array_sum(array_keys($links));
114
115 // update enable/disable links depending on if it is is_reserved or is_active
116 if ($dao->is_reserved) {
117 $action = CRM_Core_Action::UPDATE;
118 }
119 else {
120 if ($dao->is_active) {
121 $action -= CRM_Core_Action::ENABLE;
122 }
123 else {
124 $action -= CRM_Core_Action::DISABLE;
125 }
126 if ((($groupName == 'case_type') && in_array($dao->value, $caseTypeIds)) ||
127 (($groupName == 'case_status') && in_array($dao->value, $caseStatusIds))
128 ) {
129 $action -= CRM_Core_Action::DELETE;
130 }
131 }
132
b249a148 133 // disallow deletion of option values for locked groups
218d9fd2 134 if (($action & CRM_Core_Action::DELETE) && $isGroupLocked) {
b249a148
MD
135 $action -= CRM_Core_Action::DELETE;
136 }
137
6a488035
TO
138 $optionValue[$dao->id]['label'] = htmlspecialchars($optionValue[$dao->id]['label']);
139 $optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight'];
00c2ff9e 140 $optionValue[$dao->id]['icon'] = CRM_Utils_Array::value('icon', $optionValue[$dao->id], '');
6a488035 141 $optionValue[$dao->id]['action'] = CRM_Core_Action::formLink($links, $action,
be2fb01f 142 [
6a488035
TO
143 'id' => $dao->id,
144 'gid' => $optionGroupID,
145 'value' => $dao->value,
be2fb01f 146 ],
87dab4a4
AH
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.
ff625280 174 * @param string $optionGroupName
6a0b768e 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 */
ff625280 183 public static function addOptionValue(&$params, $optionGroupName, $action, $optionValueID) {
6a488035
TO
184 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
185 // checking if the group name with the given id or name (in $groupParams) exists
ff625280 186 $groupParams = ['name' => $optionGroupName, 'is_active' => 1];
187 $optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $defaults);
6a488035 188
ff625280 189 // if the corresponding group doesn't exist, create one.
6a488035 190 if (!$optionGroup->id) {
ff625280 191 $newOptionGroup = CRM_Core_BAO_OptionGroup::add($groupParams);
192 $params['weight'] = 1;
193 $optionGroupID = $newOptionGroup->id;
6a488035
TO
194 }
195 else {
196 $optionGroupID = $optionGroup->id;
197 $oldWeight = NULL;
198 if ($optionValueID) {
199 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'weight', 'id');
200 }
be2fb01f 201 $fieldValues = ['option_group_id' => $optionGroupID];
fc161185 202 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, CRM_Utils_Array::value('weight', $params), $fieldValues);
6a488035
TO
203 }
204 $params['option_group_id'] = $optionGroupID;
205
ce7ccb1e 206 if (($action & CRM_Core_Action::ADD) && !isset($params['value'])) {
be2fb01f 207 $fieldValues = ['option_group_id' => $optionGroupID];
6a488035
TO
208 // use the next available value
209 /* CONVERT(value, DECIMAL) is used to convert varchar
e70a7fc0 210 field 'value' to decimal->integer */
6a488035
TO
211
212 $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
213 $fieldValues,
214 'CONVERT(value, DECIMAL)'
215 );
216 }
217 if (!$params['label'] && $params['name']) {
218 $params['label'] = $params['name'];
219 }
220
221 // set name to label if it's not set - but *only* for ADD action (CRM-3522)
8cc574cf 222 if (($action & CRM_Core_Action::ADD) && empty($params['name']) && $params['label']) {
6a488035
TO
223 $params['name'] = $params['label'];
224 }
225 if ($action & CRM_Core_Action::UPDATE) {
7b3b2c95 226 $params['id'] = $optionValueID;
6a488035 227 }
7b3b2c95 228 $optionValue = CRM_Core_BAO_OptionValue::add($params);
6a488035
TO
229 return $optionValue;
230 }
231
232 /**
d09edf64 233 * Check if there is a record with the same name in the db.
6a488035 234 *
6a0b768e
TO
235 * @param string $value
236 * The value of the field we are checking.
237 * @param string $daoName
238 * The dao object name.
239 * @param string $daoID
240 * The id of the object being updated. u can change your name.
6a488035 241 * as long as there is no conflict
100fef9d 242 * @param int $optionGroupID
6a0b768e
TO
243 * @param string $fieldName
244 * The name of the field in the DAO.
e6101f17 245 * @param bool $domainSpecific
0c1e1321
SL
246 * Filter this check to the current domain.
247 * Some optionGroups allow for same labels or same names but
248 * they must be in different domains, so filter the check to
249 * the current domain.
6a488035 250 *
8d7a9d07 251 * @return bool
a6c01b45 252 * true if object exists
6a488035 253 */
e6101f17 254 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName = 'name', $domainSpecific) {
4d5c2eb5 255 $object = new $daoName();
6a488035
TO
256 $object->$fieldName = $value;
257 $object->option_group_id = $optionGroupID;
258
e6101f17
SL
259 if ($domainSpecific) {
260 $object->domain_id = CRM_Core_Config::domainID();
261 }
262
6a488035
TO
263 if ($object->find(TRUE)) {
264 return ($daoID && $object->id == $daoID) ? TRUE : FALSE;
265 }
266 else {
267 return TRUE;
268 }
269 }
270
271 /**
d09edf64 272 * Check if there is a record with the same name in the db.
6a488035 273 *
da6b46f4
EM
274 * @param string $mode
275 * @param string $contactType
276 *
3058f4d9 277 * @return array
6a488035 278 */
00be9182 279 public static function getFields($mode = '', $contactType = 'Individual') {
6a488035
TO
280 $key = "$mode $contactType";
281 if (empty(self::$_fields[$key]) || !self::$_fields[$key]) {
be2fb01f 282 self::$_fields[$key] = [];
6a488035
TO
283
284 $option = CRM_Core_DAO_OptionValue::import();
285
286 foreach (array_keys($option) as $id) {
287 $optionName = $option[$id];
288 }
289
be2fb01f 290 $nameTitle = [];
6a488035 291 if ($mode == 'contribute') {
8f165fa5 292 // This is part of a move towards standardising option values but we
293 // should derive them from the fields array so am deprecating it again...
294 // note that the reason this was needed was that payment_instrument_id was
295 // not set to exportable.
be2fb01f
CW
296 $nameTitle = [
297 'payment_instrument' => [
6a488035 298 'name' => 'payment_instrument',
536f0e02 299 'title' => ts('Payment Method'),
6a488035 300 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
be2fb01f
CW
301 ],
302 ];
6a488035
TO
303 }
304 elseif ($mode == '') {
305 //the fields email greeting and postal greeting are meant only for Individual and Household
306 //the field addressee is meant for all contact types, CRM-4575
be2fb01f 307 if (in_array($contactType, [
353ffa53
TO
308 'Individual',
309 'Household',
310 'Organization',
8d7a9d07 311 'All',
be2fb01f
CW
312 ])) {
313 $nameTitle = [
314 'addressee' => [
1071730c 315 'name' => 'addressee',
6a488035
TO
316 'title' => ts('Addressee'),
317 'headerPattern' => '/^addressee$/i',
be2fb01f
CW
318 ],
319 ];
320 $title = [
321 'email_greeting' => [
1071730c 322 'name' => 'email_greeting',
6a488035
TO
323 'title' => ts('Email Greeting'),
324 'headerPattern' => '/^email_greeting$/i',
be2fb01f
CW
325 ],
326 'postal_greeting' => [
6a488035
TO
327 'name' => 'postal_greeting',
328 'title' => ts('Postal Greeting'),
329 'headerPattern' => '/^postal_greeting$/i',
be2fb01f
CW
330 ],
331 ];
6a488035
TO
332 $nameTitle = array_merge($nameTitle, $title);
333 }
0c145cc0 334 }
6a488035
TO
335
336 if (is_array($nameTitle)) {
337 foreach ($nameTitle as $name => $attribs) {
338 self::$_fields[$key][$name] = $optionName;
339 list($tableName, $fieldName) = explode('.', $optionName['where']);
6a488035
TO
340 self::$_fields[$key][$name]['where'] = "{$name}.label";
341 foreach ($attribs as $k => $val) {
342 self::$_fields[$key][$name][$k] = $val;
343 }
344 }
345 }
346 }
347
348 return self::$_fields[$key];
349 }
350
351 /**
100fef9d 352 * Build select query in case of option-values
6a488035 353 *
2a6da8d7 354 * @param $query
6a488035 355 */
00be9182 356 public static function select(&$query) {
6a488035
TO
357 if (!empty($query->_params) || !empty($query->_returnProperties)) {
358 $field = self::getFields();
314dbef8 359 foreach ($field as $name => $values) {
a7488080 360 if (!empty($values['pseudoconstant'])) {
314dbef8
DL
361 continue;
362 }
363 list($tableName, $fieldName) = explode('.', $values['where']);
a7488080 364 if (!empty($query->_returnProperties[$name])) {
6a488035
TO
365 $query->_select["{$name}_id"] = "{$name}.value as {$name}_id";
366 $query->_element["{$name}_id"] = 1;
367 $query->_select[$name] = "{$name}.{$fieldName} as $name";
368 $query->_tables[$tableName] = 1;
369 $query->_element[$name] = 1;
370 }
371 }
372 }
373 }
374
375 /**
100fef9d 376 * Return option-values of a particular group
6a488035 377 *
6a0b768e
TO
378 * @param array $groupParams
379 * Array containing group fields.
6a488035 380 * whose option-values is to retrieved.
6a0b768e
TO
381 * @param array $values
382 * (reference) to the array which.
6a488035 383 * will have the values for the group
6a0b768e
TO
384 * @param string $orderBy
385 * For orderBy clause.
6a488035 386 *
8d7a9d07 387 * @param bool $isActive Do you want only active option values?
6a488035 388 *
a6c01b45 389 * @return array
16b10e64 390 * Array of option-values
6a488035 391 *
6a488035 392 */
00be9182 393 public static function getValues($groupParams, &$values, $orderBy = 'weight', $isActive = FALSE) {
6a488035
TO
394 if (empty($groupParams)) {
395 return NULL;
396 }
397 $select = "
398SELECT
399 option_value.id as id,
400 option_value.label as label,
401 option_value.value as value,
402 option_value.name as name,
403 option_value.description as description,
404 option_value.weight as weight,
405 option_value.is_active as is_active,
406 option_value.is_default as is_default";
407
408 $from = "
409FROM
410 civicrm_option_value as option_value,
411 civicrm_option_group as option_group ";
412
413 $where = " WHERE option_group.id = option_value.option_group_id ";
414
415 if ($isActive) {
416 $where .= " AND option_value.is_active = " . $isActive;
417 }
418
419 $order = " ORDER BY " . $orderBy;
420
421 $groupId = CRM_Utils_Array::value('id', $groupParams);
422 $groupName = CRM_Utils_Array::value('name', $groupParams);
423
424 if ($groupId) {
425 $where .= " AND option_group.id = %1";
be2fb01f 426 $params[1] = [$groupId, 'Integer'];
6a488035
TO
427 if (!$groupName) {
428 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
429 $groupId, 'name', 'id'
430 );
431 }
432 }
433
434 if ($groupName) {
435 $where .= " AND option_group.name = %2";
be2fb01f 436 $params[2] = [$groupName, 'String'];
6a488035
TO
437 }
438
439 if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
440 $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID();
441 }
442
443 $query = $select . $from . $where . $order;
444
445 $dao = CRM_Core_DAO::executeQuery($query, $params);
446
447 while ($dao->fetch()) {
be2fb01f 448 $values[$dao->id] = [
6a488035
TO
449 'id' => $dao->id,
450 'label' => $dao->label,
451 'value' => $dao->value,
452 'name' => $dao->name,
453 'description' => $dao->description,
454 'weight' => $dao->weight,
455 'is_active' => $dao->is_active,
456 'is_default' => $dao->is_default,
be2fb01f 457 ];
6a488035
TO
458 }
459 }
96025800 460
6a488035 461}