Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
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 | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
244bbdd8 | 13 | * This api exposes CiviCRM Groups. |
b081365f CW |
14 | * |
15 | * This api is for creating/deleting groups or fetching a list of existing groups. | |
244bbdd8 | 16 | * To add/remove contacts to a group, use the GroupContact api instead. |
6a488035 TO |
17 | * |
18 | * @package CiviCRM_APIv3 | |
6a488035 TO |
19 | */ |
20 | ||
6a488035 | 21 | /** |
22242c87 | 22 | * Create/update group. |
6a488035 | 23 | * |
cf470720 | 24 | * @param array $params |
244bbdd8 | 25 | * name/value pairs to insert in new 'Group' |
6a488035 | 26 | * |
a6c01b45 | 27 | * @return array |
72b3a70c | 28 | * API result array |
6a488035 TO |
29 | */ |
30 | function civicrm_api3_group_create($params) { | |
e34ab426 | 31 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Group'); |
6a488035 | 32 | } |
11e09c59 TO |
33 | |
34 | /** | |
0aa0303c EM |
35 | * Adjust Metadata for Create action. |
36 | * | |
37 | * The metadata is used for setting defaults, documentation & validation. | |
6a488035 | 38 | * |
cf470720 | 39 | * @param array $params |
b081365f | 40 | * Array of parameters determined by getfields. |
6a488035 TO |
41 | */ |
42 | function _civicrm_api3_group_create_spec(&$params) { | |
43 | $params['is_active']['api.default'] = 1; | |
44 | $params['title']['api.required'] = 1; | |
45 | } | |
46 | ||
47 | /** | |
244bbdd8 | 48 | * Returns array of groups matching a set of one or more Group properties. |
6a488035 | 49 | * |
cf470720 | 50 | * @param array $params |
b081365f | 51 | * Array of properties. If empty, all records will be returned. |
6a488035 | 52 | * |
a6c01b45 | 53 | * @return array |
72b3a70c | 54 | * Array of matching groups |
6a488035 TO |
55 | */ |
56 | function civicrm_api3_group_get($params) { | |
244bbdd8 | 57 | $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Group', 'get'); |
6d054a8e | 58 | |
59 | if ($options['is_count']) { | |
60 | $params['options']['is_count'] = 0; | |
61 | $params['return'] = 'id'; | |
23f6cae2 | 62 | } |
63 | ||
e34ab426 EM |
64 | $groups = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Group'); |
65 | foreach ($groups as $id => $group) { | |
23f6cae2 | 66 | if (!empty($params['check_permissions']) && !CRM_Contact_BAO_Group::checkPermission($group['id'])) { |
a0fd420c | 67 | unset($groups[$id]); |
68 | } | |
6d054a8e | 69 | if (!empty($options['return']) && in_array('member_count', $options['return'])) { |
a0fd420c | 70 | $groups[$id]['member_count'] = CRM_Contact_BAO_Group::memberCount($id); |
71 | } | |
e34ab426 | 72 | } |
244bbdd8 | 73 | return civicrm_api3_create_success($groups, $params, 'Group', 'get'); |
6a488035 TO |
74 | } |
75 | ||
76 | /** | |
244bbdd8 | 77 | * Delete an existing Group. |
6a488035 | 78 | * |
cf470720 | 79 | * @param array $params |
c28e1768 | 80 | * [id] |
8089541a | 81 | * @return array API result array |
8089541a | 82 | * @throws API_Exception |
6a488035 TO |
83 | */ |
84 | function civicrm_api3_group_delete($params) { | |
cf8f0fff | 85 | $group = civicrm_api3_group_get(['id' => $params['id']]); |
a60c0bc8 SL |
86 | if ($group['count'] == 0) { |
87 | throw new API_Exception('Could not delete group ' . $params['id']); | |
88 | } | |
6a488035 TO |
89 | CRM_Contact_BAO_Group::discard($params['id']); |
90 | return civicrm_api3_create_success(TRUE); | |
91 | } |