Skip to content

Commit

Permalink
dev/core#1257 Make relationship description searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Oct 1, 2019
1 parent 53e64ff commit 6e83b31
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,7 @@ public function whereClauseSingle(&$values, $isForcePrimaryOnly = FALSE) {
case 'relation_active_period_date_low':
case 'relation_target_name':
case 'relation_status':
case 'relation_description':
case 'relation_date_low':
case 'relation_date_high':
$this->relationship($values);
Expand Down Expand Up @@ -4160,6 +4161,7 @@ public function relationship(&$values) {
// also get values array for relation_target_name
// for relationship search we always do wildcard
$relationType = $this->getWhereValues('relation_type_id', $grouping);
$description = $this->getWhereValues('relation_description', $grouping);
$targetName = $this->getWhereValues('relation_target_name', $grouping);
$relStatus = $this->getWhereValues('relation_status', $grouping);
$targetGroup = $this->getWhereValues('relation_target_group', $grouping);
Expand Down Expand Up @@ -4276,6 +4278,13 @@ public function relationship(&$values) {
}
}

// Description
if (!empty($description[2]) && trim($description[2])) {
$this->_qill[$grouping][] = ts('Relationship description - ' . $description[2]);
$description = CRM_Core_DAO::escapeString(trim($description[2]));
$where[$grouping][] = "civicrm_relationship.description LIKE '%{$description}%'";
}

// Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
$today = date('Ymd');
//check for active, inactive and all relation status
Expand Down
4 changes: 2 additions & 2 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,12 @@ public static function task(&$form) {
}

/**
* @param $form
* @param CRM_Core_Form_Search $form
*/
public static function relationship(&$form) {
$form->add('hidden', 'hidden_relationship', 1);

$allRelationshipType = [];
$form->add('text', 'relation_description', ts('Description'), ['class' => 'twenty']);
$allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
$form->add('select', 'relation_type_id', ts('Relationship Type'), ['' => ts('- select -')] + $allRelationshipType, FALSE, ['multiple' => TRUE, 'class' => 'crm-select2']);
$form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
Expand Down
6 changes: 6 additions & 0 deletions templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
{$form.relation_target_group.html|crmAddClass:huge}
</td>
</tr>
<tr>
<td colspan="2">
{$form.relation_description.label}<br />
{$form.relation_description.html}
</td>
</tr>
<tr>
<td colspan="2"><label>{ts}Start Date{/ts}</label></td>
</tr>
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,44 @@ public function testNonNumericEqualsPostal() {

}

public function testRelationshipDescription() {
$relType = $this->callAPISuccess('RelationshipType', 'create', [
'name_a_b' => uniqid('a'),
'name_b_a' => uniqid('b'),
]);
$contactID_a = $this->individualCreate([], 1);
$contactID_b = $this->individualCreate([], 2);
$contactID_c = $this->individualCreate([], 3);
$contactID_d = $this->individualCreate([], 4);
$desc = uniqid('rel', TRUE);
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $contactID_a,
'contact_id_b' => $contactID_b,
'relationship_type_id' => $relType['id'],
'is_active' => 1,
'description' => $desc,
]);
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $contactID_c,
'contact_id_b' => $contactID_d,
'relationship_type_id' => $relType['id'],
'is_active' => 1,
'description' => 'nothing of interest',
]);
$params = [
['relation_description', '=', substr($desc, 3, 18), 0, 0],
];

$query = new CRM_Contact_BAO_Query($params);
$dao = $query->searchQuery();
// This is a little weird but seems consistent with the behavior of the search form in general.
// Technically there are 2 contacts who share a relationship with the description searched for,
// so one might expect the search form to return both of them instead of just Contact A... but it doesn't.
$this->assertEquals('1', $dao->N, "Search query returns exactly 1 result?");
$this->assertTrue($dao->fetch(), "Search query returns success?");
$this->assertEquals($contactID_a, $dao->contact_id, "Search query returns contact A?");
}

public function testNonReciprocalRelationshipTargetGroupIsCorrectResults() {
$contactID_a = $this->individualCreate();
$contactID_b = $this->individualCreate();
Expand Down

0 comments on commit 6e83b31

Please sign in to comment.