From 82fd4ec92abd75c85fcd253b2670e6c791334ca5 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 29 May 2020 11:42:07 +1200 Subject: [PATCH] dev/core#1784 fix regression on restore contacts button not working. This should fix https://lab.civicrm.org/dev/core/-/issues/1784 I think the message from deleting multiple contacts is a bit wonky, but no worse --- CRM/Contact/Form/Task/Delete.php | 99 ++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/CRM/Contact/Form/Task/Delete.php b/CRM/Contact/Form/Task/Delete.php index 32e9e8be6b..5b2b088003 100644 --- a/CRM/Contact/Form/Task/Delete.php +++ b/CRM/Contact/Form/Task/Delete.php @@ -53,10 +53,10 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { $this->_restore = (CRM_Utils_Request::retrieve('restore', 'Boolean', $this) or CRM_Utils_Array::value('task', $values) == CRM_Contact_Task::RESTORE); if ($this->_restore && !CRM_Core_Permission::check('access deleted contacts')) { - CRM_Core_Error::fatal(ts('You do not have permission to access this contact.')); + CRM_Core_Error::statusBounce(ts('You do not have permission to access this contact.')); } elseif (!CRM_Core_Permission::check('delete contacts')) { - CRM_Core_Error::fatal(ts('You do not have permission to delete this contact.')); + CRM_Core_Error::statusBounce(ts('You do not have permission to delete this contact.')); } $this->assign('trash', Civi::settings()->get('contact_undelete') and !$this->_skipUndelete); @@ -182,30 +182,14 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { * Process the form after the input has been submitted and validated. */ public function postProcess() { + if ($this->_restore) { + $this->doRestore(); + return; + } $session = CRM_Core_Session::singleton(); $currentUserId = $session->get('userID'); - $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic'); - $urlParams = 'force=1'; - $urlString = "civicrm/contact/search/$context"; - - if (CRM_Utils_Rule::qfKey($this->_searchKey)) { - $urlParams .= "&qfKey=$this->_searchKey"; - } - elseif ($context == 'search') { - $urlParams .= "&qfKey={$this->controller->_key}"; - $urlString = 'civicrm/contact/search'; - } - elseif ($context == 'smog') { - $urlParams .= "&qfKey={$this->controller->_key}&context=smog"; - $urlString = 'civicrm/group/search'; - } - else { - $urlParams = "reset=1"; - $urlString = 'civicrm/dashboard'; - } - - // Delete/Restore Contacts. Report errors. + // Delete Contacts. Report errors. $deleted = 0; $not_deleted = []; foreach ($this->_contactIds as $cid) { @@ -214,11 +198,11 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", [1 => $name]), 'Cannot Delete Domain Contact', 'error'); continue; } - if ($currentUserId == $cid && !$this->_restore) { + if ($currentUserId == $cid) { $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", [1 => $name]), 'Unable To Delete', 'error'); continue; } - if (CRM_Contact_BAO_Contact::deleteContact($cid, $this->_restore, $this->_skipUndelete)) { + if (CRM_Contact_BAO_Contact::deleteContact($cid, FALSE, $this->_skipUndelete)) { $deleted++; } else { @@ -228,15 +212,7 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { } if ($deleted) { $title = ts('Deleted'); - if ($this->_restore) { - $title = ts('Restored'); - $status = ts('%1 has been restored from the trash.', [ - 1 => $name, - 'plural' => '%count contacts restored from trash.', - 'count' => $deleted, - ]); - } - elseif ($this->_skipUndelete) { + if ($this->_skipUndelete) { $status = ts('%1 has been permanently deleted.', [ 1 => $name, 'plural' => '%count contacts permanently deleted.', @@ -259,7 +235,7 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { $session->setStatus('' . $status, $title, 'error'); } - if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0 && !$this->_restore) { + if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0) { if (count($this->_sharedAddressMessage['contactList']) == 1) { $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.'); } @@ -273,12 +249,61 @@ class CRM_Contact_Form_Task_Delete extends CRM_Contact_Form_Task { $this->set('sharedAddressMessage', NULL); } + $this->setRedirection(); + } + + /** + * Set the url for the contact to be redirected to. + */ + protected function setRedirection() { + + $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'basic'); + $urlParams = 'force=1'; + $urlString = "civicrm/contact/search/$context"; + + if (CRM_Utils_Rule::qfKey($this->_searchKey)) { + $urlParams .= "&qfKey=$this->_searchKey"; + } + elseif ($context === 'search') { + $urlParams .= "&qfKey={$this->controller->_key}"; + $urlString = 'civicrm/contact/search'; + } + elseif ($context === 'smog') { + $urlParams .= "&qfKey={$this->controller->_key}&context=smog"; + $urlString = 'civicrm/group/search'; + } + else { + $urlParams = 'reset=1'; + $urlString = 'civicrm/dashboard'; + } if ($this->_single && empty($this->_skipUndelete)) { - $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}")); + CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}")); } else { - $session->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams)); + CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams)); + } + } + + /** + * Restore the selected contact/s from the trash. + * + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + protected function doRestore() { + $name = ''; + if (count($this->_contactIds) === 1) { + $name = Civi\Api4\Contact::get()->addWhere('id', 'IN', $this->_contactIds)->setSelect(['display_name'])->execute()->first()['display_name']; } + Civi\Api4\Contact::update()->addWhere('id', 'IN', $this->_contactIds)->setValues(['is_deleted' => 0])->execute(); + $title = ts('Restored'); + $status = ts('%1 has been restored from the trash.', [ + 1 => $name, + 'plural' => '%count contacts restored from trash.', + 'count' => count($this->_contactIds), + ]); + CRM_Core_Session::setStatus($status, $title, 'success'); + $this->setRedirection(); } } -- 2.25.1