Merge pull request #23283 from eileenmcnaughton/import_saved_map
[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
9c1bc317 73 $groupName = $groupParams['name'] ?? NULL;
6a488035
TO
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
2bf24361 85 if (CRM_Core_OptionGroup::isDomainOptionGroup($groupName)) {
6a488035
TO
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 */
b05a7478 254 public static function optionExists($value, $daoName, $daoID, $optionGroupID, $fieldName, $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 263 if ($object->find(TRUE)) {
63d76404 264 return $daoID && $object->id == $daoID;
6a488035
TO
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') {
cbc11a37
EM
292 // @todo - remove this - the only code place that calls
293 // this function in a way that would hit this is commented 'remove this'
8f165fa5 294 // This is part of a move towards standardising option values but we
295 // should derive them from the fields array so am deprecating it again...
296 // note that the reason this was needed was that payment_instrument_id was
297 // not set to exportable.
be2fb01f
CW
298 $nameTitle = [
299 'payment_instrument' => [
6a488035 300 'name' => 'payment_instrument',
536f0e02 301 'title' => ts('Payment Method'),
6a488035 302 'headerPattern' => '/^payment|(p(ayment\s)?instrument)$/i',
be2fb01f
CW
303 ],
304 ];
6a488035
TO
305 }
306 elseif ($mode == '') {
307 //the fields email greeting and postal greeting are meant only for Individual and Household
308 //the field addressee is meant for all contact types, CRM-4575
be2fb01f 309 if (in_array($contactType, [
353ffa53
TO
310 'Individual',
311 'Household',
312 'Organization',
8d7a9d07 313 'All',
be2fb01f
CW
314 ])) {
315 $nameTitle = [
316 'addressee' => [
1071730c 317 'name' => 'addressee',
6a488035
TO
318 'title' => ts('Addressee'),
319 'headerPattern' => '/^addressee$/i',
be2fb01f
CW
320 ],
321 ];
322 $title = [
323 'email_greeting' => [
1071730c 324 'name' => 'email_greeting',
6a488035
TO
325 'title' => ts('Email Greeting'),
326 'headerPattern' => '/^email_greeting$/i',
be2fb01f
CW
327 ],
328 'postal_greeting' => [
6a488035
TO
329 'name' => 'postal_greeting',
330 'title' => ts('Postal Greeting'),
331 'headerPattern' => '/^postal_greeting$/i',
be2fb01f
CW
332 ],
333 ];
6a488035
TO
334 $nameTitle = array_merge($nameTitle, $title);
335 }
0c145cc0 336 }
6a488035
TO
337
338 if (is_array($nameTitle)) {
339 foreach ($nameTitle as $name => $attribs) {
340 self::$_fields[$key][$name] = $optionName;
341 list($tableName, $fieldName) = explode('.', $optionName['where']);
6a488035
TO
342 self::$_fields[$key][$name]['where'] = "{$name}.label";
343 foreach ($attribs as $k => $val) {
344 self::$_fields[$key][$name][$k] = $val;
345 }
346 }
347 }
348 }
349
350 return self::$_fields[$key];
351 }
352
353 /**
100fef9d 354 * Build select query in case of option-values
6a488035 355 *
2a6da8d7 356 * @param $query
6a488035 357 */
00be9182 358 public static function select(&$query) {
6a488035
TO
359 if (!empty($query->_params) || !empty($query->_returnProperties)) {
360 $field = self::getFields();
314dbef8 361 foreach ($field as $name => $values) {
a7488080 362 if (!empty($values['pseudoconstant'])) {
314dbef8
DL
363 continue;
364 }
365 list($tableName, $fieldName) = explode('.', $values['where']);
a7488080 366 if (!empty($query->_returnProperties[$name])) {
6a488035
TO
367 $query->_select["{$name}_id"] = "{$name}.value as {$name}_id";
368 $query->_element["{$name}_id"] = 1;
369 $query->_select[$name] = "{$name}.{$fieldName} as $name";
370 $query->_tables[$tableName] = 1;
371 $query->_element[$name] = 1;
372 }
373 }
374 }
375 }
376
377 /**
100fef9d 378 * Return option-values of a particular group
6a488035 379 *
6a0b768e
TO
380 * @param array $groupParams
381 * Array containing group fields.
6a488035 382 * whose option-values is to retrieved.
6a0b768e
TO
383 * @param array $values
384 * (reference) to the array which.
6a488035 385 * will have the values for the group
6a0b768e
TO
386 * @param string $orderBy
387 * For orderBy clause.
6a488035 388 *
8d7a9d07 389 * @param bool $isActive Do you want only active option values?
6a488035 390 *
a6c01b45 391 * @return array
16b10e64 392 * Array of option-values
6a488035 393 *
6a488035 394 */
4f664e9c 395 public static function getValues($groupParams, &$values = [], $orderBy = 'weight', $isActive = FALSE) {
6a488035
TO
396 if (empty($groupParams)) {
397 return NULL;
398 }
399 $select = "
400SELECT
401 option_value.id as id,
402 option_value.label as label,
403 option_value.value as value,
404 option_value.name as name,
405 option_value.description as description,
406 option_value.weight as weight,
407 option_value.is_active as is_active,
4f664e9c
CW
408 option_value.icon as icon,
409 option_value.color as color,
6a488035
TO
410 option_value.is_default as is_default";
411
412 $from = "
413FROM
414 civicrm_option_value as option_value,
415 civicrm_option_group as option_group ";
416
417 $where = " WHERE option_group.id = option_value.option_group_id ";
418
419 if ($isActive) {
420 $where .= " AND option_value.is_active = " . $isActive;
421 }
422
423 $order = " ORDER BY " . $orderBy;
424
9c1bc317
CW
425 $groupId = $groupParams['id'] ?? NULL;
426 $groupName = $groupParams['name'] ?? NULL;
6a488035
TO
427
428 if ($groupId) {
429 $where .= " AND option_group.id = %1";
be2fb01f 430 $params[1] = [$groupId, 'Integer'];
6a488035
TO
431 if (!$groupName) {
432 $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
433 $groupId, 'name', 'id'
434 );
435 }
436 }
437
438 if ($groupName) {
439 $where .= " AND option_group.name = %2";
be2fb01f 440 $params[2] = [$groupName, 'String'];
6a488035
TO
441 }
442
2bf24361 443 if (CRM_Core_OptionGroup::isDomainOptionGroup($groupName)) {
6a488035
TO
444 $where .= " AND option_value.domain_id = " . CRM_Core_Config::domainID();
445 }
446
447 $query = $select . $from . $where . $order;
448
449 $dao = CRM_Core_DAO::executeQuery($query, $params);
450
451 while ($dao->fetch()) {
be2fb01f 452 $values[$dao->id] = [
6a488035
TO
453 'id' => $dao->id,
454 'label' => $dao->label,
455 'value' => $dao->value,
456 'name' => $dao->name,
457 'description' => $dao->description,
458 'weight' => $dao->weight,
459 'is_active' => $dao->is_active,
460 'is_default' => $dao->is_default,
4f664e9c 461 'icon' => $dao->icon,
4727b00a 462 'color' => $dao->color,
be2fb01f 463 ];
6a488035 464 }
4f664e9c 465 return $values;
6a488035 466 }
96025800 467
6a488035 468}