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