From 548ad0b07c4e7defb7fef3a509b334045bb1f289 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 25 Feb 2020 22:33:03 +0100 Subject: [PATCH] Make widgets configurable via DI As no configuration API is provided, we use Services.yaml for now. --- Classes/Dashboard/Widgets/PageViewsBar.php | 46 +++++++++++------- .../Widgets/PageViewsPerPageDoughnut.php | 43 +++++++++++------ Classes/Dashboard/Widgets/SettingsFactory.php | 48 +++++++++++++++++++ Classes/Extension.php | 2 +- Configuration/Services.yaml | 25 ++++++++-- composer.json | 3 +- 6 files changed, 131 insertions(+), 36 deletions(-) create mode 100644 Classes/Dashboard/Widgets/SettingsFactory.php diff --git a/Classes/Dashboard/Widgets/PageViewsBar.php b/Classes/Dashboard/Widgets/PageViewsBar.php index 63ec302..c713a33 100644 --- a/Classes/Dashboard/Widgets/PageViewsBar.php +++ b/Classes/Dashboard/Widgets/PageViewsBar.php @@ -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(); } diff --git a/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php b/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php index 30eb1d2..7b59f68 100644 --- a/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php +++ b/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php @@ -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 diff --git a/Classes/Dashboard/Widgets/SettingsFactory.php b/Classes/Dashboard/Widgets/SettingsFactory.php new file mode 100644 index 0000000..65aac9d --- /dev/null +++ b/Classes/Dashboard/Widgets/SettingsFactory.php @@ -0,0 +1,48 @@ + + * + * 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); + } +} diff --git a/Classes/Extension.php b/Classes/Extension.php index 1716c56..227fa52 100644 --- a/Classes/Extension.php +++ b/Classes/Extension.php @@ -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'; } diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 0d66960..245218d 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -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' diff --git a/composer.json b/composer.json index 32ef533..a4e6c54 100644 --- a/composer.json +++ b/composer.json @@ -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" } }