Merge screen JS to use existing block information
authorJKingsnorth <john@johnkingsnorth.co.uk>
Thu, 21 Apr 2016 13:38:22 +0000 (14:38 +0100)
committerJKingsnorth <john@johnkingsnorth.co.uk>
Thu, 21 Apr 2016 13:38:22 +0000 (14:38 +0100)
CRM/Contact/Form/Merge.php
templates/CRM/Contact/Form/Merge.tpl

index ba623951996e0129efc83067e91b59fa949ee8c4..44d5574f5c9a25f0d9c81d859d0e1e4e6ec2617f 100644 (file)
@@ -208,7 +208,8 @@ class CRM_Contact_Form_Merge extends CRM_Core_Form {
     $this->_contactType = $main['contact_type'];
     $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows'));
 
-    $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_loc_block']));
+    $this->assign('mainLocBlock', json_encode($rowsElementsAndInfo['main_details']['location_blocks']));
+    $this->assign('locationBlockInfo', json_encode(CRM_Dedupe_Merger::getLocationBlockInfo()));
     $this->assign('rows', $rowsElementsAndInfo['rows']);
 
     // add elements
index 6e569d5e8252c5c9363ea1d5ad5b4dfceaf0ac55..1ed340e3d164b3b3367805fb3c43ddc5f8258585 100644 (file)
 {literal}
 <script type="text/javascript">
 
+  var locationBlockInfo = {/literal}{$locationBlockInfo}{literal};
+  var allBlock = {/literal}{$mainLocBlock}{literal};
+
   function mergeBlock(blockname, element, blockId, type) {
-    var allBlock = {/literal}{$mainLocBlock}{literal};
 
     // Get type of select list that's been changed (location or type)
     var locTypeId = '';
     // @todo Fix this 'special handling' for websites (no location id)
     if (!locTypeId) { locTypeId = 0; }
 
-    // Get the matching block, based on location and type, from the main contact record
-    var blockQuery = "allBlock.main_" + blockname + "_" + locTypeId;
-    if (typeTypeId) {
-      blockQuery += "_" + typeTypeId;
-    }
-    var block = eval( blockQuery );
+    // Look for a matching block on the main contact
     var mainBlockId = 0;
     var mainBlockDisplay = '';
+    var mainBlock = findBlock(allBlock, blockname, locTypeId, typeTypeId);
 
     // Create appropriate label / add new link after changing the block
-    if (typeof block == 'undefined') {
+    if (mainBlock == false) {
       label = '<span class="action_label">({/literal}{ts}add{/ts}{literal})</span>';
     }
     else {
 
       // Set display and ID
-      mainBlockDisplay = block['display'];
-      mainBlockId = block['id'];
+      mainBlockDisplay = mainBlock['display'];
+      mainBlockId = mainBlock['id'];
 
       // Set label
       var label = '<span class="action_label">({/literal}{ts}overwrite{/ts}{literal})</span> ';
     CRM.$( "#main_" + blockname + "_" + blockId + "_overwrite" ).html( label );
   }
 
+  // Find a matching main contact location block by entity, location and type
+  function findBlock(allBlock, entName, locationID, typeID) {
+    var entityArray = allBlock[entName];
+    var result = false;
+    for (var i = 0; i < entityArray.length; i++) {
+      // Match based on location and type ID, depending on the entity info
+      if (locationBlockInfo[entName]['hasLocation'] == false || locationID == entityArray[i]['location_type_id']) {
+        if (locationBlockInfo[entName]['hasType'] == false || typeID == entityArray[i][locationBlockInfo[entName]['hasType']]) {
+          result = {
+            display: entityArray[i][locationBlockInfo[entName]['displayField']],
+            id: entityArray[i]['id']
+          };
+          break;
+        }
+      }
+    }
+    return result;
+  }
+
   CRM.$(function($) {
 
     $('body').on('change', "input[id*='[operation]']", function() {