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