[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[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\Tables;
24 use TYPO3\CMS\Recycler\Domain\Model\DeletedRecords;
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 = array();
37
38 /**
39 * The constructor of this class
40 */
41 public function __construct() {
42 // Configuration, variable assignment
43 $this->conf['action'] = GeneralUtility::_GP('action');
44 $this->conf['table'] = GeneralUtility::_GP('table') ? GeneralUtility::_GP('table') : '';
45 $this->conf['limit'] = GeneralUtility::_GP('limit') ? (int)GeneralUtility::_GP('limit') : 25;
46 $this->conf['start'] = GeneralUtility::_GP('start') ? (int)GeneralUtility::_GP('start') : 0;
47 $this->conf['filterTxt'] = GeneralUtility::_GP('filterTxt') ? GeneralUtility::_GP('filterTxt') : '';
48 $this->conf['startUid'] = GeneralUtility::_GP('startUid') ? (int)GeneralUtility::_GP('startUid') : 0;
49 $this->conf['depth'] = GeneralUtility::_GP('depth') ? (int)GeneralUtility::_GP('depth') : 0;
50 $this->conf['records'] = GeneralUtility::_GP('records') ? GeneralUtility::_GP('records') : NULL;
51 $this->conf['recursive'] = GeneralUtility::_GP('recursive') ? (bool)(int)GeneralUtility::_GP('recursive') : FALSE;
52 }
53
54 /**
55 * The main dispatcher function. Collect data and prepare HTML output.
56 *
57 * @param ServerRequestInterface $request
58 * @param ResponseInterface $response
59 * @return ResponseInterface
60 */
61 public function dispatch(ServerRequestInterface $request, ResponseInterface $response) {
62 $extPath = ExtensionManagementUtility::extPath('recycler');
63 /* @var $view StandaloneView */
64 $view = GeneralUtility::makeInstance(StandaloneView::class);
65 $view->setPartialRootPaths(array('default' => $extPath . 'Resources/Private/Partials'));
66
67 $content = '';
68 // Determine the scripts to execute
69 switch ($this->conf['action']) {
70 case 'getTables':
71 $this->setDataInSession('depthSelection', $this->conf['depth']);
72
73 /* @var $model Tables */
74 $model = GeneralUtility::makeInstance(Tables::class);
75 $content = $model->getTables($this->conf['startUid'], $this->conf['depth']);
76 break;
77 case 'getDeletedRecords':
78 $this->setDataInSession('tableSelection', $this->conf['table']);
79 $this->setDataInSession('depthSelection', $this->conf['depth']);
80 $this->setDataInSession('resultLimit', $this->conf['limit']);
81
82 /* @var $model DeletedRecords */
83 $model = GeneralUtility::makeInstance(DeletedRecords::class);
84 $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
85 $deletedRowsArray = $model->getDeletedRows();
86
87 $model = GeneralUtility::makeInstance(DeletedRecords::class);
88 $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
89
90 /* @var $controller DeletedRecordsController */
91 $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
92 $recordsArray = $controller->transform($deletedRowsArray, $totalDeleted);
93
94 $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
95 $allowDelete = (bool)$this->getBackendUser()->user['admin'] ? TRUE : (bool)$modTS['properties']['allowDelete'];
96
97 $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
98 $view->assign('records', $recordsArray['rows']);
99 $view->assign('allowDelete', $allowDelete);
100 $view->assign('total', $recordsArray['total']);
101 $content = array(
102 'rows' => $view->render(),
103 'totalItems' => $recordsArray['total']
104 );
105 break;
106 case 'undoRecords':
107 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
108 $content = array(
109 'success' => FALSE,
110 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
111 );
112 break;
113 }
114
115 /* @var $model DeletedRecords */
116 $model = GeneralUtility::makeInstance(DeletedRecords::class);
117 $success = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
118 $affectedRecords = count($this->conf['records']);
119 $messageKey = 'flashmessage.undo.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
120 $content = array(
121 'success' => TRUE,
122 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
123 );
124 break;
125 case 'deleteRecords':
126 if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
127 $content = array(
128 'success' => FALSE,
129 'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
130 );
131 break;
132 }
133
134 /* @var $model DeletedRecords */
135 $model = GeneralUtility::makeInstance(DeletedRecords::class);
136 $success = $model->deleteData($this->conf['records']);
137 $affectedRecords = count($this->conf['records']);
138 $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
139 $content = array(
140 'success' => TRUE,
141 'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
142 );
143 break;
144 }
145 $response->getBody()->write(json_encode($content));
146 return $response;
147 }
148
149 /**
150 * Sets data in the session of the current backend user.
151 *
152 * @param string $identifier The identifier to be used to set the data
153 * @param string $data The data to be stored in the session
154 * @return void
155 */
156 protected function setDataInSession($identifier, $data) {
157 $beUser = $this->getBackendUser();
158 $beUser->uc['tx_recycler'][$identifier] = $data;
159 $beUser->writeUC();
160 }
161
162 /**
163 * Returns the BackendUser
164 *
165 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
166 */
167 protected function getBackendUser() {
168 return $GLOBALS['BE_USER'];
169 }
170
171 /**
172 * @return \TYPO3\CMS\Lang\LanguageService
173 */
174 protected function getLanguageService() {
175 return $GLOBALS['LANG'];
176 }
177
178 }