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