Fix is_primary handling
authorColeman Watts <coleman@civicrm.org>
Thu, 2 May 2013 19:53:03 +0000 (12:53 -0700)
committerColeman Watts <coleman@civicrm.org>
Thu, 2 May 2013 19:53:03 +0000 (12:53 -0700)
CRM/Contact/BAO/Contact.php
CRM/Core/BAO/Block.php

index cfd2fb10cace58711a47a5725c3fd3b909560dcc..40e746088dd7007833c3b6c6c16c192042945cb1 100644 (file)
@@ -3000,19 +3000,22 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id )
     else {
       return FALSE;
     }
-    $dao = new $daoName();
-    $dao->contact_id = $contactId;
-    $dao->is_primary = 1;
-    // Pick another record to be primary (if one isn't already)
-    if (!$dao->find(TRUE)) {
-      $dao->is_primary = 0;
-      $dao->find();
-      if ($dao->fetch()) {
-        $dao->is_primary = 1;
-        $dao->save();
-      }
+    // is_primary is only relavent if this field belongs to a contact
+    if ($contactId) {
+      $dao = new $daoName();
+      $dao->contact_id = $contactId;
+      $dao->is_primary = 1;
+      // Pick another record to be primary (if one isn't already)
+      if (!$dao->find(TRUE)) {
+        $dao->is_primary = 0;
+        $dao->find();
+        if ($dao->fetch()) {
+          $dao->is_primary = 1;
+          $dao->save();
+        }
+      }
+      $dao->free();
     }
-    $dao->free();
     CRM_Utils_Hook::post('delete', $type, $id, $obj);
     $obj->free();
     return TRUE;
index 92ff98a56147a3cd4f5170404b34e7b799d0719c..f901457a3b12f3912b2f55a8536d885a76ec369d 100644 (file)
@@ -435,19 +435,17 @@ class CRM_Core_BAO_Block {
         $table = 'civicrm_address';
         break;
     }
-    // if id is set & we don't have contact_id we need to retrieve it
-    $contactId = null;
-    if (!empty($params['id']) && empty($params['contact_id'])) {
+    // contact_id in params might be empty or the string 'null' so cast to integer
+    $contactId = (int) CRM_Utils_Array::value('contact_id', $params);
+    // If id is set & we haven't been passed a contact_id, retrieve it
+    if (!empty($params['id']) && !isset($params['contact_id'])) {
       $entity = new $class();
       $entity->id = $params['id'];
       $entity->find(TRUE);
       $contactId = $entity->contact_id;
     }
-    elseif (CRM_Utils_Array::value('contact_id', $params)) {
-      $contactId = $params['contact_id'];
-    }
-    if ( !$contactId ) {
-      // entity not associated with contact so concept of is_primary not relevant
+    // If entity is not associated with contact, concept of is_primary not relevant
+    if (!$contactId) {
       return;
     }