TYPO3  7.6
RecyclerAjaxController.php
Go to the documentation of this file.
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 
25 
30 {
36  protected $conf = array();
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  $this->conf['limit'] = GeneralUtility::_GP('limit') ? (int)GeneralUtility::_GP('limit') : 25;
47  $this->conf['start'] = GeneralUtility::_GP('start') ? (int)GeneralUtility::_GP('start') : 0;
48  $this->conf['filterTxt'] = GeneralUtility::_GP('filterTxt') ? GeneralUtility::_GP('filterTxt') : '';
49  $this->conf['startUid'] = GeneralUtility::_GP('startUid') ? (int)GeneralUtility::_GP('startUid') : 0;
50  $this->conf['depth'] = GeneralUtility::_GP('depth') ? (int)GeneralUtility::_GP('depth') : 0;
51  $this->conf['records'] = GeneralUtility::_GP('records') ? GeneralUtility::_GP('records') : null;
52  $this->conf['recursive'] = GeneralUtility::_GP('recursive') ? (bool)(int)GeneralUtility::_GP('recursive') : false;
53  }
54 
63  {
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['filterTxt']);
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  $response->getBody()->write(json_encode($content));
148  return $response;
149  }
150 
158  protected function setDataInSession($identifier, $data)
159  {
160  $beUser = $this->getBackendUser();
161  $beUser->uc['tx_recycler'][$identifier] = $data;
162  $beUser->writeUC();
163  }
164 
170  protected function getBackendUser()
171  {
172  return $GLOBALS['BE_USER'];
173  }
174 
178  protected function getLanguageService()
179  {
180  return $GLOBALS['LANG'];
181  }
182 }