Merge pull request #583 from yashodha/CRM-12463
[civicrm-core.git] / api / v3 / Group.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30/**
31 * File for the CiviCRM APIv3 group functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Group
35 * @copyright CiviCRM LLC (c) 2004-2013
36 * @version $Id: Group.php 30171 2010-10-14 09:11:27Z mover $
37 */
38
39/**
40 * Include utility functions
41 */
42require_once 'CRM/Contact/BAO/Group.php';
43
44/**
45 * create/update group
46 *
47 * This API is used to create new group or update any of the existing
48 * In case of updating existing group, id of that particular grop must
49 * be in $params array. Either id or name is required field in the
50 * $params array
51 *
52 * @param array $params (referance) Associative array of property
53 * name/value pairs to insert in new 'group'
54 *
55 * @return array returns id of the group created if success,
56 * error message otherwise
57 *@example GroupCreate.php
58 *{@getfields group_create}
59 * @access public
60 */
61function civicrm_api3_group_create($params) {
62
63 $group = CRM_Contact_BAO_Group::create($params);
64
65 if (is_null($group)) {
66 return civicrm_api3_create_error('Group not created');
67 }
68 else {
69 $values = array();
70 _civicrm_api3_object_to_array_unique_fields($group, $values[$group->id]);
71 return civicrm_api3_create_success($values, $params, 'group', 'create', $group);
72 }
73}
11e09c59
TO
74
75/**
6a488035
TO
76 * Adjust Metadata for Create action
77 *
78 * The metadata is used for setting defaults, documentation & validation
79 * @param array $params array or parameters determined by getfields
80 */
81function _civicrm_api3_group_create_spec(&$params) {
82 $params['is_active']['api.default'] = 1;
83 $params['title']['api.required'] = 1;
84}
85
86/**
87 * Returns array of groups matching a set of one or more group properties
88 *
89 * @param array $params (referance) Array of one or more valid
90 * property_name=>value pairs. If $params is set
91 * as null, all groups will be returned
92 *
93 * @return array Array of matching groups
94 * @example GroupGet.php
95 * {@getfields group_get}
96 * @access public
97 */
98function civicrm_api3_group_get($params) {
99
d64eb977 100 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'group', 'get');
6a488035
TO
101 $sort = CRM_Utils_Array::value('sort', $options, NULL);
102 $offset = CRM_Utils_Array::value('offset', $options);
103 $rowCount = CRM_Utils_Array::value('limit', $options);
104 $returnProperties = CRM_Utils_Array::value('return', $options, NULL);
105 $inputParams = CRM_Utils_Array::value('input_params', $options, array());
106 if(is_array($returnProperties) && !empty($returnProperties)){
107 // group function takes $returnProperties in non standard format & doesn't add id
96d14576 108 unset($returnProperties['group_id']);
6a488035
TO
109 $returnProperties['id'] = 1;
110 $returnProperties = array_keys($returnProperties);
111 }
ca1b55f7
AH
112 if (CRM_Utils_Array::value('group_id', $inputParams)) {
113 $inputParams['id'] = $inputParams['group_id'];
114 }
6a488035
TO
115 $groupObjects = CRM_Contact_BAO_Group::getGroups($inputParams, $returnProperties, $sort, $offset, $rowCount);
116 if (empty($groupObjects)) {
117 return civicrm_api3_create_success(FALSE);
118 }
119 $groups = array();
120 foreach ($groupObjects as $group) {
121 _civicrm_api3_object_to_array($group, $groups[$group->id]);
122 _civicrm_api3_custom_data_get($groups[$group->id], 'Group', $group->id);
123 }
124
125
126 return civicrm_api3_create_success($groups, $params, 'group', 'create');
127}
128
129/**
130 * delete an existing group
131 *
132 * This method is used to delete any existing group. id of the group
133 * to be deleted is required field in $params array
134 *
135 * @param array $params (referance) array containing id of the group
136 * to be deleted
137 *
138 * @return array (referance) returns flag true if successfull, error
139 * message otherwise
140 *@example GroupDelete.php
141 *{@getfields group_delete}
142 *
143 * @access public
144 */
145function civicrm_api3_group_delete($params) {
146
147 CRM_Contact_BAO_Group::discard($params['id']);
148 return civicrm_api3_create_success(TRUE);
149}
150