Skip to content

Commit

Permalink
Merge pull request #12181 from JMAConsulting/dev_core_134
Browse files Browse the repository at this point in the history
dev/core#134 Search Builder broken filter for Source Contact ID
  • Loading branch information
eileenmcnaughton authored Jun 8, 2018
2 parents 09d9961 + fd95406 commit c5b63c7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
5 changes: 4 additions & 1 deletion CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,10 @@ public static function &exportableFields($name = 'Activity') {
// my case hence we have defined fields as case_*
if ($name == 'Activity') {
$exportableFields = CRM_Activity_DAO_Activity::export();
$exportableFields['source_contact_id']['title'] = ts('Source Contact ID');
$exportableFields['source_contact_id'] = [
'title' => ts('Source Contact ID'),
'type' => CRM_Utils_Type::T_INT,
];
$exportableFields['source_contact'] = array(
'title' => ts('Source Contact'),
'type' => CRM_Utils_Type::T_STRING,
Expand Down
21 changes: 17 additions & 4 deletions CRM/Activity/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ public static function select(&$query) {
if (!empty($query->_returnProperties['source_contact'])) {
$query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
$query->_element['source_contact'] = 1;
$query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
$query->_tables['civicrm_activity'] = $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
}

if (!empty($query->_returnProperties['source_contact_id'])) {
$query->_select['source_contact_id'] = 'source_contact.id as source_contact_id';
$query->_element['source_contact_id'] = 1;
$query->_tables['civicrm_activity'] = $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
}

if (!empty($query->_returnProperties['activity_result'])) {
Expand Down Expand Up @@ -348,6 +354,14 @@ public static function whereClauseSingle(&$values, &$query) {
$query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
}
break;

case 'source_contact':
case 'source_contact_id':
$columnName = strstr($name, '_id') ? 'id' : 'sort_name';
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("source_contact.{$columnName}", $op, $value, CRM_Utils_Type::typeToString($fields[$name]['type']));
list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contact_DAO_Contact', $columnName, $value, $op);
$query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value));
break;
}
}

Expand Down Expand Up @@ -402,9 +416,8 @@ public static function from($name, $mode, $side) {
$activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
$sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
$from = "
LEFT JOIN civicrm_activity_contact ac
ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
INNER JOIN civicrm_contact source_contact ON
(civicrm_activity_contact.contact_id = source_contact.id) AND civicrm_activity_contact.record_type_id = {$sourceID}";
break;

case 'parent_id':
Expand Down
60 changes: 46 additions & 14 deletions tests/phpunit/CRM/Contact/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,30 @@ public function testSelectorQuery($dataSet) {
// Ensure that search builder return individual contact as per criteria
if (!empty($dataSet['context'] == 'builder')) {
$contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']);
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactID,
'location_type_id' => "Home",
'is_primary' => 1,
'country_id' => "IN",
]);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
$this->assertEquals(1, count($rows));
$sortChar = $selector->alphabetQuery()->fetchAll();
// sort name is stored in '<last_name>, <first_name>' format, as per which the first character would be B of Bond
$this->assertEquals('B', $sortChar[0]['sort_name']);
$this->assertEquals($contactID, key($rows));
if ('Search builder behaviour for Activity' == $dataSet['description']) {
$this->callAPISuccess('Activity', 'create', [
'activity_type_id' => 'Meeting',
'subject' => "Test",
'source_contact_id' => $contactID,
]);
$rows = CRM_Core_DAO::executeQuery(implode(' ', $sql))->fetchAll();
$this->assertEquals(1, count($rows));
$this->assertEquals($contactID, $rows[0]['source_contact_id']);
}
else {
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactID,
'location_type_id' => "Home",
'is_primary' => 1,
'country_id' => "IN",
]);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
$this->assertEquals(1, count($rows));
$sortChar = $selector->alphabetQuery()->fetchAll();
// sort name is stored in '<last_name>, <first_name>' format, as per which the first character would be B of Bond
$this->assertEquals('B', $sortChar[0]['sort_name']);
$this->assertEquals($contactID, key($rows));
}
}
}

Expand Down Expand Up @@ -255,6 +267,26 @@ public function querySets() {
),
),
),
array(
array(
'description' => 'Search builder behaviour for Activity',
'class' => 'CRM_Contact_Selector',
'settings' => array(),
'form_values' => array('source_contact_id' => array('IS NOT NULL' => 1)),
'params' => array(),
'return_properties' => array(
'source_contact_id' => 1,
),
'context' => 'builder',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => array(
0 => 'SELECT contact_a.id as contact_id, source_contact.id as source_contact_id',
2 => 'WHERE ( source_contact.id IS NOT NULL ) AND (contact_a.is_deleted = 0)',
),
),
),
);
}

Expand Down Expand Up @@ -441,10 +473,10 @@ public function strWrangle($string) {
* @param array $expectedQuery
*/
public function wrangleDefaultClauses(&$expectedQuery) {
if ($expectedQuery[0] == 'default') {
if (CRM_Utils_Array::value(0, $expectedQuery) == 'default') {
$expectedQuery[0] = $this->getDefaultSelectString();
}
if ($expectedQuery[1] == 'default') {
if (CRM_Utils_Array::value(1, $expectedQuery) == 'default') {
$expectedQuery[1] = $this->getDefaultFromString();
}
}
Expand Down

0 comments on commit c5b63c7

Please sign in to comment.