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