TYPO3 CMS  TYPO3_8-7
RecyclerAjaxController.php
Go to the documentation of this file.
1 <?php
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 
25 
30 {
36  protected $conf = [];
37 
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 
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([
86  'tableSelection' => $this->conf['table'],
87  'depthSelection' => $this->conf['depth'],
88  'resultLimit' => $this->conf['limit'],
89  ]);
90 
91  /* @var $model DeletedRecords */
92  $model = GeneralUtility::makeInstance(DeletedRecords::class);
93  $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
94  $deletedRowsArray = $model->getDeletedRows();
95 
96  $model = GeneralUtility::makeInstance(DeletedRecords::class);
97  $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
98 
99  /* @var $controller DeletedRecordsController */
100  $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
101  $recordsArray = $controller->transform($deletedRowsArray);
102 
103  $modTS = $this->getBackendUser()->getTSConfig('mod.recycler');
104  $allowDelete = $this->getBackendUser()->isAdmin() ? true : (bool)$modTS['properties']['allowDelete'];
105 
106  $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
107  $view->assign('records', $recordsArray['rows']);
108  $view->assign('allowDelete', $allowDelete);
109  $content = [
110  'rows' => $view->render(),
111  'totalItems' => $totalDeleted
112  ];
113  break;
114  case 'undoRecords':
115  if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
116  $content = [
117  'success' => false,
118  'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
119  ];
120  break;
121  }
122 
123  /* @var $model DeletedRecords */
124  $model = GeneralUtility::makeInstance(DeletedRecords::class);
125  $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
126  $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
127  $content = [
128  'success' => true,
129  'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
130  ];
131  break;
132  case 'deleteRecords':
133  if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
134  $content = [
135  'success' => false,
136  'message' => LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
137  ];
138  break;
139  }
140 
141  /* @var $model DeletedRecords */
142  $model = GeneralUtility::makeInstance(DeletedRecords::class);
143  $success = $model->deleteData($this->conf['records']);
144  $affectedRecords = count($this->conf['records']);
145  $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
146  $content = [
147  'success' => true,
148  'message' => sprintf(LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
149  ];
150  break;
151  }
152  $response->getBody()->write(json_encode($content));
153  return $response;
154  }
155 
161  protected function setDataInSession(array $data)
162  {
163  $beUser = $this->getBackendUser();
164  $recyclerUC = $beUser->uc['tx_recycler'] ?? [];
165  if (!empty(array_diff_assoc($data, $recyclerUC))) {
166  $beUser->uc['tx_recycler'] = array_merge($recyclerUC, $data);
167  $beUser->writeUC();
168  }
169  }
170 
176  protected function getBackendUser()
177  {
178  return $GLOBALS['BE_USER'];
179  }
180 
184  protected function getLanguageService()
185  {
186  return $GLOBALS['LANG'];
187  }
188 }
dispatch(ServerRequestInterface $request, ResponseInterface $response)
static translate($key, $extensionName=null, $arguments=null)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']