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