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