Merge pull request #2061 from civicrm/4.3
[civicrm-core.git] / CRM / Core / BAO / Block.php
index 2c76e9438e5c3b481b5c249bb1bc2ff2eca24141..bd1b3a2e79575b70ac5c5c7e8b1103664c8c11b0 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -310,7 +310,7 @@ class CRM_Core_BAO_Block {
 
               if ($blockName == 'phone') {
                 $phoneTypeBlockValue = CRM_Utils_Array::value('phoneTypeId', $blockValue);
-                if ($phoneTypeBlockValue == $value['phone_type_id']) {
+                if ($phoneTypeBlockValue == CRM_Utils_Array::value('phone_type_id', $value)) {
                   $valueId = TRUE;
                 }
               }
@@ -434,32 +434,22 @@ class CRM_Core_BAO_Block {
    * @static
    */
   public static function handlePrimary(&$params, $class) {
-    switch ($class) {
-      case 'CRM_Core_BAO_Phone':
-        $table = 'civicrm_phone';
-        break;
-
-      case 'CRM_Core_BAO_Email':
-        $table = 'civicrm_email';
-        break;
-
-      case 'CRM_Core_BAO_Address':
-        $table = 'civicrm_address';
-        break;
+    $table = CRM_Core_DAO_AllCoreTables::getTableForClass($class);
+    if (!$table) {
+      throw new API_Exception("Failed to locate table for class [$class]");
     }
-    // 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;
     }