Code improvements

This commit is contained in:
Sven Wappler 2017-12-11 00:18:29 +01:00
parent 8570c2e3cb
commit 5d57e42391
7 changed files with 154 additions and 125 deletions

View file

@ -9,6 +9,7 @@ namespace WapplerSystems\ABTest2;
* LICENSE.txt file that was distributed with this source code.
*/
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Page\PageRepository;
@ -22,27 +23,6 @@ use TYPO3\CMS\Frontend\Page\PageRepository;
class Helper
{
/** @var int|null */
protected $currentPageId = null;
/** @var int|null */
protected $rootpage_id = null;
/** @var array */
protected $realurlConfig = null;
/** @var int|null */
protected $selectBSite = null;
/** @var int|null */
protected $cookieLifeTime = null;
/** @var int|null */
protected $randomAbPageId = null;
/** @var string */
protected $additionalHeaderData;
/**
*
* @param array $params
@ -56,20 +36,20 @@ class Helper
// only try to change the page if it's not the googlebot.
if (false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) {
$this->currentPageId = $pObj->id;
$currentPageId = $randomPageId = $pObj->id;
// Get the rootpage_id from realurl config.
$this->realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl'];
if (array_key_exists($_SERVER['SERVER_NAME'], $this->realurlConfig)) {
$this->rootpage_id = $this->realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id'];
$realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl'];
if (array_key_exists($_SERVER['SERVER_NAME'], $realurlConfig)) {
$rootpage_id = $realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id'];
} else {
$this->rootpage_id = $this->realurlConfig['_DEFAULT']['pagePath']['rootpage_id'];
$rootpage_id = $realurlConfig['_DEFAULT']['pagePath']['rootpage_id'];
}
// If the ID is NULL, then we set this value to the rootpage_id. NULL is the "Home"page, ID is a specific sub-page, e.g. www.domain.de (NULL) - www.domain.de/page.html (ID)
if (!$this->currentPageId) {
if ($this->rootpage_id) {
$this->currentPageId = $this->rootpage_id;
if (!$currentPageId) {
if ($rootpage_id) {
$currentPageId = $rootpage_id;
} else {
// Leave the function because we can not determine the ID.
return;
@ -77,37 +57,62 @@ class Helper
}
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$currentPagePropertiesArray = $pageRepository->getPage($this->currentPageId);
$currentPagePropertiesArray = $pageRepository->getPage($currentPageId);
$this->selectBSite = $currentPagePropertiesArray['tx_abtest2_b_id'];
$this->cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time'];
$pageBPageId = $currentPagePropertiesArray['tx_abtest2_b_id'];
$cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time'];
if ($this->selectBSite) {
if ((int)$_COOKIE['abtest2-' . $this->currentPageId] > 0) {
$this->randomAbPageId = (int)$_COOKIE['abtest2-' . $this->currentPageId];
if ($pageBPageId) {
/* page b id exists */
$cookiePageId = (int)$_COOKIE['abtest2-' . $currentPageId];
if ($cookiePageId > 0 && ($cookiePageId === $pageBPageId || $cookiePageId === $currentPageId)) {
/* valid cookie page id -> select cookie page id */
$randomPageId = $cookiePageId;
} else {
$randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site.
if ($randomPage) {
$this->randomAbPageId = $this->selectBSite;
/* select least used page */
$pageBPropertiesArray = $pageRepository->getPage($pageBPageId);
if ((int)$currentPagePropertiesArray['tx_abtest2_counter'] > (int)$pageBPropertiesArray['tx_abtest2_counter']) {
$randomPageId = $pageBPageId;
$currentPagePropertiesArray = $pageBPropertiesArray;
} elseif ((int)$currentPagePropertiesArray['tx_abtest2_counter'] < (int)$pageBPropertiesArray['tx_abtest2_counter']) {
} else {
$this->randomAbPageId = $this->currentPageId;
/* random */
$randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site.
if ($randomPage) {
$randomPageId = $pageBPageId;
$currentPagePropertiesArray = $pageBPropertiesArray;
}
}
setcookie('abtest2-' . $this->currentPageId, $this->randomAbPageId, time() + $this->cookieLifeTime);
/* rise counter */
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'uid='. (int)$randomPageId, array('tx_abtest2_counter' => $currentPagePropertiesArray['tx_abtest2_counter'] + 1));
setcookie('abtest2-' . $currentPageId, $randomPageId, time() + $cookieLifeTime);
}
// If current page ID is different from the random page ID we set the correct page ID.
if ($this->currentPageId !== $this->randomAbPageId) {
$pObj->contentPid = $this->randomAbPageId;
$GLOBALS['TSFE']->page['content_from_pid'] = $this->randomAbPageId;
$GLOBALS['TSFE']->page['no_cache'] = true;
if ($currentPageId !== $randomPageId) {
$pObj->contentPid = $randomPageId;
$pObj->page['content_from_pid'] = $randomPageId;
}
$pObj->page['no_cache'] = true;
if ($currentPagePropertiesArray) {
$additionalHeaderData = $currentPagePropertiesArray['tx_abtest2_header'];
$additionalFooterData = $currentPagePropertiesArray['tx_abtest2_footer'];
if ($additionalHeaderData) {
$pObj->additionalHeaderData['abtest2'] = $additionalHeaderData;
}
if ($additionalFooterData) {
$pObj->additionalFooterData['abtest2'] = $additionalFooterData;
}
}
}
// If additional headerdata is present then we specify additionalHeaderData.
$randomPagePropertiesArray = $pageRepository->getPage($this->randomAbPageId);
$this->additionalHeaderData = $randomPagePropertiesArray['tx_abtest2_header'];
if ($this->additionalHeaderData) {
$GLOBALS['TSFE']->additionalHeaderData['abtest2'] = $this->additionalHeaderData;
}
}

