Merge pull request #19036 from civicrm/5.32
[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 {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Fetch object based on array of properties.
28 *
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.
33 *
34 * @return CRM_Core_BAO_OptionGroup
35 */
36 public static function retrieve(&$params, &$defaults) {
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 /**
47 * Update the is_active flag in the db.
48 *
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.
53 *
54 * @return bool
55 * true if we found and updated the object, else false
56 */
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
59 }
60
61 /**
62 * Add the Option Group.
63 *
64 * @param array $params
65 * Reference array contains the values submitted by the form.
66 * @param array $ids
67 * Reference array contains the id.
68 *
69 *
70 * @return object
71 */
72 public static function add(&$params, $ids = []) {
73 if (empty($params['id']) && !empty($ids['optionGroup'])) {
74 CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
75 $params['id'] = $ids['optionGroup'];
76 }
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 }
86 $optionGroup = new CRM_Core_DAO_OptionGroup();
87 $optionGroup->copyValues($params);
88 $optionGroup->save();
89 return $optionGroup;
90 }
91
92 /**
93 * Delete Option Group.
94 *
95 * @param int $optionGroupId
96 * Id of the Option Group to be deleted.
97 */
98 public static function del($optionGroupId) {
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 /**
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 }