From 5d57e42391ba720fad374b62ef1162ac83205592 Mon Sep 17 00:00:00 2001 From: Sven Wappler Date: Mon, 11 Dec 2017 00:18:29 +0100 Subject: [PATCH] Code improvements --- Classes/Helper.php | 105 +++++++++--------- Configuration/TCA/Overrides/pages.php | 9 +- .../Private/Language/de.locallang_db.xlf | 91 ++++++++------- Resources/Private/Language/locallang_db.xlf | 69 ++++++------ ext_emconf.php | 2 +- ext_localconf.php | 2 +- ext_tables.sql | 1 + 7 files changed, 154 insertions(+), 125 deletions(-) diff --git a/Classes/Helper.php b/Classes/Helper.php index 552aa39..5258346 100644 --- a/Classes/Helper.php +++ b/Classes/Helper.php @@ -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; } } diff --git a/Configuration/TCA/Overrides/pages.php b/Configuration/TCA/Overrides/pages.php index d666e6d..9636ebe 100644 --- a/Configuration/TCA/Overrides/pages.php +++ b/Configuration/TCA/Overrides/pages.php @@ -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' ) ) )); diff --git a/Resources/Private/Language/de.locallang_db.xlf b/Resources/Private/Language/de.locallang_db.xlf index a535e59..d10644d 100644 --- a/Resources/Private/Language/de.locallang_db.xlf +++ b/Resources/Private/Language/de.locallang_db.xlf @@ -1,48 +1,57 @@ - +
- - Site B - Seite B - - - Cookie Lifetime - Cookie Lebenszeit - - - Additional Header Information - Zusätzliche Header Informationen - - - AB Test Settings - AB Test Einstellungen - - - 1 month - 1 Monat - - - 1 week - 1 Woche - - - 1 day - 1 Tag - - - 1/2 day - 1/2 Tag - - - 1 hour - 1 Stunde - - - 1 minute - 1 Minute - + + Site B + Seite B + + + Cookie Lifetime + Cookie Lebenszeit + + + Additional Header Information + Zusätzliche Header Informationen + + + Additional footer information + Zusätzliche Footer Informationen + + + Counter + Zähler + + + AB Test Settings + AB Test Einstellungen + + + 1 month + 1 Monat + + + 1 week + 1 Woche + + + 1 day + 1 Tag + + + 1/2 day + 1/2 Tag + + + 1 hour + 1 Stunde + + + 1 minute + 1 Minute + \ No newline at end of file diff --git a/Resources/Private/Language/locallang_db.xlf b/Resources/Private/Language/locallang_db.xlf index 0ed975f..4f941b9 100644 --- a/Resources/Private/Language/locallang_db.xlf +++ b/Resources/Private/Language/locallang_db.xlf @@ -1,38 +1,45 @@ - +
- - Site B - - - Cookie Lifetime - - - Additional Header Information - - - AB Test Settings - - - 1 month - - - 1 week - - - 1 day - - - 1/2 day - - - 1 hour - - - 1 minute - + + Site B + + + Cookie Lifetime + + + Additional header information + + + Additional footer information + + + Counter + + + AB Test Settings + + + 1 month + + + 1 week + + + 1 day + + + 1/2 day + + + 1 hour + + + 1 minute + \ No newline at end of file diff --git a/ext_emconf.php b/ext_emconf.php index bcf8975..df3f186 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -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', diff --git a/ext_localconf.php b/ext_localconf.php index 404fc9a..b0bab82 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -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'; \ No newline at end of file +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->determineContentId'; \ No newline at end of file diff --git a/ext_tables.sql b/ext_tables.sql index b4db240..37a9e82 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -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 ); \ No newline at end of file