tracking/Classes/Dashboard/Widgets/PageViewsBar.php

131 lines
4 KiB
PHP
Raw Normal View History

2020-02-23 22:41:41 +01:00
<?php
namespace DanielSiepmann\Tracking\Dashboard\Widgets;
/*
* 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.
*/
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\Utility\ArrayUtility;
2020-02-23 22:41:41 +01:00
use TYPO3\CMS\Dashboard\Widgets\AbstractBarChartWidget;
class PageViewsBar extends AbstractBarChartWidget
{
2020-02-25 20:06:38 +01:00
protected $title = Extension::LANGUAGE_PATH . ':dashboard.widgets.pageViewsBar.title';
2020-02-23 22:41:41 +01:00
2020-02-26 22:57:34 +01:00
protected $description = Extension::LANGUAGE_PATH . ':dashboard.widgets.pageViewsBar.description';
2020-02-23 22:41:41 +01:00
protected $width = 2;
protected $height = 4;
2020-02-24 14:49:31 +01:00
/**
* @var QueryBuilder
*/
protected $queryBuilder;
/**
* @var \ArrayObject
*/
private $settings;
public function __construct(
string $identifier,
QueryBuilder $queryBuilder,
\ArrayObject $settings
) {
2020-02-24 14:49:31 +01:00
parent::__construct($identifier);
2020-02-24 14:49:31 +01:00
$this->queryBuilder = $queryBuilder;
$this->settings = $settings;
2020-02-24 14:49:31 +01:00
}
2020-02-23 22:41:41 +01:00
protected function prepareChartData(): void
{
list($labels, $data) = $this->calculateDataForLastDays((int) $this->settings['periodInDays']);
2020-02-23 22:41:41 +01:00
$this->chartData = [
'labels' => $labels,
'datasets' => [
[
'label' => $this->getLanguageService()->sL(
'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:widgets.pageViewsBar.chart.dataSet.0'
),
'backgroundColor' => $this->chartColors[0],
'border' => 0,
'data' => $data
]
]
];
}
protected function getPageViewsInPeriod(int $start, int $end): int
{
$constraints = [
$this->queryBuilder->expr()->gte('crdate', $start),
$this->queryBuilder->expr()->lte('crdate', $end),
];
if (count($this->settings['blackListedPages'])) {
$constraints[] = $this->queryBuilder->expr()->notIn(
'tx_tracking_pageview.pid',
$this->queryBuilder->createNamedParameter(
$this->settings['blackListedPages'],
Connection::PARAM_INT_ARRAY
)
);
}
2020-02-24 14:49:31 +01:00
return (int)$this->queryBuilder
2020-02-23 22:41:41 +01:00
->count('*')
->from('tx_tracking_pageview')
->where(... $constraints)
2020-02-23 22:41:41 +01:00
->execute()
->fetchColumn();
}
protected function calculateDataForLastDays(int $days): array
{
$labels = [];
$data = [];
$format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
for ($daysBefore = $days; $daysBefore >= 0; $daysBefore--) {
$timeForLabel = strtotime('-' . $daysBefore . ' day');
2020-02-23 22:41:41 +01:00
$startPeriod = strtotime('-' . $daysBefore . ' day 0:00:00');
$endPeriod = strtotime('-' . $daysBefore . ' day 23:59:59');
if ($timeForLabel === false || $startPeriod === false || $endPeriod === false) {
continue;
}
$labels[] = date($format, $timeForLabel);
2020-02-23 22:41:41 +01:00
$data[] = $this->getPageViewsInPeriod($startPeriod, $endPeriod);
}
return [
$labels,
$data,
];
}
}