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

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
/***************************************************************
* 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!
***************************************************************/
/**
* Controller for the Banner object
namespace T3o\Randombanners\Controller;
/*
* 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!
*/
class Tx_Randombanners_Controller_BannerController extends Tx_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;
}
class BannerController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
* @var \T3o\Randombanners\Domain\Repository\BannerRepository
* @inject
*/
protected $bannerRepository;
/**
* @return void
......@@ -49,42 +29,4 @@ class Tx_Randombanners_Controller_BannerController extends Tx_Extbase_MVC_Contro
$banners = $this->bannerRepository->findAll();
$this->view->assign('banners', $banners);
}
/**
* 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
}
\ No newline at end of file
<?php
/***************************************************************
* 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!
***************************************************************/
/**
* Statistics for clicking and displaying banners
namespace T3o\Randombanners\Domain\Model;
/*
* 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!
*/
class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_AbstractEntity {
class Banner extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* name
......@@ -52,17 +41,10 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
/**
* Logo
*
* @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
*/
protected $logo;
/**
* displayedThisMonth
*
* @var integer
*/
protected $displayedThisMonth;
/**
* clickedThisMonth
*
......@@ -70,13 +52,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
*/
protected $clickedThisMonth;
/**
* displayedLastMonth
*
* @var integer
*/
protected $displayedLastMonth;
/**
* clickedLastMonth
*
......@@ -115,21 +90,17 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
}
/**
* @param integer $logo
* @return void
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $logo
* @return void
*/
/**
* Set Logo
* @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $logo
*/
public function setLogo($logo) {
public function setLogo(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $logo) {
$this->logo = $logo;
}
/**
* Get logo
* @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $logo
*
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
public function getLogo() {
return $this->logo;
......@@ -150,21 +121,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
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
* @return void
......@@ -180,21 +136,6 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
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
* @return void
......@@ -210,5 +151,4 @@ class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_Abstr
return $this->clickedLastMonth;
}
}
?>
\ No newline at end of file
}
\ No newline at end of file
<?php
/***************************************************************
* 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!
***************************************************************/
namespace T3o\Randombanners\Domain\Repository;
/**
* Repository for Tx_Randombanners_Domain_Model_Banner
/*
* 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!
*/
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();
}
}
?>
\ No newline at end of file
class BannerRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
}
\ 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();
return TRUE;
}
/**
* Initialize environment
*
* @return void
*/
protected function initialize() {
// Dummy Extension configuration for Dispatcher
$configuration = array(
'extensionName' => 'Randombanners',
'pluginName' => 'List',
);
// Get TypoScript configuration
$setup = Tx_Randombanners_Utility_TypoScript::getSetup();
$this->settings = Tx_Randombanners_Utility_TypoScript::parse($setup['settings.'], FALSE);
$configuration = array_merge($configuration, $setup);
// Load Dispatcher
$dispatcher = t3lib_div::makeInstance('Tx_Extbase_Core_Bootstrap');
$dispatcher->initialize($configuration);
// Load required objects
$this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
$this->bannerRepository = t3lib_div::makeInstance('Tx_Randombanners_Domain_Repository_BannerRepository');
$this->htmlMail = t3lib_div::makeInstance('t3lib_htmlmail');
$this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager');
}
/**
* Sending report to sponsor
*
* @param Tx_Randombanners_Domain_Model_Banner $banner
* @return boolean
*/
protected function sendReport(Tx_Randombanners_Domain_Model_Banner $banner) {
$message = "Your banner statistics for ".date('F Y', strtotime('-1 month')).":\n\nDisplayed: ".$banner->getDisplayedThisMonth()." times\nClicked: ".$banner->getClickedThisMonth()." times\n\nThanks for sponsoring\nTYPO3 Association";
$this->htmlMail->start();
$this->htmlMail->subject = 'Report for your banner on typo3.org on '.date('F', strtotime('-1 month'));
$this->htmlMail->from_email = 'no-reply@typo3.org';
$this->htmlMail->from_name = 'TYPO3 Website Banner';
$this->htmlMail->organisation = 'TYPO3 Association';
$this->htmlMail->addPlain($message);
$sent = $this->htmlMail->send($banner->getEmail());
return $sent;
}
protected function setStatisticsForNewMonth(Tx_Randombanners_Domain_Model_Banner $banner) {
$banner->setClickedLastMonth($banner->getClickedThisMonth());
$banner->setDisplayedLastMonth($banner->getDisplayedThisMonth());
$banner->setDisplayedThisMonth(0);
$banner->setClickedThisMonth(0);
}
}
?>
\ No newline at end of file
<?php
namespace T3o\Randombanners\Utility;
/*
* 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!
*/
class StandaloneViewUtility {
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
* @inject
*/
protected $configurationManager;
/**
* objectManager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @inject
*/
protected $objectManager;
/**
* Creates a stand-alone instance of the Fluid view to render a plain text template
*
* @param string $templateName: the name of the template to use
* @return \TYPO3\CMS\Fluid\View\StandaloneView: the Fluid instance
*/
public function getPlainTextView($templateName) {
$configuration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$standaloneView = $this->objectManager->get(\TYPO3\CMS\Fluid\View\StandaloneView::class);
$standaloneView->getRequest()->setControllerExtensionName($configuration['extensionName']);
$standaloneView->setLayoutRootPaths($configuration['view']['layoutRootPaths']);
$standaloneView->setPartialRootPaths($configuration['view']['partialRootPaths']);
$standaloneView->setTemplateRootPaths($configuration['view']['templateRootPaths']);
$standaloneView->setTemplate($templateName);
return $standaloneView;
}
}
\ No newline at end of file
<?php
/*******************************************************************
* 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 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!
******************************************************************/
/**
* Utilities to manage and convert Typoscript Code
*
* @version $Id$