From 442df34b5f4c02d27af8d51f657deff0e00f240c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 13 Apr 2014 19:28:35 -0400 Subject: [PATCH] CRM-14463 - Fix custom address fields to support location type --- CRM/Contact/BAO/Query.php | 13 +++++++++---- CRM/Contact/Form/Search/Builder.php | 4 +++- CRM/Core/BAO/CustomQuery.php | 21 +++++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 7757decbb0..04864e91a6 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -338,6 +338,12 @@ class CRM_Contact_BAO_Query { */ public static $_openedPanes = array(); + /** + * For search builder - which custom fields are location-dependent + * @var array + */ + public $_locationSpecificCustomFields = array(); + /** * The tables which have a dependency on location and/or address * @@ -874,7 +880,7 @@ class CRM_Contact_BAO_Query { CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'select'); if (!empty($this->_cfIDs)) { - $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE); + $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE, $this->_locationSpecificCustomFields); $this->_customQuery->query(); $this->_select = array_merge($this->_select, $this->_customQuery->_select); $this->_element = array_merge($this->_element, $this->_customQuery->_element); @@ -1184,10 +1190,10 @@ class CRM_Contact_BAO_Query { } if (!empty($addressCustomFieldIds)) { - $cfIDs = $addressCustomFieldIds; - $customQuery = new CRM_Core_BAO_CustomQuery($cfIDs); + $customQuery = new CRM_Core_BAO_CustomQuery($addressCustomFieldIds); foreach ($addressCustomFieldIds as $cfID => $locTypeName) { foreach ($locTypeName as $name => $dnc) { + $this->_locationSpecificCustomFields[$cfID] = array($name, array_search($name, $locationTypes)); $fieldName = "$name-custom_{$cfID}"; $tName = "$name-address-custom-{$cfID}"; $aName = "`$name-address-custom-{$cfID}`"; @@ -4394,7 +4400,6 @@ civicrm_relationship.is_permission_a_b = 0 if ($returnQuery) { return $query; } - if ($count) { return CRM_Core_DAO::singleValueQuery($query); } diff --git a/CRM/Contact/Form/Search/Builder.php b/CRM/Contact/Form/Search/Builder.php index 73c4a9b9e5..4e90a97644 100644 --- a/CRM/Contact/Form/Search/Builder.php +++ b/CRM/Contact/Form/Search/Builder.php @@ -208,7 +208,9 @@ class CRM_Contact_Form_Search_Builder extends CRM_Contact_Form_Search { } else { if (substr($v[0], 0, 7) == 'custom_') { - $type = $fields[$v[0]]['data_type']; + // Get rid of appended location type id + list($fieldKey) = explode('-', $v[0]); + $type = $fields[$fieldKey]['data_type']; // hack to handle custom data of type state and country if (in_array($type, array( diff --git a/CRM/Core/BAO/CustomQuery.php b/CRM/Core/BAO/CustomQuery.php index 0f8a2ea74f..1576408d10 100644 --- a/CRM/Core/BAO/CustomQuery.php +++ b/CRM/Core/BAO/CustomQuery.php @@ -102,6 +102,8 @@ class CRM_Core_BAO_CustomQuery { */ protected $_contactSearch; + protected $_locationSpecifiCustomFields; + /** * This stores custom data group types and tables that it extends * @@ -135,8 +137,10 @@ class CRM_Core_BAO_CustomQuery { * @param array $ids the set of custom field ids * * @access public - */ function __construct($ids, $contactSearch = FALSE) { + */ + function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) { $this->_ids = &$ids; + $this->_locationSpecifiCustomFields = $locationSpecificFields; $this->_select = array(); $this->_element = array(); @@ -300,12 +304,20 @@ SELECT label, value } if ($joinTable) { - $this->_tables[$name] = "\nLEFT JOIN $name ON $name.entity_id = $joinTable.id"; + $joinClause = 1; + $joinTableAlias = $joinTable; + // Set location-specific query + if (isset($this->_locationSpecifiCustomFields[$id])) { + list($locationType, $locationTypeId) = $this->_locationSpecifiCustomFields[$id]; + $joinTableAlias = "$locationType-address"; + $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]) { $this->_whereTables[$name] = $this->_tables[$name]; } if ($joinTable != 'contact_a') { - $this->_whereTables[$joinTable] = $this->_tables[$joinTable] = 1; + $this->_whereTables[$joinTableAlias] = $this->_tables[$joinTableAlias] = $joinClause; } elseif ($this->_contactSearch) { CRM_Contact_BAO_Query::$_openedPanes[ts('Custom Fields')] = TRUE; @@ -325,9 +337,6 @@ SELECT label, value * @access public */ function where() { - //CRM_Core_Error::debug( 'fld', $this->_fields ); - //CRM_Core_Error::debug( 'ids', $this->_ids ); - foreach ($this->_ids as $id => $values) { // Fixed for Isuue CRM 607 -- 2.25.1