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 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM Groups.
15 * This api is for creating/deleting groups or fetching a list of existing groups.
16 * To add/remove contacts to a group, use the GroupContact api instead.
18 * @package CiviCRM_APIv3
22 * Create/update group.
24 * @param array $params
25 * name/value pairs to insert in new 'Group'
30 function civicrm_api3_group_create($params) {
31 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Group');
35 * Adjust Metadata for Create action.
37 * The metadata is used for setting defaults, documentation & validation.
39 * @param array $params
40 * Array of parameters determined by getfields.
42 function _civicrm_api3_group_create_spec(&$params) {
43 $params['is_active']['api.default'] = 1;
44 $params['title']['api.required'] = 1;
48 * Returns array of groups matching a set of one or more Group properties.
50 * @param array $params
51 * Array of properties. If empty, all records will be returned.
54 * Array of matching groups
56 function civicrm_api3_group_get($params) {
57 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Group', 'get');
59 if ($options['is_count']) {
60 $params['options']['is_count'] = 0;
61 $params['return'] = 'id';
64 $groups = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Group');
65 foreach ($groups as $id => $group) {
66 if (!empty($params['check_permissions']) && !CRM_Contact_BAO_Group
::checkPermission($group['id'])) {
69 if (!empty($options['return']) && in_array('member_count', $options['return'])) {
70 $groups[$id]['member_count'] = CRM_Contact_BAO_Group
::memberCount($id);
73 return civicrm_api3_create_success($groups, $params, 'Group', 'get');
77 * Delete an existing Group.
79 * @param array $params
81 * @return array API result array
82 * @throws API_Exception
84 function civicrm_api3_group_delete($params) {
85 $group = civicrm_api3_group_get(['id' => $params['id']]);
86 if ($group['count'] == 0) {
87 throw new API_Exception('Could not delete group ' . $params['id']);
89 CRM_Contact_BAO_Group
::discard($params['id']);
90 return civicrm_api3_create_success(TRUE);