CRM-16036 - Now contact references are returned the right way.
authorJohan Vervloet <johanv@johanv.org>
Thu, 19 Mar 2015 14:40:47 +0000 (15:40 +0100)
committereileenmcnaugton <eileen@fuzion.co.nz>
Wed, 12 Aug 2015 23:39:37 +0000 (11:39 +1200)
----------------------------------------
* CRM-16036: API: searching on custom fields does not work
  https://issues.civicrm.org/jira/browse/CRM-16036

api/v3/utils.php

index bdb6102f58d3708bda6877f98cbdfa4b69436322..d29dedd8c977a435ef5da634f0554d93c9323950 100644 (file)
@@ -490,7 +490,10 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params, $return
   $where_clauses = array();
 
   // Tables we need to join with to retrieve the custom values.
-  $tables_to_join = array();
+  $custom_value_tables = array();
+
+  // ID's of custom fields that refer to a contact.
+  $contact_reference_field_ids = array();
 
   // populate $select_fields
   $return_all_fields = (empty($options['return']) || !is_array($options['return']));
@@ -509,10 +512,22 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params, $return
     if ($return_all_fields || !empty($options['return'][$field_name])) {
       $table_name = $custom_field["table_name"];
       $column_name = $custom_field["column_name"];
-      $select_fields["$table_name.$column_name"] = "custom_$cf_id";
       // remember that we will need to join the correct table.
-      if (!in_array($table_name, $tables_to_join)) {
-        $tables_to_join[] = $table_name;
+      if (!in_array($table_name, $custom_value_tables)) {
+        $custom_value_tables[] = $table_name;
+      }
+      if ($custom_field["data_type"] != "ContactReference") {
+        // 'ordinary' custom field. We will select the value as custom_XX.
+        $select_fields["$table_name.$column_name"] = $field_name;
+      }
+      else {
+        // contact reference custom field. The ID will be stored in
+        // custom_XX_id. custom_XX will contain the sort name of the
+        // contact.
+        $contact_reference_field_ids[] = $cf_id;
+        $select_fields["$table_name.$column_name"] = $field_name . "_id";
+        // We will call the contact table for the join c_XX.
+        $select_fields["c_$cf_id.sort_name"] = $field_name;
       }
     }
   }
@@ -551,8 +566,8 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params, $return
           $operator,
           $rhs,
         );
-        if (!in_array($table_name, $tables_to_join)) {
-          $tables_to_join[] = $table_name;
+        if (!in_array($table_name, $custom_value_tables)) {
+          $custom_value_tables[] = $table_name;
         }
       }
     }
@@ -567,7 +582,8 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params, $return
     $query = $query->select("!column_$i as !alias_$i", array("!column_$i" => $column, "!alias_$i" => $alias));
   }
 
-  foreach ($tables_to_join as $table_name) {
+  // join with custom value tables
+  foreach ($custom_value_tables as $table_name) {
     ++$i;
     $query = $query->join(
       "!table_name_$i",
@@ -576,6 +592,19 @@ function _civicrm_api3_get_using_query_object_simple($dao_name, $params, $return
     );
   }
 
+  // join with contact for contact reference fields
+  foreach ($contact_reference_field_ids as $field_id) {
+    ++$i;
+    $query = $query->join(
+      "!contact_table_name$i",
+      "LEFT OUTER JOIN civicrm_contact !contact_table_name_$i ON !contact_table_name_$i.id = !values_table_name_$i.!column_name_$i",
+      array(
+        "!contact_table_name_$i" => "c_$field_id",
+        "!values_table_name_$i" => $custom_fields[$field_id]["table_name"],
+        "!column_name_$i" => $custom_fields[$field_id]["column_name"],
+      ));
+  };
+
   foreach ($where_clauses as $clause) {
     ++$i;
     if (substr($clause[1], -4) == "NULL") {
@@ -668,7 +697,8 @@ function _civicrm_api3_field_names($fields) {
  *   {
  *     '1' => array {
  *       'table_name' => 'table_name_1',
- *       'column_name' => ''column_name_1',
+ *       'column_name' => 'column_name_1',
+ *       'data_type' => 'data_type_1',
  *     },
  *   }
  */
@@ -696,6 +726,7 @@ SELECT f.id, f.label, f.data_type,
     $result[$dao->id] = array(
       'table_name' => $dao->table_name,
       'column_name' => $dao->column_name,
+      'data_type' => $dao->data_type,
     );
   }
   $dao->free();