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