BackendService.php 9.99 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\Service;

/***************************************************************
 *  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\Domain\Repository\TemplateRepository;
31
32
33
34
35
36
37
38
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Request;
39
use TYPO3\CMS\Extbase\Object\ObjectManager;
40
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
41
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
42
use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository;
43
44
45
46
47

/**
 * Backend Service class
 */
class BackendService {
Torsten Oppermann's avatar
Torsten Oppermann committed
48
49
50
51
52
53
54
	// options for the queue search filter
	const SENDER_FILTER_OPTION = 0;
	const RECIPIENT_FILTER_OPTION = 1;
	const SUBJECT_FILTER_OPTION = 2;
	const MAILTEXT_FILTER_OPTION = 3;
	const CC_FILTER_OPTION = 4;
	const BCC_FILTER_OPTION = 5;
55
56
	const FROM_NAME_FILTER_OPTION = 6;
	const REPLY_TO_NAME_FILTER_OPTION = 7;
57

58
59
60
61
62
63
64
	/**
	 * Get all pages the be user has access to
	 *
	 * @return array
	 * @throws \InvalidArgumentException
	 */
	public static function getPages() {
65
		$out = [];
66
67
68
		/** @var $databaseConnection DatabaseConnection */
		$databaseConnection = $GLOBALS['TYPO3_DB'];
		$rows = $databaseConnection->exec_SELECTgetRows(
69
			'*',
70
			'pages',
71
			'deleted = 0 AND is_siteroot = 1'
72
73
		);

74
75
76
77
78
79
80
		foreach ($rows as $row) {
			$pageInfo = BackendUtility::readPageAccess($row['uid'], $GLOBALS['BE_USER']->getPagePermsClause(1));
			if ($pageInfo) {
				$out[] = $pageInfo;
			}
		}
		return $out;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	}

	/**
	 * create buttons for the backend module header
	 *
	 * @param DocHeaderComponent $docHeaderComponent
	 * @param Request $request
	 * @throws \InvalidArgumentException
	 * @throws \UnexpectedValueException
	 */
	public static function makeButtons($docHeaderComponent, $request) {
		/** @var ButtonBar $buttonBar */
		$buttonBar = $docHeaderComponent->getButtonBar();

		/** @var IconFactory $iconFactory */
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);

		// Refresh
		$refreshButton = $buttonBar->makeLinkButton()
			->setHref(GeneralUtility::getIndpEnv('REQUEST_URI'))
			->setTitle(LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.reload', ''))
			->setIcon($iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));
		$buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT);

		// shortcut button
		$shortcutButton = $buttonBar->makeShortcutButton()
			->setModuleName($request->getPluginName())
			->setGetVariables(
				[
					'id',
					'M'
				]
			)
			->setSetVariables([]);

		$buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
		$docHeaderComponent->getButtonBar();
	}
Torsten Oppermann's avatar
Torsten Oppermann committed
119
120
121
122

	/**
	 * Retrieves the next site root in the page hierarchy from the current page
	 *
Torsten Oppermann's avatar
Torsten Oppermann committed
123
	 * @param int $currentPid
124
	 * @return int
Torsten Oppermann's avatar
Torsten Oppermann committed
125
	 */
Torsten Oppermann's avatar
Torsten Oppermann committed
126
127
128
	public static function getSiteRoot($currentPid) {
		$rootLine = BackendUtility::BEgetRootLine((int) $currentPid);
		$siteRoot = ['uid' => 0];
129

Torsten Oppermann's avatar
Torsten Oppermann committed
130
131
132
133
134
135
136
		foreach ($rootLine as $page) {
			if ($page['is_siteroot'] === '1') {
				$siteRoot = $page;
				break;
			}
		}

137
		return $siteRoot['uid'];
Torsten Oppermann's avatar
Torsten Oppermann committed
138
	}
139
140
141
142
143
144
145
146

	/**
	 * get an array of all the locales for the activated languages
	 *
	 * @return array
	 * @throws \InvalidArgumentException
	 */
	public static function getLanguages() {
147
148
149
150
151
152
153
154
		/** @var $databaseConnection DatabaseConnection */
		$databaseConnection = $GLOBALS['TYPO3_DB'];
		/** @var QueryResultInterface $rows */
		$rows = $databaseConnection->exec_SELECTgetRows(
			'*',
			'sys_language',
			'hidden = 0'
		);
155

156
157
		$languages = [];

158
159
160
161
162
		// adding default language
		$languages[] = ['isocode' => LocalizationUtility::translate(
			'backend.language_default', 'SgMail'
		), 'name' => LocalizationUtility::translate('backend.language_default', 'SgMail')];

163
164
165
		foreach ($rows as $language) {
			$languages[] = ['isocode' => $language['language_isocode'],
				'name' => $language['title']];
166
167
168
169
		}

		return $languages;
	}
170

