INFRA-132 - Batch 14 (g)
[civicrm-core.git] / api / v3 / Group.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * File for the CiviCRM APIv3 group functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Group
731a0992 33 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
34 * @version $Id: Group.php 30171 2010-10-14 09:11:27Z mover $
35 */
36
6a488035
TO
37/**
38 * create/update group
39 *
40 * This API is used to create new group or update any of the existing
c206647d 41 * In case of updating existing group, id of that particular group must
6a488035
TO
42 * be in $params array. Either id or name is required field in the
43 * $params array
44 *
cf470720
TO
45 * @param array $params
46 * Associative array of property.
6a488035
TO
47 * name/value pairs to insert in new 'group'
48 *
a6c01b45 49 * @return array
72b3a70c 50 * API result array
c866eb5f
TO
51 * @example GroupCreate.php
52 * {@getfields group_create}
6a488035
TO
53 */
54function civicrm_api3_group_create($params) {
e34ab426 55 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Group');
6a488035 56}
11e09c59
TO
57
58/**
6a488035
TO
59 * Adjust Metadata for Create action
60 *
61 * The metadata is used for setting defaults, documentation & validation
cf470720
TO
62 * @param array $params
63 * Array or parameters determined by getfields.
6a488035
TO
64 */
65function _civicrm_api3_group_create_spec(&$params) {
66 $params['is_active']['api.default'] = 1;
67 $params['title']['api.required'] = 1;
68}
69
70/**
71 * Returns array of groups matching a set of one or more group properties
72 *
cf470720
TO
73 * @param array $params
74 * Array of one or more valid.
6a488035
TO
75 * property_name=>value pairs. If $params is set
76 * as null, all groups will be returned
77 *
a6c01b45 78 * @return array
72b3a70c 79 * Array of matching groups
6a488035
TO
80 * @example GroupGet.php
81 * {@getfields group_get}
6a488035
TO
82 */
83function civicrm_api3_group_get($params) {
e34ab426 84 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'group', 'get');
22e263ad 85 if (empty($options['return']) || !in_array('member_count', $options['return'])) {
e34ab426 86 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Group');
6a488035
TO
87 }
88
e34ab426
EM
89 $groups = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Group');
90 foreach ($groups as $id => $group) {
91 $groups[$id]['member_count'] = CRM_Contact_BAO_Group::memberCount($id);
92 }
93 return civicrm_api3_create_success($groups, $params, 'group', 'get');
6a488035
TO
94}
95
96/**
97 * delete an existing group
98 *
99 * This method is used to delete any existing group. id of the group
100 * to be deleted is required field in $params array
101 *
cf470720
TO
102 * @param array $params
103 * Array containing id of the group.
6a488035
TO
104 * to be deleted
105 *
a6c01b45 106 * @return array
72b3a70c 107 * API result array
c866eb5f
TO
108 * @example GroupDelete.php
109 * {@getfields group_delete}
6a488035 110 *
6a488035
TO
111 */
112function civicrm_api3_group_delete($params) {
113
114 CRM_Contact_BAO_Group::discard($params['id']);
115 return civicrm_api3_create_success(TRUE);
116}