MailController.php 13.8 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
<?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!
 ***************************************************************/

29
use SGalinski\SgMail\Domain\Model\Template;
30
use SGalinski\SgMail\Service\MailTemplateService;
31
use SGalinski\SgMail\Session\PhpSession;
32
use TYPO3\CMS\Core\Messaging\FlashMessage;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
35
use TYPO3\CMS\Extbase\Object\ObjectManager;
36
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
37
38
39
40
41
42

/**
 * Controller for the mail templating service module
 */
class MailController extends ActionController {

43
44
45
46
47
48
	/**
	 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
	 * @inject
	 */
	protected $languageRepository = NULL;

49
50
51
52
53
54
	/**
	 * @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
	 * @inject
	 */
	protected $templateRepository = NULL;

55
56
57
58
59
	/**
	 * @var \SGalinski\Lfeditor\Session\PhpSession
	 */
	protected $session;

60
	/**
61
	 * Show template Selection  and enable content input + mail preview
Fabian Galinski's avatar
Fabian Galinski committed
62
	 *
63
64
65
66
67
68
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 * @param string $selectedExtensionKey
	 * @param string $selectedTemplateKey
69
	 */
70
71
72
73
	public function indexAction(
		$selectedTemplate = NULL, $selectedExtension = NULL, $selectedLanguageLeft = NULL,
		$selectedLanguageRight = NULL, $selectedExtensionKey = NULL, $selectedTemplateKey = NULL
	) {
74

75
76
77
78
79
		if (!($this->session instanceof PhpSession)) {
			$this->session = $this->objectManager->get('SGalinski\SgMail\Session\PhpSession');
			$this->session->setSessionKey('sg_mail_controller_session');
		}

80
81
82
83
84
		$registerArray = MailTemplateService::getRegisterArray();
		reset(MailTemplateService::getRegisterArray());
		$firstEntry = key(MailTemplateService::getRegisterArray());
		$secondLevelFirstEntry = key(MailTemplateService::getRegisterArray()[$firstEntry]);

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

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

94
95
96
97
98
		if (isset($selectedTemplate)
			&& !isset($selectedLanguageLeft)
			&& !isset($selectedLanguageRight)
		) {
			$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
99

100
101
102
103
104
105
106
107
108
			// get last selected languages
			!isset($selectedLanguageLeft) ? $selectedLanguageLeft = $this->session->getDataByKey(
				'selectedLanguageLeft'
			) : NULL;

			!isset($selectedLanguageRight) ? $selectedLanguageRight = $this->session->getDataByKey(
				'selectedLanguageRight'
			) : NULL;

109
110
111
			$this->view->assign('selectedTemplate', $selectedTemplateArray);
			$this->view->assign('selectedTemplateKey', $selectedTemplate);
			$this->view->assign('selectedExtensionKey', $selectedExtension);
112
113
			$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
			$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
114

115
		} elseif (!isset($selectedLanguageLeft) && !isset($selectedLanguageRight)) {
116

117
118
119
120
121
122
			if (isset(MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry])) {
				$this->view->assign(
					'selectedTemplate', MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry]
				);
			}

123
124
125
126
127
128
129
130
131
132
133
			// get last selected languages
			!isset($selectedLanguageLeft) ? $selectedLanguageLeft = $this->session->getDataByKey(
				'selectedLanguageLeft'
			) : NULL;

			!isset($selectedLanguageRight) ? $selectedLanguageRight = $this->session->getDataByKey(
				'selectedLanguageRight'
			) : NULL;

			$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
			$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
134
			$this->view->assign('selectedTemplateKey', $secondLevelFirstEntry);
135
			$this->view->assign('selectedExtensionKey', $firstEntry);
136
		}
Torsten Oppermann's avatar
Torsten Oppermann committed
137

