MailController.php 9.89 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

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

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

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

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

78
		$registerArray = MailTemplateService::getRegisterArray();
79
80
81
82
		$activatedLanguages = $this->languageRepository->findSelected();
		$languages = ['en'];
		foreach ($activatedLanguages as $language) {
			$languages[] = $language->getLocale();
83
84
		}

85
		$this->view->assign('languages', $languages);
86
		$this->view->assign('templates', MailTemplateService::getRegisterArray());
87

88
89
90
		// get last selected languages
		if ($selectedLanguageLeft === NULL) {
			$selectedLanguageLeft = $this->session->getDataByKey('selectedLanguageLeft');
91
			if ($selectedLanguageLeft === NULL) {
92
				$selectedLanguageLeft = $languages[0];
93
			}
94
		}
Torsten Oppermann's avatar
Torsten Oppermann committed
95

96
97
		if ($selectedLanguageRight === NULL) {
			$selectedLanguageRight = $this->session->getDataByKey('selectedLanguageRight');
98
			if ($selectedLanguageRight === NULL) {
99
100
101
102
				if (isset($languages[1])) {
					$selectedLanguageRight = $languages[1];
				} else {
					$selectedLanguageRight = $languages[0];
103
104
				}
			}
105
		}
106

107
108
109
		if ($selectedTemplate === NULL) {
			$selectedExtension = key($registerArray);
			$selectedTemplate = key($registerArray[$selectedExtension]);
110
111
		}
		$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
112

113
114
		$this->session->setDataByKey('selectedLanguageLeft', $selectedLanguageLeft);
		$this->session->setDataByKey('selectedLanguageRight', $selectedLanguageRight);
115

116
		$templateLeft = $this->templateRepository->findOneByTemplate(
117
			$selectedExtension, $selectedTemplate, $selectedLanguageLeft
118
		);
119

120
		$templateRight = $this->templateRepository->findOneByTemplate(
121
			$selectedExtensionKey, $selectedTemplateKey, $selectedLanguageRight
122
		);
123

124
125
		if ($templateLeft !== NULL) {
			$this->view->assign('contentLeft', $templateLeft->getContent());
126
		} else {
127
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
128
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.' . 'template.html';
129

130
131
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
132
			}
133
		}
134

135
136
137
		if ($templateRight !== NULL) {
			$this->view->assign('contentRight', $templateRight->getContent());
		} else {
138
			$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
139
			$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.' . 'template.html';
Torsten Oppermann's avatar
Torsten Oppermann committed
140

141
142
143
144
			if (file_exists($defaultTemplateFile)) {
				$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
			}
		}
Fabian Galinski's avatar
Fabian Galinski committed
145

146
147
148
		$this->view->assign(
			'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() : ''
		);
149

150
151
152
		$this->view->assign(
			'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() : ''
		);
153

154
155
156
157
158
		$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);
159
160
	}

161
162
	/**
	 * Save content (left & right)
163
	 * if left & right is the same language, the right content will apply only if its another language
164
165
166
167
168
169
170
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
171
172
	 * @param string $subjectLeft
	 * @param string $subjectRight
173
174
175
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
176
		$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL
177
	) {
178
		$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft);
179

180
181
182
183
184
		if ($selectedLanguageLeft !== $selectedLanguageRight) {
			$this->saveTemplate(
				$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight
			);
		}
185

186
		$message = LocalizationUtility::translate('backend.success', 'sg_mail');
187
188
		$this->addFlashMessage($message, '', FlashMessage::OK);

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

193
194
195
196
197
198
199
200
201
202
203
204
205
206
	/**
	 * 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 */
207
		$template = $this->templateRepository->findOneByTemplate(
208
			$selectedExtension, $selectedTemplate, $selectedLanguage
209
		);
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

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

233
234
235
236
237
	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
238
239
240
	 * @param string $selectedTemplateKey
	 * @param string $selectedExtensionKey
	 * @param string $selectedLanguageLeft
241
	 * @param string $selectedLanguageRight
242
243
	 */
	public function sendTestMailAction(
Torsten Oppermann's avatar
Torsten Oppermann committed
244
		$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft, $selectedLanguageRight
245
	) {
246
247
248
249
250
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
		$mailTemplateService = $objectManager->get(MailTemplateService::class);
		$mailTemplateService->setLanguage($selectedLanguageLeft);
		$mailTemplateService->setToAddresses($emailAddress);
251
		$mailTemplateService->setFromAddress('noreply@example.org');
252
253
		$mailTemplateService->setTemplateName($selectedTemplateKey);
		$mailTemplateService->setExtensionKey($selectedExtensionKey);
Torsten Oppermann's avatar
Torsten Oppermann committed
254
		$mailIsSend = $mailTemplateService->sendEmail($this->settings['mail']['test']['subject'], TRUE);
255
256
257
258
259
260

		if ($selectedLanguageRight !== $selectedLanguageLeft) {
			/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
			$mailTemplateService = $objectManager->get(MailTemplateService::class);
			$mailTemplateService->setLanguage($selectedLanguageRight);
			$mailTemplateService->setToAddresses($emailAddress);
261
			$mailTemplateService->setFromAddress('noreply@example.org');
262
263
			$mailTemplateService->setTemplateName($selectedTemplateKey);
			$mailTemplateService->setExtensionKey($selectedExtensionKey);
Torsten Oppermann's avatar
Torsten Oppermann committed
264
			$mailIsSend = $mailTemplateService->sendEmail($this->settings['mail']['test']['subject'], TRUE);
265
		}
266

267
		if ($mailIsSend) {
268
269
270
271
272
273
			$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);
		}
274
275
276
277

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