MailController.php 16.5 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\BackendService;
29
use SGalinski\SgMail\Service\MailTemplateService;
30
use SGalinski\SgMail\Session\PhpSession;
31
32
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
33
use TYPO3\CMS\Core\Messaging\FlashMessage;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
36
use TYPO3\CMS\Extbase\Object\ObjectManager;
37
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
38
39
40
41
42

/**
 * Controller for the mail templating service module
 */
class MailController extends ActionController {
43
44
45
46
47
48
49
	/**
	 * DocHeaderComponent
	 *
	 * @var DocHeaderComponent
	 */
	protected $docHeaderComponent;

50
51
52
53
	/**
	 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
	 * @inject
	 */
54
	protected $languageRepository;
55

56
57
58
59
	/**
	 * @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
	 * @inject
	 */
60
	protected $templateRepository;
61

62
	/**
63
	 * @var \SGalinski\SgMail\Session\PhpSession
64
65
66
	 */
	protected $session;

67
	/**
68
	 * Show template Selection and enable content input + mail preview
Fabian Galinski's avatar
Fabian Galinski committed
69
	 *
70
71
72
73
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
74
75
	 * @throws \InvalidArgumentException
	 * @throws \UnexpectedValueException
76
77
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
78
	 */
79
80
	public function indexAction(
		$selectedTemplate = NULL, $selectedExtension = NULL, $selectedLanguageLeft = NULL,
81
		$selectedLanguageRight = NULL
82
	) {
Torsten Oppermann's avatar
Torsten Oppermann committed
83
		$siteRoot = BackendService::getSiteRoot((int) GeneralUtility::_GP('id'));
84
		$siteRootId = $siteRoot['uid'];
Torsten Oppermann's avatar
Torsten Oppermann committed
85

86
87
88
89
		if (!($this->session instanceof PhpSession)) {
			$this->session = $this->objectManager->get('SGalinski\SgMail\Session\PhpSession');
			$this->session->setSessionKey('sg_mail_controller_session');
		}
90

91
		$registerArray = MailTemplateService::getRegisterArray();
92
93
94
95
96
97

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

98
99
100
101
		$activatedLanguages = $this->languageRepository->findSelected();
		$languages = ['en'];
		foreach ($activatedLanguages as $language) {
			$languages[] = $language->getLocale();
102
		}
103

104
		$this->view->assign('languages', $languages);
105
		$this->view->assign('templates', MailTemplateService::getRegisterArray());
106

107
108
		// get last selected languages
		if ($selectedLanguageLeft === NULL) {
109
110
			$selectedLanguageLeftFromSession = $this->session->getDataByKey('selectedLanguageLeft');
			if ($selectedLanguageLeftFromSession=== NULL) {
111
				$selectedLanguageLeft = $languages[0];
112
113
			} else {
				$selectedLanguageLeft = $selectedLanguageLeftFromSession;
114
			}
115
		}
116

117
		if ($selectedLanguageRight === NULL) {
118
119
			$selectedLanguageRightFromSession = $this->session->getDataByKey('selectedLanguageRight');
			if ($selectedLanguageRightFromSession === NULL) {
120
121
122
123
				if (isset($languages[1])) {
					$selectedLanguageRight = $languages[1];
				} else {
					$selectedLanguageRight = $languages[0];
124
				}
125
126
			} else {
				$selectedLanguageRight = $selectedLanguageRightFromSession;
127
			}
128
		}
129

130
		if ($selectedTemplate === NULL || $selectedTemplate === '') {
131
132
			$selectedExtension = key($registerArray);
			$selectedTemplate = key($registerArray[$selectedExtension]);
133
		}
134

135
136
137
		$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
		$this->session->setDataByKey('selectedLanguageLeft', $selectedLanguageLeft);
		$this->session->setDataByKey('selectedLanguageRight', $selectedLanguageRight);
138
		$templateLeft = $this->templateRepository->findOneByTemplate(
139
			$selectedExtension, $selectedTemplate, $selectedLanguageLeft, $siteRootId
140
141
		);
		$templateRight = $this->templateRepository->findOneByTemplate(
142
			$selectedExtension, $selectedTemplate, $selectedLanguageRight, $siteRootId
143
		);
144

145
146
		if ($templateLeft !== NULL) {
			$this->view->assign('contentLeft', $templateLeft->getContent());
147
148
149
150
151
			$this->view->assign('fromNameLeft', $templateLeft->getFromName());
			$this->view->assign('fromMailLeft', $templateLeft->getFromMail());
			$this->view->assign('ccLeft', $templateLeft->getCc());
			$this->view->assign('bccLeft', $templateLeft->getBcc());
			$this->view->assign('replyToLeft', $templateLeft->getReplyTo());
152
		} else {
153
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
154
155
156
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.' . 'template.html';
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
157
			}
158
		}
159

160
161
		if ($templateRight !== NULL) {
			$this->view->assign('contentRight', $templateRight->getContent());
162
163
164
165
166
			$this->view->assign('fromNameRight', $templateRight->getFromName());
			$this->view->assign('fromMailRight', $templateRight->getFromMail());
			$this->view->assign('ccRight', $templateRight->getCc());
			$this->view->assign('bccRight', $templateRight->getBcc());
			$this->view->assign('replyToRight', $templateRight->getReplyTo());
167
		} else {
168
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
169
170
171
172
173
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.' . 'template.html';
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
			}
		}
