SendingMonthlyMailsTask.php 4.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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) {
120
			$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";
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

			$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);
		}
	}

?>