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