Commit fa74cac4 authored by eileen's avatar eileen Committed by JKingsnorth

(towards CRM-19492) Extract bounce portion of code into separate function

parent 186e5c44
......@@ -78,14 +78,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
$this->limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this, FALSE);
$urlParams = "reset=1&rgid={$this->_rgid}&gid={$this->_gid}&limit=" . $this->limit;
// Sanity check
if ($cid == $oid) {
CRM_Core_Error::statusBounce(ts('Cannot merge a contact with itself.'));
}
$this->bounceIfInvalid($cid, $oid);
if (!CRM_Dedupe_BAO_Rule::validateContacts($cid, $oid)) {
CRM_Core_Error::statusBounce(ts('The selected pair of contacts are marked as non duplicates. If these records should be merged, you can remove this exception on the <a href="%1">Dedupe Exceptions</a> page.', array(1 => CRM_Utils_System::url('civicrm/dedupe/exception', 'reset=1'))));
}
$this->_contactType = civicrm_api3('Contact', 'getvalue', array('id' => $cid, 'return' => 'contact_type'));
$browseUrl = CRM_Utils_System::url('civicrm/contact/dedupefind', $urlParams . '&action=browse');
......@@ -111,14 +105,6 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
$pos = CRM_Core_BAO_PrevNextCache::getPositions($cacheKey, $cid, $oid, $this->_mergeId, $join, $where, $flip);
// Block access if user does not have EDIT permissions for both contacts.
if (!(CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT) &&
CRM_Contact_BAO_Contact_Permission::allow($oid, CRM_Core_Permission::EDIT)
)
) {
CRM_Utils_System::permissionDenied();
}
// get user info of main contact.
$config = CRM_Core_Config::singleton();
$config->doNotResetCache = 1;
......@@ -183,15 +169,6 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
$session = CRM_Core_Session::singleton();
// ensure that oid is not the current user, if so refuse to do the merge
if ($session->get('userID') == $oid) {
$display_name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $oid, 'display_name');
$message = ts('The contact record which is linked to the currently logged in user account - \'%1\' - cannot be deleted.',
array(1 => $display_name)
);
CRM_Core_Error::statusBounce($message);
}
$rowsElementsAndInfo = CRM_Dedupe_Merger::getRowsElementsAndInfo($cid, $oid);
$main = $this->_mainDetails = &$rowsElementsAndInfo['main_details'];
$other = $this->_otherDetails = &$rowsElementsAndInfo['other_details'];
......@@ -366,4 +343,39 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
CRM_Utils_System::redirect($url);
}
/**
* Bounce if the merge action is invalid.
*
* We don't allow the merge if it is nonsensical, marked as a duplicate
* or outside the user's permission.
*
* @param int $cid
* Contact ID to retain
* @param int $oid
* Contact ID to delete.
*/
public function bounceIfInvalid($cid, $oid) {
if ($cid == $oid) {
CRM_Core_Error::statusBounce(ts('Cannot merge a contact with itself.'));
}
if (!CRM_Dedupe_BAO_Rule::validateContacts($cid, $oid)) {
CRM_Core_Error::statusBounce(ts('The selected pair of contacts are marked as non duplicates. If these records should be merged, you can remove this exception on the <a href="%1">Dedupe Exceptions</a> page.', array(1 => CRM_Utils_System::url('civicrm/dedupe/exception', 'reset=1'))));
}
if (!(CRM_Contact_BAO_Contact_Permission::allow($cid, CRM_Core_Permission::EDIT) &&
CRM_Contact_BAO_Contact_Permission::allow($oid, CRM_Core_Permission::EDIT)
)
) {
CRM_Utils_System::permissionDenied();
}
// ensure that oid is not the current user, if so refuse to do the merge
if (CRM_Core_Session::singleton()->getLoggedInContactID() == $oid) {
$message = ts('The contact record which is linked to the currently logged in user account - \'%1\' - cannot be deleted.',
array(1 => CRM_Core_Session::singleton()->getLoggedInContactDisplayName())
);
CRM_Core_Error::statusBounce($message);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment