Commit 6aae6292 authored by Thomas Löffler's avatar Thomas Löffler
Browse files

[FEATURE] Switch to middleware for click count and redirect

parent 41d60f7b
Pipeline #12081 failed with stages
in 1 minute and 40 seconds
<?php
namespace T3o\Randombanners\Eid;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class RandombannersEidController
{
public function countClick(ServerRequestInterface $request)
{
$banner = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('banner');
/* @var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tx_randombanners_domain_model_banner');
$queryBuilder
->update('tx_randombanners_domain_model_banner')
->where(
$queryBuilder->expr()->eq('uid', (int)$banner)
)
->set('clicked_this_month', 'clicked_this_month + 1', false)
->execute();
return new Response();
}
}
<?php
namespace T3o\Randombanners\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class ClickCountMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($request->getQueryParams()['randombanners']['banner'] > 0) {
$bannerRecord = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_randombanners_domain_model_banner')
->select(
['uid', 'link'],
'tx_randombanners_domain_model_banner',
['uid' => (int)$request->getQueryParams()['randombanners']['banner']]
)
->fetch();
if ($bannerRecord) {
$queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('tx_randombanners_domain_model_banner');
$queryBuilder
->update('tx_randombanners_domain_model_banner')
->where(
$queryBuilder->expr()->eq('uid', (int)$bannerRecord['uid'])
)
->set('clicked_this_month', 'clicked_this_month + 1', false)
->execute();
if ($bannerRecord['link']) {
return new RedirectResponse($bannerRecord['link']);
}
}
}
return $handler->handle($request);
}
}
<?php
return [
'frontend' => [
'randombanners-click-redirect' => [
'target' => \T3o\Randombanners\Middleware\ClickCountMiddleware::class,
'before' => [
'typo3/cms-frontend/eid',
],
],
],
];
......@@ -8,11 +8,11 @@
<ul class="t3o-banners t3js-banners text-center">
<f:for each="{banners}" as="banner">
<li>
<f:link.external class="t3js-banner" uri="{f:uri.external(uri: banner.link)}" target="_blank" additionalAttributes="{data-uid: banner.uid, rel: settings.linkAttributeRel}">
<f:link.page class="t3js-banner" target="_blank" arguments="{randombanners: '{banner: banner.uid}'}" additionalAttributes="{data-uid: banner.uid, rel: settings.linkAttributeRel}">
<f:if condition="{banner.logo}">
<f:image image="{banner.logo}" alt="{banner.name}" maxWidth="150" />
</f:if>
</f:link.external>
</f:link.page>
</li>
</f:for>
</ul>
......
......@@ -8,7 +8,7 @@ let randombanners = {} || window.randombanners;
randombanners.shuffle = function (element) {
$element = document.querySelector(element);
if ($(element).children.length) {
if ($(element).children.length > 1) {
$element = document.querySelector(element);
for (let i = $(element).children.length; i >= 0; i--) {
$element.appendChild($element.children[Math.random() * i | 0]);
......@@ -16,20 +16,6 @@ randombanners.shuffle = function (element) {
}
};
/**
* Count click
*
* @param {string} bannerId
* @param {function} callback
*/
randombanners.countClick = function (bannerId, callback) {
let xhr = new XMLHttpRequest();
xhr.open('POST', 'index.php?eID=randombanners&banner=' + bannerId, true);
xhr.onreadystatechange = callback(xhr);
xhr.send();
};
(function () {
randombanners.shuffle('.t3js-banners');
......@@ -37,18 +23,5 @@ randombanners.countClick = function (bannerId, callback) {
setInterval(function () {
randombanners.shuffle('.t3js-banners')
}, 8000);
let banners = document.querySelectorAll('.t3js-banner');
banners.forEach(function (banner) {
banner.addEventListener('click', function () {
randombanners.countClick(banner.dataset.uid, function (request) {
if (request.readyState !== 4 || request.status !== 200) {
console.log(':)')
} else {
console.log(':(')
}
})
})
});
})();
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment