Merge pull request #20251 from larssandergreen/change-registration-button-text
[civicrm-core.git] / CRM / Core / BAO / OptionGroup.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 16 */
7b30c1dd 17class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup implements \Civi\Test\HookInterface {
6a488035 18
6a488035 19 /**
fe482240 20 * Fetch object based on array of properties.
6a488035 21 *
6a0b768e
TO
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.
6a488035 26 *
16b10e64 27 * @return CRM_Core_BAO_OptionGroup
6a488035 28 */
00be9182 29 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
fe482240 40 * Update the is_active flag in the db.
6a488035 41 *
6a0b768e
TO
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.
6a488035 46 *
8a4fede3 47 * @return bool
48 * true if we found and updated the object, else false
6a488035 49 */
00be9182 50 public static function setIsActive($id, $is_active) {
6a488035
TO
51 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
52 }
53
54 /**
fe482240 55 * Add the Option Group.
6a488035 56 *
6a0b768e
TO
57 * @param array $params
58 * Reference array contains the values submitted by the form.
59 * @param array $ids
60 * Reference array contains the id.
6a488035 61 *
6a488035
TO
62 *
63 * @return object
64 */
be2fb01f 65 public static function add(&$params, $ids = []) {
25a8e8c2 66 if (empty($params['id']) && !empty($ids['optionGroup'])) {
67 CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
68 $params['id'] = $ids['optionGroup'];
4033343a 69 }
6ebc7a89
SL
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 }
6a488035 79 $optionGroup = new CRM_Core_DAO_OptionGroup();
fe7f4414 80 $optionGroup->copyValues($params);
6a488035
TO
81 $optionGroup->save();
82 return $optionGroup;
83 }
84
85 /**
fe482240 86 * Delete Option Group.
6a488035 87 *
7b30c1dd 88 * @deprecated
6a0b768e 89 * @param int $optionGroupId
6a488035 90 */
00be9182 91 public static function del($optionGroupId) {
7b30c1dd
CW
92 static::deleteRecord(['id' => $optionGroupId]);
93 }
6a488035 94
7b30c1dd
CW
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 }
6a488035
TO
107 }
108
109 /**
fe482240 110 * Get title of the option group.
6a488035 111 *
6a0b768e
TO
112 * @param int $optionGroupId
113 * Id of the Option Group.
6a488035 114 *
ae5ffbb7 115 * @return string
a6c01b45 116 * title
6a488035 117 */
00be9182 118 public static function getTitle($optionGroupId) {
6a488035
TO
119 $optionGroup = new CRM_Core_DAO_OptionGroup();
120 $optionGroup->id = $optionGroupId;
121 $optionGroup->find(TRUE);
122 return $optionGroup->name;
123 }
96025800 124
eaecfa20
SL
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
801bafd7 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) {
be2fb01f 155 $existingValues = civicrm_api3('OptionGroup', 'get', [
801bafd7 156 'name' => $params['name'],
34273b2a 157 'return' => 'id',
be2fb01f 158 ]);
801bafd7 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
9d5c7f14 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
ece6501c
ML
203 /**
204 * Set the given values to active, and set all other values to inactive.
205 *
206 * @param string $optionGroupName
207 * e.g "languages"
041ecc95 208 * @param string[] $activeValues
ece6501c
ML
209 * e.g. array("en_CA","fr_CA")
210 */
211 public static function setActiveValues($optionGroupName, $activeValues) {
be2fb01f
CW
212 $params = [
213 1 => [$optionGroupName, 'String'],
214 ];
ece6501c
ML
215
216 // convert activeValues into placeholders / params in the query
be2fb01f 217 $placeholders = [];
ece6501c
ML
218 $i = count($params) + 1;
219 foreach ($activeValues as $value) {
220 $placeholders[] = "%{$i}";
be2fb01f 221 $params[$i] = [$value, 'String'];
ece6501c
ML
222 $i++;
223 }
224 $placeholders = implode(', ', $placeholders);
225
226 CRM_Core_DAO::executeQuery("
227UPDATE civicrm_option_value cov
228 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
229SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
230WHERE cog.name = %1",
231 $params
232 );
233 }
234
6a488035 235}