Merge pull request #9664 from konadave/CRM-19853
[civicrm-core.git] / CRM / Core / BAO / OptionGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
36
37 /**
38 * Class constructor.
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Fetch object based on array of properties.
46 *
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.
51 *
52 * @return CRM_Core_BAO_OptionGroup
53 */
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);
59 return $optionGroup;
60 }
61 return NULL;
62 }
63
64 /**
65 * Update the is_active flag in the db.
66 *
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.
71 *
72 * @return Object
73 * DAO object on success, null otherwise
74 */
75 public static function setIsActive($id, $is_active) {
76 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_OptionGroup', $id, 'is_active', $is_active);
77 }
78
79 /**
80 * Add the Option Group.
81 *
82 * @param array $params
83 * Reference array contains the values submitted by the form.
84 * @param array $ids
85 * Reference array contains the id.
86 *
87 *
88 * @return object
89 */
90 public static function add(&$params, $ids = array()) {
91 if (empty($params['id'])) {
92 $params['id'] = CRM_Utils_Array::value('optionGroup', $ids);
93 }
94
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
107 $optionGroup->save();
108 return $optionGroup;
109 }
110
111 /**
112 * Delete Option Group.
113 *
114 * @param int $optionGroupId
115 * Id of the Option Group to be deleted.
116 */
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();
122
123 $optionGroup = new CRM_Core_DAO_OptionGroup();
124 $optionGroup->id = $optionGroupId;
125 $optionGroup->delete();
126 }
127
128 /**
129 * Get title of the option group.
130 *
131 * @param int $optionGroupId
132 * Id of the Option Group.
133 *
134 * @return string
135 * title
136 */
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;
142 }
143
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
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'],
176 'return' => 'id',
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
187 }