174

175
		$subject = $registerArray[$selectedExtension][$selectedTemplate]['subject'];
176

177
178
179
180
181
182
183
184
185
186
187
		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 {
188
189
190
191
			$langFile = GeneralUtility::readLLfile(
				'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageLeft
			);

192
			$translatedSubject = $langFile['default'][$subject][0]['target'];
193
194
195
196
			if ($langFile[$selectedLanguageLeft][$subject][0]) {
				$translatedSubject = $langFile[$selectedLanguageLeft][$subject][0]['target'];
			}

197
198
			$this->view->assign(
				'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() :
199
				$translatedSubject
200
201
			);

202
203
204
205
			$langFile = GeneralUtility::readLLfile(
				'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageRight
			);

206
			$translatedSubject = $langFile['default'][$subject][0]['target'];
207
208
209
210
			if ($langFile[$selectedLanguageLeft][$subject][0][$subject]) {
				$translatedSubject = $langFile[$selectedLanguageRight][$subject][0]['target'];
			}

211
212
			$this->view->assign(
				'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() :
213
				$translatedSubject
214
215
			);
		}
216

217
218
219
220
221
222
223
224
		$templateDescription = $registerArray[$selectedExtension][$selectedTemplate]['description'];
		if (is_array($templateDescription)) {
			$templateDescription = $templateDescription[$selectedLanguageLeft];
		} else {
			$templateDescription = LocalizationUtility::translate(
				$templateDescription, $selectedExtension
			);
		}
225

226
227
228
229
		// create doc header component
		$pageUid = (int) GeneralUtility::_GP('id');
		$pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(1));

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
		if ($pageInfo) {
			$this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
			$this->docHeaderComponent->setMetaInformation($pageInfo);
			BackendService::makeButtons($this->docHeaderComponent, $this->request);

			$this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());
			$this->view->assign('templateDescription', $templateDescription);
			$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);
			$this->view->assign('mode', 'editor');
		} else {
			$this->view->assign('noPage', '1');
			$this->addFlashMessage(
				LocalizationUtility::translate('backend.notice.OtherPages', 'SgMail'), '', FlashMessage::INFO
			);
			$this->view->assign('pages', BackendService::getPages());
		}
250
251
252

	}

