dev/core#1257 Make relationship description searchable
authorColeman Watts <coleman@civicrm.org>
Sat, 28 Sep 2019 06:15:32 +0000 (02:15 -0400)
committerColeman Watts <coleman@civicrm.org>
Tue, 1 Oct 2019 00:27:32 +0000 (20:27 -0400)
CRM/Contact/BAO/Query.php
CRM/Contact/Form/Search/Criteria.php
templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl
tests/phpunit/CRM/Contact/BAO/QueryTest.php

index 9d5e64a38a51f8abc6ded53b8399ec8d61e7d0cd..dc1fbd7dabf670e850905629b797a429ac6e3843 100644 (file)
@@ -2032,6 +2032,7 @@ class CRM_Contact_BAO_Query {
       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 @@ WHERE  $smartGroupClause
     // 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 @@ WHERE  $smartGroupClause
       }
     }
 
+    // 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
index 2d5695b78d03bdf6be3d113a1cd627c89a475b3f..d6f731b0a8448bd74f55109aadc8fd64d64cfefe 100644 (file)
@@ -487,12 +487,12 @@ class CRM_Contact_Form_Search_Criteria {
   }
 
   /**
-   * @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'));
index 34fcb615d5bb803719905058d69ba93722aa7918..14619c0b5813e33b91363b19b59bcd7129efe940 100644 (file)
         {$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>
index 824a8ce215a0267b874dc3aafea2b8d1f4f8ad96..24dba459aba4f63d99c2448da49e1c99adc6d68d 100644 (file)
@@ -548,6 +548,44 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
 
   }
 
+  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();