Commit 17906737 authored by thomas.loeffler's avatar thomas.loeffler

fixed a bug in scheduler task.

git-svn-id: http://svn.t3o.typo3.org/extensions/projects/t3org/randombanners/trunk@696 e8cb78a8-64f2-4011-8042-bd1ea5058d47
parent ee52223b
<?php
class RandomBannersAjax {
protected $bannerRepository;
protected $persistanceManager;
protected $objectManager;
public function __construct() {
$this->bannerRepository = t3lib_div::makeInstance('Tx_Randombanners_Domain_Repository_BannerRepository');
$this->objectManager = t3lib_div::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 = t3lib_div::_GET('function');
switch ($function) {
case 'clickBanner':
if ($uid = intval(t3lib_div::_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
/***************************************************************
* 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
*/
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;
}
/**
*
*
* @return void
*/
public function indexAction() {
}
/**
* 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
*
* @param integer $numberOfBannersShown
* @return void
*/
public function listAction($numberOfBannersShown=0) {
// initialization
$banners = $this->bannerRepository->findRandomBanners();
$newBanners = array();
$saveIntoSession = array();
// get the last set of banners from session
$lastBanners = $GLOBALS['TSFE']->fe_user->getKey('ses', 'tx_randombanners');
$numberOfBannersShown = ($numberOfBannersShown?$numberOfBannersShown:$this->settings['numberOfBannersShown']);
// first round to get all banners not displayed the last time
foreach ($banners as $key => $banner) {
$banner->setLogo(Tx_ExtbaseDam_Utility_Dam::getOne('tx_randombanners_domain_model_banner', $banner->getUid(), 'tx_randombanner_dam_images'));
if (sizeof($newBanners) < $numberOfBannersShown) {
if (empty($lastBanners)) {
$newBanners[] = $banner;
} elseif (array_search($banner->getUid(), $lastBanners) === FALSE) {
$newBanners[] = $banner;
unset($banners[$key]);
}
} else {
break;
}
}
// second round to get some other banners if not enough banners in the list
foreach ($banners as $banner) {
if (sizeof($newBanners) < $numberOfBannersShown) {
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'));
}
$newBanners[] = $banner;
} else {
break;
}
}
foreach ($newBanners as $newBanner) {
// get uids of all shown banners to save into session
$saveIntoSession[] = $newBanner->getUid();
// increment the displayed counter
$newBanner->setDisplayedThisMonth($newBanner->getDisplayedThisMonth() + 1);
}
// set array of uids into session
$GLOBALS['TSFE']->fe_user->setKey('ses', 'tx_randombanners', $saveIntoSession);
$this->view->assign('banners', $newBanners);
}
}
?>
\ 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
*/
class Tx_Randombanners_Domain_Model_Banner extends Tx_Extbase_DomainObject_AbstractEntity {
/**
* name
*
* @var string
*/
protected $name;
/**
* link
*
* @var string
*/
protected $link;
/**
* email
*
* @var string
*/
protected $email;
/**
* logo
*
* @var integer
*/
protected $logo;
/**
* displayedThisMonth
*
* @var integer
*/
protected $displayedThisMonth;
/**
* clickedThisMonth
*
* @var integer
*/
protected $clickedThisMonth;
/**
* displayedLastMonth
*
* @var integer
*/
protected $displayedLastMonth;
/**
* clickedLastMonth
*
* @var string
*/
protected $clickedLastMonth;
/**
* @param string $name
* @return void
*/
public function setName($name) {
$this->name = $name;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param string $link
* @return void
*/
public function setLink($link) {
$this->link = $link;
}
/**
* @return string
*/
public function getLink() {
return $this->link;
}
/**
* @param integer $logo
* @return void
*/
public function setLogo($logo) {
$this->logo = $logo;
}
/**
* @return integer
*/
public function getLogo() {
return $this->logo;
}
/**
* @param string $email
* @return void
*/
public function setEmail($email) {
$this->email = $email;
}
/**
* @return string
*/
public function getEmail() {
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
*/
public function setClickedThisMonth($clickedThisMonth) {
$this->clickedThisMonth = $clickedThisMonth;
}
/**
* @return integer
*/
public function getClickedThisMonth() {
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
*/
public function setClickedLastMonth($clickedLastMonth) {
$this->clickedLastMonth = $clickedLastMonth;
}
/**
* @return string
*/
public function getClickedLastMonth() {
return $this->clickedLastMonth;
}
}
?>
\ 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!
***************************************************************/
/**
* Repository for Tx_Randombanners_Domain_Model_Banner
*/
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
<?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
/*******************************************************************
* 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$
* @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_Utility_TypoScript {
/**
* @var tslib_cObj
*/
static protected $contentObject;
/**
* @var Tx_Extbase_Configuration_ConfigurationManager
*/
static protected $configurationManager;
/**
* Initialize configuration manager and content object
*
* @return void
*/
static protected function initialize() {
// Get configuration manager
$objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
self::$configurationManager = $objectManager->get('Tx_Extbase_Configuration_ConfigurationManager');
// Simulate Frontend
if (TYPO3_MODE == 'BE') {
Tx_Extbase_Utility_FrontendSimulator::simulateFrontendEnvironment();
if (empty($GLOBALS['TSFE']->sys_page)) {
$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
}