diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 9d5e64a38a51..dc1fbd7dabf6 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -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); @@ -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); @@ -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 diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 2d5695b78d03..d6f731b0a844 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -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')); diff --git a/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl b/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl index 34fcb615d5bb..14619c0b5813 100644 --- a/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl +++ b/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl @@ -53,6 +53,12 @@ {$form.relation_target_group.html|crmAddClass:huge} + + + {$form.relation_description.label}
+ {$form.relation_description.html} + + diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 824a8ce215a0..f2cf4af3e104 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -754,6 +754,40 @@ public function testRelationshipClause() { $this->assertLike($where2, $sql5[2]); } + public function testRelationshipDescription() { + $contactID_a = $this->individualCreate([], 1); + $contactID_b = $this->individualCreate([], 2); + $contactID_c = $this->individualCreate([], 3); + $contactID_d = $this->individualCreate([], 4); + $desc = uniqid(); + $this->callAPISuccess('Relationship', 'create', [ + 'contact_id_a' => $contactID_a, + 'contact_id_b' => $contactID_b, + 'relationship_type_id' => 1, + 'is_active' => 1, + 'description' => $desc, + ]); + $this->callAPISuccess('Relationship', 'create', [ + 'contact_id_a' => $contactID_c, + 'contact_id_b' => $contactID_d, + 'relationship_type_id' => 1, + 'is_active' => 1, + 'description' => 'nothing of interest', + ]); + $params = [ + ['relation_description', '=', substr($desc, 1, 10), 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?"); + } + /** * Test we can narrow a group get by status. *