_searchKey = CRM_Utils_Request::retrieve('key', 'String', $this); // sort out whether it’s a delete-to-trash, delete-into-oblivion or restore (and let the template know) $values = $this->controller->exportValues(); $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)); $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.')); } elseif (!CRM_Core_Permission::check('delete contacts')) { CRM_Core_Error::fatal(ts('You do not have permission to delete this contact.')); } $this->assign('trash', CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL) and !$this->_skipUndelete); $this->assign('restore', $this->_restore); if ($this->_restore) { CRM_Utils_System::setTitle(ts('Restore Contact')); } if ($cid) { if (!CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT)) { CRM_Core_Error::fatal(ts('You do not have permission to delete this contact. Note: you can delete contacts if you can edit them.')); } elseif (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { 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.')); } $this->_contactIds = array($cid); $this->_single = TRUE; $this->assign('totalSelectedContacts', 1); } else { parent::preProcess(); } $this->_sharedAddressMessage = $this->get('sharedAddressMessage'); if (!$this->_restore && !$this->_sharedAddressMessage) { // we check for each contact for shared contact address $sharedContactList = array(); $sharedAddressCount = 0; foreach ($this->_contactIds as $contactId) { // check if a contact that is being deleted has any shared addresses $sharedAddressMessage = CRM_Core_BAO_Address::setSharedAddressDeleteStatus(NULL, $contactId, TRUE); if ($sharedAddressMessage['count'] > 0) { $sharedAddressCount += $sharedAddressMessage['count']; $sharedContactList = array_merge($sharedContactList, $sharedAddressMessage['contactList'] ); } } $this->_sharedAddressMessage = array( 'count' => $sharedAddressCount, 'contactList' => $sharedContactList, ); if ($sharedAddressCount > 0) { if (count($this->_contactIds) > 1) { // more than one contact deleted $message = ts('One of the selected contacts has an address record that is shared with 1 other contact.', array('plural' => 'One or more selected contacts have address records which are shared with %count other contacts.', 'count' => $sharedAddressCount)); } else { // only one contact deleted $message = ts('This contact has an address record which is shared with 1 other contact.', array('plural' => 'This contact has an address record which is shared with %count other contacts.', 'count' => $sharedAddressCount)); } CRM_Core_Session::setStatus($message . ' ' . ts('Shared addresses will not be removed or altered but will no longer be shared.'), ts('Shared Addesses Owner')); } // set in form controller so that queries are not fired again $this->set('sharedAddressMessage', $this->_sharedAddressMessage); } } /** * Build the form * * @access public * * @return void */ function buildQuickForm() { $label = $this->_restore ? ts('Restore Contact(s)') : ts('Delete Contact(s)'); if ($this->_single) { // also fix the user context stack in case the user hits cancel $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'basic'); if ($context == 'search' && CRM_Utils_Rule::qfKey($this->_searchKey)) { $urlParams = "&context=$context&key=$this->_searchKey"; } else { $urlParams = ''; } $session = CRM_Core_Session::singleton(); $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $this->_contactIds[0] . $urlParams )); $this->addDefaultButtons($label, 'done', 'cancel'); } else { $this->addDefaultButtons($label, 'done'); } } /** * process the form after the input has been submitted and validated * * @access public * * @return None */ public function postProcess() { $session = CRM_Core_Session::singleton(); $currentUserId = $session->get('userID'); $context = CRM_Utils_Request::retrieve('context', 'String', $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. $deleted = 0; $not_deleted = array(); foreach ($this->_contactIds as $cid) { $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'display_name'); if (CRM_Contact_BAO_Contact::checkDomainContact($cid)) { $session->setStatus(ts("'%1' cannot be deleted because the information is used for special system purposes.", array(1 => $name)), 'Cannot Delete Domain Contact', 'error'); continue; } if ($currentUserId == $cid && !$this->_restore) { $session->setStatus(ts("You are currently logged in as '%1'. You cannot delete yourself.", array(1 => $name)), 'Unable To Delete', 'error'); continue; } if (CRM_Contact_BAO_Contact::deleteContact($cid, $this->_restore, $this->_skipUndelete)) { $deleted++; } else { $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid=$cid"); $not_deleted[$cid] = "$name"; } } if ($deleted) { $title = ts('Deleted'); if ($this->_restore) { $title = ts('Restored'); $status = ts('%1 has been restored from the trash.', array(1 => $name, 'plural' => '%count contacts restored from trash.', 'count' => $deleted)); } elseif ($this->_skipUndelete) { $status = ts('%1 has been permanently deleted.', array(1 => $name, 'plural' => '%count contacts permanently deleted.', 'count' => $deleted)); } else { $status = ts('%1 has been moved to the trash.', array(1 => $name, 'plural' => '%count contacts moved to trash.', 'count' => $deleted)); } $session->setStatus($status, $title, 'success'); } // Alert user of any failures if ($not_deleted) { $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.'); $title = ts('Unable to Delete'); $session->setStatus('' . $status, $title, 'error'); } if (isset($this->_sharedAddressMessage) && $this->_sharedAddressMessage['count'] > 0 && !$this->_restore) { 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.'); } else { $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.'); } $message .= ''; $session->setStatus($message, ts('Shared Addesses Owner Deleted'), 'info', array('expires' => 0)); $this->set('sharedAddressMessage', NULL); } if ($this->_single && empty($this->_skipUndelete)) { $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactIds[0]}")); } else { $session->replaceUserContext(CRM_Utils_System::url($urlString, $urlParams)); } } //end of function }