171
172
173
	/**
	 * get an array of all the labels for the activated languages
	 *
174
	 * @param array $languages
175
176
177
	 * @return array
	 * @throws \InvalidArgumentException
	 */
178
179
	public static function getLanguageLabels(array $languages) {
		$languageLabels = [];
180

181
182
		foreach ($languages as $language) {
			$languageLabels[$language['isocode']] = $language['name'];
183
184
		}

185
		return $languageLabels;
186
187
	}

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	/**
	 * Get the previous selected languages, the actual selection or the default
	 * format ['left' => languagecode, 'right' => ...]
	 *
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 * @param \SGalinski\SgMail\Session\PhpSession $session
	 * @param array $languages
	 * @return array
	 */
	public static function getSelectedLanguages(
		$selectedLanguageLeft, $selectedLanguageRight, $session, array $languages
	) {
		$selectedLanguages = [
			'left' => $selectedLanguageLeft,
			'right' => $selectedLanguageRight
		];

		if ($selectedLanguageLeft === NULL) {
			$selectedLanguageLeftFromSession = $session->getDataByKey('selectedLanguageLeft');
			if ($selectedLanguageLeftFromSession !== NULL) {
				$selectedLanguages['left'] = $selectedLanguageLeftFromSession;
			} else {
				$selectedLanguages['left'] = $languages[0];
			}
		}

		if ($selectedLanguageRight === NULL) {
			$selectedLanguageRightFromSession = $session->getDataByKey('selectedLanguageRight');
			if ($selectedLanguageRightFromSession === NULL) {
				if (isset($languages[1])) {
					$selectedLanguages['right'] = $languages[1];
				} else {
					$selectedLanguages['right'] = $languages[0];
				}
			} else {
				$selectedLanguages['right'] = $selectedLanguageRightFromSession;
			}
		}
		return $selectedLanguages;
	}

	/**
	 * Get the selected templates for the selected language
	 *
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
235
	 * @param array $languages
236
	 * @param int $pid
237
238
239
240
	 * @return array
	 * @throws \InvalidArgumentException
	 */
	public static function getSelectedTemplates(
241
		$selectedExtension, $selectedTemplate, array $languages, $pid
242
243
244
245
	) {
		$selectedTemplates = [];

		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
Torsten Oppermann's avatar
Torsten Oppermann committed
246
		/** @var TemplateRepository $templateRepository */
247
248
		$templateRepository = $objectManager->get(TemplateRepository::class);

249
		foreach ($languages as $language) {
250
251
			$selectedTemplates[$language['isocode']] = $templateRepository->findOneByTemplate(
				$selectedExtension, $selectedTemplate, $language['isocode'], $pid
252
253
			);
		}
Torsten Oppermann's avatar
Torsten Oppermann committed
254

255
256
		return $selectedTemplates;
	}
257

258
259
260
261
262
263
264
	/**
	 * Get the languages in an array suitable for filtering
	 *
	 * @return array
	 * @throws \InvalidArgumentException
	 */
	public static function getLanguagesForFilter() {
265
		// @TODO
266
		$languages = self::getLanguages();
267

268
269
270
271
272
273
274
275
276
277
		array_unshift($languages, '');
		$filterLanguages = [];
		foreach ($languages as $key => $value) {
			$filterLanguages[$value] = $value;
		}

		return $filterLanguages;
	}

	/**
278
	 * Get the template keys in an array suitable for filtering
279
280
281
282
283
284
285
286
	 *
	 * @return array
	 */
	public static function getTemplatesForFilter() {
		$registerArray = MailTemplateService::getRegisterArray();
		$templates = [];
		foreach ($registerArray as $extensions) {
			foreach ($extensions as $template => $key) {
287
				$templates[$key['extension']][] = $key['templateName'];
288
289
290
291
292
293
			}
		}
		array_unshift($templates, '');

		return $templates;
	}
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

	/**
	 * Save or update the template in the DB, depending if it already exists or not
	 *
	 * @param int $pid
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $language
	 * @param array $templateData
	 * @return Template $template
	 * @throws \InvalidArgumentException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
	 */
	public static function saveTemplate($pid, $selectedExtension, $selectedTemplate, $language, $templateData) {
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		/** @var TemplateRepository $templateRepository */
		$templateRepository = $objectManager->get(TemplateRepository::class);

		/** @var Template $template */
		$template = $templateRepository->findOneByTemplate(
			$selectedExtension, $selectedTemplate, $language, $pid
		);

		$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($language);
		$template->setContent($templateData['content']);
		$template->setSubject($templateData['subject']);
		$template->setFromName($templateData['fromName']);
		$template->setFromMail($templateData['fromMail']);
		$template->setCc($templateData['cc']);
		$template->setBcc($templateData['bcc']);
		$template->setReplyTo($templateData['replyTo']);

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

		return $template;
	}
344
}