[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / Task / ImportExportTask.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Core\Messaging\FlashMessageService;
21 use TYPO3\CMS\Core\Resource\Exception;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Taskcenter\Controller\TaskModuleController;
24 use TYPO3\CMS\Taskcenter\TaskInterface;
25
26 /**
27 * This class provides a textarea to save personal notes
28 */
29 class ImportExportTask implements TaskInterface
30 {
31 /**
32 * Back-reference to the calling reports module
33 *
34 * @var TaskModuleController $taskObject
35 */
36 protected $taskObject;
37
38 /**
39 * URL to task module
40 *
41 * @var string
42 */
43 protected $moduleUrl;
44
45 /**
46 * Constructor
47 *
48 * @param TaskModuleController $taskObject
49 */
50 public function __construct(TaskModuleController $taskObject)
51 {
52 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
53 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
54 $this->moduleUrl = (string)$uriBuilder->buildUriFromRoute('user_task');
55 $this->taskObject = $taskObject;
56 $this->getLanguageService()->includeLLFile('EXT:impexp/Resources/Private/Language/locallang_csh.xlf');
57 }
58
59 /**
60 * This method renders the report
61 *
62 * @return string The status report as HTML
63 */
64 public function getTask()
65 {
66 return $this->main();
67 }
68
69 /**
70 * Render an optional additional information for the 1st view in taskcenter.
71 * Empty for this task
72 *
73 * @return string Overview as HTML
74 */
75 public function getOverview()
76 {
77 return '';
78 }
79
80 /**
81 * Main Task center module
82 *
83 * @return string HTML content.
84 */
85 public function main()
86 {
87 $content = '';
88 $id = (int)GeneralUtility::_GP('display');
89 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
90 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
91 // If a preset is found, it is rendered using an iframe
92 if ($id > 0) {
93 $url = (string)$uriBuilder->buildUriFromRoute(
94 'xMOD_tximpexp',
95 [
96 'tx_impexp[action]' => 'export',
97 'preset[load]' => 1,
98 'preset[select]' => $id,
99 'returnUrl' => $this->moduleUrl
100 ]
101 );
102 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($url);
103 } else {
104 // Header
105 $lang = $this->getLanguageService();
106 $content .= $this->taskObject->description($lang->getLL('.alttitle'), $lang->getLL('.description'));
107 $clause = $this->getBackendUser()->getPagePermsClause(1);
108 $usernames = BackendUtility::getUserNames();
109 // Create preset links:
110 $presets = $this->getPresets();
111 // If any presets found
112 if (is_array($presets) && !empty($presets)) {
113 $lines = [];
114 foreach ($presets as $key => $presetCfg) {
115 $configuration = unserialize($presetCfg['preset_data'], ['allowed_classes' => false]);
116 $title = strlen($presetCfg['title']) ? $presetCfg['title'] : '[' . $presetCfg['uid'] . ']';
117 $icon = 'EXT:impexp/Resources/Public/Images/export.gif';
118 $description = [];
119 // Is public?
120 if ($presetCfg['public']) {
121 $description[] = $lang->getLL('task.public') . ': ' . $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:yes');
122 }
123 // Owner
124 $description[] = $lang->getLL('task.owner') . ': '
125 . (
126 $presetCfg['user_uid'] === $GLOBALS['BE_USER']->user['uid']
127 ? $lang->getLL('task.own')
128 : '[' . htmlspecialchars($usernames[$presetCfg['user_uid']]['username']) . ']'
129 );
130 // Page & path
131 if ($configuration['pagetree']['id']) {
132 $description[] = $lang->getLL('task.page') . ': ' . $configuration['pagetree']['id'];
133 $description[] = $lang->getLL('task.path') . ': ' . htmlspecialchars(
134 BackendUtility::getRecordPath($configuration['pagetree']['id'], $clause, 20)
135 );
136 } else {
137 $description[] = $lang->getLL('single-record');
138 }
139 // Meta information
140 if ($configuration['meta']['title'] || $configuration['meta']['description'] || $configuration['meta']['notes']) {
141 $metaInformation = '';
142 if ($configuration['meta']['title']) {
143 $metaInformation .= '<strong>' . htmlspecialchars($configuration['meta']['title']) . '</strong><br />';
144 }
145 if ($configuration['meta']['description']) {
146 $metaInformation .= htmlspecialchars($configuration['meta']['description']);
147 }
148 if ($configuration['meta']['notes']) {
149 $metaInformation .= '<br /><br />
150 <strong>' . $lang->getLL('notes') . ': </strong>
151 <em>' . htmlspecialchars($configuration['meta']['notes']) . '</em>';
152 }
153 $description[] = '<br />' . $metaInformation;
154 }
155 // Collect all preset information
156 $lines[$key] = [
157 'uid' => 'impexp' . $key,
158 'icon' => $icon,
159 'title' => $title,
160 'descriptionHtml' => implode('<br />', $description),
161 'link' => (string)$uriBuilder->buildUriFromRoute('user_task') . '&SET[function]=impexp.TYPO3\\CMS\\Impexp\\Task\\ImportExportTask&display=' . $presetCfg['uid']
162 ];
163 }
164 // Render preset list
165 $content .= $this->taskObject->renderListMenu($lines);
166 } else {
167 // No presets found
168 $flashMessage = GeneralUtility::makeInstance(
169 FlashMessage::class,
170 $lang->getLL('no-presets'),
171 $lang->getLL('.alttitle'),
172 FlashMessage::NOTICE
173 );
174 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
175 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
176 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
177 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
178 $defaultFlashMessageQueue->enqueue($flashMessage);
179 }
180 }
181 return $content;
182 }
183
184 /**
185 * Select presets for this user
186 *
187 * @return array|bool Array of preset records
188 */
189 protected function getPresets()
190 {
191 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
192 ->getQueryBuilderForTable('tx_impexp_presets');
193
194 return $queryBuilder->select('*')
195 ->from('tx_impexp_presets')
196 ->where(
197 $queryBuilder->expr()->orX(
198 $queryBuilder->expr()->gt(
199 'public',
200 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
201 ),
202 $queryBuilder->expr()->eq(
203 'user_uid',
204 $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT)
205 )
206 )
207 )
208 ->orderBy('item_uid', 'DESC')
209 ->addOrderBy('title')
210 ->execute()
211 ->fetchAll();
212 }
213
214 /**
215 * Returns a \TYPO3\CMS\Core\Resource\Folder object for saving export files
216 * to the server and is also used for uploading import files.
217 *
218 * @throws \InvalidArgumentException
219 * @return \TYPO3\CMS\Core\Resource\Folder|null
220 */
221 protected function getDefaultImportExportFolder()
222 {
223 $defaultImportExportFolder = null;
224
225 $defaultTemporaryFolder = $this->getBackendUser()->getDefaultUploadTemporaryFolder();
226 if ($defaultTemporaryFolder !== null) {
227 $importExportFolderName = 'importexport';
228 $createFolder = !$defaultTemporaryFolder->hasFolder($importExportFolderName);
229 if ($createFolder === true) {
230 try {
231 $defaultImportExportFolder = $defaultTemporaryFolder->createFolder($importExportFolderName);
232 } catch (Exception $folderAccessException) {
233 }
234 } else {
235 $defaultImportExportFolder = $defaultTemporaryFolder->getSubfolder($importExportFolderName);
236 }
237 }
238
239 return $defaultImportExportFolder;
240 }
241
242 /**
243 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
244 */
245 protected function getBackendUser()
246 {
247 return $GLOBALS['BE_USER'];
248 }
249
250 /**
251 * @return mixed
252 */
253 protected function getLanguageService()
254 {
255 return $GLOBALS['LANG'];
256 }
257 }