Torsten Oppermann's avatar
Torsten Oppermann committed
138
139
		if (isset($selectedTemplate) && isset($selectedExtension)) {

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
			isset($selectedLanguageLeft) ? $this->session->setDataByKey(
				'selectedLanguageLeft', $selectedLanguageLeft
			) : NULL;
			isset($selectedLanguageRight) ? $this->session->setDataByKey(
				'selectedLanguageRight', $selectedLanguageRight
			) : NULL;

			// get last selected languages
			!isset($selectedLanguageLeft) ? $selectedLanguageLeft = $this->session->getDataByKey(
				'selectedLanguageLeft'
			) : NULL;

			!isset($selectedLanguageRight) ? $selectedLanguageRight = $this->session->getDataByKey(
				'$selectedLanguageRight'
			) : NULL;
155

156
157
158
			$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
			$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
			$this->view->assign(
Torsten Oppermann's avatar
Torsten Oppermann committed
159
				'selectedTemplate', MailTemplateService::getRegisterArray()[$selectedExtension][$selectedTemplate]
160
			);
Torsten Oppermann's avatar
Torsten Oppermann committed
161
162
			$this->view->assign('selectedTemplateKey', $selectedTemplate);
			$this->view->assign('selectedExtensionKey', $selectedExtension);
163

164
165
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateLeft = $this->templateRepository->findTemplate(
Torsten Oppermann's avatar
Torsten Oppermann committed
166
				$selectedExtension, $selectedTemplate, $selectedLanguageLeft
167
168
			)->getFirst();

169
170
			if ($templateLeft !== NULL) {
				$this->view->assign('contentLeft', $templateLeft->getContent());
Torsten Oppermann's avatar
Torsten Oppermann committed
171
				$this->view->assign('subjectLeft', $templateLeft->getSubject());
172
173
174
175
176
177
178
179
180
			} else {
				$defaultTemplatePath = MailTemplateService::getRegisterArray()
				[$selectedExtension][$selectedTemplate]['templatePath'];
				$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.' . 'template.html';

				if (file_exists($defaultTemplateFile)) {
					$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
				}
			}
181
182
183
184

			$this->view->assign(
				'subjectLeft',
				$templateLeft !== NULL ? $templateLeft->getSubject() : ''
185
			);
186
187
188
189
190
191

			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateRight = $this->templateRepository->findTemplate(
				$selectedExtensionKey, $selectedTemplateKey, $selectedLanguageRight
			)->getFirst();

192
193
			if ($templateRight !== NULL) {
				$this->view->assign('contentRight', $templateRight->getContent());
Torsten Oppermann's avatar
Torsten Oppermann committed
194
				$this->view->assign('subjectRight', $templateRight->getSubject());
195
196
197
198
199
200
201
202
203
			} else {
				$defaultTemplatePath = MailTemplateService::getRegisterArray()
				[$selectedExtension][$selectedTemplate]['templatePath'];
				$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.' . 'template.html';

				if (file_exists($defaultTemplateFile)) {
					$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
				}
			}
204
205
206
207

			$this->view->assign(
				'subjectRight',
				$templateRight !== NULL ? $templateRight->getSubject() : ''
208
209
			);

210
		} else {
211
212
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateLeft = $this->templateRepository->findTemplate(
213
				$firstEntry, $secondLevelFirstEntry, $selectedLanguageLeft
214
215
			)->getFirst();

216
217
218
219
220
			if ($templateLeft !== NULL) {
				$this->view->assign('contentLeft', $templateLeft->getContent());
			} else {
				$defaultTemplatePath = MailTemplateService::getRegisterArray()
				[$firstEntry][$secondLevelFirstEntry]['templatePath'];
221
				$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.template.html';
222
223
224
225
226

				if (file_exists($defaultTemplateFile)) {
					$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
				}
			}
227

228
			$this->view->assign(
229
230
				'subjectLeft',
				$templateLeft !== NULL ? $templateLeft->getSubject() : ''
231
			);
Torsten Oppermann's avatar
Torsten Oppermann committed
232

233
234
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateRight = $this->templateRepository->findTemplate(
235
				$firstEntry, $secondLevelFirstEntry, $selectedLanguageRight
236
			)->getFirst();
Fabian Galinski's avatar
Fabian Galinski committed
237

238
239
240
241
242
			if ($templateRight !== NULL) {
				$this->view->assign('contentRight', $templateRight->getContent());
			} else {
				$defaultTemplatePath = MailTemplateService::getRegisterArray()
				[$firstEntry][$secondLevelFirstEntry]['templatePath'];
243
				$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.template.html';
244
245
246
247
248

				if (file_exists($defaultTemplateFile)) {
					$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
				}
			}
249

250
251
252
253
254
			$this->view->assign(
				'subjectRight',
				$templateRight !== NULL ? $templateRight->getSubject() : ''
			);
		}
