diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 2190bd592301b18c9e933d4cc9ce77763db0a512..2b1e74ad4c7513b413bd1759b94bc2988da144d4 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -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, diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index 610478c603f94a3e269bf32723c436490ecc182a..0e5191f60c8fb1caa7c11b8fa287d822fdf4ba9f 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -93,6 +93,13 @@ class CRM_Core_BAO_CustomQuery { */ public $_fields; + /** + * @return array + */ + public function getFields() { + return $this->_fields; + } + /** * Searching for contacts? * @@ -152,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; @@ -177,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' => [ @@ -235,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]; } @@ -269,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') { diff --git a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php index 2336be65b6704a9a78269b48806795600c6aa6e8..ab266df34a441a4682c8f2a8c6098ea9d41c5ef0 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomQueryTest.php @@ -50,6 +50,32 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase { $queryObj->_where[0][0] ); $this->assertEquals($queryObj->_qill[0][0], "date field BETWEEN 'January 1st, " . date('Y') . " 12:00 AM AND December 31st, " . date('Y') . " 11:59 PM'"); + $this->assertEquals([ + 'id' => $dateCustomField['id'], + 'label' => 'date field', + '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']]); + } /**