Skip to content

Commit

Permalink
refs matomo-org#4532 this should fix custom data range values are not…
Browse files Browse the repository at this point in the history
… always working. In this case periods like week, month or year were ignored in case the data range was large enough to use one of those
  • Loading branch information
tsteur committed Jan 29, 2014
1 parent 29c7d24 commit 40eabd8
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions core/DataAccess/ArchiveSelector.php
Expand Up @@ -151,8 +151,7 @@ static public function getArchiveIds($siteIds, $periods, $segment, $plugins)
{
$getArchiveIdsSql = "SELECT idsite, name, date1, date2, MAX(idarchive) as idarchive
FROM %s
WHERE period = ?
AND %s
WHERE %s
AND " . self::getNameCondition($plugins, $segment) . "
AND idsite IN (" . implode(',', $siteIds) . ")
GROUP BY idsite, date1, date2";
Expand All @@ -169,19 +168,48 @@ static public function getArchiveIds($siteIds, $periods, $segment, $plugins)
foreach ($monthToPeriods as $table => $periods) {
$firstPeriod = reset($periods);

// if looking for a range archive. NOTE: we assume there's only one period if its a range.
$bind = array($firstPeriod->getId());
$bind = array();

if ($firstPeriod instanceof Range) {
$dateCondition = "date1 = ? AND date2 = ?";
$dateCondition = "period = ? AND date1 = ? AND date2 = ?";
$bind[] = $firstPeriod->getId();
$bind[] = $firstPeriod->getDateStart()->toString('Y-m-d');
$bind[] = $firstPeriod->getDateEnd()->toString('Y-m-d');
} else { // if looking for a normal period
$dateStrs = array();
} else {
// we assume there is no range date in $periods
$dateStrs = array();
$dayPeriod = null;
$dateCondition = '(';

foreach ($periods as $period) {
$dateStrs[] = $period->getDateStart()->toString('Y-m-d');
if ($period instanceof Period\Day) {
$dateStrs[] = $period->getDateStart()->toString('Y-m-d');
$dayPeriod = $period;
}
}

$dateCondition = "date1 IN ('" . implode("','", $dateStrs) . "')";
if (!empty($dayPeriod) && !empty($dateStrs)) {
$bind[] = $dayPeriod->getId();
$dateCondition .= "(period = ? AND date1 IN ('" . implode("','", $dateStrs) . "'))";
}

reset($periods);
foreach ($periods as $period) {
if ($period instanceof Period\Week || $period instanceof Period\Month || $period instanceof Period\Year) {

if (strlen($dateCondition) > 5) {
$dateCondition .= ' OR ';
}

$dateCondition .= "(period = ? AND date1 = ? AND date2 = ?)";
$bind[] = $period->getId();
$bind[] = $period->getDateStart()->toString('Y-m-d');
$bind[] = $period->getDateEnd()->toString('Y-m-d');
}
}

$dateCondition .= ')';

}

$sql = sprintf($getArchiveIdsSql, $table, $dateCondition);
Expand Down

0 comments on commit 40eabd8

Please sign in to comment.