253
254
	/**
	 * Save content (left & right)
255
	 * if left & right is the same language, the right content will apply only if its another language
256
257
258
259
260
261
262
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
263
264
	 * @param string $subjectLeft
	 * @param string $subjectRight
265
266
267
268
269
270
271
272
273
274
	 * @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
275
276
277
278
279
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
280
281
282
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
283
284
285
		$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
286
	) {
287
288
289
290
		$this->saveTemplate(
			$selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft,
			$fromNameLeft, $fromMailLeft, $ccLeft, $bccLeft, $replyToLeft
		);
291
292
		if ($selectedLanguageLeft !== $selectedLanguageRight) {
			$this->saveTemplate(
293
294
				$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight,
				$fromNameRight, $fromMailRight, $ccRight, $bccRight, $replyToRight
295
296
			);
		}
297

298
		$message = LocalizationUtility::translate('backend.success', 'sg_mail');
299
		$this->addFlashMessage($message, '', FlashMessage::OK);
300
		$arguments = $this->request->getArguments();
301
		$this->redirect('index', NULL, NULL, $arguments);
302
303
	}

304
305
306
307
308
309
310
311
	/**
	 * 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
312
313
314
315
316
	 * @param string $selectedFromName
	 * @param string $selectedFromMail
	 * @param string $selectedCc
	 * @param string $selectedBcc
	 * @param string $selectedReplyTo
317
	 * @return Template $template
318
319
320
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
321
322
	 */
	private function saveTemplate(
323
324
		$selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject,
		$selectedFromName, $selectedFromMail, $selectedCc, $selectedBcc, $selectedReplyTo
325
	) {
326
327
328
		$pageUid = (int) GeneralUtility::_GP('id');
		$siteRootId = BackendService::getSiteRoot($pageUid)['uid'];

329
		/** @var Template $template */
330
		$template = $this->templateRepository->findOneByTemplate(
331
			$selectedExtension, $selectedTemplate, $selectedLanguage, $siteRootId
332
		);
333

334
335
336
337
338
339
		$templateAlreadyExists = TRUE;
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		if ($template === NULL) {
			$templateAlreadyExists = FALSE;
			$template = $objectManager->get(Template::class);
		}
340

341
342
343
344
345
		$template->setExtensionKey($selectedExtension);
		$template->setTemplateName($selectedTemplate);
		$template->setLanguage($selectedLanguage);
		$template->setContent($selectedContent);
		$template->setSubject($selectedSubject);
346
347
348
349
350
		$template->setFromName($selectedFromName);
		$template->setFromMail($selectedFromMail);
		$template->setCc($selectedCc);
		$template->setBcc($selectedBcc);
		$template->setReplyTo($selectedReplyTo);
351
		$template->setSiteRootId($siteRootId);
352

353
354
355
356
357
		if ($templateAlreadyExists) {
			$this->templateRepository->update($template);
		} else {
			$this->templateRepository->add($template);
		}
358

359
360
361
		return $template;
	}

362
363
364
365
366
	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
367
368
369
	 * @param string $selectedTemplateKey
	 * @param string $selectedExtensionKey
	 * @param string $selectedLanguageLeft
370
	 * @param string $selectedLanguageRight
371
372
373
374
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
375
376
	 */
	public function sendTestMailAction(
Torsten Oppermann's avatar
Torsten Oppermann committed
377
		$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft, $selectedLanguageRight
378
	) {
379
380
381
382
383
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(MailTemplateService::class);
		$mailTemplateService->setLanguage($selectedLanguageLeft);
		$mailTemplateService->setToAddresses($emailAddress);
384
		$mailTemplateService->setFromAddress('noreply@example.org');
385
386
		$mailTemplateService->setTemplateName($selectedTemplateKey);
		$mailTemplateService->setExtensionKey($selectedExtensionKey);
387
		$mailIsSend = $mailTemplateService->sendEmail(TRUE);
388

389
390
391
392
393
		if ($selectedLanguageRight !== $selectedLanguageLeft) {
			/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
			$mailTemplateService = $objectManager->get(MailTemplateService::class);
			$mailTemplateService->setLanguage($selectedLanguageRight);
			$mailTemplateService->setToAddresses($emailAddress);
394
			$mailTemplateService->setFromAddress('noreply@example.org');
395
396
			$mailTemplateService->setTemplateName($selectedTemplateKey);
			$mailTemplateService->setExtensionKey($selectedExtensionKey);
397
			$mailIsSend = $mailTemplateService->sendEmail(TRUE);
398
		}
399

400
		if ($mailIsSend) {
401
402
403
404
405
406
			$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);
		}
407

408
409
410
		$arguments = $this->request->getArguments();
		$this->redirect('index', NULL, NULL, $arguments);
	}
411
412

	/**
413
	 * show a notice when no extension is registered
414
415
416
417
	 */
	public function emptyAction() {

	}
418
419

	/**
420
	 * reset template values to default state for a specific language (delete from db)
Torsten Oppermann's avatar
Torsten Oppermann committed
421
	 *
422
	 * @param string $template
Torsten Oppermann's avatar
Torsten Oppermann committed
423
	 * @param string $extensionKey
424
	 * @param string $language
425
426
427
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
	 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
428
	 */
429
	public function resetAction($template, $extensionKey, $language = '') {
Torsten Oppermann's avatar
Torsten Oppermann committed
430
		$this->templateRepository->deleteTemplate($extensionKey, $template, $language);
431

432
433
434
435
436
437
		$message = LocalizationUtility::translate('backend.template_reset', 'sg_mail');
		$this->addFlashMessage($message, '', FlashMessage::OK);

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