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
17 class CRM_Core_BAO_OptionGroup
extends CRM_Core_DAO_OptionGroup
{
22 public function __construct() {
23 parent
::__construct();
27 * Fetch object based on array of properties.
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
31 * @param array $defaults
32 * (reference ) an assoc array to hold the flattened values.
34 * @return CRM_Core_BAO_OptionGroup
36 public static function retrieve(&$params, &$defaults) {
37 $optionGroup = new CRM_Core_DAO_OptionGroup();
38 $optionGroup->copyValues($params);
39 if ($optionGroup->find(TRUE)) {
40 CRM_Core_DAO
::storeValues($optionGroup, $defaults);
47 * Update the is_active flag in the db.
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
55 * true if we found and updated the object, else false
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
62 * Add the Option Group.
64 * @param array $params
65 * Reference array contains the values submitted by the form.
67 * Reference array contains the id.
72 public static function add(&$params, $ids = []) {
73 if (empty($params['id']) && !empty($ids['optionGroup'])) {
74 CRM_Core_Error
::deprecatedFunctionWarning('no $ids array');
75 $params['id'] = $ids['optionGroup'];
77 if (empty($params['name']) && empty($params['id'])) {
78 $params['name'] = CRM_Utils_String
::titleToVar(strtolower($params['title']));
80 elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
81 $params['name'] = CRM_Utils_String
::titleToVar(strtolower($params['name']));
83 elseif (!empty($params['name'])) {
84 $params['name'] = strtolower($params['name']);
86 $optionGroup = new CRM_Core_DAO_OptionGroup();
87 $optionGroup->copyValues($params);
93 * Delete Option Group.
95 * @param int $optionGroupId
96 * Id of the Option Group to be deleted.
98 public static function del($optionGroupId) {
99 // need to delete all option value field before deleting group
100 $optionValue = new CRM_Core_DAO_OptionValue();
101 $optionValue->option_group_id
= $optionGroupId;
102 $optionValue->delete();
104 $optionGroup = new CRM_Core_DAO_OptionGroup();
105 $optionGroup->id
= $optionGroupId;
106 $optionGroup->delete();
110 * Get title of the option group.
112 * @param int $optionGroupId
113 * Id of the Option Group.
118 public static function getTitle($optionGroupId) {
119 $optionGroup = new CRM_Core_DAO_OptionGroup();
120 $optionGroup->id
= $optionGroupId;
121 $optionGroup->find(TRUE);
122 return $optionGroup->name
;
126 * Get DataType for a specified option Group
128 * @param int $optionGroupId
129 * Id of the Option Group.
131 * @return string|null
134 public static function getDataType($optionGroupId) {
135 $optionGroup = new CRM_Core_DAO_OptionGroup();
136 $optionGroup->id
= $optionGroupId;
137 $optionGroup->find(TRUE);
138 return $optionGroup->data_type
;
142 * Ensure an option group exists.
144 * This function is intended to be called from the upgrade script to ensure
145 * that an option group exists, without hitting an error if it already exists.
147 * This is sympathetic to sites who might pre-add it.
149 * @param array $params
152 * ID of the option group.
154 public static function ensureOptionGroupExists($params) {
155 $existingValues = civicrm_api3('OptionGroup', 'get', [
156 'name' => $params['name'],
159 if (!$existingValues['count']) {
160 $result = civicrm_api3('OptionGroup', 'create', $params);
161 return $result['id'];
164 return $existingValues['id'];
169 * Get the title of an option group by name.
171 * @param string $name
172 * The name value for the option group table.
175 * The relevant title.
177 public static function getTitleByName($name) {
178 $groups = self
::getTitlesByNames();
179 return $groups[$name];
183 * Get a cached mapping of all group titles indexed by their unique name.
185 * We tend to only have a limited number of option groups so memory caching
186 * makes more sense than multiple look-ups.
189 * Array of all group titles by name.
191 * array('activity_status' => 'Activity Status', 'msg_mode' => 'Message Mode'....)
193 public static function getTitlesByNames() {
194 if (!isset(\Civi
::$statics[__CLASS__
]) ||
!isset(\Civi
::$statics[__CLASS__
]['titles_by_name'])) {
195 $dao = CRM_Core_DAO
::executeQuery("SELECT name, title FROM civicrm_option_group");
196 while ($dao->fetch()) {
197 \Civi
::$statics[__CLASS__
]['titles_by_name'][$dao->name
] = $dao->title
;
200 return \Civi
::$statics[__CLASS__
]['titles_by_name'];
204 * Set the given values to active, and set all other values to inactive.
206 * @param string $optionGroupName
208 * @param string[] $activeValues
209 * e.g. array("en_CA","fr_CA")
211 public static function setActiveValues($optionGroupName, $activeValues) {
213 1 => [$optionGroupName, 'String'],
216 // convert activeValues into placeholders / params in the query
218 $i = count($params) +
1;
219 foreach ($activeValues as $value) {
220 $placeholders[] = "%{$i}";
221 $params[$i] = [$value, 'String'];
224 $placeholders = implode(', ', $placeholders);
226 CRM_Core_DAO
::executeQuery("
227 UPDATE civicrm_option_value cov
228 LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
229 SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END
230 WHERE cog.name = %1",