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