}
}
- $rTypeValues = $relTypes = array();
+ $rTypeValues = $relTypes = $relTypesIds = array();
if (!empty($relationType)) {
$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);
+ $rTypeValue = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues);
+ if (!empty($rTypeValue)) {
+ $rTypeValues[] = $rTypeValue;
+ $relTypesIds[] = $rel[0];
+ $relTypes[] = $relType;
+ }
}
}
if (!empty($rTypeValues)) {
if ($nameClause || !$targetGroup) {
if (!empty($relationType)) {
$relQill = '';
- foreach ($relationType[2] as $rel) {
+ foreach ($relTypes as $rel) {
if (!empty($relQill)) {
$relQill .= ' OR ';
}
}
if (!empty($relationType)) {
$relQill = '';
- foreach ($relationType[2] as $rel) {
+ foreach ($relTypes as $rel) {
if (!empty($relQill)) {
$relQill .= ' OR ';
}
$this->addRelationshipDateClauses($grouping, $where);
$this->addRelationshipActivePeriodClauses($grouping, $where);
if (!empty($relTypes)) {
- $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypes) . ')';
+ $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')';
}
$this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
$this->_useDistinct = TRUE;
}
+ /**
+ * Test Relationship Clause
+ */
+ public function testRelationshipClause() {
+ $today = date('Ymd');
+ $where1 = "WHERE ( (
+civicrm_relationship.is_active = 1 AND
+( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
+( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
+) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8) ) AND (contact_a.is_deleted = 0)";
+ $where2 = "WHERE ( (
+civicrm_relationship.is_active = 1 AND
+( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
+( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
+) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8,10) ) AND (contact_a.is_deleted = 0)";
+ // Test Traditional single select format
+ $params1 = array(array('relation_type_id', '=', '8_a_b', 0, 0));
+ $query1 = new CRM_Contact_BAO_Query(
+ $params1, array('contact_id'),
+ NULL, TRUE, FALSE, 1,
+ TRUE,
+ TRUE, FALSE
+ );
+ $sql1 = $query1->query(FALSE);
+ $this->assertEquals($where1, $sql1[2]);
+ // Test single relationship type selected in multiple select.
+ $params2 = array(array('relation_type_id', 'IN', array('8_a_b'), 0, 0));
+ $query2 = new CRM_Contact_BAO_Query(
+ $params2, array('contact_id'),
+ NULL, TRUE, FALSE, 1,
+ TRUE,
+ TRUE, FALSE
+ );
+ $sql2 = $query2->query(FALSE);
+ $this->assertEquals($where1, $sql2[2]);
+ // Test multiple relationship types selected.
+ $params3 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b'), 0, 0));
+ $query3 = new CRM_Contact_BAO_Query(
+ $params3, array('contact_id'),
+ NULL, TRUE, FALSE, 1,
+ TRUE,
+ TRUE, FALSE
+ );
+ $sql3 = $query3->query(FALSE);
+ $this->assertEquals($where2, $sql3[2]);
+ // Test Multiple Relationship type selected where one doesn't actually exist.
+ $params4 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b', '14_a_b'), 0, 0));
+ $query4 = new CRM_Contact_BAO_Query(
+ $params4, array('contact_id'),
+ NULL, TRUE, FALSE, 1,
+ TRUE,
+ TRUE, FALSE
+ );
+ $sql4 = $query4->query(FALSE);
+ $this->assertEquals($where2, $sql4[2]);
+ }
+
/**
* Test the group contact clause does not contain an OR.
*