c6b42cb99ebfc41b9c5fa009d410a2cbdb53e21f
[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 if (isset($modTS['properties']['recordsPageLimit']) && (int)$modTS['properties']['recordsPageLimit'] > 0) {
47 $this->conf['limit'] = (int)$modTS['properties']['recordsPageLimit'];
48 } else {
49 $this->conf['limit'] = 25;
50 }
51 $this->conf['start'] = GeneralUtility::_GP('start') ? (int)GeneralUtility::_GP('start') : 0;
52 $this->conf['filterTxt'] = GeneralUtility::_GP('filterTxt') ? GeneralUtility::_GP('filterTxt') : '';
53 $this->conf['startUid'] = GeneralUtility::_GP('startUid') ? (int)GeneralUtility::_GP('startUid') : 0;
54 $this->conf['depth'] = GeneralUtility::_GP('depth') ? (int)GeneralUtility::_GP('depth') : 0;
55 $this->conf['records'] = GeneralUtility::_GP('records') ? GeneralUtility::_GP('records') : null;
56 $this->conf['recursive'] = GeneralUtility::_GP('recursive') ? (bool)GeneralUtility::_GP('recursive') : false;
57 }
58
59 /**
60 * The main dispatcher function. Collect data and prepare HTML output.
61 *
62 * @param ServerRequestInterface $request
63 * @param ResponseInterface $response
64 * @return ResponseInterface
65 */
66 public function dispatch(ServerRequestInterface $request, ResponseInterface $response)
67 {
68 $extPath = ExtensionManagementUtility::extPath('recycler');
69 /* @var $view StandaloneView */
70 $view = GeneralUtility::makeInstance(StandaloneView::class);
71 $view->setPartialRootPaths(['default' => $extPath . 'Resources/Private/Partials']);
72
73 $content = '';
74 // Determine the scripts to execute
75 switch ($this->conf['action']) {
76 case 'getTables':
77 $this->setDataInSession('depthSelection', $this->conf['depth']);
78
79 /* @var $model Tables */
80 $model = GeneralUtility::makeInstance(Tables::class);
81 $content = $model->getTables($this->conf['startUid'], $this->conf['depth']);
82 break;
83 case 'getDeletedRecords':
84 $this->setDataInSession('tableSelection', $this->conf['table']);
85 $this->setDataInSession('depthSelection', $this->conf['depth']);
86 $this->setDataInSession('resultLimit', $this->conf['limit']);
87
88 /* @var $model DeletedRecords */
89 $model = GeneralUtility::makeInstance(DeletedRecords::class);
90 $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
91 $deletedRowsArray = $model->getDeletedRows();
92
93 $model = GeneralUtility::makeInstance(DeletedRecords::class);
94 $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
95
96 /* @var $controller DeletedRecordsController */
97 $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
98 $recordsArray = $controller->transform($deletedRowsArray, $totalDeleted);
99
100 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
101 $allowDelete = $this->getBackendUser()->isAdmin() ? true : (bool)$modTS['properties']['allowDelete'];
102
103 $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
104 $view->assign('records', $recordsArray['rows']);
105 $view->assign('allowDelete', $allowDelete);
106 $view->assign('total', $recordsArray['total']);
107 $content = [
108 'rows' => $view->render(),
109 'totalItems' => $recordsArray['total']
110 ];
111 break;
112 case 'undoRecords':
113 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
114 $content = [
115 'success' => false,
116 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
117 ];
118 break;
119 }
120
121 /* @var $model DeletedRecords */
122 $model = GeneralUtility::makeInstance(DeletedRecords::class);
123 $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
124 $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
125 $content = [
126 'success' => true,
127 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
128 ];
129 break;
130 case 'deleteRecords':
131 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
132 $content = [
133 'success' => false,
134 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
135 ];
136 break;
137 }
138
139 /* @var $model DeletedRecords */
140 $model = GeneralUtility::makeInstance(DeletedRecords::class);
141 $success = $model->deleteData($this->conf['records']);
142 $affectedRecords = count($this->conf['records']);
143 $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
144 $content = [
145 'success' => true,
146 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
147 ];
148 break;
149 }
150 $response->getBody()->write(json_encode($content));
151 return $response;
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\Lang\LanguageService
179 */
180 protected function getLanguageService()
181 {
182 return $GLOBALS['LANG'];
183 }
184 }