3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Core_BAO_OptionGroup
extends CRM_Core_DAO_OptionGroup
{
24 public function __construct() {
25 parent
::__construct();
29 * Fetch object based on array of properties.
31 * @param array $params
32 * (reference ) an assoc array of name/value pairs.
33 * @param array $defaults
34 * (reference ) an assoc array to hold the flattened values.
36 * @return CRM_Core_BAO_OptionGroup
38 public static function retrieve(&$params, &$defaults) {
39 $optionGroup = new CRM_Core_DAO_OptionGroup();
40 $optionGroup->copyValues($params);
41 if ($optionGroup->find(TRUE)) {
42 CRM_Core_DAO
::storeValues($optionGroup, $defaults);
49 * Update the is_active flag in the db.
52 * Id of the database record.
53 * @param bool $is_active
54 * Value we want to set the is_active field.
57 * true if we found and updated the object, else false
59 public static function setIsActive($id, $is_active) {
60 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
64 * Add the Option Group.
66 * @param array $params
67 * Reference array contains the values submitted by the form.
69 * Reference array contains the id.
74 public static function add(&$params, $ids = []) {
75 if (empty($params['id']) && !empty($ids['optionGroup'])) {
76 CRM_Core_Error
::deprecatedFunctionWarning('no $ids array');
77 $params['id'] = $ids['optionGroup'];
79 if (empty($params['name']) && empty($params['id'])) {
80 $params['name'] = CRM_Utils_String
::titleToVar(strtolower($params['title']));
82 elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
83 $params['name'] = CRM_Utils_String
::titleToVar(strtolower($params['name']));
85 elseif (!empty($params['name'])) {
86 $params['name'] = strtolower($params['name']);
88 $optionGroup = new CRM_Core_DAO_OptionGroup();
89 $optionGroup->copyValues($params);
95 * Delete Option Group.
97 * @param int $optionGroupId
98 * Id of the Option Group to be deleted.
100 public static function del($optionGroupId) {
101 // need to delete all option value field before deleting group
102 $optionValue = new CRM_Core_DAO_OptionValue();
103 $optionValue->option_group_id
= $optionGroupId;
104 $optionValue->delete();
106 $optionGroup = new CRM_Core_DAO_OptionGroup();
107 $optionGroup->id
= $optionGroupId;
108 $optionGroup->delete();
112 * Get title of the option group.
114 * @param int $optionGroupId
115 * Id of the Option Group.
120 public static function getTitle($optionGroupId) {
121 $optionGroup = new CRM_Core_DAO_OptionGroup();
122 $optionGroup->id
= $optionGroupId;
123 $optionGroup->find(TRUE);
124 return $optionGroup->name
;
128 * Get DataType for a specified option Group
130 * @param int $optionGroupId
131 * Id of the Option Group.
133 * @return string|null
136 public static function getDataType($optionGroupId) {
137 $optionGroup = new CRM_Core_DAO_OptionGroup();
138 $optionGroup->id
= $optionGroupId;
139 $optionGroup->find(TRUE);
140 return $optionGroup->data_type
;
144 * Ensure an option group exists.
146 * This function is intended to be called from the upgrade script to ensure
147 * that an option group exists, without hitting an error if it already exists.
149 * This is sympathetic to sites who might pre-add it.
151 * @param array $params
154 * ID of the option group.
156 public static function ensureOptionGroupExists($params) {
157 $existingValues = civicrm_api3('OptionGroup', 'get', [
158 'name' => $params['name'],
161 if (!$existingValues['count']) {
162 $result = civicrm_api3('OptionGroup', 'create', $params);
163 return $result['id'];
166 return $existingValues['id'];
171 * Get the title of an option group by name.
173 * @param string $name
174 * The name value for the option group table.
177 * The relevant title.
179 public static function getTitleByName($name) {
180 $groups = self
::getTitlesByNames();
181 return $groups[$name];
185 * Get a cached mapping of all group titles indexed by their unique name.
187 * We tend to only have a limited number of option groups so memory caching
188 * makes more sense than multiple look-ups.
191 * Array of all group titles by name.
193 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
195 public static function getTitlesByNames() {
196 if (!isset(\Civi
::$statics[__CLASS__
]) ||
!isset(\Civi
::$statics[__CLASS__
]['titles_by_name'])) {
197 $dao = CRM_Core_DAO
::executeQuery("SELECT name, title FROM civicrm_option_group");
198 while ($dao->fetch()) {
199 \Civi
::$statics[__CLASS__
]['titles_by_name'][$dao->name
] = $dao->title
;
202 return \Civi
::$statics[__CLASS__
]['titles_by_name'];
206 * Set the given values to active, and set all other values to inactive.
208 * @param string $optionGroupName
210 * @param string[] $activeValues
211 * e.g. array("en_CA","fr_CA")
213 public static function setActiveValues($optionGroupName, $activeValues) {
215 1 => [$optionGroupName, 'String'],
218 // convert activeValues into placeholders / params in the query
220 $i = count($params) +
1;
221 foreach ($activeValues as $value) {
222 $placeholders[] = "%{$i}";
223 $params[$i] = [$value, 'String'];
226 $placeholders = implode(', ', $placeholders);
228 CRM_Core_DAO
::executeQuery("
229 UPDATE civicrm_option_value cov
230 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
231 SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
232 WHERE cog.name = %1",