tracking/Classes/Dashboard/Provider/NewestPageviews.php
Daniel Siepmann 0927683926 Migrate dashboard part to be compatible with v10.4
10.4 introduced breaking changes for dashboard widgets.
The necessary changes to stay compatible are done within this commit.

No need anymore to configure widget through PHP, instead DI is used.
Instead only PHP to provide data is used.
2020-04-16 11:54:42 +02:00

92 lines
2.6 KiB
PHP

<?php
namespace DanielSiepmann\Tracking\Dashboard\Provider;
/*
* 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.
*/
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Dashboard\Widgets\Interfaces\ListDataProviderInterface;
class NewestPageviews implements ListDataProviderInterface
{
/**
* @var QueryBuilder
*/
private $queryBuilder;
/**
* @var int
*/
private $maxResults;
/**
* @var array
*/
private $blackListedPages;
public function __construct(
QueryBuilder $queryBuilder,
int $maxResults = 6,
array $blackListedPages = []
) {
$this->queryBuilder = $queryBuilder;
$this->maxResults = $maxResults;
$this->blackListedPages = $blackListedPages;
}
public function getItems(): array
{
$preparedItems = [];
$constraints = [];
if (count($this->blackListedPages)) {
$constraints[] = $this->queryBuilder->expr()->notIn(
'tx_tracking_pageview.pid',
$this->queryBuilder->createNamedParameter(
$this->blackListedPages,
Connection::PARAM_INT_ARRAY
)
);
}
$this->queryBuilder
->select('url', 'user_agent')
->from('tx_tracking_pageview')
->orderBy('crdate', 'desc')
->setMaxResults($this->maxResults);
if ($constraints !== []) {
$this->queryBuilder->where(... $constraints);
}
$items = $this->queryBuilder->execute()->fetchAll();
foreach ($items as $item) {
$preparedItems[] = sprintf(
'%s - %s',
$item['url'],
$item['user_agent']
);
}
return $preparedItems;
}
}