From 026242fcb163d7e421de6d078510935d34c58c48 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 26 Jan 2018 17:48:58 +1100 Subject: [PATCH] Allow for selecting multiple relationship types in Advanced Search --- CRM/Contact/BAO/Query.php | 55 +++++++++++++++++++--------- CRM/Contact/Form/Search/Criteria.php | 2 +- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 48f4852bd2..c52ab366d1 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4050,17 +4050,27 @@ WHERE $smartGroupClause } } - $rTypeValues = array(); + $rTypeValues = $relTypes = array(); 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) && $rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) { - // 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'; + $relationType[2] = (array) $relationType[2]; + foreach ($relationType[2] as $relType) { + $rel = explode('_', $relType); + self::$_relType .= $rel[1]; + $params = array('id' => $rel[0]); + $relTypes[] = $rel[0]; + $typeValues = array(); + $rTypeValues[] = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues); + } + } + if (!empty($rTypeValues)) { + foreach ($rTypeValues as $rTypeValue) { + $rTypeValue = (array) $rTypeValue; + if ($rTypeValue['name_a_b'] == $rTypeValue['name_b_a']) { + // 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'; + } + } } // if we are creating a temp table we build our own where for the relationship table $relationshipTempTable = NULL; @@ -4077,12 +4087,17 @@ WHERE $smartGroupClause $where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )"; } } - $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE); - if ($nameClause || !$targetGroup) { if (!empty($relationType)) { - $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " $name"; + $relQill = ''; + foreach ($relationType[2] as $rel) { + if (!empty($relQill)) { + $relQill .= ' OR '; + } + $relQill .= $allRelationshipType[$rel]; + } + $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " $name"; } else { $this->_qill[$grouping][] = $name; @@ -4115,7 +4130,14 @@ WHERE $smartGroupClause } } if (!empty($relationType)) { - $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " ( " . implode(", ", $qillNames) . " )"; + $relQill = ''; + foreach ($relationType[2] as $rel) { + if (!empty($relQill)) { + $relQill .= ' OR '; + } + $relQill .= $allRelationshipType[$rel]; + } + $this->_qill[$grouping][] = 'Relationship Type(s) ' . $relQill . " ( " . implode(", ", $qillNames) . " )"; } else { $this->_qill[$grouping][] = implode(", ", $qillNames); @@ -4165,8 +4187,8 @@ civicrm_relationship.is_permission_a_b = 0 $this->addRelationshipDateClauses($grouping, $where); $this->addRelationshipActivePeriodClauses($grouping, $where); - if (!empty($relationType) && !empty($rType) && isset($rType->id)) { - $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id; + if (!empty($relTypes)) { + $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypes) . ')'; } $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1; $this->_useDistinct = TRUE; @@ -4196,7 +4218,6 @@ civicrm_relationship.is_permission_a_b = 0 "; CRM_Core_DAO::executeQuery($sql); } - } /** diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index f876e40e81..db4f6d0631 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -416,7 +416,7 @@ class CRM_Contact_Form_Search_Criteria { $allRelationshipType = array(); $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); - $form->add('select', 'relation_type_id', ts('Relationship Type'), array('' => ts('- select -')) + $allRelationshipType, FALSE, array('class' => 'crm-select2')); + $form->add('select', 'relation_type_id', ts('Relationship Type'), array('' => ts('- select -')) + $allRelationshipType, FALSE, array('multiple' => TRUE, 'class' => 'crm-select2')); $form->addElement('text', 'relation_target_name', ts('Target Contact'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); // relation status $relStatusOption = array(ts('Active'), ts('Inactive'), ts('All')); -- 2.25.1