Merge pull request #14170 from mlutfy/cart-emails
[civicrm-core.git] / CRM / Core / BAO / OptionGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
36
37 /**
38 * Class constructor.
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Fetch object based on array of properties.
46 *
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.
51 *
52 * @return CRM_Core_BAO_OptionGroup
53 */
54 public static function retrieve(&$params, &$defaults) {
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 /**
65 * Update the is_active flag in the db.
66 *
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.
71 *
72 * @return bool
73 * true if we found and updated the object, else false
74 */
75 public static function setIsActive($id, $is_active) {
76 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
77 }
78
79 /**
80 * Add the Option Group.
81 *
82 * @param array $params
83 * Reference array contains the values submitted by the form.
84 * @param array $ids
85 * Reference array contains the id.
86 *
87 *
88 * @return object
89 */
90 public static function add(&$params, $ids = []) {
91 if (empty($params['id']) && !empty($ids['optionGroup'])) {
92 CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
93 $params['id'] = $ids['optionGroup'];
94 }
95 $optionGroup = new CRM_Core_DAO_OptionGroup();
96 $optionGroup->copyValues($params);;
97 $optionGroup->save();
98 return $optionGroup;
99 }
100
101 /**
102 * Delete Option Group.
103 *
104 * @param int $optionGroupId
105 * Id of the Option Group to be deleted.
106 */
107 public static function del($optionGroupId) {
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 /**
119 * Get title of the option group.
120 *
121 * @param int $optionGroupId
122 * Id of the Option Group.
123 *
124 * @return string
125 * title
126 */
127 public static function getTitle($optionGroupId) {
128 $optionGroup = new CRM_Core_DAO_OptionGroup();
129 $optionGroup->id = $optionGroupId;
130 $optionGroup->find(TRUE);
131 return $optionGroup->name;
132 }
133
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
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) {
164 $existingValues = civicrm_api3('OptionGroup', 'get', [
165 'name' => $params['name'],
166 'return' => 'id',
167 ]);
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
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
212 /**
213 * Set the given values to active, and set all other values to inactive.
214 *
215 * @param string $optionGroupName
216 * e.g "languages"
217 * @param array<string> $activeValues
218 * e.g. array("en_CA","fr_CA")
219 */
220 public static function setActiveValues($optionGroupName, $activeValues) {
221 $params = [
222 1 => [$optionGroupName, 'String'],
223 ];
224
225 // convert activeValues into placeholders / params in the query
226 $placeholders = [];
227 $i = count($params) + 1;
228 foreach ($activeValues as $value) {
229 $placeholders[] = "%{$i}";
230 $params[$i] = [$value, 'String'];
231 $i++;
232 }
233 $placeholders = implode(', ', $placeholders);
234
235 CRM_Core_DAO::executeQuery("
236 UPDATE civicrm_option_value cov
237 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
238 SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
239 WHERE cog.name = %1",
240 $params
241 );
242 }
243
244 }