Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Class constructor. |
6a488035 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
fe482240 | 45 | * Fetch object based on array of properties. |
6a488035 | 46 | * |
6a0b768e TO |
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. | |
6a488035 | 51 | * |
16b10e64 | 52 | * @return CRM_Core_BAO_OptionGroup |
6a488035 | 53 | */ |
00be9182 | 54 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
fe482240 | 65 | * Update the is_active flag in the db. |
6a488035 | 66 | * |
6a0b768e TO |
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. | |
6a488035 | 71 | * |
a6c01b45 | 72 | * @return Object |
b44e3f84 | 73 | * DAO object on success, null otherwise |
6a488035 | 74 | */ |
00be9182 | 75 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
76 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active); |
77 | } | |
78 | ||
79 | /** | |
fe482240 | 80 | * Add the Option Group. |
6a488035 | 81 | * |
6a0b768e TO |
82 | * @param array $params |
83 | * Reference array contains the values submitted by the form. | |
84 | * @param array $ids | |
85 | * Reference array contains the id. | |
6a488035 | 86 | * |
6a488035 TO |
87 | * |
88 | * @return object | |
89 | */ | |
00be9182 | 90 | public static function add(&$params, $ids = array()) { |
9b873358 | 91 | if (empty($params['id'])) { |
4033343a | 92 | $params['id'] = CRM_Utils_Array::value('optionGroup', $ids); |
93 | } | |
94 | ||
6a488035 TO |
95 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
96 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
97 | ||
98 | // action is taken depending upon the mode | |
99 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
100 | $optionGroup->copyValues($params);; | |
101 | ||
102 | if ($params['is_default']) { | |
103 | $query = "UPDATE civicrm_option_group SET is_default = 0"; | |
104 | CRM_Core_DAO::executeQuery($query); | |
105 | } | |
106 | ||
6a488035 TO |
107 | $optionGroup->save(); |
108 | return $optionGroup; | |
109 | } | |
110 | ||
111 | /** | |
fe482240 | 112 | * Delete Option Group. |
6a488035 | 113 | * |
6a0b768e TO |
114 | * @param int $optionGroupId |
115 | * Id of the Option Group to be deleted. | |
6a488035 | 116 | */ |
00be9182 | 117 | public static function del($optionGroupId) { |
6a488035 TO |
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(); | |
122 | ||
123 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
124 | $optionGroup->id = $optionGroupId; | |
125 | $optionGroup->delete(); | |
126 | } | |
127 | ||
128 | /** | |
fe482240 | 129 | * Get title of the option group. |
6a488035 | 130 | * |
6a0b768e TO |
131 | * @param int $optionGroupId |
132 | * Id of the Option Group. | |
6a488035 | 133 | * |
ae5ffbb7 | 134 | * @return string |
a6c01b45 | 135 | * title |
6a488035 | 136 | */ |
00be9182 | 137 | public static function getTitle($optionGroupId) { |
6a488035 TO |
138 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
139 | $optionGroup->id = $optionGroupId; | |
140 | $optionGroup->find(TRUE); | |
141 | return $optionGroup->name; | |
142 | } | |
96025800 | 143 | |
eaecfa20 SL |
144 | /** |
145 | * Get DataType for a specified option Group | |
146 | * | |
147 | * @param int $optionGroupId | |
148 | * Id of the Option Group. | |
149 | * | |
150 | * @return string|null | |
151 | * Data Type | |
152 | */ | |
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; | |
158 | } | |
159 | ||
801bafd7 | 160 | /** |
161 | * Ensure an option group exists. | |
162 | * | |
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. | |
165 | * | |
166 | * This is sympathetic to sites who might pre-add it. | |
167 | * | |
168 | * @param array $params | |
169 | * | |
170 | * @return int | |
171 | * ID of the option group. | |
172 | */ | |
173 | public static function ensureOptionGroupExists($params) { | |
174 | $existingValues = civicrm_api3('OptionGroup', 'get', array( | |
175 | 'name' => $params['name'], | |
34273b2a | 176 | 'return' => 'id', |
801bafd7 | 177 | )); |
178 | if (!$existingValues['count']) { | |
179 | $result = civicrm_api3('OptionGroup', 'create', $params); | |
180 | return $result['id']; | |
181 | } | |
182 | else { | |
183 | return $existingValues['id']; | |
184 | } | |
185 | } | |
186 | ||
6a488035 | 187 | } |