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