Skip to content

Commit

Permalink
dev/core#147 Use location type m/c name as table alias instead of label
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Jun 1, 2018
1 parent 1efe6b6 commit 70da609
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
6 changes: 3 additions & 3 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ public function addHierarchicalElements() {
return;
}

$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
$processed = array();
$index = 0;

Expand Down Expand Up @@ -2374,7 +2374,7 @@ public static function getLocationTableName(&$where, &$locType) {
list($tbName, $fldName) = explode(".", $where);

//get the location name
$locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
$specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
if (in_array($locType[0], $specialFields)) {
//hack to fix / special handing for phone_ext
Expand Down Expand Up @@ -2800,7 +2800,7 @@ public static function fromClause(&$tables, $inner = NULL, $right = NULL, $prima
if ($locationTypeName) {
//we have a join on an location table - possibly in conjunction with search builder - CRM-14263
$parts = explode('-', $name);
$locationTypes = CRM_Core_BAO_Address::buildOptions('location_type_id', 'get');
$locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
foreach ($locationTypes as $locationTypeID => $locationType) {
if ($parts[0] == str_replace(' ', '_', $locationType)) {
$locationID = $locationTypeID;
Expand Down
13 changes: 11 additions & 2 deletions CRM/Contact/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function &getColumnHeaders($action = NULL, $output = NULL) {
),
);

$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');

foreach ($this->_fields as $name => $field) {
if (!empty($field['in_selector']) &&
Expand Down Expand Up @@ -492,7 +492,16 @@ public function &getColumnHeaders($action = NULL, $output = NULL) {
if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
$title .= "-{$phoneType}";
}
$title .= " ($loc)";
// fetch Location type label from name as $loc, which will be later used in column header
$title .= sprintf("(%s)",
CRM_Core_PseudoConstant::getLabel(
'CRM_Core_DAO_Address',
'location_type_id',
CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Address', 'location_type_id', $loc)
)
);
// use field name instead of table alias
$prop = $fld;
}
elseif (isset($this->_query->_fields[$prop]) && isset($this->_query->_fields[$prop]['title'])) {
$title = $this->_query->_fields[$prop]['title'];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public static function &returnProperties(&$params) {
return $fields;
}

$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
foreach ($params['mapper'] as $key => $value) {
foreach ($value as $k => $v) {
if (isset($v[1])) {
Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/CRM/Contact/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,67 @@ public function testContactIDQuery() {
$searchOBJ->contactIDQuery($params, '1_u');
}

/**
* Test the Search Builder using Non ASCII location type for email filter
*/
public function testSelectorQueryOnNonASCIIlocationType() {
$contactID = $this->individualCreate();
$locationType = $this->locationTypeCreate([
'name' => 'Non ASCII Location Type',
'display_name' => 'Дом Location type',
'vcard_name' => 'Non ASCII Location Type',
'is_active' => 1,
]);
$this->callAPISuccess('Email', 'create', [
'contact_id' => $contactID,
'location_type_id' => $locationType->id,
'email' => 'test@test.com',
]);

$selector = new CRM_Contact_Selector(
'CRM_Contact_Selector',
['email' => ['IS NOT NULL' => 1]],
[[
0 => 'email-' . $locationType->id,
1 => 'IS NOT NULL',
2 => NULL,
3 => 1,
4 => 0,
]],
[
'contact_type' => 1,
'contact_sub_type' => 1,
'sort_name' => 1,
'location' => [
'Non ASCII Location Type' => [
'location_type' => $locationType->id,
'email' => 1,
],
],
],
CRM_Core_Action::NONE,
NULL,
FALSE,
'builder'
);

$sql = $selector->getQueryObject()->query();

$expectedQuery = [
0 => "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, `Non_ASCII_Location_Type-location_type`.id as `Non_ASCII_Location_Type-location_type_id`, `Non_ASCII_Location_Type-location_type`.name as `Non_ASCII_Location_Type-location_type`, `Non_ASCII_Location_Type-email`.id as `Non_ASCII_Location_Type-email_id`, `Non_ASCII_Location_Type-email`.email as `Non_ASCII_Location_Type-email`",
// @TODO these FROM clause doesn't matches due to extra spaces or special character
2 => "WHERE ( ( LOWER(`Non_ASCII_Location_Type-email`.email) IS NOT NULL ) ) AND (contact_a.is_deleted = 0)",
];
foreach ($expectedQuery as $index => $queryString) {
$this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
}

$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
$this->assertEquals(1, count($rows));
$this->assertEquals($contactID, key($rows));
$this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']);
}

/**
* Test if custom table is added in from clause when
* search results are ordered by a custom field.
Expand Down

0 comments on commit 70da609

Please sign in to comment.