mirror of
https://github.com/DanielSiepmann/tracking.git
synced 2024-12-22 00:16:08 +01:00
Make widgets configurable via DI
As no configuration API is provided, we use Services.yaml for now.
This commit is contained in:
parent
9452e66a51
commit
548ad0b07c
6 changed files with 131 additions and 36 deletions
|
@ -24,6 +24,7 @@ namespace DanielSiepmann\Tracking\Dashboard\Widgets;
|
|||
use DanielSiepmann\Tracking\Extension;
|
||||
use TYPO3\CMS\Core\Database\Connection;
|
||||
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Dashboard\Widgets\AbstractBarChartWidget;
|
||||
|
||||
class PageViewsBar extends AbstractBarChartWidget
|
||||
|
@ -41,16 +42,25 @@ class PageViewsBar extends AbstractBarChartWidget
|
|||
*/
|
||||
protected $queryBuilder;
|
||||
|
||||
public function __construct(string $identifier, QueryBuilder $queryBuilder)
|
||||
{
|
||||
/**
|
||||
* @var \ArrayObject
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
public function __construct(
|
||||
string $identifier,
|
||||
QueryBuilder $queryBuilder,
|
||||
\ArrayObject $settings
|
||||
) {
|
||||
parent::__construct($identifier);
|
||||
|
||||
$this->queryBuilder = $queryBuilder;
|
||||
$this->identifier = $identifier;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
protected function prepareChartData(): void
|
||||
{
|
||||
list($labels, $data) = $this->calculateDataForLastDays(31);
|
||||
list($labels, $data) = $this->calculateDataForLastDays((int) $this->settings['periodInDays']);
|
||||
|
||||
$this->chartData = [
|
||||
'labels' => $labels,
|
||||
|
@ -69,21 +79,25 @@ class PageViewsBar extends AbstractBarChartWidget
|
|||
|
||||
protected function getPageViewsInPeriod(int $start, int $end): int
|
||||
{
|
||||
$constraints = [
|
||||
$this->queryBuilder->expr()->gte('crdate', $start),
|
||||
$this->queryBuilder->expr()->lte('crdate', $end),
|
||||
];
|
||||
|
||||
if (count($this->settings['blackListedPages'])) {
|
||||
$constraints[] = $this->queryBuilder->expr()->notIn(
|
||||
'tx_tracking_pageview.pid',
|
||||
$this->queryBuilder->createNamedParameter(
|
||||
$this->settings['blackListedPages'],
|
||||
Connection::PARAM_INT_ARRAY
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return (int)$this->queryBuilder
|
||||
->count('*')
|
||||
->from('tx_tracking_pageview')
|
||||
->where(
|
||||
$this->queryBuilder->expr()->gte('tstamp', $start),
|
||||
$this->queryBuilder->expr()->lte('tstamp', $end),
|
||||
$this->queryBuilder->expr()->notIn(
|
||||
'tx_tracking_pageview.pid',
|
||||
$this->queryBuilder->createNamedParameter([
|
||||
1,
|
||||
11,
|
||||
38,
|
||||
], Connection::PARAM_INT_ARRAY)
|
||||
)
|
||||
)
|
||||
->where(... $constraints)
|
||||
->execute()
|
||||
->fetchColumn();
|
||||
}
|
||||
|
|
|
@ -38,15 +38,25 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
|
|||
*/
|
||||
protected $queryBuilder;
|
||||
|
||||
public function __construct(string $identifier, QueryBuilder $queryBuilder)
|
||||
{
|
||||
/**
|
||||
* @var \ArrayObject
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
public function __construct(
|
||||
string $identifier,
|
||||
QueryBuilder $queryBuilder,
|
||||
\ArrayObject $settings
|
||||
) {
|
||||
parent::__construct($identifier);
|
||||
|
||||
$this->queryBuilder = $queryBuilder;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
protected function prepareChartData(): void
|
||||
{
|
||||
list($labels, $data) = $this->getPageViewsPerPage(31);
|
||||
list($labels, $data) = $this->getPageViewsPerPage((int) $this->settings['periodInDays']);
|
||||
|
||||
$this->chartData = [
|
||||
'labels' => $labels,
|
||||
|
@ -59,11 +69,25 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
|
|||
];
|
||||
}
|
||||
|
||||
private function getPageViewsPerPage(int $period): array
|
||||
private function getPageViewsPerPage(int $days): array
|
||||
{
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
$constraints = [
|
||||
$this->queryBuilder->expr()->gte('tx_tracking_pageview.crdate', strtotime('-' . $days . ' day 0:00:00')),
|
||||
$this->queryBuilder->expr()->lte('tx_tracking_pageview.crdate', time()),
|
||||
];
|
||||
if (count($this->settings['blackListedPages'])) {
|
||||
$constraints[] = $this->queryBuilder->expr()->notIn(
|
||||
'tx_tracking_pageview.pid',
|
||||
$this->queryBuilder->createNamedParameter(
|
||||
$this->settings['blackListedPages'],
|
||||
Connection::PARAM_INT_ARRAY
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->queryBuilder
|
||||
->selectLiteral('count(tx_tracking_pageview.pid) as total')
|
||||
->addSelect('pages.title', 'pages.uid')
|
||||
|
@ -77,16 +101,7 @@ class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget
|
|||
$this->queryBuilder->quoteIdentifier('pages.uid')
|
||||
)
|
||||
)
|
||||
->where(
|
||||
$this->queryBuilder->expr()->notIn(
|
||||
'tx_tracking_pageview.pid',
|
||||
$this->queryBuilder->createNamedParameter([
|
||||
1,
|
||||
11,
|
||||
38,
|
||||
], Connection::PARAM_INT_ARRAY)
|
||||
)
|
||||
)
|
||||
->where(... $constraints)
|
||||
->groupBy('tx_tracking_pageview.pid')
|
||||
->orderBy('total', 'desc')
|
||||
->setMaxResults(6) // Because 6 colors are defined
|
||||
|
|
48
Classes/Dashboard/Widgets/SettingsFactory.php
Normal file
48
Classes/Dashboard/Widgets/SettingsFactory.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace DanielSiepmann\Tracking\Dashboard\Widgets;
|
||||
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
class SettingsFactory
|
||||
{
|
||||
private $defaults = [
|
||||
'pageViewsBar' => [
|
||||
'periodInDays' => 31,
|
||||
'blackListedPages' => [],
|
||||
],
|
||||
'pageViewsPerPageDoughnut' => [
|
||||
'periodInDays' => 31,
|
||||
'blackListedPages' => [],
|
||||
'maxResults' => 6,
|
||||
],
|
||||
];
|
||||
|
||||
public function fromArray(string $widgetIdentifier, array $settings): \ArrayObject
|
||||
{
|
||||
$settingsToUse = $this->defaults[$widgetIdentifier] ?? [];
|
||||
|
||||
ArrayUtility::mergeRecursiveWithOverrule($settingsToUse, $settings);
|
||||
|
||||
return new \ArrayObject($settingsToUse);
|
||||
}
|
||||
}
|
|
@ -25,5 +25,5 @@ final class Extension
|
|||
{
|
||||
public const EXT_KEY = 'tracking';
|
||||
|
||||
public const LANGUAGE_PATH = 'LLL:EXT:' . self::EXT_KEY . 'tracking/Resources/Private/Language/locallang.xlf';
|
||||
public const LANGUAGE_PATH = 'LLL:EXT:' . self::EXT_KEY . '/Resources/Private/Language/locallang.xlf';
|
||||
}
|
||||
|
|
|
@ -9,6 +9,21 @@ services:
|
|||
|
||||
# Virtual services
|
||||
|
||||
DanielSiepmann\Tracking\DI\Dashboard\Widgets\Settings\PageViewsBar:
|
||||
factory:
|
||||
- '@DanielSiepmann\Tracking\Dashboard\Widgets\SettingsFactory'
|
||||
- 'fromArray'
|
||||
arguments:
|
||||
$widgetIdentifier: 'pageViewsBar'
|
||||
$settings: []
|
||||
DanielSiepmann\Tracking\DI\Dashboard\Widgets\Settings\PageViewsPerPageDoughnut:
|
||||
factory:
|
||||
- '@DanielSiepmann\Tracking\Dashboard\Widgets\SettingsFactory'
|
||||
- 'fromArray'
|
||||
arguments:
|
||||
$widgetIdentifier: 'pageViewsPerPageDoughnut'
|
||||
$settings: []
|
||||
|
||||
DanielSiepmann\Tracking\DI\DatabaseConnection\Pageview:
|
||||
factory:
|
||||
- '@TYPO3\CMS\Core\Database\ConnectionPool'
|
||||
|
@ -42,8 +57,9 @@ services:
|
|||
DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar:
|
||||
class: 'DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar'
|
||||
arguments:
|
||||
- 'pageViewsBar'
|
||||
- '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
|
||||
$identifier: 'pageViewsBar'
|
||||
$queryBuilder: '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
|
||||
$settings: '@DanielSiepmann\Tracking\DI\Dashboard\Widgets\Settings\PageViewsBar'
|
||||
tags:
|
||||
- name: 'dashboard.widget'
|
||||
identifier: 'pageViewsBar'
|
||||
|
@ -52,8 +68,9 @@ services:
|
|||
DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut:
|
||||
class: 'DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut'
|
||||
arguments:
|
||||
- 'pageViewsPerPageDoughnut'
|
||||
- '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
|
||||
$identifier: 'pageViewsPerPageDoughnut'
|
||||
$queryBuilder: '@DanielSiepmann\Tracking\DI\QueryBuilder\PageView'
|
||||
$settings: '@DanielSiepmann\Tracking\DI\Dashboard\Widgets\Settings\PageViewsPerPageDoughnut'
|
||||
tags:
|
||||
- name: 'dashboard.widget'
|
||||
identifier: 'pageViewsPerPageDoughnut'
|
||||
|
|
|
@ -49,6 +49,7 @@
|
|||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.5",
|
||||
"phpunit/phpunit": "^9.0"
|
||||
"phpunit/phpunit": "^9.0",
|
||||
"typo3/cms-dashboard": "^10.3.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue