Skip to content

Commit

Permalink
Fix #162 - date field filters in person reports
Browse files Browse the repository at this point in the history
  • Loading branch information
tbar0970 committed Mar 9, 2016
1 parent d218f2d commit 5344520
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
34 changes: 21 additions & 13 deletions db_objects/person_query.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,24 +824,32 @@ function getSQL($select_fields=NULL)
'after' => Array(1, $length+1)
);
list($so, $eo) = $offsets[$values['periodanchor']];
$between = 'BETWEEN CURDATE() + INTERVAL '.$so.' DAY AND CURDATE() + INTERVAL '.$eo.' DAY';
if ($so > 0) $so = "+$so";
if ($eo > 0) $eo = "+$eo";
$from = date('Y-m-d', strtotime("{$so} days"));
$to = date('Y-m-d', strtotime("{$eo} days"));
} else {
$between = 'BETWEEN '.$db->quote($values['from']).' AND '.$db->quote($values['to']);
$from = $values['from'];
$to = $values['to'];
}
$betweenExp = 'BETWEEN '.$db->quote($from).' AND '.$db->quote($to);
$valExp = 'pd'.$fieldid.'.value_date';
$w = Array();
$w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%"
AND pd'.$fieldid.'.`value_date` '.$between.')';
$w[] = "$valExp NOT LIKE '-%' AND $valExp $betweenExp";
if ($values['criteria'] == 'anniversary') {
// Anniversary matches either have no year or a year before the 'to' year
// AND their month-day fits the range either in the from year or the to year.
$fromyearbetween = 'CONCAT('.$db->quote(substr($values['from'], 0, 4)).', RIGHT(pd'.$fieldid.'.`value_date`, 6)) '.$between;
$toyearbetween = 'CONCAT('.$db->quote(substr($values['to'], 0, 4)).', RIGHT(pd'.$fieldid.'.`value_date`, 6)) '.$between;
$w[] = '(pd'.$fieldid.'.`value_date` LIKE "-%" AND '.$fromyearbetween.')';
$w[] = '(pd'.$fieldid.'.`value_date` LIKE "-%" AND '.$toyearbetween.')';
$w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%" AND pd'.$fieldid.'.`value_date` < '.$db->quote($values['to']).' AND '.$fromyearbetween.')';
$w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%" AND pd'.$fieldid.'.`value_date` < '.$db->quote($values['to']).' AND '.$toyearbetween.')';
$qFromYear = $db->quote(substr($from, 0, 4));
$qToYear = $db->quote(substr($to, 0, 4));

$w[] = "$valExp LIKE '-%' AND (
CONCAT($qFromYear, $valExp) $betweenExp
OR CONCAT($qToYear, $valExp) $betweenExp
)";
$w[] = "$valExp NOT LIKE '-%' AND (
CONCAT($qFromYear, RIGHT($valExp, 6)) $betweenExp
OR CONCAT($qToYear, RIGHT($valExp, 6)) $betweenExp
)";
}
$customFieldWheres[] = '('.implode(' OR ', $w).')';
$customFieldWheres[] = '(('.implode("\n) OR (\n", $w).'))';
break;

}
Expand Down
1 change: 1 addition & 0 deletions include/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function format_datetime($d)

function format_date($d, $includeYear=NULL)
{
if ($d == '0000-00-00') return '';
$yearless = is_string($d) && ($d[0] == '-');
if (!is_int($d)) {
$d = strtotime($yearless ? "2012{$d}" : $d);
Expand Down

0 comments on commit 5344520

Please sign in to comment.