Support language restriction for all widgets

Relates: #15
This commit is contained in:
Daniel Siepmann 2020-08-03 12:33:02 +02:00
parent 68d1b44008
commit 1cbec2e51d
9 changed files with 190 additions and 2 deletions

View file

@ -42,14 +42,21 @@ class NewestPageviews implements ListDataProviderInterface
*/ */
private $pagesToExclude; private $pagesToExclude;
/**
* @var array<int>
*/
private $languageLimitation;
public function __construct( public function __construct(
QueryBuilder $queryBuilder, QueryBuilder $queryBuilder,
int $maxResults = 6, int $maxResults = 6,
array $pagesToExclude = [] array $pagesToExclude = [],
array $languageLimitation = []
) { ) {
$this->queryBuilder = $queryBuilder; $this->queryBuilder = $queryBuilder;
$this->maxResults = $maxResults; $this->maxResults = $maxResults;
$this->pagesToExclude = $pagesToExclude; $this->pagesToExclude = $pagesToExclude;
$this->languageLimitation = $languageLimitation;
} }
public function getItems(): array 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 $this->queryBuilder
->select('url', 'user_agent') ->select('url', 'user_agent')
->from('tx_tracking_pageview') ->from('tx_tracking_pageview')

View file

@ -55,17 +55,24 @@ class PageviewsPerDay implements ChartDataProviderInterface
*/ */
private $dateFormat; private $dateFormat;
/**
* @var array<int>
*/
private $languageLimitation;
public function __construct( public function __construct(
LanguageService $languageService, LanguageService $languageService,
QueryBuilder $queryBuilder, QueryBuilder $queryBuilder,
int $days = 31, int $days = 31,
array $pagesToExclude = [], array $pagesToExclude = [],
array $languageLimitation = [],
string $dateFormat = 'Y-m-d' string $dateFormat = 'Y-m-d'
) { ) {
$this->languageService = $languageService; $this->languageService = $languageService;
$this->queryBuilder = $queryBuilder; $this->queryBuilder = $queryBuilder;
$this->days = $days; $this->days = $days;
$this->pagesToExclude = $pagesToExclude; $this->pagesToExclude = $pagesToExclude;
$this->languageLimitation = $languageLimitation;
$this->dateFormat = $dateFormat; $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 return (int)$this->queryBuilder
->count('*') ->count('*')
->from('tx_tracking_pageview') ->from('tx_tracking_pageview')

View file

@ -21,6 +21,7 @@ namespace DanielSiepmann\Tracking\Dashboard\Provider;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Dashboard\WidgetApi; use TYPO3\CMS\Dashboard\WidgetApi;
use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface; use TYPO3\CMS\Dashboard\Widgets\ChartDataProviderInterface;
@ -42,14 +43,21 @@ class PageviewsPerOperatingSystem implements ChartDataProviderInterface
*/ */
private $maxResults; private $maxResults;
/**
* @var array<int>
*/
private $languageLimitation;
public function __construct( public function __construct(
QueryBuilder $queryBuilder, QueryBuilder $queryBuilder,
int $days = 31, int $days = 31,
int $maxResults = 6 int $maxResults = 6,
array $languageLimitation = []
) { ) {
$this->queryBuilder = $queryBuilder; $this->queryBuilder = $queryBuilder;
$this->days = $days; $this->days = $days;
$this->maxResults = $maxResults; $this->maxResults = $maxResults;
$this->languageLimitation = $languageLimitation;
} }
public function getChartData(): array 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 $result = $this->queryBuilder
->selectLiteral('count(operating_system) as total') ->selectLiteral('count(operating_system) as total')
->addSelect('operating_system') ->addSelect('operating_system')

View file

@ -61,3 +61,13 @@ Options
This becomes handy if certain pages are called in order to show specific records. 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. 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. 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.

View file

@ -70,3 +70,13 @@ Options
String defining the format used for labels. String defining the format used for labels.
Defaults to 'Y-m-d'. 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.

View file

@ -62,3 +62,13 @@ Options
Defaults to 6 because EXT:dashboard only provides 6 colors. Defaults to 6 because EXT:dashboard only provides 6 colors.
Defaults to 6. 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.

View file

@ -120,4 +120,36 @@ class NewestPageviewsTest extends TestCase
'Url 4 - User-Agent 4', 'Url 4 - User-Agent 4',
], $subject->getItems()); ], $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());
}
} }

View file

@ -133,6 +133,7 @@ class PageviewsPerDayTest extends TestCase
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'), GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
1, 1,
[], [],
[],
'd.m.Y' 'd.m.Y'
); );
@ -143,4 +144,43 @@ class PageviewsPerDayTest extends TestCase
], $result['labels']); ], $result['labels']);
static::assertCount(2, $result['datasets'][0]['data']); 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']);
}
} }

View file

@ -172,4 +172,38 @@ class PageviewsPerOperatingSystemTest extends TestCase
], $result['labels']); ], $result['labels']);
static::assertCount(4, $result['datasets'][0]['data']); 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']);
}
} }