‪TYPO3CMS  10.4
RecyclerAjaxController.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
28 
34 {
40  protected ‪$conf = [];
41 
48  public function ‪dispatch(ServerRequestInterface $request): ResponseInterface
49  {
50  $parsedBody = $request->getParsedBody();
51  $queryParams = $request->getQueryParams();
52 
53  $this->conf['action'] = $parsedBody['action'] ?? $queryParams['action'] ?? null;
54  $this->conf['table'] = $parsedBody['table'] ?? $queryParams['table'] ?? '';
55  $this->conf['limit'] = ‪MathUtility::forceIntegerInRange(
56  (int)($this->‪getBackendUser()->getTSConfig()['mod.']['recycler.']['recordsPageLimit'] ?? 25),
57  1
58  );
59  $this->conf['start'] = (int)($parsedBody['start'] ?? $queryParams['start'] ?? 0);
60  $this->conf['filterTxt'] = $parsedBody['filterTxt'] ?? $queryParams['filterTxt'] ?? '';
61  $this->conf['startUid'] = (int)($parsedBody['startUid'] ?? $queryParams['startUid'] ?? 0);
62  $this->conf['depth'] = (int)($parsedBody['depth'] ?? $queryParams['depth'] ?? 0);
63  $this->conf['records'] = $parsedBody['records'] ?? $queryParams['records'] ?? null;
64  $this->conf['recursive'] = (bool)($parsedBody['recursive'] ?? $queryParams['recursive'] ?? false);
65 
66  $extPath = ‪ExtensionManagementUtility::extPath('recycler');
67  /* @var StandaloneView $view */
68  $view = GeneralUtility::makeInstance(StandaloneView::class);
69  $view->setPartialRootPaths(['default' => $extPath . 'Resources/Private/Partials']);
70 
71  $content = '';
72  // Determine the scripts to execute
73  switch ($this->conf['action']) {
74  case 'getTables':
75  $this->‪setDataInSession(['depthSelection' => $this->conf['depth']]);
76 
77  /* @var Tables $model */
78  $model = GeneralUtility::makeInstance(Tables::class);
79  $content = $model->getTables($this->conf['startUid'], $this->conf['depth']);
80  break;
81  case 'getDeletedRecords':
82  $this->‪setDataInSession([
83  'tableSelection' => $this->conf['table'],
84  'depthSelection' => $this->conf['depth'],
85  'resultLimit' => $this->conf['limit'],
86  ]);
87 
88  /* @var DeletedRecords $model */
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 DeletedRecordsController $controller */
97  $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
98  $recordsArray = $controller->transform($deletedRowsArray);
99 
100  $allowDelete = $this->‪getBackendUser()->‪isAdmin()
101  ?: (bool)($this->‪getBackendUser()->‪getTSConfig()['mod.']['recycler.']['allowDelete'] ?? false);
102 
103  $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
104  $view->assign('records', $recordsArray['rows']);
105  $view->assign('allowDelete', $allowDelete);
106  $content = [
107  'rows' => $view->render(),
108  'totalItems' => $totalDeleted
109  ];
110  break;
111  case 'undoRecords':
112  if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
113  $content = [
114  'success' => false,
115  'message' => ‪LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
116  ];
117  break;
118  }
119 
120  /* @var DeletedRecords $model */
121  $model = GeneralUtility::makeInstance(DeletedRecords::class);
122  $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
123  $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
124  $content = [
125  'success' => true,
126  'message' => sprintf((string)‪LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
127  ];
128  break;
129  case 'deleteRecords':
130  if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
131  $content = [
132  'success' => false,
133  'message' => ‪LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
134  ];
135  break;
136  }
137 
138  /* @var DeletedRecords $model */
139  $model = GeneralUtility::makeInstance(DeletedRecords::class);
140  $success = $model->deleteData($this->conf['records'] ?? null);
141  $affectedRecords = count($this->conf['records']);
142  $messageKey = 'flashmessage.delete.' . ($success ? 'success' : 'failure') . '.' . ($affectedRecords === 1 ? 'singular' : 'plural');
143  $content = [
144  'success' => true,
145  'message' => sprintf((string)‪LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
146  ];
147  break;
148  }
149  return (new ‪JsonResponse())->setPayload($content);
150  }
151 
157  protected function ‪setDataInSession(array $data)
158  {
159  $beUser = $this->‪getBackendUser();
160  $recyclerUC = $beUser->uc['tx_recycler'] ?? [];
161  if (!empty(array_diff_assoc($data, $recyclerUC))) {
162  $beUser->uc['tx_recycler'] = array_merge($recyclerUC, $data);
163  $beUser->writeUC();
164  }
165  }
166 
172  protected function ‪getBackendUser()
173  {
174  return ‪$GLOBALS['BE_USER'];
175  }
176 
180  protected function ‪getLanguageService()
181  {
182  return ‪$GLOBALS['LANG'];
183  }
184 }
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: RecyclerAjaxController.php:171
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig()
Definition: BackendUserAuthentication.php:1217
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\$conf
‪array $conf
Definition: RecyclerAjaxController.php:39
‪TYPO3\CMS\Recycler\Domain\Model\Tables
Definition: Tables.php:27
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:292
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\setDataInSession
‪setDataInSession(array $data)
Definition: RecyclerAjaxController.php:156
‪TYPO3\CMS\Recycler\Controller
Definition: DeletedRecordsController.php:16
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\dispatch
‪ResponseInterface dispatch(ServerRequestInterface $request)
Definition: RecyclerAjaxController.php:47
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\getLanguageService
‪TYPO3 CMS Core Localization LanguageService getLanguageService()
Definition: RecyclerAjaxController.php:179
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Recycler\Domain\Model\DeletedRecords
Definition: DeletedRecords.php:38
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController
Definition: RecyclerAjaxController.php:34
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\extPath
‪static string extPath($key, $script='')
Definition: ExtensionManagementUtility.php:127
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46