Merge pull request #9325 from colemanw/multicurrency
[civicrm-core.git] / CRM / Contact / Form / Merge.php
index 5d3a6b2ff58407f6bc9188b8c65c7ffb3baa079f..b574c3115e7ef2ccdd80bc415927ba9b82f019b5 100644 (file)
@@ -44,6 +44,13 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
 
   var $_contactType = NULL;
 
+  /**
+   * Query limit to be retained in the urls.
+   *
+   * @var int
+   */
+  var $limit;
+
   // FIXME: QuickForm can't create advcheckboxes with value set to 0 or '0' :(
   // see HTML_QuickForm_advcheckbox::setValues() - but patching that doesn't
   // help, as QF doesn't put the 0-value elements in exportValues() anyway...
@@ -55,7 +62,6 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       CRM_Core_Error::fatal(ts('You do not have access to this page'));
     }
 
-    $rows = array();
     $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
     $oid = CRM_Utils_Request::retrieve('oid', 'Positive', $this, TRUE);
     $flip = CRM_Utils_Request::retrieve('flip', 'Positive', $this, FALSE);
@@ -63,6 +69,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $this->_rgid = CRM_Utils_Request::retrieve('rgid', 'Positive', $this, FALSE);
     $this->_gid = $gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE);
     $this->_mergeId = CRM_Utils_Request::retrieve('mergeId', 'Positive', $this, FALSE);
+    $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) {
@@ -73,7 +81,9 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       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'));
+    $isFromDedupeScreen = TRUE;
     if (!$this->_rgid) {
+      $isFromDedupeScreen = FALSE;
       $this->_rgid = civicrm_api3('RuleGroup', 'getvalue', array(
         'contact_type' => $this->_contactType,
         'used' => 'Supervised',
@@ -131,16 +141,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
              ) as $position) {
       if (!empty($pos[$position])) {
         if ($pos[$position]['id1'] && $pos[$position]['id2']) {
-          $urlParam = "reset=1&cid={$pos[$position]['id1']}&oid={$pos[$position]['id2']}&mergeId={$pos[$position]['mergeId']}&action=update";
-
-          if ($this->_rgid) {
-            $urlParam .= "&rgid={$this->_rgid}";
-          }
-          if ($gid) {
-            $urlParam .= "&gid={$gid}";
-          }
-
-          $this->$position = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
+          $urlParams .= "&cid={$pos[$position]['id1']}&oid={$pos[$position]['id2']}&mergeId={$pos[$position]['mergeId']}&action=update";
+          $this->$position = CRM_Utils_System::url('civicrm/contact/merge', $urlParams);
           $this->assign($position, $this->$position);
         }
       }
@@ -169,13 +171,11 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $session = CRM_Core_Session::singleton();
 
     // context fixed.
-    if ($this->_rgid) {
-      $urlParam = "reset=1&action=browse&rgid={$this->_rgid}";
-      if ($gid) {
-        $urlParam .= "&gid={$gid}";
-      }
-      $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlParam));
+    if ($isFromDedupeScreen) {
+      $browseUrl = CRM_Utils_System::url('civicrm/contact/dedupefind', $urlParams . '&action=browse');
+      $session->pushUserContext($browseUrl);
     }
+    $this->assign('browseUrl', empty($browseUrl) ? '' : $browseUrl);
 
     // ensure that oid is not the current user, if so refuse to do the merge
     if ($session->get('userID') == $oid) {
@@ -317,21 +317,21 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
 
     $formValues['main_details'] = $this->_mainDetails;
     $formValues['other_details'] = $this->_otherDetails;
-
-    CRM_Dedupe_Merger::moveAllBelongings($this->_cid, $this->_oid, $formValues);
+    $migrationData = array('migration_info' => $formValues);
+    CRM_Utils_Hook::merge('form', $migrationData, $this->_cid, $this->_oid);
+    CRM_Dedupe_Merger::moveAllBelongings($this->_cid, $this->_oid, $migrationData['migration_info']);
 
     $name = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_cid, 'display_name');
     $message = '<ul><li>' . ts('%1 has been updated.', array(1 => $name)) . '</li><li>' . ts('Contact ID %1 has been deleted.', array(1 => $this->_oid)) . '</li></ul>';
     CRM_Core_Session::setStatus($message, ts('Contacts Merged'), 'success');
 
     $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_cid}");
+    $urlParams = "reset=1&gid={$this->_gid}&rgid={$this->_rgid}&limit={$this->limit}";
+
     if (!empty($formValues['_qf_Merge_submit'])) {
-      $listParamsURL = "reset=1&action=update&rgid={$this->_rgid}";
-      if ($this->_gid) {
-        $listParamsURL .= "&gid={$this->_gid}";
-      }
+      $urlParams .= "&action=update";
       $lisitingURL = CRM_Utils_System::url('civicrm/contact/dedupefind',
-        $listParamsURL
+        $urlParams
       );
       CRM_Utils_System::redirect($lisitingURL);
     }
@@ -352,15 +352,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
         $pos['next']['id2']
       ) {
 
-        $urlParam = "reset=1&cid={$pos['next']['id1']}&oid={$pos['next']['id2']}&mergeId={$pos['next']['mergeId']}&action=update";
-        if ($this->_rgid) {
-          $urlParam .= "&rgid={$this->_rgid}";
-        }
-        if ($this->_gid) {
-          $urlParam .= "&gid={$this->_gid}";
-        }
-
-        $url = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
+        $urlParams .= "&cid={$pos['next']['id1']}&oid={$pos['next']['id2']}&mergeId={$pos['next']['mergeId']}&action=update";
+        $url = CRM_Utils_System::url('civicrm/contact/merge', $urlParams);
       }
     }