2020-02-23 22:41:41 +01:00
|
|
|
<?php
|
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
namespace DanielSiepmann\Tracking\Dashboard\Provider;
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-02-25 20:06:38 +01:00
|
|
|
use DanielSiepmann\Tracking\Extension;
|
2020-02-23 22:41:41 +01:00
|
|
|
use Doctrine\DBAL\ParameterType;
|
2020-07-30 12:22:32 +02:00
|
|
|
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;
|
2020-07-30 12:22:32 +02:00
|
|
|
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
|
2020-04-07 17:54:18 +02:00
|
|
|
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 PageviewsPerPage implements ChartDataProviderInterface
|
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-07-30 12:22:32 +02:00
|
|
|
/**
|
|
|
|
* @var PageRepository
|
|
|
|
*/
|
|
|
|
private $pageRepository;
|
|
|
|
|
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 int
|
2020-02-24 14:49:31 +01:00
|
|
|
*/
|
2020-04-07 17:54:18 +02:00
|
|
|
private $maxResults;
|
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 array<int>
|
2020-02-25 22:33:03 +01:00
|
|
|
*/
|
2020-07-30 08:00:14 +02:00
|
|
|
private $pagesToExclude;
|
2020-02-25 22:33:03 +01:00
|
|
|
|
2020-07-30 12:22:32 +02:00
|
|
|
/**
|
|
|
|
* @var array<int>
|
|
|
|
*/
|
|
|
|
private $languageLimitation;
|
|
|
|
|
2020-02-25 22:33:03 +01:00
|
|
|
public function __construct(
|
|
|
|
QueryBuilder $queryBuilder,
|
2020-07-30 12:22:32 +02:00
|
|
|
PageRepository $pageRepository,
|
2020-04-07 17:54:18 +02:00
|
|
|
int $days = 31,
|
|
|
|
int $maxResults = 6,
|
2020-07-30 12:22:32 +02:00
|
|
|
array $pagesToExclude = [],
|
|
|
|
array $languageLimitation = []
|
2020-02-25 22:33:03 +01:00
|
|
|
) {
|
2020-02-24 14:49:31 +01:00
|
|
|
$this->queryBuilder = $queryBuilder;
|
2020-07-30 12:22:32 +02:00
|
|
|
$this->pageRepository = $pageRepository;
|
2020-04-07 17:54:18 +02:00
|
|
|
$this->days = $days;
|
|
|
|
$this->maxResults = $maxResults;
|
2020-07-30 12:22:32 +02:00
|
|
|
$this->pagesToExclude = $pagesToExclude;
|
|
|
|
$this->languageLimitation = $languageLimitation;
|
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->getPageviewsPerPage();
|
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
|
|
|
'backgroundColor' => WidgetApi::getDefaultChartColors(),
|
2020-02-23 22:41:41 +01:00
|
|
|
'data' => $data,
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:54:18 +02:00
|
|
|
private function getPageviewsPerPage(): array
|
2020-02-23 22:41:41 +01:00
|
|
|
{
|
|
|
|
$labels = [];
|
|
|
|
$data = [];
|
|
|
|
|
2020-02-25 22:33:03 +01:00
|
|
|
$constraints = [
|
2020-04-07 17:54:18 +02:00
|
|
|
$this->queryBuilder->expr()->gte(
|
|
|
|
'tx_tracking_pageview.crdate',
|
|
|
|
strtotime('-' . $this->days . ' day 0:00:00')
|
|
|
|
),
|
2020-02-25 22:33:03 +01:00
|
|
|
];
|
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-07-30 12:22:32 +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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:49:31 +01:00
|
|
|
$result = $this->queryBuilder
|
2020-08-12 15:54:03 +02:00
|
|
|
->selectLiteral(
|
|
|
|
$this->queryBuilder->expr()->count('pid', 'total'),
|
|
|
|
$this->queryBuilder->expr()->max('uid', 'latest')
|
2020-02-23 22:41:41 +01:00
|
|
|
)
|
2020-08-12 15:54:03 +02:00
|
|
|
->addSelect('pid')
|
|
|
|
->from('tx_tracking_pageview')
|
2020-02-25 22:33:03 +01:00
|
|
|
->where(... $constraints)
|
2020-08-12 15:54:03 +02:00
|
|
|
->groupBy('pid')
|
2020-02-23 22:41:41 +01:00
|
|
|
->orderBy('total', 'desc')
|
2020-08-12 15:54:03 +02:00
|
|
|
->addOrderBy('latest', 'desc')
|
2020-04-07 17:54:18 +02:00
|
|
|
->setMaxResults($this->maxResults)
|
2020-02-23 22:41:41 +01:00
|
|
|
->execute()
|
|
|
|
->fetchAll();
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
2020-08-12 15:54:03 +02:00
|
|
|
$labels[] = $this->getRecordTitle($row['pid']);
|
2020-02-23 22:41:41 +01:00
|
|
|
$data[] = $row['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
$labels,
|
|
|
|
$data,
|
|
|
|
];
|
|
|
|
}
|
2020-07-30 12:22:32 +02:00
|
|
|
|
2020-08-12 15:54:03 +02:00
|
|
|
private function getRecordTitle(int $uid): string
|
2020-07-30 12:22:32 +02:00
|
|
|
{
|
|
|
|
$record = BackendUtility::getRecord('pages', $uid);
|
2020-08-12 15:54:03 +02:00
|
|
|
if (count($this->languageLimitation) === 1 && $record !== null) {
|
|
|
|
$record = $this->pageRepository->getRecordOverlay('pages', $record, $this->languageLimitation[0]);
|
2020-07-30 12:22:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 08:14:41 +02:00
|
|
|
return strip_tags(BackendUtility::getRecordTitle('pages', $record, true));
|
2020-07-30 12:22:32 +02:00
|
|
|
}
|
2020-02-23 22:41:41 +01:00
|
|
|
}
|