(NFC) (dev/core#878) Simplify copyright header (api/*)
[civicrm-core.git] / api / v3 / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Groups.
14 *
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.
17 *
18 * @package CiviCRM_APIv3
19 */
20
21 /**
22 * Create/update group.
23 *
24 * @param array $params
25 * name/value pairs to insert in new 'Group'
26 *
27 * @return array
28 * API result array
29 */
30 function civicrm_api3_group_create($params) {
31 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Group');
32 }
33
34 /**
35 * Adjust Metadata for Create action.
36 *
37 * The metadata is used for setting defaults, documentation & validation.
38 *
39 * @param array $params
40 * Array of parameters determined by getfields.
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 /**
48 * Returns array of groups matching a set of one or more Group properties.
49 *
50 * @param array $params
51 * Array of properties. If empty, all records will be returned.
52 *
53 * @return array
54 * Array of matching groups
55 */
56 function civicrm_api3_group_get($params) {
57 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'Group', 'get');
58
59 if ($options['is_count']) {
60 $params['options']['is_count'] = 0;
61 $params['return'] = 'id';
62 }
63
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'])) {
67 unset($groups[$id]);
68 }
69 if (!empty($options['return']) && in_array('member_count', $options['return'])) {
70 $groups[$id]['member_count'] = CRM_Contact_BAO_Group::memberCount($id);
71 }
72 }
73 return civicrm_api3_create_success($groups, $params, 'Group', 'get');
74 }
75
76 /**
77 * Delete an existing Group.
78 *
79 * @param array $params
80 * [id]
81 * @return array API result array
82 * @throws API_Exception
83 */
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']);
88 }
89 CRM_Contact_BAO_Group::discard($params['id']);
90 return civicrm_api3_create_success(TRUE);
91 }