2020-04-01 21:04:32 +02:00
|
|
|
<?php
|
|
|
|
|
2022-09-16 13:53:49 +02:00
|
|
|
declare(strict_types=1);
|
2020-04-01 21:04:32 +02: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;
|
|
|
|
|
2024-02-05 11:25:00 +01:00
|
|
|
use Exception;
|
2020-08-03 12:33:02 +02:00
|
|
|
use TYPO3\CMS\Core\Database\Connection;
|
2020-04-01 21:04:32 +02:00
|
|
|
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
|
|
|
|
use TYPO3\CMS\Dashboard\WidgetApi;
|
|
|
|
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
|
|
|
|
|
|
|
|
class PageviewsPerOperatingSystem implements ChartDataProviderInterface
|
|
|
|
{
|
|
|
|
/**
|
2024-02-05 11:25:00 +01:00
|
|
|
* @param int[] $languageLimitation
|
2020-04-01 21:04:32 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2024-02-05 11:25:00 +01:00
|
|
|
private readonly QueryBuilder $queryBuilder,
|
|
|
|
private readonly int $days = 31,
|
|
|
|
private readonly int $maxResults = 6,
|
|
|
|
private readonly array $languageLimitation = []
|
2020-04-01 21:04:32 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getChartData(): array
|
|
|
|
{
|
2022-12-07 13:37:19 +01:00
|
|
|
[$labels, $data] = $this->getPageViewsPerPage();
|
2020-04-01 21:04:32 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'labels' => $labels,
|
|
|
|
'datasets' => [
|
|
|
|
[
|
|
|
|
'backgroundColor' => WidgetApi::getDefaultChartColors(),
|
|
|
|
'data' => $data,
|
2022-12-07 13:37:19 +01:00
|
|
|
],
|
2020-04-01 21:04:32 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPageViewsPerPage(): array
|
|
|
|
{
|
|
|
|
$labels = [];
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$constraints = [
|
|
|
|
$this->queryBuilder->expr()->gte(
|
|
|
|
'tx_tracking_pageview.crdate',
|
|
|
|
strtotime('-' . $this->days . ' day 0:00:00')
|
|
|
|
),
|
|
|
|
$this->queryBuilder->expr()->neq(
|
|
|
|
'tx_tracking_pageview.operating_system',
|
|
|
|
$this->queryBuilder->createNamedParameter('')
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2020-08-03 12:33:02 +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-04-01 21:04:32 +02:00
|
|
|
$result = $this->queryBuilder
|
|
|
|
->selectLiteral('count(operating_system) as total')
|
|
|
|
->addSelect('operating_system')
|
|
|
|
->from('tx_tracking_pageview')
|
2021-05-21 18:49:38 +02:00
|
|
|
->where(...$constraints)
|
2020-04-01 21:04:32 +02:00
|
|
|
->groupBy('tx_tracking_pageview.operating_system')
|
|
|
|
->orderBy('total', 'desc')
|
2020-07-30 12:22:32 +02:00
|
|
|
->addOrderBy('operating_system', 'asc')
|
2020-04-01 21:04:32 +02:00
|
|
|
->setMaxResults($this->maxResults)
|
2024-02-05 11:25:00 +01:00
|
|
|
->executeQuery()
|
|
|
|
->fetchAllAssociative()
|
2022-12-07 13:37:19 +01:00
|
|
|
;
|
2020-04-01 21:04:32 +02:00
|
|
|
|
|
|
|
foreach ($result as $row) {
|
2024-02-05 11:25:00 +01:00
|
|
|
if (is_string($row['operating_system']) === false) {
|
|
|
|
throw new Exception('operating_system of row was not string: ' . var_export($row['operating_system'], true), 1707326866);
|
2021-11-25 14:40:00 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 21:04:32 +02:00
|
|
|
$labels[] = mb_strimwidth($row['operating_system'], 0, 50, '…');
|
|
|
|
$data[] = $row['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
$labels,
|
|
|
|
$data,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|