Commit | Line | Data |
---|---|---|
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 | */ |
17 | ||
18 | /** | |
5a409b50 | 19 | * This class provides the functionality to delete a group of contacts. |
20 | * | |
21 | * This class provides functionality for the actual deletion. | |
6a488035 TO |
22 | */ |
23 | class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { | |
24 | ||
25 | /** | |
26 | * Are we operating in "single mode", i.e. sending email to one | |
27 | * specific contact? | |
28 | * | |
d51c6add | 29 | * @var bool |
6a488035 TO |
30 | */ |
31 | protected $_single = FALSE; | |
32 | ||
33 | /** | |
100fef9d | 34 | * Cache shared address message so we don't query twice |
69078420 | 35 | * @var string |
6a488035 TO |
36 | */ |
37 | protected $_sharedAddressMessage = NULL; | |
38 | ||
39 | /** | |
fe482240 | 40 | * Build all the data structures needed to build the form. |
6a488035 | 41 | */ |
00be9182 | 42 | public function preProcess() { |
6a488035 TO |
43 | |
44 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', | |
45 | $this, FALSE | |
46 | ); | |
47 | ||
48 | $this->_searchKey = CRM_Utils_Request::retrieve('key', 'String', $this); | |
49 | ||
50 | // sort out whether it’s a delete-to-trash, delete-into-oblivion or restore (and let the template know) | |
353ffa53 | 51 | $values = $this->controller->exportValues(); |
6a488035 | 52 | $this->_skipUndelete = (CRM_Core_Permission::check('access deleted contacts') and (CRM_Utils_Request::retrieve('skip_undelete', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::DELETE_PERMANENTLY)); |
353ffa53 | 53 | $this->_restore = (CRM_Utils_Request::retrieve('restore', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::RESTORE); |
6a488035 TO |
54 | |
55 | if ($this->_restore && !CRM_Core_Permission::check('access deleted contacts')) { | |
82fd4ec9 | 56 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this contact.')); |
6a488035 TO |
57 | } |
58 | elseif (!CRM_Core_Permission::check('delete contacts')) { | |
82fd4ec9 | 59 | CRM_Core_Error::statusBounce(ts('You do not have permission to delete this contact.')); |
6a488035 TO |
60 | } |
61 | ||
89595c92 | 62 | $this->assign('trash', Civi::settings()->get('contact_undelete') and !$this->_skipUndelete); |
6a488035 TO |
63 | $this->assign('restore', $this->_restore); |
64 | ||
65 | if ($this->_restore) { | |
66 | CRM_Utils_System::setTitle(ts('Restore Contact')); | |
67 | } | |
68 | ||
69 | if ($cid) { | |
70 | if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) { | |
2d296f18 | 71 | CRM_Core_Error::statusBounce(ts('You do not have permission to delete this contact. Note: you can delete contacts if you can edit them.')); |
0db6c3e1 TO |
72 | } |
73 | elseif (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { | |
2d296f18 | 74 | CRM_Core_Error::statusBounce(ts('This contact is a special one for the contact information associated with the CiviCRM installation for this domain. No one is allowed to delete it because the information is used for special system purposes.')); |
6a488035 TO |
75 | } |
76 | ||
be2fb01f | 77 | $this->_contactIds = [$cid]; |
6a488035 TO |
78 | $this->_single = TRUE; |
79 | $this->assign('totalSelectedContacts', 1); | |
80 | } | |
81 | else { | |
82 | parent::preProcess(); | |
83 | } | |
84 | ||
85 | $this->_sharedAddressMessage = $this->get('sharedAddressMessage'); | |
86 | if (!$this->_restore && !$this->_sharedAddressMessage) { | |
87 | // we check for each contact for shared contact address | |
be2fb01f | 88 | $sharedContactList = []; |
6a488035 TO |
89 | $sharedAddressCount = 0; |
90 | foreach ($this->_contactIds as $contactId) { | |
91 | // check if a contact that is being deleted has any shared addresses | |
92 | $sharedAddressMessage = CRM_Core_BAO_Address::setSharedAddressDeleteStatus(NULL, $contactId, TRUE); | |
93 | ||
94 | if ($sharedAddressMessage['count'] > 0) { | |
95 | $sharedAddressCount += $sharedAddressMessage['count']; | |
96 | $sharedContactList = array_merge($sharedContactList, | |
97 | $sharedAddressMessage['contactList'] | |
98 | ); | |
99 | } | |
100 | } | |
101 | ||
be2fb01f | 102 | $this->_sharedAddressMessage = [ |
6a488035 TO |
103 | 'count' => $sharedAddressCount, |
104 | 'contactList' => $sharedContactList, | |
be2fb01f | 105 | ]; |
6a488035 TO |
106 | |
107 | if ($sharedAddressCount > 0) { | |
108 | if (count($this->_contactIds) > 1) { | |
109 | // more than one contact deleted | |
be2fb01f | 110 | $message = ts('One of the selected contacts has an address record that is shared with 1 other contact.', [ |
69078420 SL |
111 | 'plural' => 'One or more selected contacts have address records which are shared with %count other contacts.', |
112 | 'count' => $sharedAddressCount, | |
113 | ]); | |
6a488035 TO |
114 | } |
115 | else { | |
116 | // only one contact deleted | |
be2fb01f | 117 | $message = ts('This contact has an address record which is shared with 1 other contact.', [ |
69078420 SL |
118 | 'plural' => 'This contact has an address record which is shared with %count other contacts.', |
119 | 'count' => $sharedAddressCount, | |
120 | ]); | |
6a488035 TO |
121 | } |
122 | CRM_Core_Session::setStatus($message . ' ' . ts('Shared addresses will not be removed or altered but will no longer be shared.'), ts('Shared Addesses Owner')); | |
123 | } | |
124 | ||
125 | // set in form controller so that queries are not fired again | |
126 | $this->set('sharedAddressMessage', $this->_sharedAddressMessage); | |
127 | } | |
128 | } | |
129 | ||
130 | /** | |
fe482240 | 131 | * Build the form object. |
6a488035 | 132 | */ |
00be9182 | 133 | public function buildQuickForm() { |
6a488035 TO |
134 | $label = $this->_restore ? ts('Restore Contact(s)') : ts('Delete Contact(s)'); |
135 | ||
136 | if ($this->_single) { | |
137 | // also fix the user context stack in case the user hits cancel | |
edc80cda | 138 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic'); |
6a488035 TO |
139 | if ($context == 'search' && CRM_Utils_Rule::qfKey($this->_searchKey)) { |
140 | $urlParams = "&context=$context&key=$this->_searchKey"; | |
141 | } | |
142 | else { | |
143 | $urlParams = ''; | |
144 | } | |
145 | ||
146 | $session = CRM_Core_Session::singleton(); | |
147 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
353ffa53 TO |
148 | 'reset=1&cid=' . $this->_contactIds[0] . $urlParams |
149 | )); | |
6a488035 TO |
150 | $this->addDefaultButtons($label, 'done', 'cancel'); |
151 | } | |
152 | else { | |
153 | $this->addDefaultButtons($label, 'done'); | |
154 | } | |
2efcf0c2 | 155 | |
be2fb01f | 156 | $this->addFormRule(['CRM_Contact_Form_Task_Delete', 'formRule'], $this); |
f182074e PN |
157 | } |
158 | ||
159 | /** | |
fe482240 | 160 | * Global form rule. |
f182074e | 161 | * |
77c5b619 TO |
162 | * @param array $fields |
163 | * The input form values. | |
164 | * @param array $files | |
165 | * The uploaded files if any. | |
166 | * @param object $self | |
167 | * Form object. | |
f182074e | 168 | * |
72b3a70c CW |
169 | * @return bool|array |
170 | * true if no errors, else array of errors | |
f182074e | 171 | */ |
00be9182 | 172 | public static function formRule($fields, $files, $self) { |
f182074e | 173 | // CRM-12929 |
be2fb01f | 174 | $error = []; |
f182074e PN |
175 | if ($self->_skipUndelete) { |
176 | CRM_Financial_BAO_FinancialItem::checkContactPresent($self->_contactIds, $error); | |
177 | } | |
178 | return $error; | |
6a488035 TO |
179 | } |
180 | ||
181 | /** | |
fe482240 | 182 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
183 | */ |
184 | public function postProcess() { | |
82fd4ec9 | 185 | if ($this->_restore) { |
186 | $this->doRestore(); | |
187 | return; | |
188 | } | |
6a488035 TO |
189 | $session = CRM_Core_Session::singleton(); |
190 | $currentUserId = $session->get('userID'); | |
191 | ||
82fd4ec9 | 192 | // Delete Contacts. Report errors. |
6a488035 | 193 | $deleted = 0; |
be2fb01f | 194 | $not_deleted = []; |
6a488035 TO |
195 | foreach ($this->_contactIds as $cid) { |
196 | $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); | |
197 | if (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { | |
be2fb01f | 198 | $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", [1 => $name]), 'Cannot Delete Domain Contact', 'error'); |
6a488035 TO |
199 | continue; |
200 | } | |
82fd4ec9 | 201 | if ($currentUserId == $cid) { |
be2fb01f | 202 | $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", [1 => $name]), 'Unable To Delete', 'error'); |
6a488035 TO |
203 | continue; |
204 | } | |
82fd4ec9 | 205 | if (CRM_Contact_BAO_Contact::deleteContact($cid, FALSE, $this->_skipUndelete)) { |
6a488035 TO |
206 | $deleted++; |
207 | } | |
208 | else { | |
209 | $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$cid"); | |
210 | $not_deleted[$cid] = "<a href='$url'>$name</a>"; | |
211 | } | |
212 | } | |
213 | if ($deleted) { | |
214 | $title = ts('Deleted'); | |
82fd4ec9 | 215 | if ($this->_skipUndelete) { |
be2fb01f | 216 | $status = ts('%1 has been permanently deleted.', [ |
69078420 SL |
217 | 1 => $name, |
218 | 'plural' => '%count contacts permanently deleted.', | |
219 | 'count' => $deleted, | |
220 | ]); | |
6a488035 TO |
221 | } |
222 | else { | |
be2fb01f | 223 | $status = ts('%1 has been moved to the trash.', [ |
69078420 SL |
224 | 1 => $name, |
225 | 'plural' => '%count contacts moved to trash.', | |
226 | 'count' => $deleted, | |
227 | ]); | |
6a488035 TO |
228 | } |
229 | $session->setStatus($status, $title, 'success'); | |
230 | } | |
231 | // Alert user of any failures | |
232 | if ($not_deleted) { | |
233 | $status = ts('The contact might be the Membership Organization of a Membership Type. You will need to edit the Membership Type and change the Membership Organization before you can delete this contact.'); | |
234 | $title = ts('Unable to Delete'); | |
235 | $session->setStatus('<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>' . $status, $title, 'error'); | |
236 | } | |
237 | ||
82fd4ec9 | 238 | if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0) { |
6a488035 TO |
239 | if (count($this->_sharedAddressMessage['contactList']) == 1) { |
240 | $message = ts('The following contact had been sharing an address with a contact you just deleted. Their address will no longer be shared, but has not been removed or altered.'); | |
241 | } | |
242 | else { | |
243 | $message = ts('The following contacts had been sharing addresses with a contact you just deleted. Their addressses will no longer be shared, but have not been removed or altered.'); | |
244 | } | |
245 | $message .= '<ul><li>' . implode('</li><li>', $this->_sharedAddressMessage['contactList']) . '</li></ul>'; | |
246 | ||
be2fb01f | 247 | $session->setStatus($message, ts('Shared Addesses Owner Deleted'), 'info', ['expires' => 0]); |
6a488035 TO |
248 | |
249 | $this->set('sharedAddressMessage', NULL); | |
250 | } | |
251 | ||
82fd4ec9 | 252 | $this->setRedirection(); |
253 | } | |
254 | ||
255 | /** | |
256 | * Set the url for the contact to be redirected to. | |
257 | */ | |
258 | protected function setRedirection() { | |
259 | ||
260 | $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic'); | |
261 | $urlParams = 'force=1'; | |
262 | $urlString = "civicrm/contact/search/$context"; | |
263 | ||
264 | if (CRM_Utils_Rule::qfKey($this->_searchKey)) { | |
265 | $urlParams .= "&qfKey=$this->_searchKey"; | |
266 | } | |
267 | elseif ($context === 'search') { | |
268 | $urlParams .= "&qfKey={$this->controller->_key}"; | |
269 | $urlString = 'civicrm/contact/search'; | |
270 | } | |
271 | elseif ($context === 'smog') { | |
272 | $urlParams .= "&qfKey={$this->controller->_key}&context=smog"; | |
273 | $urlString = 'civicrm/group/search'; | |
274 | } | |
275 | else { | |
276 | $urlParams = 'reset=1'; | |
277 | $urlString = 'civicrm/dashboard'; | |
278 | } | |
6a488035 | 279 | if ($this->_single && empty($this->_skipUndelete)) { |
82fd4ec9 | 280 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}")); |
6a488035 TO |
281 | } |
282 | else { | |
82fd4ec9 | 283 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams)); |
284 | } | |
285 | } | |
286 | ||
287 | /** | |
288 | * Restore the selected contact/s from the trash. | |
289 | * | |
290 | * @throws \API_Exception | |
291 | * @throws \Civi\API\Exception\UnauthorizedException | |
292 | */ | |
293 | protected function doRestore() { | |
294 | $name = ''; | |
295 | if (count($this->_contactIds) === 1) { | |
296 | $name = Civi\Api4\Contact::get()->addWhere('id', 'IN', $this->_contactIds)->setSelect(['display_name'])->execute()->first()['display_name']; | |
6a488035 | 297 | } |
82fd4ec9 | 298 | Civi\Api4\Contact::update()->addWhere('id', 'IN', $this->_contactIds)->setValues(['is_deleted' => 0])->execute(); |
299 | $title = ts('Restored'); | |
300 | $status = ts('%1 has been restored from the trash.', [ | |
301 | 1 => $name, | |
302 | 'plural' => '%count contacts restored from trash.', | |
303 | 'count' => count($this->_contactIds), | |
304 | ]); | |
305 | CRM_Core_Session::setStatus($status, $title, 'success'); | |
306 | $this->setRedirection(); | |
6a488035 | 307 | } |
96025800 | 308 | |
6a488035 | 309 | } |