4c1ccc9dfb9a856dd54afd4bbd7b435a108224f6
3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 * true if we found and updated the object, else false
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']) && !empty($ids['optionGroup'])) {
92 CRM_Core_Error
::deprecatedFunctionWarning('no $ids array');
93 $params['id'] = $ids['optionGroup'];
95 $optionGroup = new CRM_Core_DAO_OptionGroup();
96 $optionGroup->copyValues($params);;
102 * Delete Option Group.
104 * @param int $optionGroupId
105 * Id of the Option Group to be deleted.
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();
113 $optionGroup = new CRM_Core_DAO_OptionGroup();
114 $optionGroup->id
= $optionGroupId;
115 $optionGroup->delete();
119 * Get title of the option group.
121 * @param int $optionGroupId
122 * Id of the Option Group.
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
;
135 * Get DataType for a specified option Group
137 * @param int $optionGroupId
138 * Id of the Option Group.
140 * @return string|null
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
;
151 * Ensure an option group exists.
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.
156 * This is sympathetic to sites who might pre-add it.
158 * @param array $params
161 * ID of the option group.
163 public static function ensureOptionGroupExists($params) {
164 $existingValues = civicrm_api3('OptionGroup', 'get', array(
165 'name' => $params['name'],
168 if (!$existingValues['count']) {
169 $result = civicrm_api3('OptionGroup', 'create', $params);
170 return $result['id'];
173 return $existingValues['id'];
178 * Get the title of an option group by name.
180 * @param string $name
181 * The name value for the option group table.
184 * The relevant title.
186 public static function getTitleByName($name) {
187 $groups = self
::getTitlesByNames();
188 return $groups[$name];
192 * Get a cached mapping of all group titles indexed by their unique name.
194 * We tend to only have a limited number of option groups so memory caching
195 * makes more sense than multiple look-ups.
198 * Array of all group titles by name.
200 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
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
;
209 return \Civi
::$statics[__CLASS__
]['titles_by_name'];
213 * Set the given values to active, and set all other values to inactive.
215 * @param string $optionGroupName
217 * @param array<string> $activeValues
218 * e.g. array("en_CA","fr_CA")
220 public static function setActiveValues($optionGroupName, $activeValues) {
222 1 => array($optionGroupName, 'String'),
225 // convert activeValues into placeholders / params in the query
226 $placeholders = array();
227 $i = count($params) +
1;
228 foreach ($activeValues as $value) {
229 $placeholders[] = "%{$i}";
230 $params[$i] = array($value, 'String');
233 $placeholders = implode(', ', $placeholders);
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",