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