dev/core#560 Replace various CRM_Core_Error::fatal with statusBounces and Exceptions
[civicrm-core.git] / CRM / Contact / Form / Task / Delete.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
18 /**
19 * This class provides the functionality to delete a group of contacts.
20 *
21 * This class provides functionality for the actual deletion.
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 *
29 * @var bool
30 */
31 protected $_single = FALSE;
32
33 /**
34 * Cache shared address message so we don't query twice
35 * @var string
36 */
37 protected $_sharedAddressMessage = NULL;
38
39 /**
40 * Build all the data structures needed to build the form.
41 */
42 public function preProcess() {
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)
51 $values = $this->controller->exportValues();
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));
53 $this->_restore = (CRM_Utils_Request::retrieve('restore', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::RESTORE);
54
55 if ($this->_restore && !CRM_Core_Permission::check('access deleted contacts')) {
56 CRM_Core_Error::statusBounce(ts('You do not have permission to access this contact.'));
57 }
58 elseif (!CRM_Core_Permission::check('delete contacts')) {
59 CRM_Core_Error::statusBounce(ts('You do not have permission to delete this contact.'));
60 }
61
62 $this->assign('trash', Civi::settings()->get('contact_undelete') and !$this->_skipUndelete);
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)) {
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.'));
72 }
73 elseif (CRM_Contact_BAO_Contact::checkDomainContact($cid)) {
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.'));
75 }
76
77 $this->_contactIds = [$cid];
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
88 $sharedContactList = [];
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
102 $this->_sharedAddressMessage = [
103 'count' => $sharedAddressCount,
104 'contactList' => $sharedContactList,
105 ];
106
107 if ($sharedAddressCount > 0) {
108 if (count($this->_contactIds) > 1) {
109 // more than one contact deleted
110 $message = ts('One of the selected contacts has an address record that is shared with 1 other contact.', [
111 'plural' => 'One or more selected contacts have address records which are shared with %count other contacts.',
112 'count' => $sharedAddressCount,
113 ]);
114 }
115 else {
116 // only one contact deleted
117 $message = ts('This contact has an address record which is shared with 1 other contact.', [
118 'plural' => 'This contact has an address record which is shared with %count other contacts.',
119 'count' => $sharedAddressCount,
120 ]);
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 /**
131 * Build the form object.
132 */
133 public function buildQuickForm() {
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
138 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic');
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',
148 'reset=1&cid=' . $this->_contactIds[0] . $urlParams
149 ));
150 $this->addDefaultButtons($label, 'done', 'cancel');
151 }
152 else {
153 $this->addDefaultButtons($label, 'done');
154 }
155
156 $this->addFormRule(['CRM_Contact_Form_Task_Delete', 'formRule'], $this);
157 }
158
159 /**
160 * Global form rule.
161 *
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.
168 *
169 * @return bool|array
170 * true if no errors, else array of errors
171 */
172 public static function formRule($fields, $files, $self) {
173 // CRM-12929
174 $error = [];
175 if ($self->_skipUndelete) {
176 CRM_Financial_BAO_FinancialItem::checkContactPresent($self->_contactIds, $error);
177 }
178 return $error;
179 }
180
181 /**
182 * Process the form after the input has been submitted and validated.
183 */
184 public function postProcess() {
185 $session = CRM_Core_Session::singleton();
186 $currentUserId = $session->get('userID');
187
188 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic');
189 $urlParams = 'force=1';
190 $urlString = "civicrm/contact/search/$context";
191
192 if (CRM_Utils_Rule::qfKey($this->_searchKey)) {
193 $urlParams .= "&qfKey=$this->_searchKey";
194 }
195 elseif ($context == 'search') {
196 $urlParams .= "&qfKey={$this->controller->_key}";
197 $urlString = 'civicrm/contact/search';
198 }
199 elseif ($context == 'smog') {
200 $urlParams .= "&qfKey={$this->controller->_key}&context=smog";
201 $urlString = 'civicrm/group/search';
202 }
203 else {
204 $urlParams = "reset=1";
205 $urlString = 'civicrm/dashboard';
206 }
207
208 // Delete/Restore Contacts. Report errors.
209 $deleted = 0;
210 $not_deleted = [];
211 foreach ($this->_contactIds as $cid) {
212 $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name');
213 if (CRM_Contact_BAO_Contact::checkDomainContact($cid)) {
214 $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", [1 => $name]), 'Cannot Delete Domain Contact', 'error');
215 continue;
216 }
217 if ($currentUserId == $cid && !$this->_restore) {
218 $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", [1 => $name]), 'Unable To Delete', 'error');
219 continue;
220 }
221 if (CRM_Contact_BAO_Contact::deleteContact($cid, $this->_restore, $this->_skipUndelete)) {
222 $deleted++;
223 }
224 else {
225 $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$cid");
226 $not_deleted[$cid] = "<a href='$url'>$name</a>";
227 }
228 }
229 if ($deleted) {
230 $title = ts('Deleted');
231 if ($this->_restore) {
232 $title = ts('Restored');
233 $status = ts('%1 has been restored from the trash.', [
234 1 => $name,
235 'plural' => '%count contacts restored from trash.',
236 'count' => $deleted,
237 ]);
238 }
239 elseif ($this->_skipUndelete) {
240 $status = ts('%1 has been permanently deleted.', [
241 1 => $name,
242 'plural' => '%count contacts permanently deleted.',
243 'count' => $deleted,
244 ]);
245 }
246 else {
247 $status = ts('%1 has been moved to the trash.', [
248 1 => $name,
249 'plural' => '%count contacts moved to trash.',
250 'count' => $deleted,
251 ]);
252 }
253 $session->setStatus($status, $title, 'success');
254 }
255 // Alert user of any failures
256 if ($not_deleted) {
257 $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.');
258 $title = ts('Unable to Delete');
259 $session->setStatus('<ul><li>' . implode('</li><li>', $not_deleted) . '</li></ul>' . $status, $title, 'error');
260 }
261
262 if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0 && !$this->_restore) {
263 if (count($this->_sharedAddressMessage['contactList']) == 1) {
264 $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.');
265 }
266 else {
267 $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.');
268 }
269 $message .= '<ul><li>' . implode('</li><li>', $this->_sharedAddressMessage['contactList']) . '</li></ul>';
270
271 $session->setStatus($message, ts('Shared Addesses Owner Deleted'), 'info', ['expires' => 0]);
272
273 $this->set('sharedAddressMessage', NULL);
274 }
275
276 if ($this->_single && empty($this->_skipUndelete)) {
277 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}"));
278 }
279 else {
280 $session->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams));
281 }
282 }
283
284 }