View file

@ -35,11 +35,18 @@
'type' => 'text'
)
),
'tx_abtest2_footer' => array(
'exclude' => 1,
'label' => 'LLL:EXT:abtest2/Resources/Private/Language/locallang_db.xlf:pages.tx_abtest2_footer',
'config' => array(
'type' => 'text'
)
),
'tx_abtest2_counter' => array(
'exclude' => 1,
'label' => 'LLL:EXT:abtest2/Resources/Private/Language/locallang_db.xlf:pages.tx_abtest2_counter',
'config' => array(
'type' => 'text'
'type' => 'input'
)
)
));

View file

@ -1,48 +1,57 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z" product-name="abtest2">
<file source-language="en" target-language="de" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z"
product-name="abtest2">
<header/>
<body>
<trans-unit id="pages.tx_abtest2_b_id">
<source>Site B</source>
<target>Seite B</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_cookie_time">
<source>Cookie Lifetime</source>
<target>Cookie Lebenszeit</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_header">
<source>Additional Header Information</source>
<target>Zusätzliche Header Informationen</target>
</trans-unit>
<trans-unit id="palette_title">
<source>AB Test Settings</source>
<target>AB Test Einstellungen</target>
</trans-unit>
<trans-unit id="cookie_1_month">
<source>1 month</source>
<target>1 Monat</target>
</trans-unit>
<trans-unit id="cookie_1_week">
<source>1 week</source>
<target>1 Woche</target>
</trans-unit>
<trans-unit id="cookie_1_day">
<source>1 day</source>
<target>1 Tag</target>
</trans-unit>
<trans-unit id="cookie_12_day">
<source>1/2 day</source>
<target>1/2 Tag</target>
</trans-unit>
<trans-unit id="cookie_1_hour">
<source>1 hour</source>
<target>1 Stunde</target>
</trans-unit>
<trans-unit id="cookie_1_minute">
<source>1 minute</source>
<target>1 Minute</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_b_id">
<source>Site B</source>
<target>Seite B</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_cookie_time">
<source>Cookie Lifetime</source>
<target>Cookie Lebenszeit</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_header">
<source>Additional Header Information</source>
<target>Zusätzliche Header Informationen</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_footer">
<source>Additional footer information</source>
<target>Zusätzliche Footer Informationen</target>
</trans-unit>
<trans-unit id="pages.tx_abtest2_counter">
<source>Counter</source>
<target>Zähler</target>
</trans-unit>
<trans-unit id="palette_title">
<source>AB Test Settings</source>
<target>AB Test Einstellungen</target>
</trans-unit>
<trans-unit id="cookie_1_month">
<source>1 month</source>
<target>1 Monat</target>
</trans-unit>
<trans-unit id="cookie_1_week">
<source>1 week</source>
<target>1 Woche</target>
</trans-unit>
<trans-unit id="cookie_1_day">
<source>1 day</source>
<target>1 Tag</target>
</trans-unit>
<trans-unit id="cookie_12_day">
<source>1/2 day</source>
<target>1/2 Tag</target>
</trans-unit>
<trans-unit id="cookie_1_hour">
<source>1 hour</source>
<target>1 Stunde</target>
</trans-unit>
<trans-unit id="cookie_1_minute">
<source>1 minute</source>
<target>1 Minute</target>
</trans-unit>
</body>
</file>
</xliff>

