Commit | Line | Data |
---|---|---|
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 | 17 | class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup implements \Civi\Test\HookInterface { |
6a488035 | 18 | |
6a488035 | 19 | /** |
4f940304 | 20 | * Retrieve DB object and copy to defaults array. |
6a488035 | 21 | * |
6a0b768e | 22 | * @param array $params |
4f940304 | 23 | * Array of criteria values. |
6a0b768e | 24 | * @param array $defaults |
4f940304 | 25 | * Array to be populated with found values. |
6a488035 | 26 | * |
4f940304 CW |
27 | * @return self|null |
28 | * The DAO object, if found. | |
29 | * | |
30 | * @deprecated | |
6a488035 | 31 | */ |
4f940304 CW |
32 | public static function retrieve($params, &$defaults) { |
33 | return self::commonRetrieve(self::class, $params, $defaults); | |
6a488035 TO |
34 | } |
35 | ||
36 | /** | |
fe482240 | 37 | * Update the is_active flag in the db. |
6a488035 | 38 | * |
6a0b768e TO |
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. | |
6a488035 | 43 | * |
8a4fede3 | 44 | * @return bool |
45 | * true if we found and updated the object, else false | |
6a488035 | 46 | */ |
00be9182 | 47 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
48 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active); |
49 | } | |
50 | ||
51 | /** | |
fe482240 | 52 | * Add the Option Group. |
6a488035 | 53 | * |
6a0b768e TO |
54 | * @param array $params |
55 | * Reference array contains the values submitted by the form. | |
56 | * @param array $ids | |
57 | * Reference array contains the id. | |
6a488035 | 58 | * |
ddba05d3 CW |
59 | * @deprecated |
60 | * @return CRM_Core_DAO_OptionGroup | |
6a488035 | 61 | */ |
be2fb01f | 62 | public static function add(&$params, $ids = []) { |
25a8e8c2 | 63 | if (empty($params['id']) && !empty($ids['optionGroup'])) { |
64 | CRM_Core_Error::deprecatedFunctionWarning('no $ids array'); | |
65 | $params['id'] = $ids['optionGroup']; | |
4033343a | 66 | } |
6ebc7a89 SL |
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 | } | |
6a488035 | 76 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
fe7f4414 | 77 | $optionGroup->copyValues($params); |
6a488035 TO |
78 | $optionGroup->save(); |
79 | return $optionGroup; | |
80 | } | |
81 | ||
82 | /** | |
fe482240 | 83 | * Delete Option Group. |
6a488035 | 84 | * |
7b30c1dd | 85 | * @deprecated |
6a0b768e | 86 | * @param int $optionGroupId |
6a488035 | 87 | */ |
00be9182 | 88 | public static function del($optionGroupId) { |
7b30c1dd CW |
89 | static::deleteRecord(['id' => $optionGroupId]); |
90 | } | |
6a488035 | 91 | |
7b30c1dd CW |
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 | } | |
6a488035 TO |
104 | } |
105 | ||
106 | /** | |
fe482240 | 107 | * Get title of the option group. |
6a488035 | 108 | * |
6a0b768e TO |
109 | * @param int $optionGroupId |
110 | * Id of the Option Group. | |
6a488035 | 111 | * |
ae5ffbb7 | 112 | * @return string |
a6c01b45 | 113 | * title |
6a488035 | 114 | */ |
00be9182 | 115 | public static function getTitle($optionGroupId) { |
6a488035 TO |
116 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
117 | $optionGroup->id = $optionGroupId; | |
118 | $optionGroup->find(TRUE); | |
119 | return $optionGroup->name; | |
120 | } | |
96025800 | 121 | |
eaecfa20 SL |
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 | ||
801bafd7 | 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) { | |
be2fb01f | 152 | $existingValues = civicrm_api3('OptionGroup', 'get', [ |
801bafd7 | 153 | 'name' => $params['name'], |
34273b2a | 154 | 'return' => 'id', |
be2fb01f | 155 | ]); |
801bafd7 | 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 | ||
9d5c7f14 | 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 | ||
ece6501c ML |
200 | /** |
201 | * Set the given values to active, and set all other values to inactive. | |
202 | * | |
203 | * @param string $optionGroupName | |
204 | * e.g "languages" | |
041ecc95 | 205 | * @param string[] $activeValues |
ece6501c ML |
206 | * e.g. array("en_CA","fr_CA") |
207 | */ | |
208 | public static function setActiveValues($optionGroupName, $activeValues) { | |
be2fb01f CW |
209 | $params = [ |
210 | 1 => [$optionGroupName, 'String'], | |
211 | ]; | |
ece6501c ML |
212 | |
213 | // convert activeValues into placeholders / params in the query | |
be2fb01f | 214 | $placeholders = []; |
ece6501c ML |
215 | $i = count($params) + 1; |
216 | foreach ($activeValues as $value) { | |
217 | $placeholders[] = "%{$i}"; | |
be2fb01f | 218 | $params[$i] = [$value, 'String']; |
ece6501c ML |
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 | ||
6a488035 | 232 | } |