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