tracking/Classes/Dashboard/Provider/PageviewsPerPage.php

144 lines
4.5 KiB
PHP
Raw Permalink 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;
use Exception;
use TYPO3\CMS\Backend\Utility\BackendUtility;
2020-02-23 22:41:41 +01:00
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
2020-02-23 22:41:41 +01:00
class PageviewsPerPage implements ChartDataProviderInterface
2020-02-23 22:41:41 +01:00
{
/**
* @param int[] $pagesToExclude
* @param int[] $languageLimitation
*/
public function __construct(
private readonly QueryBuilder $queryBuilder,
private readonly PageRepository $pageRepository,
private readonly int $days = 31,
private readonly int $maxResults = 6,
private readonly array $pagesToExclude = [],
private readonly array $languageLimitation = []
) {
2020-02-24 14:49:31 +01:00
}
public function getChartData(): array
2020-02-23 22:41:41 +01:00
{
[$labels, $data] = $this->getPageviewsPerPage();
2020-02-23 22:41:41 +01:00
return [
2020-02-23 22:41:41 +01:00
'labels' => $labels,
'datasets' => [
[
'backgroundColor' => WidgetApi::getDefaultChartColors(),
2020-02-23 22:41:41 +01:00
'data' => $data,
],
2020-02-23 22:41:41 +01:00
],
];
}
private function getPageviewsPerPage(): array
2020-02-23 22:41:41 +01:00
{
$labels = [];
$data = [];
$constraints = [
$this->queryBuilder->expr()->gte(
'tx_tracking_pageview.crdate',
strtotime('-' . $this->days . ' day 0:00:00')
),
];
if (count($this->pagesToExclude)) {
$constraints[] = $this->queryBuilder->expr()->notIn(
'tx_tracking_pageview.pid',
$this->queryBuilder->createNamedParameter(
$this->pagesToExclude,
Connection::PARAM_INT_ARRAY
)
);
}
if (count($this->languageLimitation)) {
$constraints[] = $this->queryBuilder->expr()->in(
'tx_tracking_pageview.sys_language_uid',
$this->queryBuilder->createNamedParameter(
$this->languageLimitation,
Connection::PARAM_INT_ARRAY
)
);
}
2020-02-24 14:49:31 +01:00
$result = $this->queryBuilder
->selectLiteral(
$this->queryBuilder->expr()->count('pid', 'total'),
$this->queryBuilder->expr()->max('uid', 'latest')
2020-02-23 22:41:41 +01:00
)
->addSelect('pid')
->from('tx_tracking_pageview')
->where(...$constraints)
->groupBy('pid')
2020-02-23 22:41:41 +01:00
->orderBy('total', 'desc')
->addOrderBy('latest', 'desc')
->setMaxResults($this->maxResults)
->executeQuery()
->fetchAllAssociative()
;
2020-02-23 22:41:41 +01:00
foreach ($result as $row) {
if (is_numeric($row['pid']) === false) {
throw new Exception('PID of row was not numeric: ' . var_export($row['pid'], true), 1707326783);
}
$labels[] = $this->getRecordTitle((int) $row['pid']);
2020-02-23 22:41:41 +01:00
$data[] = $row['total'];
}
return [
$labels,
$data,
];
}
private function getRecordTitle(int $uid): string
{
$record = BackendUtility::getRecord('pages', $uid);
if (count($this->languageLimitation) === 1 && $record !== null) {
$record = $this->pageRepository->getPageOverlay(
$record,
$this->languageLimitation[0]
);
}
if (is_array($record) === false) {
return 'Unkown';
}
return strip_tags((string) BackendUtility::getRecordTitle('pages', $record, true));
}
2020-02-23 22:41:41 +01:00
}