[TASK] Use JsonResponse for all Backend AJAX calls
[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('tableSelection', $this->conf['table']);
86 $this->setDataInSession('depthSelection', $this->conf['depth']);
87 $this->setDataInSession('resultLimit', $this->conf['limit']);
88
89 /* @var $model DeletedRecords */
90 $model = GeneralUtility::makeInstance(DeletedRecords::class);
91 $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
92 $deletedRowsArray = $model->getDeletedRows();
93
94 $model = GeneralUtility::makeInstance(DeletedRecords::class);
95 $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
96
97 /* @var $controller DeletedRecordsController */
98 $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
99 $recordsArray = $controller->transform($deletedRowsArray, $totalDeleted);
100
101 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
102 $allowDelete = $this->getBackendUser()->isAdmin() ? true : (bool)$modTS['properties']['allowDelete'];
103
104 $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
105 $view->assign('records', $recordsArray['rows']);
106 $view->assign('allowDelete', $allowDelete);
107 $view->assign('total', $recordsArray['total']);
108 $content = [
109 'rows' => $view->render(),
110 'totalItems' => $recordsArray['total']
111 ];
112 break;
113 case 'undoRecords':
114 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
115 $content = [
116 'success' => false,
117 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
118 ];
119 break;
120 }
121
122 /* @var $model DeletedRecords */
123 $model = GeneralUtility::makeInstance(DeletedRecords::class);
124 $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
125 $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
126 $content = [
127 'success' => true,
128 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
129 ];
130 break;
131 case 'deleteRecords':
132 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
133 $content = [
134 'success' => false,
135 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
136 ];
137 break;
138 }
139
140 /* @var $model DeletedRecords */
141 $model = GeneralUtility::makeInstance(DeletedRecords::class);
142 $success = $model->deleteData($this->conf['records']);
143 $affectedRecords = count($this->conf['records']);
144 $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
145 $content = [
146 'success' => true,
147 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
148 ];
149 break;
150 }
151 return GeneralUtility::makeInstance(JsonResponse::class)->setPayload($content);
152 }
153
154 /**
155 * Sets data in the session of the current backend user.
156 *
157 * @param string $identifier The identifier to be used to set the data
158 * @param string $data The data to be stored in the session
159 */
160 protected function setDataInSession($identifier, $data)
161 {
162 $beUser = $this->getBackendUser();
163 $beUser->uc['tx_recycler'][$identifier] = $data;
164 $beUser->writeUC();
165 }
166
167 /**
168 * Returns the BackendUser
169 *
170 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
171 */
172 protected function getBackendUser()
173 {
174 return $GLOBALS['BE_USER'];
175 }
176
177 /**
178 * @return \TYPO3\CMS\Core\Localization\LanguageService
179 */
180 protected function getLanguageService()
181 {
182 return $GLOBALS['LANG'];
183 }
184 }