dev/core#389 [preliminary cleanup] Standardise metadat for custom field use
authoreileen <emcnaughton@wikimedia.org>
Thu, 30 May 2019 22:45:06 +0000 (10:45 +1200)
committereileen <emcnaughton@wikimedia.org>
Fri, 31 May 2019 01:19:41 +0000 (13:19 +1200)
In digging into dev/core#389 I found a major complexity in fixing it was inconsistent metadata available at different
points in the code. This simply adds metadata to the 'inner-most place' & makes the BAO_CustomQuery object use that

I didn't remove the options loop because I wanted to leave that out of scope as I can't see how / if it is used

CRM/Core/BAO/CustomField.php
CRM/Core/BAO/CustomQuery.php
tests/phpunit/CRM/Core/BAO/CustomQueryTest.php

index 2190bd592301b18c9e933d4cc9ce77763db0a512..2b1e74ad4c7513b413bd1759b94bc2988da144d4 100644 (file)
@@ -433,7 +433,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
    * Store and return an array of all active custom fields.
    *
    * @param string $customDataType
-   *   Type of Custom Data; empty is a synonym for "all contact data types".
+   *   Type of Custom Data; 'ANY' is a synonym for "all contact data types".
    * @param bool $showAll
    *   If true returns all fields (includes disabled fields).
    * @param bool $inline
@@ -465,6 +465,11 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
     if (empty($customDataType)) {
       $customDataType = array('Contact', 'Individual', 'Organization', 'Household');
     }
