tracking/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php

122 lines
3.8 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 Doctrine\DBAL\ParameterType;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Dashboard\Widgets\AbstractDoughnutChartWidget;
class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
{
2020-02-25 20:06:38 +01:00
protected $title = Extension::LANGUAGE_PATH . ':dashboard.widgets.pageViewsPerPageDoughnut.title';
2020-02-23 22:41:41 +01:00
2020-02-25 20:06:38 +01:00
protected $description = Extension::LANGUAGE_PATH . ':dashboard.widgets.pageViewsPerPageDoughnut.description';
2020-02-23 22:41:41 +01:00
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->getPageViewsPerPage((int) $this->settings['periodInDays']);
2020-02-23 22:41:41 +01:00
$this->chartData = [
'labels' => $labels,
'datasets' => [
[
'backgroundColor' => $this->chartColors,
'data' => $data,
]
],
];
}
private function getPageViewsPerPage(int $days): array
2020-02-23 22:41:41 +01:00
{
$labels = [];
$data = [];
$constraints = [
$this->queryBuilder->expr()->gte('tx_tracking_pageview.crdate', strtotime('-' . $days . ' day 0:00:00')),
$this->queryBuilder->expr()->lte('tx_tracking_pageview.crdate', time()),
];
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
$result = $this->queryBuilder
2020-02-23 22:41:41 +01:00
->selectLiteral('count(tx_tracking_pageview.pid) as total')
->addSelect('pages.title', 'pages.uid')
->from('tx_tracking_pageview')
->leftJoin(
'tx_tracking_pageview',
'pages',
'pages',
2020-02-24 14:49:31 +01:00
$this->queryBuilder->expr()->eq(
'tx_tracking_pageview.pid',
$this->queryBuilder->quoteIdentifier('pages.uid')
)
2020-02-23 22:41:41 +01:00
)
->where(... $constraints)
2020-02-23 22:41:41 +01:00
->groupBy('tx_tracking_pageview.pid')
->orderBy('total', 'desc')
->setMaxResults(6) // Because 6 colors are defined
->execute()
->fetchAll();
foreach ($result as $row) {
$labels[] = $row['title'] . ' [' . $row['uid'] . ']';
$data[] = $row['total'];
}
return [
$labels,
$data,
];
}
}