View file

@ -1,38 +1,45 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z" product-name="abtest2">
<file source-language="en" datatype="plaintext" original="messages" date="2017-10-20T11:36:00Z"
product-name="abtest2">
<header/>
<body>
<trans-unit id="pages.tx_abtest2_b_id">
<source>Site B</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_cookie_time">
<source>Cookie Lifetime</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_header">
<source>Additional Header Information</source>
</trans-unit>
<trans-unit id="palette_title">
<source>AB Test Settings</source>
</trans-unit>
<trans-unit id="cookie_1_month">
<source>1 month</source>
</trans-unit>
<trans-unit id="cookie_1_week">
<source>1 week</source>
</trans-unit>
<trans-unit id="cookie_1_day">
<source>1 day</source>
</trans-unit>
<trans-unit id="cookie_12_day">
<source>1/2 day</source>
</trans-unit>
<trans-unit id="cookie_1_hour">
<source>1 hour</source>
</trans-unit>
<trans-unit id="cookie_1_minute">
<source>1 minute</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_b_id">
<source>Site B</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_cookie_time">
<source>Cookie Lifetime</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_header">
<source>Additional header information</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_footer">
<source>Additional footer information</source>
</trans-unit>
<trans-unit id="pages.tx_abtest2_counter">
<source>Counter</source>
</trans-unit>
<trans-unit id="palette_title">
<source>AB Test Settings</source>
</trans-unit>
<trans-unit id="cookie_1_month">
<source>1 month</source>
</trans-unit>
<trans-unit id="cookie_1_week">
<source>1 week</source>
</trans-unit>
<trans-unit id="cookie_1_day">
<source>1 day</source>
</trans-unit>
<trans-unit id="cookie_12_day">
<source>1/2 day</source>
</trans-unit>
<trans-unit id="cookie_1_hour">
<source>1 hour</source>
</trans-unit>
<trans-unit id="cookie_1_minute">
<source>1 minute</source>
</trans-unit>
</body>
</file>
</xliff>

View file

@ -12,7 +12,7 @@
$EM_CONF[$_EXTKEY] = array(
'title' => 'AB Test Pages',
'description' => 'With this extension, administrators can deliver different content for the same URL (AB test), depending on cookies or parameters.',
'description' => 'With this extension, administrators can deliver different content for the same URL (AB test), depending on cookies or least showed page to realize a most accurate possible test.',
'category' => 'misc',
'author' => 'Sven Wappler',
'author_email' => 'typo3YYYY@wappler.systems',

View file

@ -3,4 +3,4 @@
defined('TYPO3_MODE') or die();
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->SelectId';
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->determineContentId';

View file

@ -2,5 +2,6 @@ CREATE TABLE pages (
tx_abtest2_b_id int(11) DEFAULT '0' NOT NULL,
tx_abtest2_cookie_time int(11) DEFAULT '86400' NOT NULL,
tx_abtest2_header text,
tx_abtest2_footer text,
tx_abtest2_counter int(11) DEFAULT '0' NOT NULL
);