CRM-15121 - Fix searching for Permissioned Relationship property in Advanced Search.
authorDave Greenberg <dave@civicrm.org>
Wed, 27 Aug 2014 21:55:26 +0000 (14:55 -0700)
committerDave Greenberg <dave@civicrm.org>
Wed, 27 Aug 2014 21:55:26 +0000 (14:55 -0700)
----------------------------------------
* CRM-15121:
  https://issues.civicrm.org/jira/browse/CRM-15121

CRM/Contact/BAO/Query.php

index 4821f15a15d8238cc8ee3f6fd113f0b1b3a25cef..4a4330d6f4572d5fc5d777bd3679a97e7c7f3593 100644 (file)
@@ -4029,11 +4029,12 @@ WHERE  id IN ( $groupIDs )
     }
     // also get values array for relation_target_name
     // for relatinship search we always do wildcard
+    $relationType = $this->getWhereValues('relation_type_id', $grouping);    
     $targetName = $this->getWhereValues('relation_target_name', $grouping);
     $relStatus = $this->getWhereValues('relation_status', $grouping);
     $relPermission = $this->getWhereValues('relation_permission', $grouping);
     $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
-
+    
     $nameClause = $name = NULL;
     if ($targetName) {
       $name = trim($targetName[2]);
@@ -4050,13 +4051,14 @@ WHERE  id IN ( $groupIDs )
       }
     }
 
-    $rel = explode('_', $value);
-
-    self::$_relType = $rel[1];
-    $params = array('id' => $rel[0]);
     $rTypeValues = array();
-    $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
-    if ($rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
+    if( !empty($relationType) ) {
+      $rel = explode('_', $relationType[2]);
+      self::$_relType = $rel[1];
+      $params = array('id' => $rel[0]);
+      $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
+    }
+    if ( empty($rTypeValues) ) {    
       // if we don't know which end of the relationship we are dealing with we'll create a temp table
       //@todo unless we are dealing with a target group
       self::$_relType = 'reciprocal';
@@ -4088,7 +4090,11 @@ WHERE  id IN ( $groupIDs )
     $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
 
     if ($nameClause || !$targetGroup) {
-      $this->_qill[$grouping][] = "$allRelationshipType[$value]  $name";
+      if( !empty($relationType) ) {
+        $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] ." $name";
+      } else {
+        $this->_qill[$grouping][] = $name;
+      }
     }
 
 
@@ -4119,7 +4125,11 @@ WHERE  id IN ( $groupIDs )
           $qillNames[] = $groupNames[$groupId];
         }
       }
-      $this->_qill[$grouping][] = "$allRelationshipType[$value]  ( " . implode(", ", $qillNames) . " )";
+      if ( !empty($relationType) ) {
+        $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] ." ( " . implode(", ", $qillNames) . " )";
+      } else {
+        $this->_qill[$grouping][] = implode(", ", $qillNames);
+      }
     }
 
     // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
@@ -4144,20 +4154,20 @@ civicrm_relationship.start_date > {$today}
 
     //check for permissioned, non-permissioned and all permissioned relations
     if ($relPermission[2] == 1) {
-      $this->_where[$grouping][] = "(
+      $where[$grouping][] = "(
 civicrm_relationship.is_permission_a_b = 1
 )";
       $this->_qill[$grouping][] = ts('Relationship - Permissioned');
     } elseif ($relPermission[2] == 2) {
       //non-allowed permission relationship.
-      $this->_where[$grouping][] = "(
+      $where[$grouping][] = "(
 civicrm_relationship.is_permission_a_b = 0
 )";
       $this->_qill[$grouping][] = ts('Relationship - Non-permissioned');
     }
 
     $this->addRelationshipDateClauses($grouping, $where);
-    if(!empty($rType) && isset($rType->id)){
+    if( !empty($relationType) && !empty($rType) && isset($rType->id)){
       $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id;
     }
     $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
@@ -4168,8 +4178,11 @@ civicrm_relationship.is_permission_a_b = 0
     // @todo creating a temp table could be expanded to group filter
     // as even creating a temp table of all relationships is much much more efficient than
     // an OR in the join
-    if($relationshipTempTable) {
-      $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
+    if ($relationshipTempTable) {
+      $whereClause = '';
+      if ( ! empty( $where[$grouping] ) ) {
+        $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
+      }
       $sql = "
         CREATE TEMPORARY TABLE {$relationshipTempTable}
           (SELECT contact_id_b as contact_id, civicrm_relationship.id