Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Class constructor. |
6a488035 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
fe482240 | 45 | * Fetch object based on array of properties. |
6a488035 | 46 | * |
6a0b768e TO |
47 | * @param array $params |
48 | * (reference ) an assoc array of name/value pairs. | |
49 | * @param array $defaults | |
50 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 51 | * |
16b10e64 | 52 | * @return CRM_Core_BAO_OptionGroup |
6a488035 | 53 | */ |
00be9182 | 54 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
55 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
56 | $optionGroup->copyValues($params); | |
57 | if ($optionGroup->find(TRUE)) { | |
58 | CRM_Core_DAO::storeValues($optionGroup, $defaults); | |
59 | return $optionGroup; | |
60 | } | |
61 | return NULL; | |
62 | } | |
63 | ||
64 | /** | |
fe482240 | 65 | * Update the is_active flag in the db. |
6a488035 | 66 | * |
6a0b768e TO |
67 | * @param int $id |
68 | * Id of the database record. | |
69 | * @param bool $is_active | |
70 | * Value we want to set the is_active field. | |
6a488035 | 71 | * |
a6c01b45 | 72 | * @return Object |
b44e3f84 | 73 | * DAO object on success, null otherwise |
6a488035 | 74 | */ |
00be9182 | 75 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
76 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active); |
77 | } | |
78 | ||
79 | /** | |
fe482240 | 80 | * Add the Option Group. |
6a488035 | 81 | * |
6a0b768e TO |
82 | * @param array $params |
83 | * Reference array contains the values submitted by the form. | |
84 | * @param array $ids | |
85 | * Reference array contains the id. | |
6a488035 | 86 | * |
6a488035 TO |
87 | * |
88 | * @return object | |
89 | */ | |
00be9182 | 90 | public static function add(&$params, $ids = array()) { |
9b873358 | 91 | if (empty($params['id'])) { |
4033343a | 92 | $params['id'] = CRM_Utils_Array::value('optionGroup', $ids); |
93 | } | |
94 | ||
6a488035 | 95 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
6a488035 TO |
96 | |
97 | // action is taken depending upon the mode | |
98 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
99 | $optionGroup->copyValues($params);; | |
100 | ||
6a488035 TO |
101 | $optionGroup->save(); |
102 | return $optionGroup; | |
103 | } | |
104 | ||
105 | /** | |
fe482240 | 106 | * Delete Option Group. |
6a488035 | 107 | * |
6a0b768e TO |
108 | * @param int $optionGroupId |
109 | * Id of the Option Group to be deleted. | |
6a488035 | 110 | */ |
00be9182 | 111 | public static function del($optionGroupId) { |
6a488035 TO |
112 | // need to delete all option value field before deleting group |
113 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
114 | $optionValue->option_group_id = $optionGroupId; | |
115 | $optionValue->delete(); | |
116 | ||
117 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
118 | $optionGroup->id = $optionGroupId; | |
119 | $optionGroup->delete(); | |
120 | } | |
121 | ||
122 | /** | |
fe482240 | 123 | * Get title of the option group. |
6a488035 | 124 | * |
6a0b768e TO |
125 | * @param int $optionGroupId |
126 | * Id of the Option Group. | |
6a488035 | 127 | * |
ae5ffbb7 | 128 | * @return string |
a6c01b45 | 129 | * title |
6a488035 | 130 | */ |
00be9182 | 131 | public static function getTitle($optionGroupId) { |
6a488035 TO |
132 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
133 | $optionGroup->id = $optionGroupId; | |
134 | $optionGroup->find(TRUE); | |
135 | return $optionGroup->name; | |
136 | } | |
96025800 | 137 | |
eaecfa20 SL |
138 | /** |
139 | * Get DataType for a specified option Group | |
140 | * | |
141 | * @param int $optionGroupId | |
142 | * Id of the Option Group. | |
143 | * | |
144 | * @return string|null | |
145 | * Data Type | |
146 | */ | |
147 | public static function getDataType($optionGroupId) { | |
148 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
149 | $optionGroup->id = $optionGroupId; | |
150 | $optionGroup->find(TRUE); | |
151 | return $optionGroup->data_type; | |
152 | } | |
153 | ||
801bafd7 | 154 | /** |
155 | * Ensure an option group exists. | |
156 | * | |
157 | * This function is intended to be called from the upgrade script to ensure | |
158 | * that an option group exists, without hitting an error if it already exists. | |
159 | * | |
160 | * This is sympathetic to sites who might pre-add it. | |
161 | * | |
162 | * @param array $params | |
163 | * | |
164 | * @return int | |
165 | * ID of the option group. | |
166 | */ | |
167 | public static function ensureOptionGroupExists($params) { | |
168 | $existingValues = civicrm_api3('OptionGroup', 'get', array( | |
169 | 'name' => $params['name'], | |
34273b2a | 170 | 'return' => 'id', |
801bafd7 | 171 | )); |
172 | if (!$existingValues['count']) { | |
173 | $result = civicrm_api3('OptionGroup', 'create', $params); | |
174 | return $result['id']; | |
175 | } | |
176 | else { | |
177 | return $existingValues['id']; | |
178 | } | |
179 | } | |
180 | ||
9d5c7f14 | 181 | /** |
182 | * Get the title of an option group by name. | |
183 | * | |
184 | * @param string $name | |
185 | * The name value for the option group table. | |
186 | * | |
187 | * @return string | |
188 | * The relevant title. | |
189 | */ | |
190 | public static function getTitleByName($name) { | |
191 | $groups = self::getTitlesByNames(); | |
192 | return $groups[$name]; | |
193 | } | |
194 | ||
195 | /** | |
196 | * Get a cached mapping of all group titles indexed by their unique name. | |
197 | * | |
198 | * We tend to only have a limited number of option groups so memory caching | |
199 | * makes more sense than multiple look-ups. | |
200 | * | |
201 | * @return array | |
202 | * Array of all group titles by name. | |
203 | * e.g | |
204 | * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....) | |
205 | */ | |
206 | public static function getTitlesByNames() { | |
207 | if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['titles_by_name'])) { | |
208 | $dao = CRM_Core_DAO::executeQuery("SELECT name, title FROM civicrm_option_group"); | |
209 | while ($dao->fetch()) { | |
210 | \Civi::$statics[__CLASS__]['titles_by_name'][$dao->name] = $dao->title; | |
211 | } | |
212 | } | |
213 | return \Civi::$statics[__CLASS__]['titles_by_name']; | |
214 | } | |
215 | ||
ece6501c ML |
216 | /** |
217 | * Set the given values to active, and set all other values to inactive. | |
218 | * | |
219 | * @param string $optionGroupName | |
220 | * e.g "languages" | |
221 | * @param array<string> $activeValues | |
222 | * e.g. array("en_CA","fr_CA") | |
223 | */ | |
224 | public static function setActiveValues($optionGroupName, $activeValues) { | |
225 | $params = array( | |
226 | 1 => array($optionGroupName, 'String'), | |
227 | ); | |
228 | ||
229 | // convert activeValues into placeholders / params in the query | |
230 | $placeholders = array(); | |
231 | $i = count($params) + 1; | |
232 | foreach ($activeValues as $value) { | |
233 | $placeholders[] = "%{$i}"; | |
234 | $params[$i] = array($value, 'String'); | |
235 | $i++; | |
236 | } | |
237 | $placeholders = implode(', ', $placeholders); | |
238 | ||
239 | CRM_Core_DAO::executeQuery(" | |
240 | UPDATE civicrm_option_value cov | |
241 | LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id | |
242 | SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END | |
243 | WHERE cog.name = %1", | |
244 | $params | |
245 | ); | |
246 | } | |
247 | ||
6a488035 | 248 | } |