‪TYPO3CMS  9.5
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 
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
27 
33 {
39  protected ‪$conf = [];
40 
47  public function ‪dispatch(ServerRequestInterface $request): ResponseInterface
48  {
49  $parsedBody = $request->getParsedBody();
50  $queryParams = $request->getQueryParams();
51 
52  $this->conf['action'] = $parsedBody['action'] ?? $queryParams['action'] ?? null;
53  $this->conf['table'] = $parsedBody['table'] ?? $queryParams['table'] ?? '';
54  $this->conf['limit'] = ‪MathUtility::forceIntegerInRange(
55  (int)($this->‪getBackendUser()->getTSConfig()['mod.']['recycler.']['recordsPageLimit'] ?? 25),
56  1
57  );
58  $this->conf['start'] = (int)($parsedBody['start'] ?? $queryParams['start'] ?? 0);
59  $this->conf['filterTxt'] = $parsedBody['filterTxt'] ?? $queryParams['filterTxt'] ?? '';
60  $this->conf['startUid'] = (int)($parsedBody['startUid'] ?? $queryParams['startUid'] ?? 0);
61  $this->conf['depth'] = (int)($parsedBody['depth'] ?? $queryParams['depth'] ?? 0);
62  $this->conf['records'] = $parsedBody['records'] ?? $queryParams['records'] ?? null;
63  $this->conf['recursive'] = (bool)($parsedBody['recursive'] ?? $queryParams['recursive'] ?? false);
64 
65  $extPath = ‪ExtensionManagementUtility::extPath('recycler');
66  /* @var StandaloneView $view */
67  $view = GeneralUtility::makeInstance(StandaloneView::class);
68  $view->setPartialRootPaths(['default' => $extPath . 'Resources/Private/Partials']);
69 
70  $content = '';
71  // Determine the scripts to execute
72  switch ($this->conf['action']) {
73  case 'getTables':
74  $this->‪setDataInSession(['depthSelection' => $this->conf['depth']]);
75 
76  /* @var Tables $model */
77  $model = GeneralUtility::makeInstance(Tables::class);
78  $content = $model->getTables($this->conf['startUid'], $this->conf['depth']);
79  break;
80  case 'getDeletedRecords':
81  $this->‪setDataInSession([
82  'tableSelection' => $this->conf['table'],
83  'depthSelection' => $this->conf['depth'],
84  'resultLimit' => $this->conf['limit'],
85  ]);
86 
87  /* @var DeletedRecords $model */
88  $model = GeneralUtility::makeInstance(DeletedRecords::class);
89  $model->loadData($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['start'] . ',' . $this->conf['limit'], $this->conf['filterTxt']);
90  $deletedRowsArray = $model->getDeletedRows();
91 
92  $model = GeneralUtility::makeInstance(DeletedRecords::class);
93  $totalDeleted = $model->getTotalCount($this->conf['startUid'], $this->conf['table'], $this->conf['depth'], $this->conf['filterTxt']);
94 
95  /* @var DeletedRecordsController $controller */
96  $controller = GeneralUtility::makeInstance(DeletedRecordsController::class);
97  $recordsArray = $controller->transform($deletedRowsArray);
98 
99  $allowDelete = $this->‪getBackendUser()->‪isAdmin()
100  ?: (bool)($this->‪getBackendUser()->‪getTSConfig()['mod.']['recycler.']['allowDelete'] ?? false);
101 
102  $view->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/Ajax/RecordsTable.html');
103  $view->assign('records', $recordsArray['rows']);
104  $view->assign('allowDelete', $allowDelete);
105  $content = [
106  'rows' => $view->render(),
107  'totalItems' => $totalDeleted
108  ];
109  break;
110  case 'undoRecords':
111  if (empty($this->conf['records']) || !is_array($this->conf['records'])) {
112  $content = [
113  'success' => false,
114  'message' => ‪LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
115  ];
116  break;
117  }
118 
119  /* @var DeletedRecords $model */
120  $model = GeneralUtility::makeInstance(DeletedRecords::class);
121  $affectedRecords = $model->undeleteData($this->conf['records'], $this->conf['recursive']);
122  $messageKey = 'flashmessage.undo.' . ($affectedRecords !== false ? 'success' : 'failure') . '.' . ((int)$affectedRecords === 1 ? 'singular' : 'plural');
123  $content = [
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 = [
131  'success' => false,
132  'message' => ‪LocalizationUtility::translate('flashmessage.delete.norecordsselected', 'recycler')
133  ];
134  break;
135  }
136 
137  /* @var DeletedRecords $model */
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 = [
143  'success' => true,
144  'message' => sprintf(‪LocalizationUtility::translate($messageKey, 'recycler'), $affectedRecords)
145  ];
146  break;
147  }
148  return (new ‪JsonResponse())->setPayload($content);
149  }
150 
156  protected function ‪setDataInSession(array $data)
157  {
158  $beUser = $this->‪getBackendUser();
159  $recyclerUC = $beUser->uc['tx_recycler'] ?? [];
160  if (!empty(array_diff_assoc($data, $recyclerUC))) {
161  $beUser->uc['tx_recycler'] = array_merge($recyclerUC, $data);
162  $beUser->writeUC();
163  }
164  }
165 
171  protected function ‪getBackendUser()
172  {
173  return ‪$GLOBALS['BE_USER'];
174  }
175 
179  protected function ‪getLanguageService()
180  {
181  return ‪$GLOBALS['LANG'];
182  }
183 }
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: RecyclerAjaxController.php:170
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:29
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\$conf
‪array $conf
Definition: RecyclerAjaxController.php:38
‪TYPO3\CMS\Recycler\Domain\Model\Tables
Definition: Tables.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:294
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig($objectString=null, $config=null)
Definition: BackendUserAuthentication.php:1232
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\setDataInSession
‪setDataInSession(array $data)
Definition: RecyclerAjaxController.php:155
‪TYPO3\CMS\Recycler\Controller
Definition: DeletedRecordsController.php:3
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:36
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\dispatch
‪ResponseInterface dispatch(ServerRequestInterface $request)
Definition: RecyclerAjaxController.php:46
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController\getLanguageService
‪TYPO3 CMS Core Localization LanguageService getLanguageService()
Definition: RecyclerAjaxController.php:178
‪TYPO3\CMS\Recycler\Domain\Model\DeletedRecords
Definition: DeletedRecords.php:37
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:32
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Recycler\Controller\RecyclerAjaxController
Definition: RecyclerAjaxController.php:33
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\extPath
‪static string extPath($key, $script='')
Definition: ExtensionManagementUtility.php:149
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45