[BUGFIX] Make number of listed records in EXT:recycler configurable again
[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\Utility\ExtensionManagementUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22 use TYPO3\CMS\Fluid\View\StandaloneView;
23 use TYPO3\CMS\Recycler\Domain\Model\DeletedRecords;
24 use TYPO3\CMS\Recycler\Domain\Model\Tables;
25
26 /**
27 * Controller class for the 'recycler' extension. Handles the AJAX Requests
28 */
29 class RecyclerAjaxController
30 {
31 /**
32 * The local configuration array
33 *
34 * @var array
35 */
36 protected $conf = [];
37
38 /**
39 * The constructor of this class
40 */
41 public function __construct()
42 {
43 // Configuration, variable assignment
44 $this->conf['action'] = GeneralUtility::_GP('action');
45 $this->conf['table'] = GeneralUtility::_GP('table') ? GeneralUtility::_GP('table') : '';
46 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
47 if (isset($modTS['properties']['recordsPageLimit']) && (int)$modTS['properties']['recordsPageLimit'] > 0) {
48 $this->conf['limit'] = (int)$modTS['properties']['recordsPageLimit'];
49 } else {
50 $this->conf['limit'] = 25;
51 }
52 $this->conf['start'] = GeneralUtility::_GP('start') ? (int)GeneralUtility::_GP('start') : 0;
53 $this->conf['filterTxt'] = GeneralUtility::_GP('filterTxt') ? GeneralUtility::_GP('filterTxt') : '';
54 $this->conf['startUid'] = GeneralUtility::_GP('startUid') ? (int)GeneralUtility::_GP('startUid') : 0;
55 $this->conf['depth'] = GeneralUtility::_GP('depth') ? (int)GeneralUtility::_GP('depth') : 0;
56 $this->conf['records'] = GeneralUtility::_GP('records') ? GeneralUtility::_GP('records') : null;
57 $this->conf['recursive'] = GeneralUtility::_GP('recursive') ? (bool)GeneralUtility::_GP('recursive') : false;
58 }
59
60 /**
61 * The main dispatcher function. Collect data and prepare HTML output.
62 *
63 * @param ServerRequestInterface $request
64 * @param ResponseInterface $response
65 * @return ResponseInterface
66 */
67 public function dispatch(ServerRequestInterface $request, ResponseInterface $response)
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 $response->getBody()->write(json_encode($content));
152 return $response;
153 }
154
155 /**
156 * Sets data in the session of the current backend user.
157 *
158 * @param string $identifier The identifier to be used to set the data
159 * @param string $data The data to be stored in the session
160 */
161 protected function setDataInSession($identifier, $data)
162 {
163 $beUser = $this->getBackendUser();
164 $beUser->uc['tx_recycler'][$identifier] = $data;
165 $beUser->writeUC();
166 }
167
168 /**
169 * Returns the BackendUser
170 *
171 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
172 */
173 protected function getBackendUser()
174 {
175 return $GLOBALS['BE_USER'];
176 }
177
178 /**
179 * @return \TYPO3\CMS\Lang\LanguageService
180 */
181 protected function getLanguageService()
182 {
183 return $GLOBALS['LANG'];
184 }
185 }