tracking/Classes/Dashboard/Provider/PageviewsPerDay.php

142 lines
4.3 KiB
PHP
Raw Normal View History

2020-02-23 22:41:41 +01:00
<?php
declare(strict_types=1);
2020-02-23 22:41:41 +01:00
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace DanielSiepmann\Tracking\Dashboard\Provider;
2020-02-25 20:06:38 +01:00
use DanielSiepmann\Tracking\Extension;
2020-02-23 22:41:41 +01:00
use TYPO3\CMS\Core\Database\Connection;
2020-02-24 14:49:31 +01:00
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
2020-02-23 22:41:41 +01:00
class PageviewsPerDay implements ChartDataProviderInterface
2020-02-23 22:41:41 +01:00
{
/**
* @var LanguageService
*/
private $languageService;
2020-02-23 22:41:41 +01:00
/**
* @var QueryBuilder
*/
private $queryBuilder;
2020-02-23 22:41:41 +01:00
/**
* @var Demand
*/
private $demand;
2020-02-24 14:49:31 +01:00
/**
* @var string
*/
private $dateFormat;
public function __construct(
LanguageService $languageService,
QueryBuilder $queryBuilder,
Demand $demand,
string $dateFormat = 'Y-m-d'
) {
$this->languageService = $languageService;
2020-02-24 14:49:31 +01:00
$this->queryBuilder = $queryBuilder;
$this->demand = $demand;
$this->dateFormat = $dateFormat;
2020-02-24 14:49:31 +01:00
}
public function getChartData(): array
2020-02-23 22:41:41 +01:00
{
list($labels, $data) = $this->calculateData();
2020-02-23 22:41:41 +01:00
return [
2020-02-23 22:41:41 +01:00
'labels' => $labels,
'datasets' => [
[
'label' => $this->languageService->sL(
Extension::LANGUAGE_PATH . 'widgets.pageViewsBar.chart.dataSet.0'
2020-02-23 22:41:41 +01:00
),
'backgroundColor' => WidgetApi::getDefaultChartColors()[0],
2020-02-23 22:41:41 +01:00
'border' => 0,
'data' => $data
]
]
];
}
private function calculateData(): array
{
$labels = [];
$data = [];
for ($daysBefore = $this->demand->getDays(); $daysBefore >= 0; $daysBefore--) {
$label = date($this->dateFormat, (int) strtotime('-' . $daysBefore . ' day'));
$labels[$label] = $label;
$data[$label] = 0;
}
$start = (int) strtotime('-' . $this->demand->getDays() . ' day 0:00:00');
$end = (int) strtotime('tomorrow midnight');
foreach ($this->getPageviewsInPeriod($start, $end) as $day) {
$data[$day['label']] = (int) $day['count'];
}
return [
array_values($labels),
array_values($data),
];
}
private function getPageviewsInPeriod(int $start, int $end): array
2020-02-23 22:41:41 +01:00
{
$constraints = [
$this->queryBuilder->expr()->gte('tx_tracking_pageview.crdate', $start),
$this->queryBuilder->expr()->lte('tx_tracking_pageview.crdate', $end),
];
$constraints = array_merge($constraints, $this->demand->getConstraints(
$this->queryBuilder,
'tx_tracking_pageview'
));
$this->demand->addJoins($this->queryBuilder, 'tx_tracking_pageview');
$this->queryBuilder
->addSelectLiteral('COUNT(tx_tracking_pageview.uid) as "count"')
2020-02-23 22:41:41 +01:00
->from('tx_tracking_pageview')
->where(...$constraints)
->groupBy('label')
->orderBy('label', 'ASC')
;
if ($this->queryBuilder->getConnection()->getDatabasePlatform()->getName() === 'sqlite') {
$this->queryBuilder->addSelectLiteral('date(tx_tracking_pageview.crdate, "unixepoch") as "label"');
} else {
$this->queryBuilder->addSelectLiteral('FROM_UNIXTIME(tx_tracking_pageview.crdate, "%Y-%m-%d") as "label"');
}
return $this->queryBuilder->execute()->fetchAll();
2020-02-23 22:41:41 +01:00
}
}