+    if ($customDataType === 'ANY') {
+      // NULL should have been respected but the line above broke that.
+      // boo for us not having enough unit tests back them.
+      $customDataType = NULL;
+    }
     if ($customDataType && !is_array($customDataType)) {
 
       if (in_array($customDataType, CRM_Contact_BAO_ContactType::subTypes())) {
@@ -630,6 +635,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
 
         $fields = array();
         while (($dao->fetch()) != NULL) {
+          $fields[$dao->id]['id'] = $dao->id;
           $fields[$dao->id]['label'] = $dao->label;
           $fields[$dao->id]['groupTitle'] = $dao->title;
           $fields[$dao->id]['data_type'] = $dao->data_type;
@@ -650,7 +656,16 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
           $fields[$dao->id]['is_required'] = $dao->is_required;
           $fields[$dao->id]['table_name'] = $dao->table_name;
           $fields[$dao->id]['column_name'] = $dao->column_name;
+          // Probably we should use a different fn to get the extends tables but this is a refactor so not changing that now.
+          $fields[$dao->id]['extends_table'] = array_key_exists($dao->extends, CRM_Core_BAO_CustomQuery::$extendsMap) ? CRM_Core_BAO_CustomQuery::$extendsMap[$dao->extends] : '';
+          if (in_array($dao->extends, CRM_Contact_BAO_ContactType::subTypes())) {
+            // if $extends is a subtype, refer contact table
+            $fields[$dao->id]['extends_table'] = 'civicrm_contact';
+          }
+          // Search table is used by query object searches..
+          $fields[$dao->id]['search_table'] = ($fields[$dao->id]['extends_table'] == 'civicrm_contact') ? 'contact_a' : $fields[$dao->id]['extends_table'];
           self::getOptionsForField($fields[$dao->id], $dao->option_group_name);
+
         }
 
         CRM_Core_BAO_Cache::setItem($fields,
index 59584661a48716f5d1cf88071121bdaa119b3be0..0e5191f60c8fb1caa7c11b8fa287d822fdf4ba9f 100644 (file)
@@ -159,8 +159,8 @@ class CRM_Core_BAO_CustomQuery {
     $this->_qill = [];
     $this->_options = [];
 
-    $this->_fields = [];
     $this->_contactSearch = $contactSearch;
+    $this->_fields = CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
 
     if (empty($this->_ids)) {
       return;
@@ -184,28 +184,6 @@ SELECT f.id, f.label, f.data_type,
 
     $dao = CRM_Core_DAO::executeQuery($query);
     while ($dao->fetch()) {
-      // get the group dao to figure which class this custom field extends
-      $extends = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id, 'extends');
-      $extendsTable = '';
-      if (array_key_exists($extends, self::$extendsMap)) {
-        $extendsTable = self::$extendsMap[$extends];
-      }
-      elseif (in_array($extends, CRM_Contact_BAO_ContactType::subTypes())) {
-        // if $extends is a subtype, refer contact table
-        $extendsTable = self::$extendsMap['Contact'];
-      }
-      $this->_fields[$dao->id] = [
-        'id' => $dao->id,
-        'label' => $dao->label,
-        'extends' => $extendsTable,
-        'data_type' => $dao->data_type,
-        'html_type' => $dao->html_type,
-        'is_search_range' => $dao->is_search_range,
-        'column_name' => $dao->column_name,
-        'table_name' => $dao->table_name,
-        'option_group_id' => $dao->option_group_id,
-      ];
-
       // Deprecated (and poorly named) cache of field attributes
       $this->_options[$dao->id] = [
         'attributes' => [
@@ -242,27 +220,15 @@ SELECT f.id, f.label, f.data_type,
       $this->_element["{$name}_id"] = 1;
       $this->_select[$fieldName] = "{$field['table_name']}.{$field['column_name']} as $fieldName";
       $this->_element[$fieldName] = 1;
-      $joinTable = NULL;
+      $joinTable = $field['search_table'];
       // CRM-14265
-      if ($field['extends'] == 'civicrm_group') {
-        return;
-      }
-      elseif ($field['extends'] == 'civicrm_contact') {
-        $joinTable = 'contact_a';
-      }
-      elseif ($field['extends'] == 'civicrm_contribution') {
-        $joinTable = $field['extends'];
-      }
-      elseif (in_array($field['extends'], self::$extendsMap)) {
-        $joinTable = $field['extends'];
-      }
-      else {
+      if ($joinTable == 'civicrm_group' || empty($joinTable)) {
         return;
       }
 
       $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id";
 
-      if ($this->_ids[$id]) {
+      if (!empty($this->_ids[$id])) {
         $this->_whereTables[$name] = $this->_tables[$name];
       }
 
@@ -276,7 +242,7 @@ SELECT f.id, f.label, f.data_type,
           $joinClause = "\nLEFT JOIN $joinTable `$locationType-address` ON (`$locationType-address`.contact_id = contact_a.id AND `$locationType-address`.location_type_id = $locationTypeId)";
         }
         $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = `$joinTableAlias`.id";
-        if ($this->_ids[$id]) {
+        if (!empty($this->_ids[$id])) {
           $this->_whereTables[$name] = $this->_tables[$name];
         }
         if ($joinTable != 'contact_a') {
index 02a3b602037b8d1d48d3906144a0e42c623de0ac..ab266df34a441a4682c8f2a8c6098ea9d41c5ef0 100644 (file)
@@ -53,13 +53,27 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
     $this->assertEquals([
       'id' => $dateCustomField['id'],
       'label' => 'date field',
-      'extends' => 'civicrm_contact',
+      'extends' => 'Contact',
       'data_type' => 'Date',
       'html_type' => 'Select Date',
       'is_search_range' => '0',
       'column_name' => 'date_field_' . $dateCustomField['id'],
       'table_name' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'],
       'option_group_id' => NULL,
+      'groupTitle' => 'testSearchCustomDataDateRelative',
+      'default_value' => NULL,
+      'text_length' => NULL,
+      'options_per_line' => NULL,
+      'custom_group_id' => '1',
+      'extends_entity_column_value' => NULL,
+      'extends_entity_column_id' => NULL,
+      'is_view' => '0',
+      'is_multiple' => '0',
+      'date_format' => 'mm/dd/yy',
+      'time_format' => NULL,
+      'is_required' => '0',
+      'extends_table' => 'civicrm_contact',
+      'search_table' => 'contact_a',
     ], $queryObj->getFields()[$dateCustomField['id']]);
 
   }