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 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
|
|
|
/**
|
2021-08-16 09:56:19 +02:00
|
|
|
* @var Demand
|
2020-04-07 17:54:18 +02:00
|
|
|
*/
|
2021-08-16 09:56:19 +02:00
|
|
|
private $demand;
|
2020-07-30 12:22:32 +02:00
|
|
|
|
2020-02-25 22:33:03 +01:00
|
|
|
public function __construct(
|
|
|
|
QueryBuilder $queryBuilder,
|
2020-07-30 12:22:32 +02:00
|
|
|
PageRepository $pageRepository,
|
2021-08-16 09:56:19 +02:00
|
|
|
Demand $demand
|
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;
|
2021-08-16 09:56:19 +02:00
|
|
|
$this->demand = $demand;
|
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 = [
|
2021-08-16 09:56:19 +02:00
|
|
|
(string) $this->queryBuilder->expr()->gte(
|
2020-04-07 17:54:18 +02:00
|
|
|
'tx_tracking_pageview.crdate',
|
2021-08-16 09:56:19 +02:00
|
|
|
strtotime('-' . $this->demand->getDays() . ' day 0:00:00')
|
2020-04-07 17:54:18 +02:00
|
|
|
),
|
2020-02-25 22:33:03 +01:00
|
|
|
];
|
2021-08-16 09:56:19 +02:00
|
|
|
$constraints = array_merge($constraints, $this->demand->getConstraints(
|
|
|
|
$this->queryBuilder,
|
|
|
|
'tx_tracking_pageview'
|
|
|
|
));
|
|
|
|
$this->demand->addJoins(
|
|
|
|
$this->queryBuilder,
|
|
|
|
'tx_tracking_pageview'
|
|
|
|
);
|
2020-07-30 12:22:32 +02:00
|
|
|
|
2020-02-24 14:49:31 +01:00
|
|
|
$result = $this->queryBuilder
|
2020-08-12 15:54:03 +02:00
|
|
|
->selectLiteral(
|
2021-08-16 09:56:19 +02:00
|
|
|
$this->queryBuilder->expr()->count('tx_tracking_pageview.pid', 'total'),
|
|
|
|
$this->queryBuilder->expr()->max('tx_tracking_pageview.uid', 'latest')
|
2020-02-23 22:41:41 +01:00
|
|
|
)
|
2021-08-16 09:56:19 +02:00
|
|
|
->addSelect('tx_tracking_pageview.pid')
|
2020-08-12 15:54:03 +02:00
|
|
|
->from('tx_tracking_pageview')
|
2021-05-21 18:49:38 +02:00
|
|
|
->where(...$constraints)
|
2021-08-16 09:56:19 +02:00
|
|
|
->groupBy('tx_tracking_pageview.pid')
|
2020-02-23 22:41:41 +01:00
|
|
|
->orderBy('total', 'desc')
|
2020-08-12 15:54:03 +02:00
|
|
|
->addOrderBy('latest', 'desc')
|
2021-08-16 09:56:19 +02:00
|
|
|
->setMaxResults($this->demand->getMaxResults())
|
2020-02-23 22:41:41 +01:00
|
|
|
->execute()
|
|
|
|
->fetchAll();
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
2021-11-25 14:40:00 +01:00
|
|
|
if (is_array($row) === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:53:49 +02:00
|
|
|
$labels[] = $this->getRecordTitle((int) $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);
|
2021-08-16 09:56:19 +02:00
|
|
|
if (count($this->demand->getLanguageLimitation()) === 1 && $record !== null) {
|
|
|
|
$record = $this->pageRepository->getRecordOverlay('pages', $record, $this->demand->getLanguageLimitation()[0]);
|
2020-07-30 12:22:32 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:40:00 +01:00
|
|
|
if (is_array($record) === false) {
|
|
|
|
return 'Unkown';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|