CRM-19435 Fix
[civicrm-core.git] / CRM / Contact / Page / View / GroupContact.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33class CRM_Contact_Page_View_GroupContact extends CRM_Core_Page {
34
35 /**
95cdcc0f 36 * Called when action is browse.
6a488035 37 */
00be9182 38 public function browse() {
6a488035 39
b120248c 40 $count = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, NULL, NULL, TRUE, FALSE, FALSE, TRUE, NULL, TRUE);
6a488035 41
b120248c
AJ
42 $in = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Added', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
43 $pending = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Pending', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
44 $out = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Removed', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
6a488035
TO
45
46 // keep track of all 'added' contact groups so we can remove them from the smart group
47 // section
48 $staticGroups = array();
49 if (!empty($in)) {
50 foreach ($in as $group) {
51 $staticGroups[$group['group_id']] = 1;
52 }
53 }
54
55 $this->assign('groupCount', $count);
56 $this->assign_by_ref('groupIn', $in);
57 $this->assign_by_ref('groupPending', $pending);
58 $this->assign_by_ref('groupOut', $out);
59
559865a0 60 // get the info on contact smart groups
aaffa79f 61 $contactSmartGroupSettings = Civi::settings()->get('contact_smart_group_display');
559865a0 62 $this->assign('contactSmartGroupSettings', $contactSmartGroupSettings);
4e8065a9
CW
63
64 $this->ajaxResponse['tabCount'] = count($in);
6a488035
TO
65 }
66
67 /**
fe482240 68 * called when action is update.
6a488035 69 *
c490a46a 70 * @param int $groupId
6a488035 71 *
6a488035 72 */
00be9182 73 public function edit($groupId = NULL) {
6a488035
TO
74 $controller = new CRM_Core_Controller_Simple(
75 'CRM_Contact_Form_GroupContact',
76 ts('Contact\'s Groups'),
77 $this->_action
78 );
79 $controller->setEmbedded(TRUE);
80
81 // set the userContext stack
82 $session = CRM_Core_Session::singleton();
83
84 $session->pushUserContext(
85 CRM_Utils_System::url(
86 'civicrm/contact/view',
87 "action=browse&selectedChild=group&cid={$this->_contactId}"
88 ),
89 FALSE
90 );
91 $controller->reset();
92
93 $controller->set('contactId', $this->_contactId);
94 $controller->set('groupId', $groupId);
95
96 $controller->process();
97 $controller->run();
98 }
99
00be9182 100 public function preProcess() {
6a488035
TO
101 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
102 $this->assign('contactId', $this->_contactId);
103
104 // check logged in url permission
105 CRM_Contact_Page_View::checkUserPermission($this);
106
107 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
108 $this->assign('action', $this->_action);
109 }
110
111 /**
dc195289 112 * the main function that is called
6a488035
TO
113 * when the page loads, it decides the which action has
114 * to be taken for the page.
115 *
76e7a76c 116 * @return null
6a488035 117 */
00be9182 118 public function run() {
6a488035
TO
119 $this->preProcess();
120
121 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
122 $this->assign('displayName', $displayName);
123
124 if ($this->_action == CRM_Core_Action::DELETE) {
125 $groupContactId = CRM_Utils_Request::retrieve('gcid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
126 $status = CRM_Utils_Request::retrieve('st', 'String', CRM_Core_DAO::$_nullObject, TRUE);
127 if (is_numeric($groupContactId) && $status) {
128 $this->del($groupContactId, $status, $this->_contactId);
129 }
130 $session = CRM_Core_Session::singleton();
131 CRM_Utils_System::redirect($session->popUserContext());
132 }
133
134 $this->edit(NULL, CRM_Core_Action::ADD);
135 $this->browse();
136 return parent::run();
137 }
138
139 /**
100fef9d 140 * Remove/ rejoin the group
6a488035 141 *
77c5b619
TO
142 * @param int $groupContactId
143 * Id of crm_group_contact.
144 * @param string $status
145 * This is the status that should be updated.
6a488035
TO
146 *
147 * $access public
100fef9d 148 * @param int $contactID
77b97be7
EM
149 *
150 * @return bool
6a488035 151 */
00be9182 152 public static function del($groupContactId, $status, $contactID) {
6a488035
TO
153 $groupId = CRM_Contact_BAO_GroupContact::getGroupId($groupContactId);
154
155 switch ($status) {
156 case 'i':
157 $groupStatus = 'Added';
158 break;
159
160 case 'p':
161 $groupStatus = 'Pending';
162 break;
163
164 case 'o':
165 $groupStatus = 'Removed';
166 break;
167
168 case 'd':
169 $groupStatus = 'Deleted';
170 break;
171 }
172
317fceb4 173 $groupNum = CRM_Contact_BAO_GroupContact::getContactGroup($contactID, 'Added', NULL, TRUE, TRUE);
aaffa79f 174 if ($groupNum == 1 && $groupStatus == 'Removed' && Civi::settings()->get('is_enabled')) {
6a488035
TO
175 CRM_Core_Session::setStatus(ts('Please ensure at least one contact group association is maintained.'), ts('Could Not Remove'));
176 return FALSE;
177 }
178
179 $ids = array($contactID);
180 $method = 'Admin';
181
182 $session = CRM_Core_Session::singleton();
183 $userID = $session->get('userID');
184
185 if ($userID == $contactID) {
186 $method = 'Web';
187 }
188
189 CRM_Contact_BAO_GroupContact::removeContactsFromGroup($ids, $groupId, $method, $groupStatus);
190 }
96025800 191
6a488035 192}