Commit 19f4608b authored by Sven Burkert's avatar Sven Burkert
Browse files

Update/rewrite for TYPO3 8.x

parent f6876f0c
2017-03-02 Sven Burkert <bedienung@sbtheke.de>
* Compatibility to TYPO3 version 8
* Extbase scheduler job
* Email template with Fluid
* AJAX with eId
<?php
class RandomBannersAjax {
protected $bannerRepository;
protected $persistanceManager;
protected $objectManager;
public function __construct() {
$this->bannerRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Randombanners_Domain_Repository_BannerRepository');
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Object_ObjectManager');
$this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager');
}
public function increaseClickNumber($uid) {
$banner = $this->bannerRepository->findByUid($uid);
$banner->setClickedThisMonth($banner->getClickedThisMonth() + 1);
$this->persistanceManager->persistAll();
}
}
$function = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('function');
switch ($function) {
case 'clickBanner':
if ($uid = intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('banner'))) {
$output = new RandomBannersAjax();
$output->increaseClickNumber($uid);
}
break;
default:
$returnValue = 'This function is not implemented!';
break;
}
echo json_encode($returnValue);
?>
\ No newline at end of file
<?php
namespace T3o\Randombanners\Command;
/*
* 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!
*/
/**
* Handle click statistics and send monthly mails
*/
class MonthlyCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController {
/**
* @var \T3o\Randombanners\Domain\Repository\BannerRepository
* @inject
*/
protected $bannerRepository;
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
* @inject
*/
protected $configurationManager;
/**
* @return void
*/
public function MonthlyCommand() {
// No storagePid
$querySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(FALSE);
$this->bannerRepository->setDefaultQuerySettings($querySettings);
$banners = $this->bannerRepository->findAll();
foreach($banners as $banner) {
$this->sendReport($banner);
$this->setStatistics($banner);
}
$this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class)->persistAll();
return TRUE;
}
/**
* Send monthly report to sponsor
*
* @param \T3o\Randombanners\Domain\Model\Banner $banner
* @return boolean
*/
protected function sendReport(\T3o\Randombanners\Domain\Model\Banner $banner) {
if($banner->getEmail()) {
$mailObject = $this->objectManager->get(\TYPO3\CMS\Core\Mail\MailMessage::class);
/* @var \TYPO3\CMS\Core\Mail\MailMessage $mailObject */
// Get email template
$standaloneViewUtility = $this->objectManager->get(\T3o\Randombanners\Utility\StandaloneViewUtility::class);
$renderer = $standaloneViewUtility->getPlainTextView('Scheduler/Email');
$renderer->assign('banner', $banner);
$message = $renderer->render();
$firstLinebreak = strpos($message, PHP_EOL);
$subject = substr($message, 0, $firstLinebreak);
$message = ltrim(substr($message, $firstLinebreak));
// Generate and send mail
$mailObject->setSubject($subject);
$mailObject->addPart($message, 'text/plain');
$mailObject->setFrom(['no-reply@typo3.org' => 'typo3.org Banner']);
if(\TYPO3\CMS\Core\Utility\GeneralUtility::getApplicationContext()->isDevelopment()) {
$configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$mailObject->setTo($configuration['settings']['developmentEmail'], $banner->getName());
} else {
$mailObject->setTo($banner->getEmail(), $banner->getName());
}
return $mailObject->send();
}
}
/**
* Set monthly statistics
*
* @param \T3o\Randombanners\Domain\Model\Banner $banner
*/
protected function setStatistics(\T3o\Randombanners\Domain\Model\Banner $banner) {
$banner->setClickedLastMonth($banner->getClickedThisMonth());
$banner->setClickedThisMonth(0);
$this->bannerRepository->update($banner);
}
}
\ No newline at end of file
<?php <?php
/*************************************************************** namespace T3o\Randombanners\Controller;
* Copyright notice
* /*
* (c) 2011 Thomas Loeffler <loeffler@spooner-web.de> * This file is part of the TYPO3 CMS project.
* *
* All rights reserved * It is free software; you can redistribute it and/or modify it under
* * the terms of the GNU General Public License, either version 2
* This script is part of the TYPO3 project. The TYPO3 project is * of the License, or any later version.
* free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * For the full copyright and license information, please read the
* the Free Software Foundation; either version 3 of the License, or * LICENSE.txt file that was distributed with this source code.
* (at your option) any later version. *
* * The TYPO3 project - inspiring people to share!
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Controller for the Banner object
*/ */
class Tx_Randombanners_Controller_BannerController extends Tx_Extbase_MVC_Controller_ActionController {
/** class BannerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
* @var Tx_Randombanners_Domain_Repository_BannerRepository
*/
protected $bannerRepository;
/**
* @param Tx_Randombanners_Domain_Repository_BannerRepository $bannerRepository
* @return void
*/
public function injectBannerRepository(Tx_Randombanners_Domain_Repository_BannerRepository $bannerRepository) {
$this->bannerRepository = $bannerRepository;
}
/**
* @var \T3o\Randombanners\Domain\Repository\BannerRepository
* @inject
*/
protected $bannerRepository;
/** /**
* @return void * @return void
...@@ -49,42 +29,4 @@ class Tx_Randombanners_Controller_BannerController extends Tx_Extbase_MVC_Contro ...@@ -49,42 +29,4 @@ class Tx_Randombanners_Controller_BannerController extends Tx_Extbase_MVC_Contro
$banners = $this->bannerRepository->findAll(); $banners = $this->bannerRepository->findAll();
$this->view->assign('banners', $banners); $this->view->assign('banners', $banners);
} }
}
\ No newline at end of file
/**
* Set click counter +1 and redirect to sponsor
*
* @param Tx_Randombanners_Domain_Model_Banner $banner
* @return void
*/
public function showAction(Tx_Randombanners_Domain_Model_Banner $banner) {
$banner->setClickedThisMonth($banner->getClickedThisMonth() + 1);
$this->response->setStatus(200);
#$this->response->setHeader('Location', (string)$banner->getLink());
throw new Tx_Extbase_MVC_Exception_StopAction();
#$this->redirectToURI($banner->getLink());
}
/**
* Displays all Statistics
*
* @return void
*/
// public function listAction() {
// // initialization
// $banners = $this->bannerRepository->findAll();
//
// foreach ($banners as $banner) {
// if (!($banner->getLogo() instanceof Tx_Extbase_Domain_Model_Dam)) {
// $banner->setLogo(Tx_ExtbaseDam_Utility_Dam::getOne('tx_randombanners_domain_model_banner', $banner->getUid(), 'tx_randombanner_dam_images'));
// }
//
// // increment the displayed counter
//// $banner->setDisplayedThisMonth($banner->getDisplayedThisMonth() + 1); // seems rather useless when caching is enabled
// }
// $this->view->assign('banners', $banners);
// }
}
?>
\ No newline at end of file
<?php <?php
/*************************************************************** namespace T3o\Randombanners\Domain\Model;
* Copyright notice
* /*
* (c) 2011 Thomas Loeffler <loeffler@spooner-web.de> * This file is part of the TYPO3 CMS project.
* *
* All rights reserved * It is free software; you can redistribute it and/or modify it under
* * the terms of the GNU General Public License, either version 2
* This script is part of the TYPO3 project. The TYPO3 project is * of the License, or any later version.
* free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * For the full copyright and license information, please read the
* the Free Software Foundation; either version 3 of the License, or * LICENSE.txt file that was distributed with this source code.
* (at your option) any later version. *
* * The TYPO3 project - inspiring people to share!
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Statistics for clicking and displaying banners
*/ */
class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_AbstractEntity {
class Banner extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/** /**
* name * name
...@@ -52,17 +41,10 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -52,17 +41,10 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
/** /**
* Logo * Logo
* *
* @var \TYPO3\CMS\Extbase\Domain\Model\FileReference * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
*/ */
protected $logo; protected $logo;
/**
* displayedThisMonth
*
* @var integer
*/
protected $displayedThisMonth;
/** /**
* clickedThisMonth * clickedThisMonth
* *
...@@ -70,13 +52,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -70,13 +52,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
*/ */
protected $clickedThisMonth; protected $clickedThisMonth;
/**
* displayedLastMonth
*
* @var integer
*/
protected $displayedLastMonth;
/** /**
* clickedLastMonth * clickedLastMonth
* *
...@@ -115,21 +90,17 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -115,21 +90,17 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
} }
/** /**
* @param integer $logo * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $logo
* @return void * @return void
*/ */
public function setLogo(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $logo) {
/**
* Set Logo
* @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $logo
*/
public function setLogo($logo) {
$this->logo = $logo; $this->logo = $logo;
} }
/** /**
* Get logo * Get logo
* @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $logo *
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/ */
public function getLogo() { public function getLogo() {
return $this->logo; return $this->logo;
...@@ -150,21 +121,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -150,21 +121,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
return $this->email; return $this->email;
} }
/**
* @param integer $displayedThisMonth
* @return void
*/
public function setDisplayedThisMonth($displayedThisMonth) {
$this->displayedThisMonth = $displayedThisMonth;
}
/**
* @return integer
*/
public function getDisplayedThisMonth() {
return $this->displayedThisMonth;
}
/** /**
* @param integer $clickedThisMonth * @param integer $clickedThisMonth
* @return void * @return void
...@@ -180,21 +136,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -180,21 +136,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
return $this->clickedThisMonth; return $this->clickedThisMonth;
} }
/**
* @param integer $displayedLastMonth
* @return void
*/
public function setDisplayedLastMonth($displayedLastMonth) {
$this->displayedLastMonth = $displayedLastMonth;
}
/**
* @return integer
*/
public function getDisplayedLastMonth() {
return $this->displayedLastMonth;
}
/** /**
* @param string $clickedLastMonth * @param string $clickedLastMonth
* @return void * @return void
...@@ -210,5 +151,4 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr ...@@ -210,5 +151,4 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
return $this->clickedLastMonth; return $this->clickedLastMonth;
} }
} }
?> \ No newline at end of file
\ No newline at end of file
<?php <?php
/*************************************************************** namespace T3o\Randombanners\Domain\Repository;
* Copyright notice
*
* (c) 2011 Thomas Loeffler <loeffler@spooner-web.de>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/*
/** * This file is part of the TYPO3 CMS project.
* Repository for Tx_Randombanners_Domain_Model_Banner *
* 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!
*/ */
class Tx_Randombanners_Domain_Repository_BannerRepository extends Tx_Extbase_Persistence_Repository {
public function findRandomBanners() {
$query = $this->createQuery();
// Workaround for random ordering until Extbase doesn't support this
// See: http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2010-July/005870.html
$backend = $this->objectManager->get('Tx_Extbase_Persistence_Storage_Typo3DbBackend');
$parameters = array();
$statementParts = $backend->parseQuery($query, $parameters);
$statementParts['orderings'][] = ' RAND()';
$statement = $backend->buildQuery($statementParts, $parameters);
$query->statement($statement, $parameters);
return $query->execute(); class BannerRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
} }
} \ No newline at end of file
?>
\ No newline at end of file
<?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 TYPO3\CMS\Core\Utility\GeneralUtility;
$gp = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST();
/* @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)$gp['banner']))
->set('clicked_this_month', 'clicked_this_month + 1', FALSE)
->execute();
\ No newline at end of file
<?php
/*******************************************************************
* Copyright notice
*
* (c) 2011 Thomas Loeffer <loeffler@spooner-web.de>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
******************************************************************/
/**
* Sending monthly mails
*
* @version $Id$
* @copyright Copyright belongs to the respective authors
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*/
class Tx_Randombanners_Task_SendingMonthlyMailsTask extends tx_scheduler_Task {
/**
* @var array
*/
protected $settings;
/**
* @var Tx_Randombanners_Domain_Repository_BannerRepository
*/
protected $bannerRepository;
/**
* @var t3lib_htmlmail
*/
protected $htmlMail;
/**
* @var Tx_Extbase_Object_ObjectManager
*/
protected $objectManager;
/**
* @var Tx_Extbase_Persistence_Manager
*/
protected $persistenceManager;
/**
* Public method, usually called by scheduler.
*
* @return boolean TRUE on success
*/
public function execute() {
$this->initialize();
$banners = $this->bannerRepository->findAll();
foreach ($banners as $banner) {
if ($banner->getEmail() and $this->sendReport($banner)) {
$this->setStatisticsForNewMonth($banner);
}
}
$this->persistenceManager->persistAll();