MailController.php 13.4 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
<?php
namespace SGalinski\SgMail\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.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!
 ***************************************************************/
27
use SGalinski\SgMail\Domain\Model\Template;
28
use SGalinski\SgMail\Service\MailTemplateService;
29
use SGalinski\SgMail\Session\PhpSession;
30
use TYPO3\CMS\Core\Messaging\FlashMessage;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
34
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
35
36
37
38
39

/**
 * Controller for the mail templating service module
 */
class MailController extends ActionController {
40
41
42
43
	/**
	 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
	 * @inject
	 */
44
	protected $languageRepository;
45

46
47
48
49
	/**
	 * @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
	 * @inject
	 */
50
	protected $templateRepository;
51

52
	/**
53
	 * @var \SGalinski\SgMail\Session\PhpSession
54
55
56
	 */
	protected $session;

57
	/**
58
	 * Show template Selection and enable content input + mail preview
Fabian Galinski's avatar
Fabian Galinski committed
59
	 *
60
61
62
63
64
65
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 * @param string $selectedExtensionKey
	 * @param string $selectedTemplateKey
66
	 */
67
68
69
70
	public function indexAction(
		$selectedTemplate = NULL, $selectedExtension = NULL, $selectedLanguageLeft = NULL,
		$selectedLanguageRight = NULL, $selectedExtensionKey = NULL, $selectedTemplateKey = NULL
	) {
71
72
73
74
		if (!($this->session instanceof PhpSession)) {
			$this->session = $this->objectManager->get('SGalinski\SgMail\Session\PhpSession');
			$this->session->setSessionKey('sg_mail_controller_session');
		}
75

76
		$registerArray = MailTemplateService::getRegisterArray();
77
78
79
80
81
82

		// if no extensions are registered, redirect to empty action
		if (empty($registerArray)) {
			$this->redirect('empty');
		}

83
84
85
86
		$activatedLanguages = $this->languageRepository->findSelected();
		$languages = ['en'];
		foreach ($activatedLanguages as $language) {
			$languages[] = $language->getLocale();
87
		}
88

89
		$this->view->assign('languages', $languages);
90
		$this->view->assign('templates', MailTemplateService::getRegisterArray());
91

92
93
94
		// get last selected languages
		if ($selectedLanguageLeft === NULL) {
			$selectedLanguageLeft = $this->session->getDataByKey('selectedLanguageLeft');
95
			if ($selectedLanguageLeft === NULL) {
96
				$selectedLanguageLeft = $languages[0];
97
			}
98
		}
99

100
101
		if ($selectedLanguageRight === NULL) {
			$selectedLanguageRight = $this->session->getDataByKey('selectedLanguageRight');
102
			if ($selectedLanguageRight === NULL) {
103
104
105
106
				if (isset($languages[1])) {
					$selectedLanguageRight = $languages[1];
				} else {
					$selectedLanguageRight = $languages[0];
107
108
				}
			}
109
		}
110

111
112
113
		if ($selectedTemplate === NULL) {
			$selectedExtension = key($registerArray);
			$selectedTemplate = key($registerArray[$selectedExtension]);
114
		}
115

116
117
118
		$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
		$this->session->setDataByKey('selectedLanguageLeft', $selectedLanguageLeft);
		$this->session->setDataByKey('selectedLanguageRight', $selectedLanguageRight);
119
		$templateLeft = $this->templateRepository->findOneByTemplate(
120
			$selectedExtension, $selectedTemplate, $selectedLanguageLeft
121
122
		);
		$templateRight = $this->templateRepository->findOneByTemplate(
123
			$selectedExtensionKey, $selectedTemplateKey, $selectedLanguageRight
124
		);
125

126
127
		if ($templateLeft !== NULL) {
			$this->view->assign('contentLeft', $templateLeft->getContent());
128
		} else {
129
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
130
131
132
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.' . 'template.html';
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
133
			}
134
		}
135

136
137
138
		if ($templateRight !== NULL) {
			$this->view->assign('contentRight', $templateRight->getContent());
		} else {
139
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
140
141
142
143
144
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.' . 'template.html';
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
			}
		}
145

146
		$subject = $registerArray[$selectedExtension][$selectedTemplate]['subject'];
147

