/
OptionGroup.php
218 lines (203 loc) · 6.32 KB
/
OptionGroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup implements \Civi\Core\HookInterface {
/**
* @deprecated
* @param array $params
* @param array $defaults
* @return self|null
*/
public static function retrieve($params, &$defaults) {
return self::commonRetrieve(self::class, $params, $defaults);
}
/**
* @deprecated - this bypasses hooks.
* @param int $id
* @param bool $is_active
* @return bool
*/
public static function setIsActive($id, $is_active) {
CRM_Core_Error::deprecatedFunctionWarning('writeRecord');
return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
}
/**
* Add the Option Group.
*
* @param array $params
*
* @deprecated
* @return CRM_Core_DAO_OptionGroup
*/
public static function add($params) {
// This is very similar to CRM_Core_DAO::makeNameFromLabel which would be
// called automatically via `self::writeRecord()`
// TODO: Check if the differences matter, then deprecate this function and switch to writeRecord.
if (empty($params['name']) && empty($params['id'])) {
$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['title']));
}
elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['name']));
}
elseif (!empty($params['name'])) {
$params['name'] = strtolower($params['name']);
}
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->copyValues($params);
$optionGroup->save();
return $optionGroup;
}
/**
* Delete Option Group.
*
* @deprecated
* @param int $optionGroupId
*/
public static function del($optionGroupId) {
CRM_Core_Error::deprecatedFunctionWarning('deleteRecord');
static::deleteRecord(['id' => $optionGroupId]);
}
/**
* Callback for hook_civicrm_pre().
* @param \Civi\Core\Event\PreEvent $event
* @throws CRM_Core_Exception
*/
public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
if ($event->action === 'delete') {
// need to delete all option value field before deleting group
\Civi\Api4\OptionValue::delete(FALSE)
->addWhere('option_group_id', '=', $event->id)
->execute();
}
}
/**
* Get title of the option group.
*
* @param int $optionGroupId
* Id of the Option Group.
*
* @return string
* title
*/
public static function getTitle($optionGroupId) {
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->id = $optionGroupId;
$optionGroup->find(TRUE);
return $optionGroup->name;
}
/**
* Get DataType for a specified option Group
*
* @param int $optionGroupId
* Id of the Option Group.
*
* @return string|null
* Data Type
*/
public static function getDataType($optionGroupId) {
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->id = $optionGroupId;
$optionGroup->find(TRUE);
return $optionGroup->data_type;
}
/**
* Ensure an option group exists.
*
* This function is intended to be called from the upgrade script to ensure
* that an option group exists, without hitting an error if it already exists.
*
* This is sympathetic to sites who might pre-add it.
*
* @param array $params
*
* @return int
* ID of the option group.
*/
public static function ensureOptionGroupExists($params) {
$existingValues = civicrm_api3('OptionGroup', 'get', [
'name' => $params['name'],
'return' => 'id',
]);
if (!$existingValues['count']) {
$result = civicrm_api3('OptionGroup', 'create', $params);
return $result['id'];
}
else {
return $existingValues['id'];
}
}
/**
* Get the title of an option group by name.
*
* @param string $name
* The name value for the option group table.
*
* @return string
* The relevant title.
*/
public static function getTitleByName($name) {
$groups = self::getTitlesByNames();
return $groups[$name];
}
/**
* Get a cached mapping of all group titles indexed by their unique name.
*
* We tend to only have a limited number of option groups so memory caching
* makes more sense than multiple look-ups.
*
* @return array
* Array of all group titles by name.
* e.g
* array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
*/
public static function getTitlesByNames() {
if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['titles_by_name'])) {
$dao = CRM_Core_DAO::executeQuery("SELECT name, title FROM civicrm_option_group");
while ($dao->fetch()) {
\Civi::$statics[__CLASS__]['titles_by_name'][$dao->name] = $dao->title;
}
}
return \Civi::$statics[__CLASS__]['titles_by_name'];
}
/**
* Set the given values to active, and set all other values to inactive.
*
* @param string $optionGroupName
* e.g "languages"
* @param string[] $activeValues
* e.g. array("en_CA","fr_CA")
*/
public static function setActiveValues($optionGroupName, $activeValues) {
$params = [
1 => [$optionGroupName, 'String'],
];
// convert activeValues into placeholders / params in the query
$placeholders = [];
$i = count($params) + 1;
foreach ($activeValues as $value) {
$placeholders[] = "%{$i}";
$params[$i] = [$value, 'String'];
$i++;
}
$placeholders = implode(', ', $placeholders);
CRM_Core_DAO::executeQuery("
UPDATE civicrm_option_value cov
LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
WHERE cog.name = %1",
$params
);
}
}