Merge pull request #268 from deepak-srivastava/alpha
[civicrm-core.git] / api / v3 / Group.php
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 */
42 require_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 */
61 function 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 }
74
75 /**
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 */
81 function _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 */
98 function civicrm_api3_group_get($params) {
99
100 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'group', 'get');
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
108 $returnProperties['id'] = 1;
109 $returnProperties = array_keys($returnProperties);
110 }
111 if (CRM_Utils_Array::value('group_id', $inputParams)) {
112 $inputParams['id'] = $inputParams['group_id'];
113 }
114 $groupObjects = CRM_Contact_BAO_Group::getGroups($inputParams, $returnProperties, $sort, $offset, $rowCount);
115 if (empty($groupObjects)) {
116 return civicrm_api3_create_success(FALSE);
117 }
118 $groups = array();
119 foreach ($groupObjects as $group) {
120 _civicrm_api3_object_to_array($group, $groups[$group->id]);
121 _civicrm_api3_custom_data_get($groups[$group->id], 'Group', $group->id);
122 }
123
124
125 return civicrm_api3_create_success($groups, $params, 'group', 'create');
126 }
127
128 /**
129 * delete an existing group
130 *
131 * This method is used to delete any existing group. id of the group
132 * to be deleted is required field in $params array
133 *
134 * @param array $params (referance) array containing id of the group
135 * to be deleted
136 *
137 * @return array (referance) returns flag true if successfull, error
138 * message otherwise
139 *@example GroupDelete.php
140 *{@getfields group_delete}
141 *
142 * @access public
143 */
144 function civicrm_api3_group_delete($params) {
145
146 CRM_Contact_BAO_Group::discard($params['id']);
147 return civicrm_api3_create_success(TRUE);
148 }
149