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 TO |
16 | */ |
17 | class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup { | |
18 | ||
19 | /** | |
fe482240 | 20 | * Class constructor. |
6a488035 | 21 | */ |
00be9182 | 22 | public function __construct() { |
6a488035 TO |
23 | parent::__construct(); |
24 | } | |
25 | ||
26 | /** | |
fe482240 | 27 | * Fetch object based on array of properties. |
6a488035 | 28 | * |
6a0b768e TO |
29 | * @param array $params |
30 | * (reference ) an assoc array of name/value pairs. | |
31 | * @param array $defaults | |
32 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 33 | * |
16b10e64 | 34 | * @return CRM_Core_BAO_OptionGroup |
6a488035 | 35 | */ |
00be9182 | 36 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
37 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
38 | $optionGroup->copyValues($params); | |
39 | if ($optionGroup->find(TRUE)) { | |
40 | CRM_Core_DAO::storeValues($optionGroup, $defaults); | |
41 | return $optionGroup; | |
42 | } | |
43 | return NULL; | |
44 | } | |
45 | ||
46 | /** | |
fe482240 | 47 | * Update the is_active flag in the db. |
6a488035 | 48 | * |
6a0b768e TO |
49 | * @param int $id |
50 | * Id of the database record. | |
51 | * @param bool $is_active | |
52 | * Value we want to set the is_active field. | |
6a488035 | 53 | * |
8a4fede3 | 54 | * @return bool |
55 | * true if we found and updated the object, else false | |
6a488035 | 56 | */ |
00be9182 | 57 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
58 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active); |
59 | } | |
60 | ||
61 | /** | |
fe482240 | 62 | * Add the Option Group. |
6a488035 | 63 | * |
6a0b768e TO |
64 | * @param array $params |
65 | * Reference array contains the values submitted by the form. | |
66 | * @param array $ids | |
67 | * Reference array contains the id. | |
6a488035 | 68 | * |
6a488035 TO |
69 | * |
70 | * @return object | |
71 | */ | |
be2fb01f | 72 | public static function add(&$params, $ids = []) { |
25a8e8c2 | 73 | if (empty($params['id']) && !empty($ids['optionGroup'])) { |
74 | CRM_Core_Error::deprecatedFunctionWarning('no $ids array'); | |
75 | $params['id'] = $ids['optionGroup']; | |
4033343a | 76 | } |
6ebc7a89 SL |
77 | if (empty($params['name']) && empty($params['id'])) { |
78 | $params['name'] = CRM_Utils_String::titleToVar(strtolower($params['title'])); | |
79 | } | |
80 | elseif (!empty($params['name']) && strpos($params['name'], ' ')) { | |
81 | $params['name'] = CRM_Utils_String::titleToVar(strtolower($params['name'])); | |
82 | } | |
83 | elseif (!empty($params['name'])) { | |
84 | $params['name'] = strtolower($params['name']); | |
85 | } | |
6a488035 | 86 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
fe7f4414 | 87 | $optionGroup->copyValues($params); |
6a488035 TO |
88 | $optionGroup->save(); |
89 | return $optionGroup; | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Delete Option Group. |
6a488035 | 94 | * |
6a0b768e TO |
95 | * @param int $optionGroupId |
96 | * Id of the Option Group to be deleted. | |
6a488035 | 97 | */ |
00be9182 | 98 | public static function del($optionGroupId) { |
6a488035 TO |
99 | // need to delete all option value field before deleting group |
100 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
101 | $optionValue->option_group_id = $optionGroupId; | |
102 | $optionValue->delete(); | |
103 | ||
104 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
105 | $optionGroup->id = $optionGroupId; | |
106 | $optionGroup->delete(); | |
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(" | |
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 | ||
6a488035 | 235 | } |