148
149
150
151
152
153
154
155
156
157
158
		if (is_array($subject)) {
			$this->view->assign(
				'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() :
				$registerArray[$selectedExtension][$selectedTemplate]['subject'][$selectedLanguageLeft]
			);

			$this->view->assign(
				'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() :
				$registerArray[$selectedExtension][$selectedTemplate]['subject'][$selectedLanguageRight]
			);
		} else {
159
160
161
162
163
164
165
166
167
168
169
			$langFile = GeneralUtility::readLLfile(
				'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageLeft
			);
			$translatedSubject = '';

			if ($langFile[$selectedLanguageLeft][$subject][0]) {
				$translatedSubject = $langFile[$selectedLanguageLeft][$subject][0]['target'];
			} else {
				$translatedSubject = $langFile['default'][$subject][0]['target'];
			}

170
171
			$this->view->assign(
				'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() :
172
				$translatedSubject
173
174
			);

175
176
177
178
179
180
181
182
183
184
185
			$langFile = GeneralUtility::readLLfile(
				'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageRight
			);
			$translatedSubject = '';

			if ($langFile[$selectedLanguageLeft][$subject][0][$subject]) {
				$translatedSubject = $langFile[$selectedLanguageRight][$subject][0]['target'];
			} else {
				$translatedSubject = $langFile['default'][$subject][0]['target'];
			}

186
187
			$this->view->assign(
				'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() :
188
				$translatedSubject
189
190
			);
		}
191

192
193
194
195
196
197
198
199
		$templateDescription = $registerArray[$selectedExtension][$selectedTemplate]['description'];
		if (is_array($templateDescription)) {
			$templateDescription = $templateDescription[$selectedLanguageLeft];
		} else {
			$templateDescription = LocalizationUtility::translate(
				$templateDescription, $selectedExtension
			);
		}
200

201
		$this->view->assign('templateDescription', $templateDescription);
202
203
204
205
206
		$this->view->assign('selectedTemplate', $selectedTemplateArray);
		$this->view->assign('selectedTemplateKey', $selectedTemplate);
		$this->view->assign('selectedExtensionKey', $selectedExtension);
		$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
		$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
207
208
	}

209
210
	/**
	 * Save content (left & right)
211
	 * if left & right is the same language, the right content will apply only if its another language
212
213
214
215
216
217
218
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
219
220
	 * @param string $subjectLeft
	 * @param string $subjectRight
221
222
223
224
225
226
227
228
229
230
	 * @param string $fromNameLeft
	 * @param string $fromMailLeft
	 * @param string $ccLeft
	 * @param string $bccLeft
	 * @param string $replyToLeft
	 * @param string $fromNameRight
	 * @param string $fromMailRight
	 * @param string $ccRight
	 * @param string $bccRight
	 * @param string $replyToRight
231
232
233
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
234
235
236
		$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL,
		$fromNameLeft = NULL, $fromMailLeft = NULL, $ccLeft = NULL, $bccLeft = NULL, $replyToLeft = NULL,
		$fromNameRight = NULL, $fromMailRight = NULL, $ccRight = NULL, $bccRight = NULL, $replyToRight = NULL
237
	) {
238
239
		$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft,
			$fromNameLeft, $fromMailLeft, $ccLeft, $bccLeft, $replyToLeft);
240
241
		if ($selectedLanguageLeft !== $selectedLanguageRight) {
			$this->saveTemplate(
242
243
				$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight,
				$fromNameRight, $fromMailRight, $ccRight, $bccRight, $replyToRight
244
245
			);
		}
246

247
		$message = LocalizationUtility::translate('backend.success', 'sg_mail');
248
		$this->addFlashMessage($message, '', FlashMessage::OK);
249
		$arguments = $this->request->getArguments();
250
		$this->redirect('index', NULL, NULL, $arguments);
251
252
	}

253
254
255
256
257
258
259
260
	/**
	 * Save or update the template in the DB, depending if it already exists or not
	 *
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguage
	 * @param string $selectedContent
	 * @param string $selectedSubject
261
262
263
264
265
	 * @param string $selectedFromName
	 * @param string $selectedFromMail
	 * @param string $selectedCc
	 * @param string $selectedBcc
	 * @param string $selectedReplyTo
266
267
268
	 * @return Template $template
	 */
	private function saveTemplate(
269
270
		$selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject,
		$selectedFromName, $selectedFromMail, $selectedCc, $selectedBcc, $selectedReplyTo
271
272
	) {
		/** @var Template $template */
273
		$template = $this->templateRepository->findOneByTemplate(
274
			$selectedExtension, $selectedTemplate, $selectedLanguage
275
		);
276

277
278
279
280
281
282
		$templateAlreadyExists = TRUE;
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		if ($template === NULL) {
			$templateAlreadyExists = FALSE;
			$template = $objectManager->get(Template::class);
		}
283

284
285
286
287
288
		$template->setExtensionKey($selectedExtension);
		$template->setTemplateName($selectedTemplate);
		$template->setLanguage($selectedLanguage);
		$template->setContent($selectedContent);
		$template->setSubject($selectedSubject);
289
290
291
292
293
		$template->setFromName($selectedFromName);
		$template->setFromMail($selectedFromMail);
		$template->setCc($selectedCc);
		$template->setBcc($selectedBcc);
		$template->setReplyTo($selectedReplyTo);
294

295
296
297
298
299
		if ($templateAlreadyExists) {
			$this->templateRepository->update($template);
		} else {
			$this->templateRepository->add($template);
		}
300

301
302
303
		return $template;
	}

