Merge pull request #17253 from mattwire/utf8convertblocksize
[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 * $Id$
17 *
18 */
19 class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Fetch object based on array of properties.
30 *
31 * @param array $params
32 * (reference ) an assoc array of name/value pairs.
33 * @param array $defaults
34 * (reference ) an assoc array to hold the flattened values.
35 *
36 * @return CRM_Core_BAO_OptionGroup
37 */
38 public static function retrieve(&$params, &$defaults) {
39 $optionGroup = new CRM_Core_DAO_OptionGroup();
40 $optionGroup->copyValues($params);
41 if ($optionGroup->find(TRUE)) {
42 CRM_Core_DAO::storeValues($optionGroup, $defaults);
43 return $optionGroup;
44 }
45 return NULL;
46 }
47
48 /**
49 * Update the is_active flag in the db.
50 *
51 * @param int $id
52 * Id of the database record.
53 * @param bool $is_active
54 * Value we want to set the is_active field.
55 *
56 * @return bool
57 * true if we found and updated the object, else false
58 */
59 public static function setIsActive($id, $is_active) {
60 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
61 }
62
63 /**
64 * Add the Option Group.
65 *
66 * @param array $params
67 * Reference array contains the values submitted by the form.
68 * @param array $ids
69 * Reference array contains the id.
70 *
71 *
72 * @return object
73 */
74 public static function add(&$params, $ids = []) {
75 if (empty($params['id']) && !empty($ids['optionGroup'])) {
76 CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
77 $params['id'] = $ids['optionGroup'];
78 }
79 if (empty($params['name']) && empty($params['id'])) {
80 $params['name'] = CRM_Utils_String::titleToVar(strtolower($params['title']));
81 }
82 elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
83 $params['name'] = CRM_Utils_String::titleToVar(strtolower($params['name']));
84 }
85 elseif (!empty($params['name'])) {
86 $params['name'] = strtolower($params['name']);
87 }
88 $optionGroup = new CRM_Core_DAO_OptionGroup();
89 $optionGroup->copyValues($params);
90 $optionGroup->save();
91 return $optionGroup;
92 }
93
94 /**
95 * Delete Option Group.
96 *
97 * @param int $optionGroupId
98 * Id of the Option Group to be deleted.
99 */
100 public static function del($optionGroupId) {
101 // need to delete all option value field before deleting group
102 $optionValue = new CRM_Core_DAO_OptionValue();
103 $optionValue->option_group_id = $optionGroupId;
104 $optionValue->delete();
105
106 $optionGroup = new CRM_Core_DAO_OptionGroup();
107 $optionGroup->id = $optionGroupId;
108 $optionGroup->delete();
109 }
110
111 /**
112 * Get title of the option group.
113 *
114 * @param int $optionGroupId
115 * Id of the Option Group.
116 *
117 * @return string
118 * title
119 */
120 public static function getTitle($optionGroupId) {
121 $optionGroup = new CRM_Core_DAO_OptionGroup();
122 $optionGroup->id = $optionGroupId;
123 $optionGroup->find(TRUE);
124 return $optionGroup->name;
125 }
126
127 /**
128 * Get DataType for a specified option Group
129 *
130 * @param int $optionGroupId
131 * Id of the Option Group.
132 *
133 * @return string|null
134 * Data Type
135 */
136 public static function getDataType($optionGroupId) {
137 $optionGroup = new CRM_Core_DAO_OptionGroup();
138 $optionGroup->id = $optionGroupId;
139 $optionGroup->find(TRUE);
140 return $optionGroup->data_type;
141 }
142
143 /**
144 * Ensure an option group exists.
145 *
146 * This function is intended to be called from the upgrade script to ensure
147 * that an option group exists, without hitting an error if it already exists.
148 *
149 * This is sympathetic to sites who might pre-add it.
150 *
151 * @param array $params
152 *
153 * @return int
154 * ID of the option group.
155 */
156 public static function ensureOptionGroupExists($params) {
157 $existingValues = civicrm_api3('OptionGroup', 'get', [
158 'name' => $params['name'],
159 'return' => 'id',
160 ]);
161 if (!$existingValues['count']) {
162 $result = civicrm_api3('OptionGroup', 'create', $params);
163 return $result['id'];
164 }
165 else {
166 return $existingValues['id'];
167 }
168 }
169
170 /**
171 * Get the title of an option group by name.
172 *
173 * @param string $name
174 * The name value for the option group table.
175 *
176 * @return string
177 * The relevant title.
178 */
179 public static function getTitleByName($name) {
180 $groups = self::getTitlesByNames();
181 return $groups[$name];
182 }
183
184 /**
185 * Get a cached mapping of all group titles indexed by their unique name.
186 *
187 * We tend to only have a limited number of option groups so memory caching
188 * makes more sense than multiple look-ups.
189 *
190 * @return array
191 * Array of all group titles by name.
192 * e.g
193 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
194 */
195 public static function getTitlesByNames() {
196 if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['titles_by_name'])) {
197 $dao = CRM_Core_DAO::executeQuery("SELECT name, title FROM civicrm_option_group");
198 while ($dao->fetch()) {
199 \Civi::$statics[__CLASS__]['titles_by_name'][$dao->name] = $dao->title;
200 }
201 }
202 return \Civi::$statics[__CLASS__]['titles_by_name'];
203 }
204
205 /**
206 * Set the given values to active, and set all other values to inactive.
207 *
208 * @param string $optionGroupName
209 * e.g "languages"
210 * @param string[] $activeValues
211 * e.g. array("en_CA","fr_CA")
212 */
213 public static function setActiveValues($optionGroupName, $activeValues) {
214 $params = [
215 1 => [$optionGroupName, 'String'],
216 ];
217
218 // convert activeValues into placeholders / params in the query
219 $placeholders = [];
220 $i = count($params) + 1;
221 foreach ($activeValues as $value) {
222 $placeholders[] = "%{$i}";
223 $params[$i] = [$value, 'String'];
224 $i++;
225 }
226 $placeholders = implode(', ', $placeholders);
227
228 CRM_Core_DAO::executeQuery("
229 UPDATE civicrm_option_value cov
230 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
231 SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
232 WHERE cog.name = %1",
233 $params
234 );
235 }
236
237 }