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