Merge in 5.48
[civicrm-core.git] / CRM / Contact / Page / View / GroupContact.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_Page_View_GroupContact extends CRM_Core_Page {
18
19 /**
95cdcc0f 20 * Called when action is browse.
6a488035 21 */
00be9182 22 public function browse() {
6a488035 23
b120248c 24 $count = CRM_Contact_BAO_GroupContact::getContactGroup($this->_contactId, NULL, NULL, TRUE, FALSE, FALSE, TRUE, NULL, TRUE);
6a488035 25
b120248c
AJ
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);
6a488035
TO
29
30 // keep track of all 'added' contact groups so we can remove them from the smart group
31 // section
be2fb01f 32 $staticGroups = [];
6a488035
TO
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
559865a0 44 // get the info on contact smart groups
aaffa79f 45 $contactSmartGroupSettings = Civi::settings()->get('contact_smart_group_display');
559865a0 46 $this->assign('contactSmartGroupSettings', $contactSmartGroupSettings);
4e8065a9
CW
47
48 $this->ajaxResponse['tabCount'] = count($in);
6a488035
TO
49 }
50
51 /**
fe482240 52 * called when action is update.
6a488035 53 *
c490a46a 54 * @param int $groupId
6a488035 55 *
6a488035 56 */
00be9182 57 public function edit($groupId = NULL) {
6a488035
TO
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
00be9182 84 public function preProcess() {
6a488035
TO
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 /**
dc195289 96 * the main function that is called
6a488035
TO
97 * when the page loads, it decides the which action has
98 * to be taken for the page.
99 *
76e7a76c 100 * @return null
6a488035 101 */
00be9182 102 public function run() {
6a488035
TO
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 /**
100fef9d 124 * Remove/ rejoin the group
6a488035 125 *
77c5b619
TO
126 * @param int $groupContactId
127 * Id of crm_group_contact.
128 * @param string $status
129 * This is the status that should be updated.
6a488035
TO
130 *
131 * $access public
100fef9d 132 * @param int $contactID
77b97be7
EM
133 *
134 * @return bool
6a488035 135 */
00be9182 136 public static function del($groupContactId, $status, $contactID) {
6a488035
TO
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
317fceb4 157 $groupNum = CRM_Contact_BAO_GroupContact::getContactGroup($contactID, 'Added', NULL, TRUE, TRUE);
aaffa79f 158 if ($groupNum == 1 && $groupStatus == 'Removed' && Civi::settings()->get('is_enabled')) {
6a488035
TO
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
be2fb01f 163 $ids = [$contactID];
6a488035
TO
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 }
96025800 175
6a488035 176}