Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Contact / Page / View / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_Page_View_GroupContact extends CRM_Core_Page {
18
19 /**
20 * Called when action is browse.
21 */
22 public function browse() {
23
24 $count = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, NULL, NULL, TRUE, FALSE, FALSE, TRUE, NULL, TRUE);
25
26 $in = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Added', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
27 $pending = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Pending', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
28 $out = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, 'Removed', NULL, FALSE, FALSE, FALSE, TRUE, NULL, TRUE);
29
30 // keep track of all 'added' contact groups so we can remove them from the smart group
31 // section
32 $staticGroups = [];
33 if (!empty($in)) {
34 foreach ($in as $group) {
35 $staticGroups[$group['group_id']] = 1;
36 }
37 }
38
39 $this->assign('groupCount', $count);
40 $this->assign_by_ref('groupIn', $in);
41 $this->assign_by_ref('groupPending', $pending);
42 $this->assign_by_ref('groupOut', $out);
43
44 // get the info on contact smart groups
45 $contactSmartGroupSettings = Civi::settings()->get('contact_smart_group_display');
46 $this->assign('contactSmartGroupSettings', $contactSmartGroupSettings);
47
48 $this->ajaxResponse['tabCount'] = count($in);
49 }
50
51 /**
52 * called when action is update.
53 *
54 * @param int $groupId
55 *
56 */
57 public function edit($groupId = NULL) {
58 $controller = new CRM_Core_Controller_Simple(
59 'CRM_Contact_Form_GroupContact',
60 ts('Contact\'s Groups'),
61 $this->_action
62 );
63 $controller->setEmbedded(TRUE);
64
65 // set the userContext stack
66 $session = CRM_Core_Session::singleton();
67
68 $session->pushUserContext(
69 CRM_Utils_System::url(
70 'civicrm/contact/view',
71 "action=browse&selectedChild=group&cid={$this->_contactId}"
72 ),
73 FALSE
74 );
75 $controller->reset();
76
77 $controller->set('contactId', $this->_contactId);
78 $controller->set('groupId', $groupId);
79
80 $controller->process();
81 $controller->run();
82 }
83
84 public function preProcess() {
85 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
86 $this->assign('contactId', $this->_contactId);
87
88 // check logged in url permission
89 CRM_Contact_Page_View::checkUserPermission($this);
90
91 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
92 $this->assign('action', $this->_action);
93 }
94
95 /**
96 * the main function that is called
97 * when the page loads, it decides the which action has
98 * to be taken for the page.
99 *
100 * @return null
101 */
102 public function run() {
103 $this->preProcess();
104
105 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
106 $this->assign('displayName', $displayName);
107
108 if ($this->_action == CRM_Core_Action::DELETE) {
109 $groupContactId = CRM_Utils_Request::retrieve('gcid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
110 $status = CRM_Utils_Request::retrieve('st', 'String', CRM_Core_DAO::$_nullObject, TRUE);
111 if (is_numeric($groupContactId) && $status) {
112 $this->del($groupContactId, $status, $this->_contactId);
113 }
114 $session = CRM_Core_Session::singleton();
115 CRM_Utils_System::redirect($session->popUserContext());
116 }
117
118 $this->edit(NULL, CRM_Core_Action::ADD);
119 $this->browse();
120 return parent::run();
121 }
122
123 /**
124 * Remove/ rejoin the group
125 *
126 * @param int $groupContactId
127 * Id of crm_group_contact.
128 * @param string $status
129 * This is the status that should be updated.
130 *
131 * $access public
132 * @param int $contactID
133 *
134 * @return bool
135 */
136 public static function del($groupContactId, $status, $contactID) {
137 $groupId = CRM_Contact_BAO_GroupContact::getGroupId($groupContactId);
138
139 switch ($status) {
140 case 'i':
141 $groupStatus = 'Added';
142 break;
143
144 case 'p':
145 $groupStatus = 'Pending';
146 break;
147
148 case 'o':
149 $groupStatus = 'Removed';
150 break;
151
152 case 'd':
153 $groupStatus = 'Deleted';
154 break;
155 }
156
157 $groupNum = CRM_Contact_BAO_GroupContact::getContactGroup($contactID, 'Added', NULL, TRUE, TRUE);
158 if ($groupNum == 1 && $groupStatus == 'Removed' && Civi::settings()->get('is_enabled')) {
159 CRM_Core_Session::setStatus(ts('Please ensure at least one contact group association is maintained.'), ts('Could Not Remove'));
160 return FALSE;
161 }
162
163 $ids = [$contactID];
164 $method = 'Admin';
165
166 $session = CRM_Core_Session::singleton();
167 $userID = $session->get('userID');
168
169 if ($userID == $contactID) {
170 $method = 'Web';
171 }
172
173 CRM_Contact_BAO_GroupContact::removeContactsFromGroup($ids, $groupId, $method, $groupStatus);
174 }
175
176 }