CRM-11610 - Search js - Abstract table row highlight/select
[civicrm-core.git] / CRM / Contact / Form / Merge.php
index e35351d607f4c4e1272d1d0e01ade5dc8c012f39..f53cddbc7ad21c663526a5c6e24d2e2d4c6b5e1e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -51,8 +51,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
   // see HTML_QuickForm_advcheckbox::setValues() - but patching that doesn't
   // help, as QF doesn't put the 0-value elements in exportValues() anyway...
   // to side-step this, we use the below UUID as a (re)placeholder
-  var $_qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9'; 
-  
+  var $_qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
+
   function preProcess() {
     if (!CRM_Core_Permission::check('merge duplicate contacts')) {
       CRM_Core_Error::fatal(ts('You do not have access to this page'));
@@ -77,7 +77,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $cacheKey .= $rgid ? "_{$rgid}" : '_0';
     $cacheKey .= $gid ? "_{$gid}" : '_0';
 
-    $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND 
+    $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
                                                              pn.entity_id2 = de.contact_id2 )";
     $where = "de.id IS NULL";
 
@@ -110,7 +110,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       $this->assign('mainUfName', $mainUser ? $mainUser->name : NULL);
     }
 
-    $flipUrl = CRM_Utils_system::url('civicrm/contact/merge',
+    $flipUrl = CRM_Utils_System::url('civicrm/contact/merge',
       "reset=1&action=update&cid={$oid}&oid={$cid}&rgid={$rgid}&gid={$gid}"
     );
     if (!$flip) {
@@ -132,7 +132,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
             $urlParam .= "&gid={$gid}";
           }
 
-          $this->$position = CRM_Utils_system::url('civicrm/contact/merge', $urlParam);
+          $this->$position = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
           $this->assign($position, $this->$position);
         }
       }
@@ -166,7 +166,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       if ($gid) {
         $urlParam .= "&gid={$gid}";
       }
-      $session->pushUserContext(CRM_Utils_system::url('civicrm/contact/dedupefind', $urlParam));
+      $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/dedupefind', $urlParam));
     }
 
     // ensure that oid is not the current user, if so refuse to do the merge
@@ -193,12 +193,12 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $subtypes = CRM_Contact_BAO_ContactType::subTypePairs(NULL, TRUE, '');
 
     $this->assign('contact_type', $main['contact_type']);
-    if (isset($main['contact_sub_type'])) {
+    if (!empty($main['contact_sub_type'])) {
       $this->assign('main_contact_subtype',
         CRM_Utils_Array::value('contact_sub_type', $subtypes[$main['contact_sub_type'][0]])
       );
     }
-    if (isset($other['contact_sub_type'])) {
+    if (!empty($other['contact_sub_type'])) {
       $this->assign('other_contact_subtype',
         CRM_Utils_Array::value('contact_sub_type', $subtypes[$other['contact_sub_type'][0]])
       );
@@ -207,12 +207,13 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $this->assign('other_name', $other['display_name']);
     $this->assign('main_cid', $main['contact_id']);
     $this->assign('other_cid', $other['contact_id']);
+    $this->assign('rgid', $rgid);
 
     $this->_cid         = $cid;
     $this->_oid         = $oid;
     $this->_rgid        = $rgid;
     $this->_contactType = $main['contact_type'];
-    $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "return toggleCheckboxVals('move_',this);"));
+    $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
 
     $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_loc_block']));
     $this->assign('rows', $rowsElementsAndInfo['rows']);
@@ -292,11 +293,24 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     }
 
     $this->addButtons($button);
+    $this->addFormRule(array('CRM_Contact_Form_Merge', 'formRule'), $this);
+  }
+
+  static function formRule($fields, $files, $self) {
+    $errors = array();
+    $link = CRM_Utils_System::href(ts('Flip between the original and duplicate contacts.'),
+      'civicrm/contact/merge',
+      'reset=1&action=update&cid=' . $self->_oid . '&oid=' . $self->_cid . '&rgid=' . $self->_rgid . '&flip=1'
+    );
+    if (CRM_Contact_BAO_Contact::checkDomainContact($self->_oid)) {
+      $errors['_qf_default'] = ts("The Default Organization contact cannot be merged into another contact record. It is associated with the CiviCRM installation for this domain and contains information used for system functions. If you want to merge these records, you can: %1", array(1 => $link));
+    }
+    return $errors;
   }
 
   public function postProcess() {
     $formValues = $this->exportValues();
+
     // reset all selected contact ids from session
     // when we came from search context, CRM-3526
     $session = CRM_Core_Session::singleton();
@@ -313,7 +327,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
 
     CRM_Core_Session::setStatus(ts('Contact id %1 has been updated and contact id %2 has been deleted.', array(1 => $this->_cid, 2 => $this->_oid)), ts('Contacts Merged'), 'success');
     $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_cid}");
-    if (CRM_Utils_Array::value('_qf_Merge_submit', $formValues)) {
+    if (!empty($formValues['_qf_Merge_submit'])) {
       $listParamsURL = "reset=1&action=update&rgid={$this->_rgid}";
       if ($this->_gid) {
         $listParamsURL .= "&gid={$this->_gid}";
@@ -323,7 +337,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       );
       CRM_Utils_System::redirect($lisitingURL);
     }
-     if (CRM_Utils_Array::value('_qf_Merge_done', $formValues)) {
+     if (!empty($formValues['_qf_Merge_done'])) {
       CRM_Utils_System::redirect($url);
     }
 
@@ -332,7 +346,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
       $cacheKey .= $this->_rgid ? "_{$this->_rgid}" : '_0';
       $cacheKey .= $this->_gid ? "_{$this->_gid}" : '_0';
 
-      $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND 
+      $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
                                                                  pn.entity_id2 = de.contact_id2 )";
       $where = "de.id IS NULL";
 
@@ -351,7 +365,7 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
           $urlParam .= "&gid={$this->_gid}";
         }
 
-        $url = CRM_Utils_system::url('civicrm/contact/merge', $urlParam);
+        $url = CRM_Utils_System::url('civicrm/contact/merge', $urlParam);
       }
     }