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