e7237d4755ad24d682d679a0ee5e6c6d56035e1c
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Controller / RecyclerAjaxController.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Controller;
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Http\JsonResponse;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
23 use TYPO3\CMS\Fluid\View\StandaloneView;
24 use TYPO3\CMS\Recycler\Domain\Model\DeletedRecords;
25 use TYPO3\CMS\Recycler\Domain\Model\Tables;
26
27 /**
28 * Controller class for the 'recycler' extension. Handles the AJAX Requests
29 */
30 class RecyclerAjaxController
31 {
32 /**
33 * The local configuration array
34 *
35 * @var array
36 */
37 protected $conf = [];
38
39 /**
40 * The constructor of this class
41 */
42 public function __construct()
43 {
44 // Configuration, variable assignment
45 $this->conf['action'] = GeneralUtility::_GP('action');
46 $this->conf['table'] = GeneralUtility::_GP('table') ? GeneralUtility::_GP('table') : '';
47 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
48 if (isset($modTS['properties']['recordsPageLimit']) && (int)$modTS['properties']['recordsPageLimit'] > 0) {
49 $this->conf['limit'] = (int)$modTS['properties']['recordsPageLimit'];
50 } else {
51 $this->conf['limit'] = 25;
52 }
53 $this->conf['start'] = GeneralUtility::_GP('start') ? (int)GeneralUtility::_GP('start') : 0;
54 $this->conf['filterTxt'] = GeneralUtility::_GP('filterTxt') ? GeneralUtility::_GP('filterTxt') : '';
55 $this->conf['startUid'] = GeneralUtility::_GP('startUid') ? (int)GeneralUtility::_GP('startUid') : 0;
56 $this->conf['depth'] = GeneralUtility::_GP('depth') ? (int)GeneralUtility::_GP('depth') : 0;
57 $this->conf['records'] = GeneralUtility::_GP('records') ? GeneralUtility::_GP('records') : null;
58 $this->conf['recursive'] = GeneralUtility::_GP('recursive') ? (bool)GeneralUtility::_GP('recursive') : false;
59 }
60
61 /**
62 * The main dispatcher function. Collect data and prepare HTML output.
63 *
64 * @param ServerRequestInterface $request
65 * @return ResponseInterface
66 */
67 public function dispatch(ServerRequestInterface $request)
68 {
69 $extPath = ExtensionManagementUtility::extPath('recycler');
70 /* @var $view StandaloneView */
71 $view = GeneralUtility::makeInstance(StandaloneView::class);
72 $view->setPartialRootPaths(['default' => $extPath . 'Resources/Private/Partials']);
73
74 $content = '';
75 // Determine the scripts to execute
76 switch ($this->conf['action']) {
77 case 'getTables':
78 $this->setDataInSession(['depthSelection' => $this->conf['depth']]);
79
80 /* @var $model Tables */
81 $model = GeneralUtility::makeInstance(Tables::class);
82 $content = $model->getTables($this->conf['startUid'], $this->conf['depth']);
83 break;
84 case 'getDeletedRecords':
85 $this->setDataInSession([
86 'tableSelection' => $this->conf['table'],
87 'depthSelection' => $this->conf['depth'],
88 'resultLimit' => $this->conf['limit'],
89 ]);
90
91 /* @var $model DeletedRecords */
92 $model = GeneralUtility::makeInstance(DeletedRecords::class);
93 $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
94 $deletedRowsArray = $model->getDeletedRows();
95
96 $model = GeneralUtility::makeInstance(DeletedRecords::class);
97 $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
98
99 /* @var $controller DeletedRecordsController */
100 $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
101 $recordsArray = $controller->transform($deletedRowsArray, $totalDeleted);
102
103 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
104 $allowDelete = $this->getBackendUser()->isAdmin() ? true : (bool)$modTS['properties']['allowDelete'];
105
106 $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
107 $view->assign('records', $recordsArray['rows']);
108 $view->assign('allowDelete', $allowDelete);
109 $view->assign('total', $recordsArray['total']);
110 $content = [
111 'rows' => $view->render(),
112 'totalItems' => $recordsArray['total']
113 ];
114 break;
115 case 'undoRecords':
116 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
117 $content = [
118 'success' => false,
119 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
120 ];
121 break;
122 }
123
124 /* @var $model DeletedRecords */
125 $model = GeneralUtility::makeInstance(DeletedRecords::class);
126 $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
127 $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
128 $content = [
129 'success' => true,
130 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
131 ];
132 break;
133 case 'deleteRecords':
134 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
135 $content = [
136 'success' => false,
137 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
138 ];
139 break;
140 }
141
142 /* @var $model DeletedRecords */
143 $model = GeneralUtility::makeInstance(DeletedRecords::class);
144 $success = $model->deleteData($this->conf['records']);
145 $affectedRecords = count($this->conf['records']);
146 $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
147 $content = [
148 'success' => true,
149 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
150 ];
151 break;
152 }
153 return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($content);
154 }
155
156 /**
157 * Sets data in the session of the current backend user.
158 *
159 * @param array $data The data to be stored in the session
160 */
161 protected function setDataInSession(array $data)
162 {
163 $beUser = $this->getBackendUser();
164 $recyclerUC = $beUser->uc['tx_recycler'] ?? [];
165 if (!empty(array_diff_assoc($data, $recyclerUC))) {
166 $beUser->uc['tx_recycler'] = array_merge($recyclerUC, $data);
167 $beUser->writeUC();
168 }
169 }
170
171 /**
172 * Returns the BackendUser
173 *
174 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
175 */
176 protected function getBackendUser()
177 {
178 return $GLOBALS['BE_USER'];
179 }
180
181 /**
182 * @return \TYPO3\CMS\Core\Localization\LanguageService
183 */
184 protected function getLanguageService()
185 {
186 return $GLOBALS['LANG'];
187 }
188 }