diff --git a/Classes/Dashboard/Provider/NewestPageviews.php b/Classes/Dashboard/Provider/NewestPageviews.php index cafa707..53c4e75 100644 --- a/Classes/Dashboard/Provider/NewestPageviews.php +++ b/Classes/Dashboard/Provider/NewestPageviews.php @@ -42,14 +42,21 @@ class NewestPageviews implements ListDataProviderInterface */ private $pagesToExclude; + /** + * @var array + */ + private $languageLimitation; + public function __construct( QueryBuilder $queryBuilder, int $maxResults = 6, - array $pagesToExclude = [] + array $pagesToExclude = [], + array $languageLimitation = [] ) { $this->queryBuilder = $queryBuilder; $this->maxResults = $maxResults; $this->pagesToExclude = $pagesToExclude; + $this->languageLimitation = $languageLimitation; } public function getItems(): array @@ -67,6 +74,16 @@ class NewestPageviews implements ListDataProviderInterface ); } + if (count($this->languageLimitation)) { + $constraints[] = $this->queryBuilder->expr()->in( + 'tx_tracking_pageview.sys_language_uid', + $this->queryBuilder->createNamedParameter( + $this->languageLimitation, + Connection::PARAM_INT_ARRAY + ) + ); + } + $this->queryBuilder ->select('url', 'user_agent') ->from('tx_tracking_pageview') diff --git a/Classes/Dashboard/Provider/PageviewsPerDay.php b/Classes/Dashboard/Provider/PageviewsPerDay.php index 0dc2e29..28bbb5d 100644 --- a/Classes/Dashboard/Provider/PageviewsPerDay.php +++ b/Classes/Dashboard/Provider/PageviewsPerDay.php @@ -55,17 +55,24 @@ class PageviewsPerDay implements ChartDataProviderInterface */ private $dateFormat; + /** + * @var array + */ + private $languageLimitation; + public function __construct( LanguageService $languageService, QueryBuilder $queryBuilder, int $days = 31, array $pagesToExclude = [], + array $languageLimitation = [], string $dateFormat = 'Y-m-d' ) { $this->languageService = $languageService; $this->queryBuilder = $queryBuilder; $this->days = $days; $this->pagesToExclude = $pagesToExclude; + $this->languageLimitation = $languageLimitation; $this->dateFormat = $dateFormat; } @@ -125,6 +132,16 @@ class PageviewsPerDay implements ChartDataProviderInterface ); } + if (count($this->languageLimitation)) { + $constraints[] = $this->queryBuilder->expr()->in( + 'tx_tracking_pageview.sys_language_uid', + $this->queryBuilder->createNamedParameter( + $this->languageLimitation, + Connection::PARAM_INT_ARRAY + ) + ); + } + return (int)$this->queryBuilder ->count('*') ->from('tx_tracking_pageview') diff --git a/Classes/Dashboard/Provider/PageviewsPerOperatingSystem.php b/Classes/Dashboard/Provider/PageviewsPerOperatingSystem.php index 3dda1a9..9a474df 100644 --- a/Classes/Dashboard/Provider/PageviewsPerOperatingSystem.php +++ b/Classes/Dashboard/Provider/PageviewsPerOperatingSystem.php @@ -21,6 +21,7 @@ namespace DanielSiepmann\Tracking\Dashboard\Provider; * 02110-1301, USA. */ +use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Dashboard\WidgetApi; use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface; @@ -42,14 +43,21 @@ class PageviewsPerOperatingSystem implements ChartDataProviderInterface */ private $maxResults; + /** + * @var array + */ + private $languageLimitation; + public function __construct( QueryBuilder $queryBuilder, int $days = 31, - int $maxResults = 6 + int $maxResults = 6, + array $languageLimitation = [] ) { $this->queryBuilder = $queryBuilder; $this->days = $days; $this->maxResults = $maxResults; + $this->languageLimitation = $languageLimitation; } public function getChartData(): array @@ -83,6 +91,16 @@ class PageviewsPerOperatingSystem implements ChartDataProviderInterface ), ]; + if (count($this->languageLimitation)) { + $constraints[] = $this->queryBuilder->expr()->in( + 'tx_tracking_pageview.sys_language_uid', + $this->queryBuilder->createNamedParameter( + $this->languageLimitation, + Connection::PARAM_INT_ARRAY + ) + ); + } + $result = $this->queryBuilder ->selectLiteral('count(operating_system) as total') ->addSelect('operating_system') diff --git a/Documentation/PageviewWidgets/NewestPageviews.rst b/Documentation/PageviewWidgets/NewestPageviews.rst index ec34eab..ff82154 100644 --- a/Documentation/PageviewWidgets/NewestPageviews.rst +++ b/Documentation/PageviewWidgets/NewestPageviews.rst @@ -61,3 +61,13 @@ Options This becomes handy if certain pages are called in order to show specific records. In those cases the pages will be called very often but don't provide much benefit and can be excluded. Use this in combination with :ref:`recordview` to show the records instead. + +.. option:: $languageLimitation + + Array of ``sys_language_uid``'s to include. + Defaults to empty array, all languages are shown. + + Allows to limit results to specific lanuages. + All entries tracked when visiting page with this language are shown. + If multiple languages are shown, default system language labels are used. + If only a single lanugage is allowed, record labels are translated to that language. diff --git a/Documentation/PageviewWidgets/PageviewsPerDay.rst b/Documentation/PageviewWidgets/PageviewsPerDay.rst index d0f34ec..de6a8d0 100644 --- a/Documentation/PageviewWidgets/PageviewsPerDay.rst +++ b/Documentation/PageviewWidgets/PageviewsPerDay.rst @@ -70,3 +70,13 @@ Options String defining the format used for labels. Defaults to 'Y-m-d'. + +.. option:: $languageLimitation + + Array of ``sys_language_uid``'s to include. + Defaults to empty array, all languages are shown. + + Allows to limit results to specific lanuages. + All entries tracked when visiting page with this language are shown. + If multiple languages are shown, default system language labels are used. + If only a single lanugage is allowed, record labels are translated to that language. diff --git a/Documentation/PageviewWidgets/PageviewsPerOperatingSystem.rst b/Documentation/PageviewWidgets/PageviewsPerOperatingSystem.rst index 5fb9cc9..d4b90b1 100644 --- a/Documentation/PageviewWidgets/PageviewsPerOperatingSystem.rst +++ b/Documentation/PageviewWidgets/PageviewsPerOperatingSystem.rst @@ -62,3 +62,13 @@ Options Defaults to 6 because EXT:dashboard only provides 6 colors. Defaults to 6. + +.. option:: $languageLimitation + + Array of ``sys_language_uid``'s to include. + Defaults to empty array, all languages are shown. + + Allows to limit results to specific lanuages. + All entries tracked when visiting page with this language are shown. + If multiple languages are shown, default system language labels are used. + If only a single lanugage is allowed, record labels are translated to that language. diff --git a/Tests/Functional/Dashboard/Provider/NewestPageviewsTest.php b/Tests/Functional/Dashboard/Provider/NewestPageviewsTest.php index bb70fcf..7ebdfcf 100644 --- a/Tests/Functional/Dashboard/Provider/NewestPageviewsTest.php +++ b/Tests/Functional/Dashboard/Provider/NewestPageviewsTest.php @@ -120,4 +120,36 @@ class NewestPageviewsTest extends TestCase 'Url 4 - User-Agent 4', ], $subject->getItems()); } + + /** + * @test + */ + public function respectsLimitToLanguages(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview'); + for ($i = 1; $i <= 10; $i++) { + $connection->insert('tx_tracking_pageview', [ + 'pid' => $i, + 'url' => 'Url ' . $i, + 'sys_language_uid' => $i % 2, + 'user_agent' => 'User-Agent ' . $i, + 'crdate' => $i, + ]); + } + + $subject = new NewestPageviews( + GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'), + 6, + [], + [1] + ); + + static::assertSame([ + 'Url 9 - User-Agent 9', + 'Url 7 - User-Agent 7', + 'Url 5 - User-Agent 5', + 'Url 3 - User-Agent 3', + 'Url 1 - User-Agent 1', + ], $subject->getItems()); + } } diff --git a/Tests/Functional/Dashboard/Provider/PageviewsPerDayTest.php b/Tests/Functional/Dashboard/Provider/PageviewsPerDayTest.php index f811c10..acf8f09 100644 --- a/Tests/Functional/Dashboard/Provider/PageviewsPerDayTest.php +++ b/Tests/Functional/Dashboard/Provider/PageviewsPerDayTest.php @@ -133,6 +133,7 @@ class PageviewsPerDayTest extends TestCase GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'), 1, [], + [], 'd.m.Y' ); @@ -143,4 +144,43 @@ class PageviewsPerDayTest extends TestCase ], $result['labels']); static::assertCount(2, $result['datasets'][0]['data']); } + + /** + * @test + */ + public function respectsLimitToLanguages(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview'); + for ($i = 1; $i <= 10; $i++) { + $connection->insert('tx_tracking_pageview', [ + 'pid' => $i, + 'crdate' => strtotime('-' . $i . ' days'), + 'sys_language_uid' => $i % 2, + ]); + } + + $subject = new PageviewsPerDay( + GeneralUtility::makeInstance(LanguageService::class), + GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'), + 11, + [], + [1] + ); + + $result = $subject->getChartData(); + static::assertSame([ + 0 => 0, + 1 => 0, + 2 => 1, + 3 => 0, + 4 => 1, + 5 => 0, + 6 => 1, + 7 => 0, + 8 => 1, + 9 => 0, + 10 => 1, + 11 => 0, + ], $result['datasets'][0]['data']); + } } diff --git a/Tests/Functional/Dashboard/Provider/PageviewsPerOperatingSystemTest.php b/Tests/Functional/Dashboard/Provider/PageviewsPerOperatingSystemTest.php index 311f534..2adbbe2 100644 --- a/Tests/Functional/Dashboard/Provider/PageviewsPerOperatingSystemTest.php +++ b/Tests/Functional/Dashboard/Provider/PageviewsPerOperatingSystemTest.php @@ -172,4 +172,38 @@ class PageviewsPerOperatingSystemTest extends TestCase ], $result['labels']); static::assertCount(4, $result['datasets'][0]['data']); } + + /** + * @test + */ + public function respectsLimitToLanguages(): void + { + $this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml'); + $connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview'); + for ($i = 1; $i <= 10; $i++) { + $connection->insert('tx_tracking_pageview', [ + 'pid' => $i, + 'sys_language_uid' => $i % 2, + 'operating_system' => 'System ' . $i, + 'crdate' => time(), + ]); + } + + $subject = new PageviewsPerOperatingSystem( + GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'), + 31, + 6, + [1] + ); + + $result = $subject->getChartData(); + static::assertSame([ + 'System 1', + 'System 3', + 'System 5', + 'System 7', + 'System 9', + ], $result['labels']); + static::assertCount(5, $result['datasets'][0]['data']); + } }