304
305
306
307
308
	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
309
310
311
	 * @param string $selectedTemplateKey
	 * @param string $selectedExtensionKey
	 * @param string $selectedLanguageLeft
312
	 * @param string $selectedLanguageRight
313
314
	 */
	public function sendTestMailAction(
Torsten Oppermann's avatar
Torsten Oppermann committed
315
		$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft, $selectedLanguageRight
316
	) {
317
318
319
320
321
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(MailTemplateService::class);
		$mailTemplateService->setLanguage($selectedLanguageLeft);
		$mailTemplateService->setToAddresses($emailAddress);
322
		$mailTemplateService->setFromAddress('noreply@example.org');
323
324
		$mailTemplateService->setTemplateName($selectedTemplateKey);
		$mailTemplateService->setExtensionKey($selectedExtensionKey);
325
		$mailIsSend = $mailTemplateService->sendEmail(TRUE);
326

327
328
329
330
331
		if ($selectedLanguageRight !== $selectedLanguageLeft) {
			/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
			$mailTemplateService = $objectManager->get(MailTemplateService::class);
			$mailTemplateService->setLanguage($selectedLanguageRight);
			$mailTemplateService->setToAddresses($emailAddress);
332
			$mailTemplateService->setFromAddress('noreply@example.org');
333
334
			$mailTemplateService->setTemplateName($selectedTemplateKey);
			$mailTemplateService->setExtensionKey($selectedExtensionKey);
335
			$mailIsSend = $mailTemplateService->sendEmail(TRUE);
336
		}
337

338
		if ($mailIsSend) {
339
340
341
342
343
344
			$message = LocalizationUtility::translate('backend.success_mail', 'sg_mail');
			$this->addFlashMessage($message, '', FlashMessage::OK);
		} else {
			$message = LocalizationUtility::translate('backend.failure_mail', 'sg_mail');
			$this->addFlashMessage($message, '', FlashMessage::ERROR);
		}
345

346
347
348
		$arguments = $this->request->getArguments();
		$this->redirect('index', NULL, NULL, $arguments);
	}
349
350

	/**
351
	 * show a notice when no extension is registered
352
353
354
355
	 */
	public function emptyAction() {

	}
356
357

	/**
358
	 * reset template values to default state for a specific language (delete from db)
Torsten Oppermann's avatar
Torsten Oppermann committed
359
	 *
360
	 * @param string $template
Torsten Oppermann's avatar
Torsten Oppermann committed
361
	 * @param string $extensionKey
362
	 * @param string $language
363
	 */
364
	public function resetAction($template, $extensionKey, $language = '') {
Torsten Oppermann's avatar
Torsten Oppermann committed
365
		$this->templateRepository->deleteTemplate($extensionKey, $template, $language);
366

367
368
369
370
371
372
		$message = LocalizationUtility::translate('backend.template_reset', 'sg_mail');
		$this->addFlashMessage($message, '', FlashMessage::OK);

		$arguments = $this->request->getArguments();
		$this->redirect('index', NULL, NULL, $arguments);
	}
373
}