diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index bd6da793e74..0076679d63f 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -381,7 +381,11 @@ class CRM_Contact_BAO_Query { * so we can skip the other */ protected $_rangeCache = array(); - +/** + * Set to true when $this->relationship is run to avoid adding twice + * @var Boolean + */ + protected $_relationshipValuesAdded = FALSE; /** * class constructor which also does all the work * @@ -1539,7 +1543,12 @@ function whereClauseSingle(&$values) { return; case 'relation_type_id': + case 'relation_start_date_high': + case 'relation_start_date_low': + case 'relation_end_date_high': + case 'relation_end_date_low': $this->relationship($values); + $this->_relationshipValuesAdded = TRUE; return; case 'relation_target_name': @@ -3644,7 +3653,9 @@ function preferredCommunication(&$values) { */ function relationship(&$values) { list($name, $op, $value, $grouping, $wildcard) = $values; - + if($this->_relationshipValuesAdded){ + return; + } // also get values array for relation_target_name // for relatinship search we always do wildcard $targetName = $this->getWhereValues('relation_target_name', $grouping); @@ -3672,17 +3683,12 @@ function relationship(&$values) { $rel = explode('_', $value); self::$_relType = $rel[1]; - - $params = array('id' => $rel[0]); - $rTypeValues = array(); - $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues); - if (!$rType) { - return; - } - - if ($rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) { - self::$_relType = 'reciprocal'; - } + $params = array('id' => $rel[0]); + $rTypeValues = array(); + $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues); + if ($rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) { + self::$_relType = 'reciprocal'; + } if ($nameClause) { $this->_where[$grouping][] = "( contact_b.sort_name $nameClause AND contact_b.id != contact_a.id )"; @@ -3748,22 +3754,40 @@ function relationship(&$values) { $this->_qill[$grouping][] = ts('Relationship - Inactive or not Current'); } - // Search by dates - if ($start || $end) { - foreach (array('start' => '>=', 'end' => '<=') as $d => $op) { - if (!empty(${$d}[2])) { - $date = date('Ymd', strtotime(${$d}[2])); - $this->_where[$grouping][] = "civicrm_relationship.{$d}_date $op $date"; - $this->_qill[$grouping][] = ($d == 'end' ? ts('Relationship Ended by') : ts('Relationship Started On or After')) . " " . CRM_Utils_Date::customFormat($date); - } - } + $this->addRelationshipDateClauses($grouping); + if(!empty($rType) && isset($rType->id)){ + $this->_where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id; } - - $this->_where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rel[0]; $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1; $this->_useDistinct = TRUE; + $this->_relationshipValuesAdded = TRUE; } +/** + * Add start & end date criteria in + * @param string $grouping + */ + function addRelationshipDateClauses($grouping){ + $dateValues = array(); + $dateTypes = array( + 'start_date', + 'end_date', + ); + foreach ($dateTypes as $dateField){ + $dateValueLow = $this->getWhereValues('relation_'. $dateField .'_low', $grouping); + $dateValueHigh= $this->getWhereValues('relation_'. $dateField .'_high', $grouping); + if(!empty($dateValueLow)){ + $date = date('Ymd', strtotime($dateValueLow[2])); + $this->_where[$grouping][] = "civicrm_relationship.$dateField >= $date"; + $this->_qill[$grouping][] = ($dateField == 'end_date' ? ts('Relationship Ended on or After') : ts('Relationship Recorded Start Date On or Before')) . " " . CRM_Utils_Date::customFormat($date); + } + if(!empty($dateValueHigh)){ + $date = date('Ymd', strtotime($dateValueHigh[2])); + $this->_where[$grouping][] = "civicrm_relationship.$dateField <= $date"; + $this->_qill[$grouping][] = ( $dateField == 'end_date' ? ts('Relationship Ended on or Before') : ts('Relationship Recorded Start Date On or After')) . " " . CRM_Utils_Date::customFormat($date); + } + } + } /** * default set of return properties * diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 8af5ba00260..e6ddda1d4fd 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -442,6 +442,8 @@ static function relationship(&$form) { array('id' => 'relation_target_group', 'multiple' => 'multiple', 'title' => ts('- select -')) ); } + CRM_Core_Form_Date::buildDateRange($form, 'relation_start_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE); + CRM_Core_Form_Date::buildDateRange($form, 'relation_end_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE); // Add reltionship dates CRM_Core_Form_Date::buildDateRange($form, 'relation_date', 1, '_low', '_high', ts('From:'), FALSE, FALSE); diff --git a/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl b/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl index fffb15827b8..91890b87109 100644 --- a/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl +++ b/templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl @@ -31,7 +31,7 @@ {$form.relation_type_id.html} -
+
{$form.relation_target_name.label}
{$form.relation_target_name.html|crmAddClass:huge}
@@ -40,7 +40,7 @@
- + {$form.relation_status.label}
{$form.relation_status.html} @@ -50,11 +50,17 @@ {$form.relation_target_group.html|crmAddClass:huge} - - + + + + + {include file="CRM/Core/DateRange.tpl" fieldName="relation_start_date" from='_low' to='_high'} - - {include file="CRM/Core/DateRange.tpl" fieldName="relation_date" from='_low' to='_high'} + + + + + {include file="CRM/Core/DateRange.tpl" fieldName="relation_end_date" from='_low' to='_high'} {if $relationshipGroupTree} @@ -73,13 +79,6 @@ sortable: true, respectParents: true }); - cj("#relation_type_id").change(function() { - if (cj(this).val() == '') { - cj('.relation-type-dependent').hide(); - } else { - cj('.relation-type-dependent').show(); - } - }).change(); {/literal}