2 // $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv2 custom group functions
34 * @package CiviCRM_APIv2
35 * @subpackage API_CustomGroup
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $
42 * Files required for this package
44 require_once 'api/v2/utils.php';
47 * Most API functions take in associative arrays ( name => value pairs
48 * as parameters. Some of the most commonly used parameters are
51 * @param array $params an associative array used in construction
52 * retrieval of the object
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.
61 * @param $params array Associative array of property name/value pairs to insert in group.
64 * @return Newly create custom_group object
68 function civicrm_custom_group_create($params) {
69 _civicrm_initialize();
71 if (!is_array($params)) {
72 return civicrm_create_error("params is not an array");
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']);
82 if (!isset($params['extends']) ||
!is_array($params['extends'])) {
83 return civicrm_create_error("Params must include either 'class_name' (string) or 'extends' (array).");
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').");
92 $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomGroup');
94 require_once 'CRM/Utils/String.php';
95 if (!isset($params['title']) ||
96 !trim($params['title'])
98 return civicrm_create_error("Title parameter is required.");
101 if (!isset($params['style']) ||
!trim($params['style'])) {
102 $params['style'] = 'Inline';
105 if (is_a($error, 'CRM_Core_Error')) {
106 return civicrm_create_error($error->_errors
[0]['message']);
109 require_once 'CRM/Core/BAO/CustomGroup.php';
110 $customGroup = CRM_Core_BAO_CustomGroup
::create($params);
112 _civicrm_object_to_array($customGroup, $values);
114 if (is_a($customGroup, 'CRM_Core_Error')) {
115 return civicrm_create_error($customGroup->_errors
[0]['message']);
118 $values['is_error'] = 0;
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']);
129 * Use this API to delete an existing group.
131 * @param array id of the group to be deleted
133 * @return Null if success
136 function civicrm_custom_group_delete($params) {
137 _civicrm_initialize();
139 if (!is_array($params)) {
140 return civicrm_create_error('Params is not an array');
143 if (!CRM_Utils_Array
::value('id', $params)) {
144 return civicrm_create_error('Invalid or no value for Custom group ID');
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'];
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');
158 * Defines 'custom field' within a group.
161 * @param $params array Associative array of property name/value pairs to create new custom field.
163 * @return Newly created custom_field id array
168 function civicrm_custom_field_create($params) {
169 _civicrm_initialize();
171 if (!is_array($params)) {
172 return civicrm_create_error("params is not an array ");
175 if (!CRM_Utils_Array
::value('custom_group_id', $params)) {
176 return civicrm_create_error("Missing Required field :custom_group_id");
179 if (!(CRM_Utils_Array
::value('label', $params))) {
180 return civicrm_create_error("Missing Required field :label");
183 if (!(CRM_Utils_Array
::value('option_type', $params))) {
184 if (CRM_Utils_Array
::value('id', $params)) {
185 $params['option_type'] = 2;
188 $params['option_type'] = 1;
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']);
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'];
206 require_once 'CRM/Core/BAO/CustomField.php';
207 $customField = CRM_Core_BAO_CustomField
::create($params);
209 $values['customFieldId'] = $customField->id
;
211 if (is_a($customField, 'CRM_Core_Error') && is_a($column, 'CRM_Core_Error')) {
212 return civicrm_create_error($customField->_errors
[0]['message']);
215 return civicrm_create_success($values);
220 * Use this API to delete an existing custom group field.
222 * @param $params Array id of the field to be deleted
227 function civicrm_custom_field_delete($params) {
228 _civicrm_initialize();
230 if (!is_array($params)) {
231 return civicrm_create_error('Params is not an array');
234 if (!CRM_Utils_Array
::value('id', $params)) {
235 return civicrm_create_error('Invalid or no value for Custom Field ID');
238 require_once 'CRM/Core/DAO/CustomField.php';
239 $field = new CRM_Core_DAO_CustomField();
240 $field->id
= $params['id'];
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();