MailController.php 10.2 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 TYPO3\CMS\Core\Messaging\FlashMessage;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
34
use TYPO3\CMS\Extbase\Object\ObjectManager;
35
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
36
37
38
39
40
41

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

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

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

54
	/**
55
	 * Show template Selection  and enable content input + mail preview
Fabian Galinski's avatar
Fabian Galinski committed
56
	 *
57
58
59
60
61
62
	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 * @param string $selectedExtensionKey
	 * @param string $selectedTemplateKey
63
	 */
64
65
66
67
	public function indexAction(
		$selectedTemplate = NULL, $selectedExtension = NULL, $selectedLanguageLeft = NULL,
		$selectedLanguageRight = NULL, $selectedExtensionKey = NULL, $selectedTemplateKey = NULL
	) {
68

69
70
71
72
73
		$registerArray = MailTemplateService::getRegisterArray();
		reset(MailTemplateService::getRegisterArray());
		$firstEntry = key(MailTemplateService::getRegisterArray());
		$secondLevelFirstEntry = key(MailTemplateService::getRegisterArray()[$firstEntry]);

74
75
76
77
		$activatedLanguages = $this->languageRepository->findSelected();
		$languages = ['en'];
		foreach ($activatedLanguages as $language) {
			$languages[] = $language->getLocale();
78
79
		}

80
		$this->view->assign('languages', $languages);
81
		$this->view->assign('templates', MailTemplateService::getRegisterArray());
82

83
84
85
86
87
		if (isset($selectedTemplate)
			&& !isset($selectedLanguageLeft)
			&& !isset($selectedLanguageRight)
		) {
			$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
88

89
90
91
			$this->view->assign('selectedTemplate', $selectedTemplateArray);
			$this->view->assign('selectedTemplateKey', $selectedTemplate);
			$this->view->assign('selectedExtensionKey', $selectedExtension);
92

93
		} elseif (!isset($selectedLanguageLeft) && !isset($selectedLanguageRight)) {
94

95
96
97
98
99
100
			if (isset(MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry])) {
				$this->view->assign(
					'selectedTemplate', MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry]
				);
			}

101
102
			$this->view->assign('selectedLanguageLeft', $this->settings['templateDefaultLanguage']);
			$this->view->assign('selectedLanguageRight', $this->settings['templateDefaultLanguage']);
103
			$this->view->assign('selectedTemplateKey', $secondLevelFirstEntry);
104
			$this->view->assign('selectedExtensionKey', $firstEntry);
105
		}
Torsten Oppermann's avatar
Torsten Oppermann committed
106

Torsten Oppermann's avatar
Torsten Oppermann committed
107
108
109
110
		if (isset($selectedTemplate) && isset($selectedExtension)) {

			!isset($selectedLanguageLeft) ? $selectedLanguageLeft = 'en' : NULL;
			!isset($selectedLanguageRight) ? $selectedLanguageRight = 'en' : NULL;
111

112
113
114
			$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
			$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
			$this->view->assign(
Torsten Oppermann's avatar
Torsten Oppermann committed
115
				'selectedTemplate', MailTemplateService::getRegisterArray()[$selectedExtension][$selectedTemplate]
116
			);
Torsten Oppermann's avatar
Torsten Oppermann committed
117
118
			$this->view->assign('selectedTemplateKey', $selectedTemplate);
			$this->view->assign('selectedExtensionKey', $selectedExtension);
119

120
121
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateLeft = $this->templateRepository->findTemplate(
Torsten Oppermann's avatar
Torsten Oppermann committed
122
				$selectedExtension, $selectedTemplate, $selectedLanguageLeft
123
124
			)->getFirst();

125
			$this->view->assign(
126
				'contentLeft',
127
128
129
130
131
132
				$templateLeft !== NULL ? $templateLeft->getContent() : ''
			);

			$this->view->assign(
				'subjectLeft',
				$templateLeft !== NULL ? $templateLeft->getSubject() : ''
133
			);
134
135
136
137
138
139

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

140
			$this->view->assign(
141
				'contentRight',
142
143
144
145
146
147
				$templateRight !== NULL ? $templateRight->getContent() : ''
			);

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

150
		} else {
151
152
153
154
155
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateLeft = $this->templateRepository->findTemplate(
				$firstEntry, $secondLevelFirstEntry, 'en'
			)->getFirst();

156
			$this->view->assign(
157
158
				'contentLeft',
				$templateLeft !== NULL ? $templateLeft->getContent() : ''
159
			);
160

161
			$this->view->assign(
162
163
				'subjectLeft',
				$templateLeft !== NULL ? $templateLeft->getSubject() : ''
164
			);
Torsten Oppermann's avatar
Torsten Oppermann committed
165

166
167
168
169
			/** @var /SGalinski/SgMail/Domain/Model/Template $result */
			$templateRight = $this->templateRepository->findTemplate(
				$firstEntry, $secondLevelFirstEntry, 'en'
			)->getFirst();
Fabian Galinski's avatar
Fabian Galinski committed
170

171
172
173
174
			$this->view->assign(
				'contentRight',
				$templateRight !== NULL ? $templateRight->getContent() : ''
			);
175

176
177
178
179
180
			$this->view->assign(
				'subjectRight',
				$templateRight !== NULL ? $templateRight->getSubject() : ''
			);
		}
181
182
	}

183
184
	/**
	 * Save content (left & right)
185
	 * if left & right is the same language, the right content will apply only if its another language
186
187
188
189
190
191
192
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
193
194
	 * @param string $subjectLeft
	 * @param string $subjectRight
195
196
197
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
198
		$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL
199
	) {
200
		$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft);
201

202
203
204
205
206
		if ($selectedLanguageLeft !== $selectedLanguageRight) {
			$this->saveTemplate(
				$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight
			);
		}
207

208
		$message = LocalizationUtility::translate('backend.success', 'sg_mail');
209
210
		$this->addFlashMessage($message, '', FlashMessage::OK);

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

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	/**
	 * 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;
	}

255
256
257
258
259
	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
260
261
262
	 * @param string $selectedTemplateKey
	 * @param string $selectedExtensionKey
	 * @param string $selectedLanguageLeft
263
264
	 */
	public function sendTestMailAction(
265
		$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft
266
	) {
Torsten Oppermann's avatar
Torsten Oppermann committed
267
268
269
270
271
272
273
274
275

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

276
277
278
279
280
281
282
283
284
		$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->setSubject($this->settings['mail']['test']['subject']);
		$mailTemplateService->setTemplateName($selectedTemplateKey);
		$mailTemplateService->setExtensionKey($selectedExtensionKey);
285

286
287
288
289
290
291
292
		if ($mailTemplateService->sendEmail()) {
			$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);
		}
293
294
295
296
297

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

	}
298
}