Skip to content

Commit

Permalink
CRM-12619 fix relationship search start end dates to be separate & do…
Browse files Browse the repository at this point in the history
…n't require type
  • Loading branch information
eileenmcnaughton committed Jun 22, 2013
1 parent 9c6c018 commit ad68213
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
72 changes: 48 additions & 24 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 )";
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 2 additions & 0 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 12 additions & 13 deletions templates/CRM/Contact/Form/Search/Criteria/Relationship.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
{$form.relation_type_id.html}
</td>
<td>
<div class="relation-type-dependent">
<div>
{$form.relation_target_name.label}<br />
{$form.relation_target_name.html|crmAddClass:huge}
<div class="description font-italic">
Expand All @@ -40,7 +40,7 @@
</div>
</td>
</tr>
<tr class="relation-type-dependent">
<tr>
<td>
{$form.relation_status.label}<br />
{$form.relation_status.html}
Expand All @@ -50,11 +50,17 @@
{$form.relation_target_group.html|crmAddClass:huge}
</td>
</tr>
<tr class="relation-type-dependent">
<td colspan="2"><label>{ts}Start/End Dates{/ts}</label></td>
<tr>
<td colspan="2"><label>{ts}Start Date{/ts}</label></td>
</tr>
<tr>
{include file="CRM/Core/DateRange.tpl" fieldName="relation_start_date" from='_low' to='_high'}
</tr>
<tr class="relation-type-dependent">
{include file="CRM/Core/DateRange.tpl" fieldName="relation_date" from='_low' to='_high'}
<tr>
<td colspan="2"><label>{ts}End Date{/ts}</label></td>
</tr>
<tr>
{include file="CRM/Core/DateRange.tpl" fieldName="relation_end_date" from='_low' to='_high'}
</tr>
{if $relationshipGroupTree}
<tr>
Expand All @@ -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();
</script>
{/literal}
</div>

0 comments on commit ad68213

Please sign in to comment.