Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $ | |
3 | ||
4 | ||
5 | /* | |
6 | +--------------------------------------------------------------------+ | |
7 | | CiviCRM version 4.3 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
10 | +--------------------------------------------------------------------+ | |
11 | | This file is a part of CiviCRM. | | |
12 | | | | |
13 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
14 | | under the terms of the GNU Affero General Public License | | |
15 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
16 | | | | |
17 | | CiviCRM is distributed in the hope that it will be useful, but | | |
18 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
19 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
20 | | See the GNU Affero General Public License for more details. | | |
21 | | | | |
22 | | You should have received a copy of the GNU Affero General Public | | |
23 | | License and the CiviCRM Licensing Exception along | | |
24 | | with this program; if not, contact CiviCRM LLC | | |
25 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
26 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
27 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
28 | +--------------------------------------------------------------------+ | |
29 | */ | |
30 | ||
31 | /** | |
32 | * File for the CiviCRM APIv2 custom group functions | |
33 | * | |
34 | * @package CiviCRM_APIv2 | |
35 | * @subpackage API_CustomGroup | |
36 | * | |
37 | * @copyright CiviCRM LLC (c) 2004-2013 | |
38 | * @version $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $ | |
39 | */ | |
40 | ||
41 | /** | |
42 | * Files required for this package | |
43 | */ | |
44 | require_once 'api/v2/utils.php'; | |
45 | ||
46 | /** | |
47 | * Most API functions take in associative arrays ( name => value pairs | |
48 | * as parameters. Some of the most commonly used parameters are | |
49 | * described below | |
50 | * | |
51 | * @param array $params an associative array used in construction | |
52 | * retrieval of the object | |
53 | * | |
54 | * | |
55 | */ | |
56 | ||
57 | /** | |
58 | * Use this API to create a new group. See the CRM Data Model for custom_group property definitions | |
59 | * $params['class_name'] is a required field, class being extended. | |
60 | * | |
61 | * @param $params array Associative array of property name/value pairs to insert in group. | |
62 | * | |
63 | * | |
64 | * @return Newly create custom_group object | |
65 | * | |
66 | * @access public | |
67 | */ | |
68 | function civicrm_custom_group_create($params) { | |
69 | _civicrm_initialize(); | |
70 | ||
71 | if (!is_array($params)) { | |
72 | return civicrm_create_error("params is not an array"); | |
73 | } | |
74 | ||
75 | // Require either param['class_name'] (string) - for backwards compatibility - OR parm['extends'] (array) | |
76 | // If passing extends array - set class_name (e.g. 'Contact', 'Participant'...) as extends[0]. You may optionally | |
77 | // pass an extends_entity_column_value as extends[1] (e.g. an Activity Type ID). | |
78 | if (isset($params['class_name']) && trim($params['class_name'])) { | |
79 | $params['extends'][0] = trim($params['class_name']); | |
80 | } | |
81 | else { | |
82 | if (!isset($params['extends']) || !is_array($params['extends'])) { | |
83 | return civicrm_create_error("Params must include either 'class_name' (string) or 'extends' (array)."); | |
84 | } | |
85 | else { | |
86 | if (!isset($params['extends'][0]) || !trim($params['extends'][0])) { | |
87 | return civicrm_create_error("First item in params['extends'] must be a class name (e.g. 'Contact')."); | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomGroup'); | |
93 | ||
94 | require_once 'CRM/Utils/String.php'; | |
95 | if (!isset($params['title']) || | |
96 | !trim($params['title']) | |
97 | ) { | |
98 | return civicrm_create_error("Title parameter is required."); | |
99 | } | |
100 | ||
101 | if (!isset($params['style']) || !trim($params['style'])) { | |
102 | $params['style'] = 'Inline'; | |
103 | } | |
104 | ||
105 | if (is_a($error, 'CRM_Core_Error')) { | |
106 | return civicrm_create_error($error->_errors[0]['message']); | |
107 | } | |
108 | ||
109 | require_once 'CRM/Core/BAO/CustomGroup.php'; | |
110 | $customGroup = CRM_Core_BAO_CustomGroup::create($params); | |
111 | ||
112 | _civicrm_object_to_array($customGroup, $values); | |
113 | ||
114 | if (is_a($customGroup, 'CRM_Core_Error')) { | |
115 | return civicrm_create_error($customGroup->_errors[0]['message']); | |
116 | } | |
117 | else { | |
118 | $values['is_error'] = 0; | |
119 | } | |
120 | if (CRM_Utils_Array::value('html_type', $params)) { | |
121 | $params['custom_group_id'] = $customGroup->id; | |
122 | $fieldValues = civicrm_custom_field_create($params); | |
123 | $values = array_merge($values, $fieldValues['result']); | |
124 | } | |
125 | return $values; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Use this API to delete an existing group. | |
130 | * | |
131 | * @param array id of the group to be deleted | |
132 | * | |
133 | * @return Null if success | |
134 | * @access public | |
135 | **/ | |
136 | function civicrm_custom_group_delete($params) { | |
137 | _civicrm_initialize(); | |
138 | ||
139 | if (!is_array($params)) { | |
140 | return civicrm_create_error('Params is not an array'); | |
141 | } | |
142 | ||
143 | if (!CRM_Utils_Array::value('id', $params)) { | |
144 | return civicrm_create_error('Invalid or no value for Custom group ID'); | |
145 | } | |
146 | // convert params array into Object | |
147 | require_once 'CRM/Core/DAO/CustomGroup.php'; | |
148 | $values = new CRM_Core_DAO_CustomGroup(); | |
149 | $values->id = $params['id']; | |
150 | $values->find(TRUE); | |
151 | ||
152 | require_once 'CRM/Core/BAO/CustomGroup.php'; | |
153 | $result = CRM_Core_BAO_CustomGroup::deleteGroup($values); | |
154 | return $result ? civicrm_create_success() : civicrm_error('Error while deleting custom group'); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Defines 'custom field' within a group. | |
159 | * | |
160 | * | |
161 | * @param $params array Associative array of property name/value pairs to create new custom field. | |
162 | * | |
163 | * @return Newly created custom_field id array | |
164 | * | |
165 | * @access public | |
166 | * | |
167 | */ | |
168 | function civicrm_custom_field_create($params) { | |
169 | _civicrm_initialize(); | |
170 | ||
171 | if (!is_array($params)) { | |
172 | return civicrm_create_error("params is not an array "); | |
173 | } | |
174 | ||
175 | if (!CRM_Utils_Array::value('custom_group_id', $params)) { | |
176 | return civicrm_create_error("Missing Required field :custom_group_id"); | |
177 | } | |
178 | ||
179 | if (!(CRM_Utils_Array::value('label', $params))) { | |
180 | return civicrm_create_error("Missing Required field :label"); | |
181 | } | |
182 | ||
183 | if (!(CRM_Utils_Array::value('option_type', $params))) { | |
184 | if (CRM_Utils_Array::value('id', $params)) { | |
185 | $params['option_type'] = 2; | |
186 | } | |
187 | else { | |
188 | $params['option_type'] = 1; | |
189 | } | |
190 | } | |
191 | ||
192 | $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomField'); | |
193 | if (is_a($error, 'CRM_Core_Error')) { | |
194 | return civicrm_create_error($error->_errors[0]['message']); | |
195 | } | |
196 | ||
197 | // Array created for passing options in params | |
198 | if (isset($params['option_values']) && is_array($params['option_values'])) { | |
199 | foreach ($params['option_values'] as $key => $value) { | |
200 | $params['option_label'][$value['weight']] = $value['label']; | |
201 | $params['option_value'][$value['weight']] = $value['value']; | |
202 | $params['option_status'][$value['weight']] = $value['is_active']; | |
203 | $params['option_weight'][$value['weight']] = $value['weight']; | |
204 | } | |
205 | } | |
206 | require_once 'CRM/Core/BAO/CustomField.php'; | |
207 | $customField = CRM_Core_BAO_CustomField::create($params); | |
208 | ||
209 | $values['customFieldId'] = $customField->id; | |
210 | ||
211 | if (is_a($customField, 'CRM_Core_Error') && is_a($column, 'CRM_Core_Error')) { | |
212 | return civicrm_create_error($customField->_errors[0]['message']); | |
213 | } | |
214 | else { | |
215 | return civicrm_create_success($values); | |
216 | } | |
217 | } | |
218 | ||
219 | /** | |
220 | * Use this API to delete an existing custom group field. | |
221 | * | |
222 | * @param $params Array id of the field to be deleted | |
223 | * | |
224 | * | |
225 | * @access public | |
226 | **/ | |
227 | function civicrm_custom_field_delete($params) { | |
228 | _civicrm_initialize(); | |
229 | ||
230 | if (!is_array($params)) { | |
231 | return civicrm_create_error('Params is not an array'); | |
232 | } | |
233 | ||
234 | if (!CRM_Utils_Array::value('id', $params)) { | |
235 | return civicrm_create_error('Invalid or no value for Custom Field ID'); | |
236 | } | |
237 | ||
238 | require_once 'CRM/Core/DAO/CustomField.php'; | |
239 | $field = new CRM_Core_DAO_CustomField(); | |
240 | $field->id = $params['id']; | |
241 | $field->find(TRUE); | |
242 | ||
243 | require_once 'CRM/Core/BAO/CustomField.php'; | |
244 | $customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field); | |
245 | return $customFieldDelete ? civicrm_create_error('Error while deleting custom field') : civicrm_create_success(); | |
246 | } | |
247 |