Merge pull request #1775 from pratik-joshi/CRM-13237
[civicrm-core.git] / CRM / Core / BAO / Block.php
index a978cd1562fa9336bb9f81d42196283faad457ae..bd1b3a2e79575b70ac5c5c7e8b1103664c8c11b0 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -177,6 +177,7 @@ class CRM_Core_BAO_Block {
    */
   static function getBlockIds($blockName, $contactId = NULL, $entityElements = NULL, $updateBlankLocInfo = FALSE) {
     $allBlocks = array();
+
     $name = ucfirst($blockName);
     if ($blockName == 'im') {
       $name = 'IM';
@@ -185,11 +186,19 @@ class CRM_Core_BAO_Block {
       $name = 'OpenID';
     }
 
+    $baoString = 'CRM_Core_BAO_' . $name;
     if ($contactId) {
-      eval('$allBlocks = CRM_Core_BAO_' . $name . '::all' . $name . 's( $contactId, $updateBlankLocInfo );');
+      //@todo a cleverer way to do this would be to use the same fn name on each
+      // BAO rather than constructing the fn
+      // it would also be easier to grep for
+      // e.g $bao = new $baoString;
+      // $bao->getAllBlocks()
+      $baoFunction = 'all' . $name . 's';
+      $allBlocks = $baoString::$baoFunction( $contactId, $updateBlankLocInfo );
     }
     elseif (!empty($entityElements) && $blockName != 'openid') {
-      eval('$allBlocks = CRM_Core_BAO_' . $name . '::allEntity' . $name . 's( $entityElements );');
+      $baoFunction = 'allEntity' . $name . 's';
+      $allBlocks = $baoString::$baoFunction( $entityElements );
     }
 
     return $allBlocks;
@@ -260,7 +269,8 @@ class CRM_Core_BAO_Block {
             }
           }
           if ($resetPrimaryId) {
-            eval('$block = new CRM_Core_BAO_' . $blockName . '( );');
+            $baoString = 'CRM_Core_BAO_' . $blockName;
+            $block = new $baoString( );
             $block->selectAdd();
             $block->selectAdd("id, is_primary");
             $block->id = $resetPrimaryId;
@@ -300,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;
                 }
               }
@@ -394,8 +404,8 @@ class CRM_Core_BAO_Block {
       $name = 'OpenID';
     }
 
-    require_once "CRM/Core/DAO/{$name}.php";
-    eval('$block = new CRM_Core_DAO_' . $name . '( );');
+    $baoString = 'CRM_Core_DAO_' . $name;
+    $block = new $baoString( );
 
     $block->copyValues($params);
     /*
@@ -424,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;
     }