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