255
256
	}

257
258
	/**
	 * Save content (left & right)
259
	 * if left & right is the same language, the right content will apply only if its another language
260
261
262
263
264
265
266
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
267
268
	 * @param string $subjectLeft
	 * @param string $subjectRight
269
270
271
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
272
		$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL
273
	) {
274
		$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft);
275

276
277
278
279
280
		if ($selectedLanguageLeft !== $selectedLanguageRight) {
			$this->saveTemplate(
				$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight
			);
		}
281

282
		$message = LocalizationUtility::translate('backend.success', 'sg_mail');
283
284
		$this->addFlashMessage($message, '', FlashMessage::OK);

285
		$arguments = $this->request->getArguments();
286
		$this->redirect('index', NULL, NULL, $arguments);
287
288
	}

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	/**
	 * 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
	 * @return Template $template
	 */
	private function saveTemplate(
		$selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject
	) {
		/** @var Template $template */
		$template = $this->templateRepository->findTemplate(
			$selectedExtension, $selectedTemplate, $selectedLanguage
		)->getFirst();

		$templateAlreadyExists = TRUE;
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		if ($template === NULL) {
			$templateAlreadyExists = FALSE;
			$template = $objectManager->get(Template::class);
		}

		$template->setExtensionKey($selectedExtension);
		$template->setTemplateName($selectedTemplate);
		$template->setLanguage($selectedLanguage);
		$template->setContent($selectedContent);
		$template->setSubject($selectedSubject);

		if ($templateAlreadyExists) {
			$this->templateRepository->update($template);
		} else {
			$this->templateRepository->add($template);
		}

		return $template;
	}

329
330
331
332
333
	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
334
335
336
	 * @param string $selectedTemplateKey
	 * @param string $selectedExtensionKey
	 * @param string $selectedLanguageLeft
337
	 * @param string $selectedLanguageRight
338
339
	 */
	public function sendTestMailAction(
Torsten Oppermann's avatar
Torsten Oppermann committed
340
		$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft, $selectedLanguageRight
341
	) {
Torsten Oppermann's avatar
Torsten Oppermann committed
342
343
344
345
346
347
348
349
350

		if (!filter_var($this->settings['mail']['test']['from'], FILTER_VALIDATE_EMAIL)) {
			$message = LocalizationUtility::translate('backend.invalid_test_email', 'sg_mail');
			$this->addFlashMessage($message, '', FlashMessage::ERROR);

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

351
352
353
354
355
356
357
358
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(MailTemplateService::class);
		$mailTemplateService->setLanguage($selectedLanguageLeft);
		$mailTemplateService->setToAddresses($emailAddress);
		$mailTemplateService->setFromAddress($this->settings['mail']['test']['from']);
		$mailTemplateService->setTemplateName($selectedTemplateKey);
		$mailTemplateService->setExtensionKey($selectedExtensionKey);
Torsten Oppermann's avatar
Torsten Oppermann committed
359
		$mailIsSend = $mailTemplateService->sendEmail($this->settings['mail']['test']['subject'], TRUE);
360
361
362
363
364
365
366
367
368

		if ($selectedLanguageRight !== $selectedLanguageLeft) {
			/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
			$mailTemplateService = $objectManager->get(MailTemplateService::class);
			$mailTemplateService->setLanguage($selectedLanguageRight);
			$mailTemplateService->setToAddresses($emailAddress);
			$mailTemplateService->setFromAddress($this->settings['mail']['test']['from']);
			$mailTemplateService->setTemplateName($selectedTemplateKey);
			$mailTemplateService->setExtensionKey($selectedExtensionKey);
Torsten Oppermann's avatar
Torsten Oppermann committed
369
			$mailIsSend = $mailTemplateService->sendEmail($this->settings['mail']['test']['subject'], TRUE);
370
		}
371

372
		if ($mailIsSend) {
373
374
375
376
377
378
			$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);
		}
379
380
381
382
383

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

	}
384
}