Merge pull request #14806 from eileenmcnaughton/ex
[civicrm-core.git] / CRM / Core / BAO / OptionGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35class 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 *
8a4fede3 72 * @return bool
73 * true if we found and updated the object, else false
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 */
be2fb01f 90 public static function add(&$params, $ids = []) {
25a8e8c2 91 if (empty($params['id']) && !empty($ids['optionGroup'])) {
92 CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
93 $params['id'] = $ids['optionGroup'];
4033343a 94 }
6a488035
TO
95 $optionGroup = new CRM_Core_DAO_OptionGroup();
96 $optionGroup->copyValues($params);;
6a488035
TO
97 $optionGroup->save();
98 return $optionGroup;
99 }
100
101 /**
fe482240 102 * Delete Option Group.
6a488035 103 *
6a0b768e
TO
104 * @param int $optionGroupId
105 * Id of the Option Group to be deleted.
6a488035 106 */
00be9182 107 public static function del($optionGroupId) {
6a488035
TO
108 // need to delete all option value field before deleting group
109 $optionValue = new CRM_Core_DAO_OptionValue();
110 $optionValue->option_group_id = $optionGroupId;
111 $optionValue->delete();
112
113 $optionGroup = new CRM_Core_DAO_OptionGroup();
114 $optionGroup->id = $optionGroupId;
115 $optionGroup->delete();
116 }
117
118 /**
fe482240 119 * Get title of the option group.
6a488035 120 *
6a0b768e
TO
121 * @param int $optionGroupId
122 * Id of the Option Group.
6a488035 123 *
ae5ffbb7 124 * @return string
a6c01b45 125 * title
6a488035 126 */
00be9182 127 public static function getTitle($optionGroupId) {
6a488035
TO
128 $optionGroup = new CRM_Core_DAO_OptionGroup();
129 $optionGroup->id = $optionGroupId;
130 $optionGroup->find(TRUE);
131 return $optionGroup->name;
132 }
96025800 133
eaecfa20
SL
134 /**
135 * Get DataType for a specified option Group
136 *
137 * @param int $optionGroupId
138 * Id of the Option Group.
139 *
140 * @return string|null
141 * Data Type
142 */
143 public static function getDataType($optionGroupId) {
144 $optionGroup = new CRM_Core_DAO_OptionGroup();
145 $optionGroup->id = $optionGroupId;
146 $optionGroup->find(TRUE);
147 return $optionGroup->data_type;
148 }
149
801bafd7 150 /**
151 * Ensure an option group exists.
152 *
153 * This function is intended to be called from the upgrade script to ensure
154 * that an option group exists, without hitting an error if it already exists.
155 *
156 * This is sympathetic to sites who might pre-add it.
157 *
158 * @param array $params
159 *
160 * @return int
161 * ID of the option group.
162 */
163 public static function ensureOptionGroupExists($params) {
be2fb01f 164 $existingValues = civicrm_api3('OptionGroup', 'get', [
801bafd7 165 'name' => $params['name'],
34273b2a 166 'return' => 'id',
be2fb01f 167 ]);
801bafd7 168 if (!$existingValues['count']) {
169 $result = civicrm_api3('OptionGroup', 'create', $params);
170 return $result['id'];
171 }
172 else {
173 return $existingValues['id'];
174 }
175 }
176
9d5c7f14 177 /**
178 * Get the title of an option group by name.
179 *
180 * @param string $name
181 * The name value for the option group table.
182 *
183 * @return string
184 * The relevant title.
185 */
186 public static function getTitleByName($name) {
187 $groups = self::getTitlesByNames();
188 return $groups[$name];
189 }
190
191 /**
192 * Get a cached mapping of all group titles indexed by their unique name.
193 *
194 * We tend to only have a limited number of option groups so memory caching
195 * makes more sense than multiple look-ups.
196 *
197 * @return array
198 * Array of all group titles by name.
199 * e.g
200 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
201 */
202 public static function getTitlesByNames() {
203 if (!isset(\Civi::$statics[__CLASS__]) || !isset(\Civi::$statics[__CLASS__]['titles_by_name'])) {
204 $dao = CRM_Core_DAO::executeQuery("SELECT name, title FROM civicrm_option_group");
205 while ($dao->fetch()) {
206 \Civi::$statics[__CLASS__]['titles_by_name'][$dao->name] = $dao->title;
207 }
208 }
209 return \Civi::$statics[__CLASS__]['titles_by_name'];
210 }
211
ece6501c
ML
212 /**
213 * Set the given values to active, and set all other values to inactive.
214 *
215 * @param string $optionGroupName
216 * e.g "languages"
041ecc95 217 * @param string[] $activeValues
ece6501c
ML
218 * e.g. array("en_CA","fr_CA")
219 */
220 public static function setActiveValues($optionGroupName, $activeValues) {
be2fb01f
CW
221 $params = [
222 1 => [$optionGroupName, 'String'],
223 ];
ece6501c
ML
224
225 // convert activeValues into placeholders / params in the query
be2fb01f 226 $placeholders = [];
ece6501c
ML
227 $i = count($params) + 1;
228 foreach ($activeValues as $value) {
229 $placeholders[] = "%{$i}";
be2fb01f 230 $params[$i] = [$value, 'String'];
ece6501c
ML
231 $i++;
232 }
233 $placeholders = implode(', ', $placeholders);
234
235 CRM_Core_DAO::executeQuery("
236UPDATE civicrm_option_value cov
237 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
238SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
239WHERE cog.name = %1",
240 $params
241 );
242 }
243
6a488035 244}