3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 class CRM_Core_BAO_OptionGroup
extends CRM_Core_DAO_OptionGroup
{
40 public function __construct() {
41 parent
::__construct();
45 * Fetch object based on array of properties.
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.
52 * @return CRM_Core_BAO_OptionGroup
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);
65 * Update the is_active flag in the db.
68 * Id of the database record.
69 * @param bool $is_active
70 * Value we want to set the is_active field.
73 * DAO object on success, null otherwise
75 public static function setIsActive($id, $is_active) {
76 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
80 * Add the Option Group.
82 * @param array $params
83 * Reference array contains the values submitted by the form.
85 * Reference array contains the id.
90 public static function add(&$params, $ids = array()) {
91 if (empty($params['id'])) {
92 $params['id'] = CRM_Utils_Array
::value('optionGroup', $ids);
95 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
96 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
98 // action is taken depending upon the mode
99 $optionGroup = new CRM_Core_DAO_OptionGroup();
100 $optionGroup->copyValues($params);;
102 if ($params['is_default']) {
103 $query = "UPDATE civicrm_option_group SET is_default = 0";
104 CRM_Core_DAO
::executeQuery($query);
107 $optionGroup->save();
112 * Delete Option Group.
114 * @param int $optionGroupId
115 * Id of the Option Group to be deleted.
117 public static function del($optionGroupId) {
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();
123 $optionGroup = new CRM_Core_DAO_OptionGroup();
124 $optionGroup->id
= $optionGroupId;
125 $optionGroup->delete();
129 * Get title of the option group.
131 * @param int $optionGroupId
132 * Id of the Option Group.
137 public static function getTitle($optionGroupId) {
138 $optionGroup = new CRM_Core_DAO_OptionGroup();
139 $optionGroup->id
= $optionGroupId;
140 $optionGroup->find(TRUE);
141 return $optionGroup->name
;
145 * Get DataType for a specified option Group
147 * @param int $optionGroupId
148 * Id of the Option Group.
150 * @return string|null
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
;
161 * Ensure an option group exists.
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.
166 * This is sympathetic to sites who might pre-add it.
168 * @param array $params
171 * ID of the option group.
173 public static function ensureOptionGroupExists($params) {
174 $existingValues = civicrm_api3('OptionGroup', 'get', array(
175 'name' => $params['name'],
178 if (!$existingValues['count']) {
179 $result = civicrm_api3('OptionGroup', 'create', $params);
180 return $result['id'];
183 return $existingValues['id'];
188 * Get the title of an option group by name.
190 * @param string $name
191 * The name value for the option group table.
194 * The relevant title.
196 public static function getTitleByName($name) {
197 $groups = self
::getTitlesByNames();
198 return $groups[$name];
202 * Get a cached mapping of all group titles indexed by their unique name.
204 * We tend to only have a limited number of option groups so memory caching
205 * makes more sense than multiple look-ups.
208 * Array of all group titles by name.
210 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
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
;
219 return \Civi
::$statics[__CLASS__
]['titles_by_name'];
223 * Set the given values to active, and set all other values to inactive.
225 * @param string $optionGroupName
227 * @param array<string> $activeValues
228 * e.g. array("en_CA","fr_CA")
230 public static function setActiveValues($optionGroupName, $activeValues) {
232 1 => array($optionGroupName, 'String'),
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');
243 $placeholders = implode(', ', $placeholders);
245 CRM_Core_DAO
::executeQuery("
246 UPDATE civicrm_option_value cov
247 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
248 SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
249 WHERE cog.name = %1",