2017-12-10 22:19:30 +01:00
< ? php
namespace WapplerSystems\ABTest2 ;
/**
* This file is part of the " abtest2 " Extension for TYPO3 CMS .
*
* For the full copyright and license information , please read the
* LICENSE . txt file that was distributed with this source code .
*/
use TYPO3\CMS\Core\Utility\GeneralUtility ;
2017-12-10 22:33:11 +01:00
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController ;
2017-12-24 00:00:55 +01:00
use TYPO3\CMS\Frontend\Page\CacheHashCalculator ;
2017-12-10 22:19:30 +01:00
use TYPO3\CMS\Frontend\Page\PageRepository ;
/**
* This class detects which page version ( either by cookie or by random ) and sets the page content ID accordingly .
*
* @ package WapplerSystems\ABTest2
* @ author Sven Wapler < typo3YYYYY @ wappler . systems >
*/
2017-12-10 22:33:11 +01:00
class Helper
{
2017-12-10 22:19:30 +01:00
/**
*
* @ param array $params
2017-12-24 00:00:55 +01:00
* @ param $tsFeController TypoScriptFrontendController
2017-12-10 22:19:30 +01:00
* @ return void
* @ throws \InvalidArgumentException
*/
2017-12-24 00:00:55 +01:00
public function determineContentId ( array $params , & $tsFeController )
2017-12-10 22:33:11 +01:00
{
// only try to change the page if it's not the googlebot.
2017-12-24 00:00:55 +01:00
if ( true === stripos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'googlebot' )) return ;
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
$currentPageId = $targetPageId = $tsFeController -> id ;
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
// Get the rootpage_id from realurl config.
$realurlConfig = $tsFeController -> TYPO3_CONF_VARS [ 'EXTCONF' ][ 'realurl' ];
if ( array_key_exists ( $_SERVER [ 'SERVER_NAME' ], $realurlConfig )) {
$rootpage_id = $realurlConfig [ $_SERVER [ 'SERVER_NAME' ]][ 'pagePath' ][ 'rootpage_id' ];
} else {
$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 ( ! $currentPageId ) {
if ( $rootpage_id ) {
$currentPageId = $rootpage_id ;
2017-12-10 22:33:11 +01:00
} else {
2017-12-24 00:00:55 +01:00
// Leave the function because we can not determine the ID.
return ;
2017-12-10 22:33:11 +01:00
}
2017-12-24 00:00:55 +01:00
}
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
$pageRepository = GeneralUtility :: makeInstance ( PageRepository :: class );
$currentPagePropertiesArray = $pageRepository -> getPage ( $currentPageId );
$pageBPageId = $currentPagePropertiesArray [ 'tx_abtest2_b_id' ];
/* TODO: check if page b exists */
$cookieLifeTime = $currentPagePropertiesArray [ 'tx_abtest2_cookie_time' ];
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
if ( $pageBPageId ) {
2017-12-11 00:18:29 +01:00
2017-12-24 00:00:55 +01:00
$pageBPropertiesArray = $pageRepository -> getPage ( $pageBPageId );
$cookieValue = $_COOKIE [ 'abtest2' ];
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
if ( $cookieValue === 'b' ) {
$targetPageId = $pageBPageId ;
$currentPagePropertiesArray = $pageBPropertiesArray ;
} else if ( $cookieValue === 'a' ) {
2017-12-10 22:33:11 +01:00
2017-12-24 00:00:55 +01:00
} else {
$cookieValue = 'a' ;
/* select least used page */
2017-12-11 19:45:03 +01:00
2017-12-24 00:00:55 +01:00
if (( int ) $currentPagePropertiesArray [ 'tx_abtest2_counter' ] > ( int ) $pageBPropertiesArray [ 'tx_abtest2_counter' ]) {
/* take b */
2017-12-11 19:45:03 +01:00
$targetPageId = $pageBPageId ;
2017-12-11 20:12:34 +01:00
$currentPagePropertiesArray = $pageBPropertiesArray ;
2017-12-24 00:00:55 +01:00
$cookieValue = 'b' ;
}
2017-12-11 19:45:03 +01:00
2017-12-24 00:00:55 +01:00
/* rise counter */
$GLOBALS [ 'TYPO3_DB' ] -> exec_UPDATEquery ( 'pages' , 'uid=' . ( int ) $targetPageId , array ( 'tx_abtest2_counter' => $currentPagePropertiesArray [ 'tx_abtest2_counter' ] + 1 ));
2017-12-11 20:12:34 +01:00
2017-12-24 00:00:55 +01:00
setcookie ( 'abtest2' , $cookieValue , time () + $cookieLifeTime );
}
2017-12-11 00:18:29 +01:00
2017-12-24 00:00:55 +01:00
// If current page ID is different from the random page ID we set the correct page ID.
if ( $currentPageId !== $targetPageId ) {
$tsFeController -> contentPid = $targetPageId ;
$tsFeController -> page [ 'content_from_pid' ] = $targetPageId ;
}
2017-12-11 00:18:29 +01:00
2017-12-24 00:00:55 +01:00
$_GET [ 'abtest' ] = $cookieValue ;
2017-12-11 00:18:29 +01:00
2017-12-24 00:00:55 +01:00
$this -> makeCacheHash ( $tsFeController );
2017-12-11 00:18:29 +01:00
2017-12-24 00:00:55 +01:00
if ( $currentPagePropertiesArray ) {
$additionalHeaderData = $currentPagePropertiesArray [ 'tx_abtest2_header' ];
$additionalFooterData = $currentPagePropertiesArray [ 'tx_abtest2_footer' ];
if ( $additionalHeaderData ) {
$tsFeController -> additionalHeaderData [ 'abtest2' ] = $additionalHeaderData ;
}
if ( $additionalFooterData ) {
$tsFeController -> additionalFooterData [ 'abtest2' ] = $additionalFooterData ;
2017-12-10 22:33:11 +01:00
}
}
2017-12-10 22:19:30 +01:00
2017-12-10 22:33:11 +01:00
}
2017-12-10 22:19:30 +01:00
2017-12-24 00:00:55 +01:00
}
/**
*
* @ param $tsFeController TypoScriptFrontendController
* @ return void
* @ throws \InvalidArgumentException
*/
private function makeCacheHash ( & $tsFeController )
{
$GET = GeneralUtility :: _GET ();
2017-12-24 00:28:36 +01:00
/* Fix for root pages */
if ( ! isset ( $GET [ 'id' ])) {
$GET [ 'id' ] = $tsFeController -> id ;
}
2017-12-24 00:00:55 +01:00
/** @var CacheHashCalculator $cacheHash */
$cacheHash = GeneralUtility :: makeInstance ( CacheHashCalculator :: class );
$tsFeController -> cHash_array = $cacheHash -> getRelevantParameters ( GeneralUtility :: implodeArrayForUrl ( '' , $GET ));
$tsFeController -> cHash = $cacheHash -> calculateCacheHash ( $tsFeController -> cHash_array );
2017-12-10 22:33:11 +01:00
}
2017-12-10 22:19:30 +01:00
}