2020-02-23 22:41:41 +01:00
|
|
|
<?php
|
|
|
|
|
2022-09-16 13:53:49 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-09-16 13:53:49 +02:00
|
|
|
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;
|
2020-04-07 17:54:18 +02:00
|
|
|
use TYPO3\CMS\Core\Localization\LanguageService;
|
|
|
|
use TYPO3\CMS\Dashboard\WidgetApi;
|
2020-04-17 16:16:19 +02:00
|
|
|
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
|
2020-02-23 22:41:41 +01:00
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
class PageviewsPerDay implements ChartDataProviderInterface
|
2020-02-23 22:41:41 +01:00
|
|
|
{
|
2020-04-07 17:54:18 +02:00
|
|
|
/**
|
|
|
|
* @var LanguageService
|
|
|
|
*/
|
|
|
|
private $languageService;
|
2020-02-23 22:41:41 +01:00
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
/**
|
|
|
|
* @var QueryBuilder
|
|
|
|
*/
|
|
|
|
private $queryBuilder;
|
2020-02-23 22:41:41 +01:00
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $days;
|
2020-02-23 22:41:41 +01:00
|
|
|
|
2020-02-24 14:49:31 +01:00
|
|
|
/**
|
2020-04-07 17:54:18 +02:00
|
|
|
* @var array<int>
|
2020-02-24 14:49:31 +01:00
|
|
|
*/
|
2020-07-30 08:00:14 +02:00
|
|
|
private $pagesToExclude;
|
2020-02-24 14:49:31 +01:00
|
|
|
|
2020-02-25 22:33:03 +01:00
|
|
|
/**
|
2020-04-07 17:54:18 +02:00
|
|
|
* @var string
|
2020-02-25 22:33:03 +01:00
|
|
|
*/
|
2020-04-07 17:54:18 +02:00
|
|
|
private $dateFormat;
|
2020-02-25 22:33:03 +01:00
|
|
|
|
2020-08-03 12:33:02 +02:00
|
|
|
/**
|
|
|
|
* @var array<int>
|
|
|
|
*/
|
|
|
|
private $languageLimitation;
|
|
|
|
|
2020-02-25 22:33:03 +01:00
|
|
|
public function __construct(
|
2020-04-07 17:54:18 +02:00
|
|
|
LanguageService $languageService,
|
2020-02-25 22:33:03 +01:00
|
|
|
QueryBuilder $queryBuilder,
|
2020-04-07 17:54:18 +02:00
|
|
|
int $days = 31,
|
2020-07-30 08:00:14 +02:00
|
|
|
array $pagesToExclude = [],
|
2020-08-03 12:33:02 +02:00
|
|
|
array $languageLimitation = [],
|
2020-04-07 17:54:18 +02:00
|
|
|
string $dateFormat = 'Y-m-d'
|
2020-02-25 22:33:03 +01:00
|
|
|
) {
|
2020-07-30 12:22:32 +02:00
|
|
|
$this->languageService = $languageService;
|
2020-02-24 14:49:31 +01:00
|
|
|
$this->queryBuilder = $queryBuilder;
|
2020-04-07 17:54:18 +02:00
|
|
|
$this->days = $days;
|
2020-07-30 08:00:14 +02:00
|
|
|
$this->pagesToExclude = $pagesToExclude;
|
2020-08-03 12:33:02 +02:00
|
|
|
$this->languageLimitation = $languageLimitation;
|
2020-04-07 17:54:18 +02:00
|
|
|
$this->dateFormat = $dateFormat;
|
2020-02-24 14:49:31 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
public function getChartData(): array
|
2020-02-23 22:41:41 +01:00
|
|
|
{
|
2020-04-07 17:54:18 +02:00
|
|
|
list($labels, $data) = $this->calculateData();
|
2020-02-23 22:41:41 +01:00
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
return [
|
2020-02-23 22:41:41 +01:00
|
|
|
'labels' => $labels,
|
|
|
|
'datasets' => [
|
|
|
|
[
|
2020-04-07 17:54:18 +02:00
|
|
|
'label' => $this->languageService->sL(
|
|
|
|
Extension::LANGUAGE_PATH . 'widgets.pageViewsBar.chart.dataSet.0'
|
2020-02-23 22:41:41 +01:00
|
|
|
),
|
2020-04-07 17:54:18 +02:00
|
|
|
'backgroundColor' => WidgetApi::getDefaultChartColors()[0],
|
2020-02-23 22:41:41 +01:00
|
|
|
'border' => 0,
|
|
|
|
'data' => $data
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
private function calculateData(): array
|
|
|
|
{
|
|
|
|
$labels = [];
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
for ($daysBefore = $this->days; $daysBefore >= 0; $daysBefore--) {
|
2021-08-03 11:08:12 +02:00
|
|
|
$label = date($this->dateFormat, (int) strtotime('-' . $daysBefore . ' day'));
|
|
|
|
$labels[$label] = $label;
|
|
|
|
$data[$label] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$start = (int) strtotime('-' . $this->days . ' day 0:00:00');
|
|
|
|
$end = (int) strtotime('tomorrow midnight');
|
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
|
2021-08-03 11:08:12 +02:00
|
|
|
foreach ($this->getPageviewsInPeriod($start, $end) as $day) {
|
|
|
|
$data[$day['label']] = (int) $day['count'];
|
2020-04-07 17:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2021-08-03 11:08:12 +02:00
|
|
|
array_values($labels),
|
|
|
|
array_values($data),
|
2020-04-07 17:54:18 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-08-03 11:08:12 +02:00
|
|
|
private function getPageviewsInPeriod(int $start, int $end): array
|
2020-02-23 22:41:41 +01:00
|
|
|
{
|
2020-02-25 22:33:03 +01:00
|
|
|
$constraints = [
|
|
|
|
$this->queryBuilder->expr()->gte('crdate', $start),
|
|
|
|
$this->queryBuilder->expr()->lte('crdate', $end),
|
|
|
|
];
|
|
|
|
|
2020-07-30 08:00:14 +02:00
|
|
|
if (count($this->pagesToExclude)) {
|
2020-02-25 22:33:03 +01:00
|
|
|
$constraints[] = $this->queryBuilder->expr()->notIn(
|
|
|
|
'tx_tracking_pageview.pid',
|
|
|
|
$this->queryBuilder->createNamedParameter(
|
2020-07-30 08:00:14 +02:00
|
|
|
$this->pagesToExclude,
|
2020-02-25 22:33:03 +01:00
|
|
|
Connection::PARAM_INT_ARRAY
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:33:02 +02:00
|
|
|
if (count($this->languageLimitation)) {
|
|
|
|
$constraints[] = $this->queryBuilder->expr()->in(
|
|
|
|
'tx_tracking_pageview.sys_language_uid',
|
|
|
|
$this->queryBuilder->createNamedParameter(
|
|
|
|
$this->languageLimitation,
|
|
|
|
Connection::PARAM_INT_ARRAY
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-03 11:08:12 +02:00
|
|
|
$this->queryBuilder
|
|
|
|
->addSelectLiteral('COUNT(*) as "count"')
|
2020-02-23 22:41:41 +01:00
|
|
|
->from('tx_tracking_pageview')
|
2021-05-21 18:49:38 +02:00
|
|
|
->where(...$constraints)
|
2021-08-03 11:08:12 +02:00
|
|
|
->groupBy('label')
|
|
|
|
->orderBy('label', 'ASC')
|
|
|
|
;
|
|
|
|
|
|
|
|
if ($this->queryBuilder->getConnection()->getDatabasePlatform()->getName() === 'sqlite') {
|
|
|
|
$this->queryBuilder->addSelectLiteral('date(crdate, "unixepoch") as "label"');
|
|
|
|
} else {
|
|
|
|
$this->queryBuilder->addSelectLiteral('FROM_UNIXTIME(crdate, "%Y-%m-%d") as "label"');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->queryBuilder->execute()->fetchAll();
|
2020-02-23 22:41:41 +01:00
|
|
|
}
|
|
|
|
}
|