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
* 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
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())) {
$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;
$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,
$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;
$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' => [
$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];
}
$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') {
$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']]);
}