[TASK] Doctrine: Migrate AbstractExportTestCase
[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 array(
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 = array();
115 // Is public?
116 if ($presetCfg['public']) {
117 $description[] = $lang->getLL('task.public') . ': ' . $lang->sL('LLL:EXT:lang/locallang_common.xlf:yes');
118 }
119 // Owner
120 $description[] = $lang->getLL('task.owner') . ': '
121 . ($presetCfg['user_uid'] === $GLOBALS['BE_USER']->user['uid']
122 ? $lang->getLL('task.own')
123 : '[' . htmlspecialchars($usernames[$presetCfg['user_uid']]['username']) . ']'
124 );
125 // Page & path
126 if ($configuration['pagetree']['id']) {
127 $description[] = $lang->getLL('task.page') . ': ' . $configuration['pagetree']['id'];
128 $description[] = $lang->getLL('task.path') . ': ' . htmlspecialchars(
129 BackendUtility::getRecordPath($configuration['pagetree']['id'], $clause, 20));
130 } else {
131 $description[] = $lang->getLL('single-record');
132 }
133 // Meta information
134 if ($configuration['meta']['title'] || $configuration['meta']['description'] || $configuration['meta']['notes']) {
135 $metaInformation = '';
136 if ($configuration['meta']['title']) {
137 $metaInformation .= '<strong>' . htmlspecialchars($configuration['meta']['title']) . '</strong><br />';
138 }
139 if ($configuration['meta']['description']) {
140 $metaInformation .= htmlspecialchars($configuration['meta']['description']);
141 }
142 if ($configuration['meta']['notes']) {
143 $metaInformation .= '<br /><br />
144 <strong>' . $lang->getLL('notes') . ': </strong>
145 <em>' . htmlspecialchars($configuration['meta']['notes']) . '</em>';
146 }
147 $description[] = '<br />' . $metaInformation;
148 }
149 // Collect all preset information
150 $lines[$key] = array(
151 'uid' => 'impexp' . $key,
152 'icon' => $icon,
153 'title' => $title,
154 'descriptionHtml' => implode('<br />', $description),
155 'link' => BackendUtility::getModuleUrl('user_task') . '&SET[function]=impexp.TYPO3\\CMS\\Impexp\\Task\\ImportExportTask&display=' . $presetCfg['uid']
156 );
157 }
158 // Render preset list
159 $content .= $this->taskObject->renderListMenu($lines);
160 } else {
161 // No presets found
162 $flashMessage = GeneralUtility::makeInstance(
163 FlashMessage::class,
164 $lang->getLL('no-presets'),
165 $lang->getLL('.alttitle'),
166 FlashMessage::NOTICE
167 );
168 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
169 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
170 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
171 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
172 $defaultFlashMessageQueue->enqueue($flashMessage);
173 }
174 }
175 return $content;
176 }
177
178 /**
179 * Select presets for this user
180 *
181 * @return array|bool Array of preset records
182 */
183 protected function getPresets()
184 {
185 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
186 ->getQueryBuilderForTable('tx_impexp_presets');
187
188 return $queryBuilder->select('*')
189 ->from('tx_impexp_presets')
190 ->where(
191 $queryBuilder->expr()->orX(
192 $queryBuilder->expr()->gt('public', 0),
193 $queryBuilder->expr()->eq('user_uid', (int)$this->getBackendUser()->user['uid'])
194 )
195 )
196 ->orderBy('item_uid', 'DESC')
197 ->addOrderBy('title')
198 ->execute()
199 ->fetchAll();
200 }
201
202 /**
203 * Returns a \TYPO3\CMS\Core\Resource\Folder object for saving export files
204 * to the server and is also used for uploading import files.
205 *
206 * @throws \InvalidArgumentException
207 * @return NULL|\TYPO3\CMS\Core\Resource\Folder
208 */
209 protected function getDefaultImportExportFolder()
210 {
211 $defaultImportExportFolder = null;
212
213 $defaultTemporaryFolder = $this->getBackendUser()->getDefaultUploadTemporaryFolder();
214 if ($defaultTemporaryFolder !== null) {
215 $importExportFolderName = 'importexport';
216 $createFolder = !$defaultTemporaryFolder->hasFolder($importExportFolderName);
217 if ($createFolder === true) {
218 try {
219 $defaultImportExportFolder = $defaultTemporaryFolder->createFolder($importExportFolderName);
220 } catch (Exception $folderAccessException) {
221 }
222 } else {
223 $defaultImportExportFolder = $defaultTemporaryFolder->getSubfolder($importExportFolderName);
224 }
225 }
226
227 return $defaultImportExportFolder;
228 }
229
230 /**
231 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
232 */
233 protected function getBackendUser()
234 {
235 return $GLOBALS['BE_USER'];
236 }
237
238 /**
239 * @return mixed
240 */
241 protected function getLanguageService()
242 {
243 return $GLOBALS['LANG'];
244 }
245 }