[CLEANUP] Alwas put null at the last position
[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 $this->moduleUrl = BackendUtility::getModuleUrl('user_task');
53 $this->taskObject = $taskObject;
54 $this->getLanguageService()->includeLLFile('EXT:impexp/Resources/Private/Language/locallang_csh.xlf');
55 }
56
57 /**
58 * This method renders the report
59 *
60 * @return string The status report as HTML
61 */
62 public function getTask()
63 {
64 return $this->main();
65 }
66
67 /**
68 * Render an optional additional information for the 1st view in taskcenter.
69 * Empty for this task
70 *
71 * @return string Overview as HTML
72 */
73 public function getOverview()
74 {
75 return '';
76 }
77
78 /**
79 * Main Task center module
80 *
81 * @return string HTML content.
82 */
83 public function main()
84 {
85 $content = '';
86 $id = (int)GeneralUtility::_GP('display');
87 // If a preset is found, it is rendered using an iframe
88 if ($id > 0) {
89 $url = BackendUtility::getModuleUrl(
90 'xMOD_tximpexp',
91 [
92 'tx_impexp[action]' => 'export',
93 'preset[load]' => 1,
94 'preset[select]' => $id,
95 'returnUrl' => $this->moduleUrl
96 ]
97 );
98 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($url);
99 } else {
100 // Header
101 $lang = $this->getLanguageService();
102 $content .= $this->taskObject->description($lang->getLL('.alttitle'), $lang->getLL('.description'));
103 $clause = $this->getBackendUser()->getPagePermsClause(1);
104 $usernames = BackendUtility::getUserNames();
105 // Create preset links:
106 $presets = $this->getPresets();
107 // If any presets found
108 if (is_array($presets) && !empty($presets)) {
109 $lines = [];
110 foreach ($presets as $key => $presetCfg) {
111 $configuration = unserialize($presetCfg['preset_data'], ['allowed_classes' => false]);
112 $title = strlen($presetCfg['title']) ? $presetCfg['title'] : '[' . $presetCfg['uid'] . ']';
113 $icon = 'EXT:impexp/Resources/Public/Images/export.gif';
114 $description = [];
115 // Is public?
116 if ($presetCfg['public']) {
117 $description[] = $lang->getLL('task.public') . ': ' . $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:yes');
118 }
119 // Owner
120 $description[] = $lang->getLL('task.owner') . ': '
121 . (
122 $presetCfg['user_uid'] === $GLOBALS['BE_USER']->user['uid']
123 ? $lang->getLL('task.own')
124 : '[' . htmlspecialchars($usernames[$presetCfg['user_uid']]['username']) . ']'
125 );
126 // Page & path
127 if ($configuration['pagetree']['id']) {
128 $description[] = $lang->getLL('task.page') . ': ' . $configuration['pagetree']['id'];
129 $description[] = $lang->getLL('task.path') . ': ' . htmlspecialchars(
130 BackendUtility::getRecordPath($configuration['pagetree']['id'], $clause, 20)
131 );
132 } else {
133 $description[] = $lang->getLL('single-record');
134 }
135 // Meta information
136 if ($configuration['meta']['title'] || $configuration['meta']['description'] || $configuration['meta']['notes']) {
137 $metaInformation = '';
138 if ($configuration['meta']['title']) {
139 $metaInformation .= '<strong>' . htmlspecialchars($configuration['meta']['title']) . '</strong><br />';
140 }
141 if ($configuration['meta']['description']) {
142 $metaInformation .= htmlspecialchars($configuration['meta']['description']);
143 }
144 if ($configuration['meta']['notes']) {
145 $metaInformation .= '<br /><br />
146 <strong>' . $lang->getLL('notes') . ': </strong>
147 <em>' . htmlspecialchars($configuration['meta']['notes']) . '</em>';
148 }
149 $description[] = '<br />' . $metaInformation;
150 }
151 // Collect all preset information
152 $lines[$key] = [
153 'uid' => 'impexp' . $key,
154 'icon' => $icon,
155 'title' => $title,
156 'descriptionHtml' => implode('<br />', $description),
157 'link' => BackendUtility::getModuleUrl('user_task') . '&SET[function]=impexp.TYPO3\\CMS\\Impexp\\Task\\ImportExportTask&display=' . $presetCfg['uid']
158 ];
159 }
160 // Render preset list
161 $content .= $this->taskObject->renderListMenu($lines);
162 } else {
163 // No presets found
164 $flashMessage = GeneralUtility::makeInstance(
165 FlashMessage::class,
166 $lang->getLL('no-presets'),
167 $lang->getLL('.alttitle'),
168 FlashMessage::NOTICE
169 );
170 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
171 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
172 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
173 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
174 $defaultFlashMessageQueue->enqueue($flashMessage);
175 }
176 }
177 return $content;
178 }
179
180 /**
181 * Select presets for this user
182 *
183 * @return array|bool Array of preset records
184 */
185 protected function getPresets()
186 {
187 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
188 ->getQueryBuilderForTable('tx_impexp_presets');
189
190 return $queryBuilder->select('*')
191 ->from('tx_impexp_presets')
192 ->where(
193 $queryBuilder->expr()->orX(
194 $queryBuilder->expr()->gt(
195 'public',
196 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
197 ),
198 $queryBuilder->expr()->eq(
199 'user_uid',
200 $queryBuilder->createNamedParameter($this->getBackendUser()->user['uid'], \PDO::PARAM_INT)
201 )
202 )
203 )
204 ->orderBy('item_uid', 'DESC')
205 ->addOrderBy('title')
206 ->execute()
207 ->fetchAll();
208 }
209
210 /**
211 * Returns a \TYPO3\CMS\Core\Resource\Folder object for saving export files
212 * to the server and is also used for uploading import files.
213 *
214 * @throws \InvalidArgumentException
215 * @return \TYPO3\CMS\Core\Resource\Folder|null
216 */
217 protected function getDefaultImportExportFolder()
218 {
219 $defaultImportExportFolder = null;
220
221 $defaultTemporaryFolder = $this->getBackendUser()->getDefaultUploadTemporaryFolder();
222 if ($defaultTemporaryFolder !== null) {
223 $importExportFolderName = 'importexport';
224 $createFolder = !$defaultTemporaryFolder->hasFolder($importExportFolderName);
225 if ($createFolder === true) {
226 try {
227 $defaultImportExportFolder = $defaultTemporaryFolder->createFolder($importExportFolderName);
228 } catch (Exception $folderAccessException) {
229 }
230 } else {
231 $defaultImportExportFolder = $defaultTemporaryFolder->getSubfolder($importExportFolderName);
232 }
233 }
234
235 return $defaultImportExportFolder;
236 }
237
238 /**
239 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
240 */
241 protected function getBackendUser()
242 {
243 return $GLOBALS['BE_USER'];
244 }
245
246 /**
247 * @return mixed
248 */
249 protected function getLanguageService()
250 {
251 return $GLOBALS['LANG'];
252 }
253 }