Merge pull request #227 from deepak-srivastava/crm
[civicrm-core.git] / api / v2 / GroupOrganization.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 APIv2 group contact functions
32 *
33 * @package CiviCRM_APIv2
34 * @subpackage API_Group
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: GroupContact.php 21624 2009-06-04 22:02:55Z mover $
38 *
39 */
40
41 /**
42 * Include utility functions
43 */
44 require_once 'api/v2/utils.php';
45
46 /**
47 * This API will give list of the groups for particular contact
48 * Particualr status can be sent in params array
49 * If no status mentioned in params, by default 'added' will be used
50 * to fetch the records
51 *
52 * @param array $params name value pair of contact information
53 *
54 * @return array list of groups, given contact subsribed to
55 */
56 function civicrm_group_organization_get(&$params) {
57 _civicrm_initialize();
58
59 if (!is_array($params)) {
60 return civicrm_create_error(ts('Input parameter is not an array'));
61 }
62
63 if (empty($params)) {
64 return civicrm_create_error('No input parameter present');
65 }
66
67 if (!array_key_exists('organization_id', $params) &&
68 !array_key_exists('group_id', $params)
69 ) {
70 return civicrm_create_error(ts('at least one of organization_id or group_id is a required field'));
71 }
72
73 require_once 'CRM/Contact/DAO/GroupOrganization.php';
74 $dao = new CRM_Contact_DAO_GroupOrganization();
75 if (array_key_exists('organization_id', $params)) {
76 $dao->organization_id = $params['organization_id'];
77 }
78 if (array_key_exists('group_id', $params)) {
79 $dao->group_id = $params['group_id'];
80 }
81 $dao->find();
82 $values = array();
83 _civicrm_object_to_array($dao, $values);
84 return civicrm_create_success($values);
85 }
86
87 /**
88 *
89 * @param <type> $params
90 *
91 * @return <type>
92 */
93 function civicrm_group_organization_create(&$params) {
94
95 if (!is_array($params)) {
96 return civicrm_create_error(ts('Input parameter is not an array'));
97 }
98
99 if (empty($params)) {
100 return civicrm_create_error('No input parameter present');
101 }
102
103 if (!array_key_exists('organization_id', $params) ||
104 !array_key_exists('group_id', $params)
105 ) {
106 return civicrm_create_error(ts('organization_id and group_id are required field'));
107 }
108
109 require_once 'CRM/Contact/BAO/GroupOrganization.php';
110 $groupOrgBAO = CRM_Contact_BAO_GroupOrganization::add($params);
111 if (is_a($groupOrgBAO, 'CRM_Core_Error') || is_null($groupOrgBAO)) {
112 return civicrm_create_error("Group Organization can not be created");
113 }
114 _civicrm_object_to_array($groupOrgBAO, $values);
115 return civicrm_create_success($values);
116 }
117
118 /**
119 * Deletes an existing Group Organization
120 *
121 * This API is used for deleting a Group Organization
122 *
123 * @param Array $params ID of the Group Organization to be deleted
124 *
125 * @return null if successfull, array with is_error = 1 otherwise
126 * @access public
127 */
128 function civicrm_group_organization_remove(&$params) {
129 _civicrm_initialize();
130
131 if (!is_array($params)) {
132 $error = civicrm_create_error('Input parameter is not an array');
133 return $error;
134 }
135
136 if (empty($params)) {
137 return civicrm_create_error('No input parameter present');
138 }
139
140 if (!CRM_Utils_Array::value('id', $params)) {
141 $error = civicrm_create_error('Invalid or no value for Group Organization ID');
142 return $error;
143 }
144 require_once 'CRM/Contact/BAO/GroupOrganization.php';
145 $result = CRM_Contact_BAO_GroupOrganization::delete($params['id']);
146 return $result ? civicrm_create_success(ts('Deleted Group Organization successfully')) : civicrm_create_error(ts('Could not delete Group Organization'));
147 }
148