Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Contact / Page / View / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Contact_Page_View_GroupContact extends CRM_Core_Page {
36
37 /**
38 * This function is called when action is browse
39 *
40 * return null
41 * @access public
42 */
43 function browse() {
44
45 $count = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, NULL, NULL, TRUE);
46
47 $in = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Added');
48 $pending = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Pending');
49 $out = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Removed');
50
51 // keep track of all 'added' contact groups so we can remove them from the smart group
52 // section
53 $staticGroups = array();
54 if (!empty($in)) {
55 foreach ($in as $group) {
56 $staticGroups[$group['group_id']] = 1;
57 }
58 }
59
60 $this->assign('groupCount', $count);
61 $this->assign_by_ref('groupIn', $in);
62 $this->assign_by_ref('groupPending', $pending);
63 $this->assign_by_ref('groupOut', $out);
64
65 $allGroup = CRM_Contact_BAO_GroupContactCache::contactGroup($this->_contactId);
66 $this->assign('groupSmart' , null);
67 $this->assign('groupParent', null);
68
69 if (!empty($allGroup)) {
70 $smart = $parent = array( );
71 foreach ($allGroup['group'] as $group) {
72 // delete all smart groups which are also in static groups
73 if (isset($staticGroups[$group['id']])) {
74 continue;
75 }
76 if (empty($group['children'])) {
77 $smart[] = $group;
78 }
79 else {
80 $parent[] = $group;
81 }
82 }
83
84 if (!empty($smart)) {
85 $this->assign_by_ref('groupSmart', $smart);
86 }
87 if (!empty($parent)) {
88 $this->assign_by_ref('groupParent', $parent);
89 }
90 }
91 }
92
93 /**
94 * This function is called when action is update
95 *
96 * @param int $groupID group id
97 *
98 * return null
99 * @access public
100 */
101 function edit($groupId = NULL) {
102 $controller = new CRM_Core_Controller_Simple(
103 'CRM_Contact_Form_GroupContact',
104 ts('Contact\'s Groups'),
105 $this->_action
106 );
107 $controller->setEmbedded(TRUE);
108
109 // set the userContext stack
110 $session = CRM_Core_Session::singleton();
111
112 $session->pushUserContext(
113 CRM_Utils_System::url(
114 'civicrm/contact/view',
115 "action=browse&selectedChild=group&cid={$this->_contactId}"
116 ),
117 FALSE
118 );
119 $controller->reset();
120
121 $controller->set('contactId', $this->_contactId);
122 $controller->set('groupId', $groupId);
123
124 $controller->process();
125 $controller->run();
126 }
127
128 function preProcess() {
129 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
130 $this->assign('contactId', $this->_contactId);
131
132 // check logged in url permission
133 CRM_Contact_Page_View::checkUserPermission($this);
134
135 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
136 $this->assign('action', $this->_action);
137 }
138
139 /**
140 * This function is the main function that is called
141 * when the page loads, it decides the which action has
142 * to be taken for the page.
143 *
144 * return null
145 * @access public
146 */
147 function run() {
148 $this->preProcess();
149
150 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
151 $this->assign('displayName', $displayName);
152
153 if ($this->_action == CRM_Core_Action::DELETE) {
154 $groupContactId = CRM_Utils_Request::retrieve('gcid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
155 $status = CRM_Utils_Request::retrieve('st', 'String', CRM_Core_DAO::$_nullObject, TRUE);
156 if (is_numeric($groupContactId) && $status) {
157 $this->del($groupContactId, $status, $this->_contactId);
158 }
159 $session = CRM_Core_Session::singleton();
160 CRM_Utils_System::redirect($session->popUserContext());
161 }
162
163 $this->edit(NULL, CRM_Core_Action::ADD);
164 $this->browse();
165 return parent::run();
166 }
167
168 /**
169 * function to remove/ rejoin the group
170 *
171 * @param int $groupContactId id of crm_group_contact
172 * @param string $status this is the status that should be updated.
173 *
174 * $access public
175 */
176 function del($groupContactId, $status, $contactID) {
177 $groupId = CRM_Contact_BAO_GroupContact::getGroupId($groupContactId);
178
179 switch ($status) {
180 case 'i':
181 $groupStatus = 'Added';
182 break;
183
184 case 'p':
185 $groupStatus = 'Pending';
186 break;
187
188 case 'o':
189 $groupStatus = 'Removed';
190 break;
191
192 case 'd':
193 $groupStatus = 'Deleted';
194 break;
195 }
196
197 $groupNum = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Added',
198 NULL, TRUE, TRUE
199 );
200 if ($groupNum == 1 &&
201 $groupStatus == 'Removed' &&
202 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
203 'is_enabled'
204 )
205 ) {
206 CRM_Core_Session::setStatus(ts('Please ensure at least one contact group association is maintained.'), ts('Could Not Remove'));
207 return FALSE;
208 }
209
210 $ids = array($contactID);
211 $method = 'Admin';
212
213 $session = CRM_Core_Session::singleton();
214 $userID = $session->get('userID');
215
216 if ($userID == $contactID) {
217 $method = 'Web';
218 }
219
220 CRM_Contact_BAO_GroupContact::removeContactsFromGroup($ids, $groupId, $method, $groupStatus);
221 }
222 }
223