Merge pull request #227 from deepak-srivastava/crm
[civicrm-core.git] / api / v2 / Group.php
CommitLineData
6a488035
TO
1<?php
2// $Id: Group.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5/*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29*/
30
31/**
32 * File for the CiviCRM APIv2 group functions
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_Group
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Group.php 45502 2013-02-08 13:32:55Z kurund $
38 */
39
40/**
41 * Include utility functions
42 */
43require_once 'CRM/Contact/BAO/Group.php';
44require_once 'api/v2/utils.php';
45
46/**
47 * create/update group
48 *
49 * This API is used to create new group or update any of the existing
50 * In case of updating existing group, id of that particular grop must
51 * be in $params array. Either id or name is required field in the
52 * $params array
53 *
54 * @param array $params (referance) Associative array of property
55 * name/value pairs to insert in new 'group'
56 *
57 * @return array returns id of the group created if success,
58 * error message otherwise
59 *
60 * @access public
61 */
62function civicrm_group_add(&$params) {
63 _civicrm_initialize();
64 if (is_null($params) || !is_array($params) || empty($params)) {
65 return civicrm_create_error('Required parameter missing');
66 }
67
68 if (!CRM_Utils_Array::value('title', $params)) {
69 return civicrm_create_error('Required parameter title missing');
70 }
71
72 $group = CRM_Contact_BAO_Group::create($params);
73
74 if (is_null($group)) {
75 return civicrm_create_error('Group not created');
76 }
77 else {
78 return civicrm_create_success($group);
79 }
80}
81/*
82 * Wrapper for civicrm_group_add so function can take new (v3) name
83 */
84function civicrm_group_create(&$params) {
85 $result = civicrm_group_add($params);
86 return $result;
87}
88
89/**
90 * Returns array of groups matching a set of one or more group properties
91 *
92 * @param array $params (referance) Array of one or more valid
93 * property_name=>value pairs. If $params is set
94 * as null, all groups will be returned
95 *
96 * @return array (referance) Array of matching groups
97 * @access public
98 */
99function civicrm_group_get(&$params) {
100 _civicrm_initialize();
101 if (!is_null($params) && !is_array($params)) {
102 return civicrm_create_error('Params should be array');
103 }
104
105 $returnProperties = array();
106 foreach ($params as $n => $v) {
107 if (substr($n, 0, 7) == 'return.') {
108 $returnProperties[] = substr($n, 7);
109 }
110 }
111
112 if (!empty($returnProperties)) {
113 $returnProperties[] = 'id';
114 }
115
116 $groupObjects = CRM_Contact_BAO_Group::getGroups($params, $returnProperties);
117
118 if (count($groupObjects) == 0) {
119 return civicrm_create_error('No such group exists');
120 }
121
122 $groups = array();
123 foreach ($groupObjects as $group) {
124 _civicrm_object_to_array($group, $groups[$group->id]);
125 }
126
127 return $groups;
128}
129
130/**
131 * delete an existing group
132 *
133 * This method is used to delete any existing group. id of the group
134 * to be deleted is required field in $params array
135 *
136 * @param array $params (referance) array containing id of the group
137 * to be deleted
138 *
139 * @return array (referance) returns flag true if successfull, error
140 * message otherwise
141 *
142 * @access public
143 */
144function civicrm_group_delete(&$params) {
145 _civicrm_initialize();
146 if (is_null($params) || !is_array($params) || !CRM_Utils_Array::value('id', $params)) {
147 return civicrm_create_error('Required parameter missing');
148 }
149
150 CRM_Contact_BAO_Group::discard($params['id']);
151 return civicrm_create_success(TRUE);
152}
153