Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-24-15-59-25
[civicrm-core.git] / CRM / Contact / Page / View / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 // get the info on contact smart groups
66 $contactSmartGroupSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
67 'contact_smart_group_display');
68 $this->assign('contactSmartGroupSettings', $contactSmartGroupSettings);
69
70 $this->ajaxResponse['tabCount'] = count($in);
71 }
72
73 /**
74 * This function is called when action is update
75 *
76 * @param null $groupId
77 *
78 * @internal param int $groupID group id
79 *
80 * return null
81 * @access public
82 */
83 function edit($groupId = NULL) {
84 $controller = new CRM_Core_Controller_Simple(
85 'CRM_Contact_Form_GroupContact',
86 ts('Contact\'s Groups'),
87 $this->_action
88 );
89 $controller->setEmbedded(TRUE);
90
91 // set the userContext stack
92 $session = CRM_Core_Session::singleton();
93
94 $session->pushUserContext(
95 CRM_Utils_System::url(
96 'civicrm/contact/view',
97 "action=browse&selectedChild=group&cid={$this->_contactId}"
98 ),
99 FALSE
100 );
101 $controller->reset();
102
103 $controller->set('contactId', $this->_contactId);
104 $controller->set('groupId', $groupId);
105
106 $controller->process();
107 $controller->run();
108 }
109
110 function preProcess() {
111 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
112 $this->assign('contactId', $this->_contactId);
113
114 // check logged in url permission
115 CRM_Contact_Page_View::checkUserPermission($this);
116
117 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
118 $this->assign('action', $this->_action);
119 }
120
121 /**
122 * This function is the main function that is called
123 * when the page loads, it decides the which action has
124 * to be taken for the page.
125 *
126 * return null
127 * @access public
128 */
129 function run() {
130 $this->preProcess();
131
132 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
133 $this->assign('displayName', $displayName);
134
135 if ($this->_action == CRM_Core_Action::DELETE) {
136 $groupContactId = CRM_Utils_Request::retrieve('gcid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
137 $status = CRM_Utils_Request::retrieve('st', 'String', CRM_Core_DAO::$_nullObject, TRUE);
138 if (is_numeric($groupContactId) && $status) {
139 $this->del($groupContactId, $status, $this->_contactId);
140 }
141 $session = CRM_Core_Session::singleton();
142 CRM_Utils_System::redirect($session->popUserContext());
143 }
144
145 $this->edit(NULL, CRM_Core_Action::ADD);
146 $this->browse();
147 return parent::run();
148 }
149
150 /**
151 * function to remove/ rejoin the group
152 *
153 * @param int $groupContactId id of crm_group_contact
154 * @param string $status this is the status that should be updated.
155 *
156 * $access public
157 * @param $contactID
158 *
159 * @return bool
160 */
161 static function del($groupContactId, $status, $contactID) {
162 $groupId = CRM_Contact_BAO_GroupContact::getGroupId($groupContactId);
163
164 switch ($status) {
165 case 'i':
166 $groupStatus = 'Added';
167 break;
168
169 case 'p':
170 $groupStatus = 'Pending';
171 break;
172
173 case 'o':
174 $groupStatus = 'Removed';
175 break;
176
177 case 'd':
178 $groupStatus = 'Deleted';
179 break;
180 }
181
182 $groupNum =
183 CRM_Contact_BAO_GroupContact::getContactGroup($contactID, 'Added', NULL, TRUE, TRUE);
184 if ($groupNum == 1 &&
185 $groupStatus == 'Removed' &&
186 CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME,
187 'is_enabled'
188 )
189 ) {
190 CRM_Core_Session::setStatus(ts('Please ensure at least one contact group association is maintained.'), ts('Could Not Remove'));
191 return FALSE;
192 }
193
194 $ids = array($contactID);
195 $method = 'Admin';
196
197 $session = CRM_Core_Session::singleton();
198 $userID = $session->get('userID');
199
200 if ($userID == $contactID) {
201 $method = 'Web';
202 }
203
204 CRM_Contact_BAO_GroupContact::removeContactsFromGroup($ids, $groupId, $method, $groupStatus);
205 }
206 }
207