CRM-20858: Remove Try/Catch Blocks that Ignored Caught Exceptions
authorCamilo Rodriguez <camilo@compucorp.co.uk>
Mon, 21 Aug 2017 18:10:44 +0000 (18:10 +0000)
committerCamilo Rodriguez <camilo@compucorp.co.uk>
Mon, 21 Aug 2017 18:10:44 +0000 (18:10 +0000)
The try catch blocks were apparently added because not all fields passed were
custom fields, generating exceptions when the API call was made to obtain its
option group. This happened because actually all fields selected for merge
were being inserted into the $submittedCustomFields array, instead of just
custom fields. Plus, obtaining the field's ID on a field that was not custom
rendered strange strings to be used on search of the field, as this ID is
obtained by getting a substring from the field's key, that should have the
format 'move_custom_<fieldid>' (fields that are not custom don't have that
format).

CRM/Dedupe/Merger.php

index e09ef11d04d732ddb88fca86581063d7bf97d29d..3a3204c3797e6aa9fff2c46ba5b2ede41b9207db 100755 (executable)
@@ -1489,19 +1489,18 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       if ($value == $qfZeroBug) {
         $value = '0';
       }
-      if ((in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) ||
-          substr($key, 0, 12) == 'move_custom_') &&
-        $value != NULL
-      ) {
+
+      if (substr($key, 0, 12) == 'move_custom_' && $value != NULL) {
         $submitted[substr($key, 5)] = $value;
         $submittedCustomFields[] = substr($key, 12);
       }
-
+      elseif (in_array(substr($key, 5), CRM_Dedupe_Merger::getContactFields()) && $value != NULL) {
+        $submitted[substr($key, 5)] = $value;
+      }
       // Set up initial information for handling migration of location blocks
       elseif (substr($key, 0, 14) == 'move_location_' and $value != NULL) {
         $locationMigrationInfo[$key] = $value;
       }
-
       elseif (substr($key, 0, 15) == 'move_rel_table_' and $value == '1') {
         $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']);
         if (array_key_exists('operation', $migrationInfo)) {
@@ -1777,28 +1776,15 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
 
     foreach ($customFieldIDs as $fieldID) {
       if (!empty($fieldID)) {
-        $customField = NULL;
-        try {
-          $customField = civicrm_api3('custom_field', 'getsingle', array(
-            'id' => $fieldID,
+        $customField = civicrm_api3('custom_field', 'getsingle', array(
+          'id' => $fieldID,
+          'is_active' => TRUE,
+        ));
+        if (!civicrm_error($customField) && !empty($customField['custom_group_id'])) {
+          $customGroup = civicrm_api3('custom_group', 'getsingle', array(
+            'id' => $customField['custom_group_id'],
             'is_active' => TRUE,
           ));
-        }
-        catch (CiviCRM_API3_Exception $e) {
-          continue;
-        }
-        if (!civicrm_error($customField) && !empty($customField['custom_group_id'])) {
-          $customGroup = NULL;
-          try {
-            $customGroup = civicrm_api3('custom_group', 'getsingle', array(
-              'id' => $customField['custom_group_id'],
-              'is_active' => TRUE,
-            ));
-          }
-          catch (CiviCRM_API3_Exception $e) {
-            // just ignore and continue
-            continue;
-          }
 
           if (!civicrm_error($customGroup) && !empty($customGroup['table_name'])) {
             $customTableToCopyValues[] = $customGroup['table_name'];