TYPO3 CMS  TYPO3_6-2
DeletedRecords.php
Go to the documentation of this file.
1 <?php
3 
23 
29  protected $deletedRows = array();
30 
36  protected $limit = '';
37 
43  protected $table = array();
44 
50  protected $recyclerHelper;
51 
57  public $label;
58 
64  public $title;
65 
66  /************************************************************
67  * GET DATA FUNCTIONS
68  *
69  *
70  ************************************************************/
82  public function loadData($id, $table, $depth, $limit = '', $filter = '') {
83  // set the limit
84  $this->limit = trim($limit);
85  if ($table) {
86  if (array_key_exists($table, $GLOBALS['TCA'])) {
87  $this->table[] = $table;
88  $this->setData($id, $table, $depth, $GLOBALS['TCA'][$table]['ctrl'], $filter);
89  }
90  } else {
91  foreach ($GLOBALS['TCA'] as $tableKey => $tableValue) {
92  // only go into this table if the limit allows it
93  if ($this->limit != '') {
94  $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->limit);
95  // abort loop if LIMIT 0,0
96  if ($parts[0] == 0 && $parts[1] == 0) {
97  break;
98  }
99  }
100  $this->table[] = $tableKey;
101  $this->setData($id, $tableKey, $depth, $tableValue['ctrl'], $filter);
102  }
103  }
104  return $this;
105  }
106 
116  public function getTotalCount($id, $table, $depth, $filter) {
117  $deletedRecords = $this->loadData($id, $table, $depth, '', $filter)->getDeletedRows();
118  $countTotal = 0;
119  foreach ($this->table as $tableName) {
120  $countTotal += count($deletedRecords[$tableName]);
121  }
122  return $countTotal;
123  }
124 
135  protected function setData($id = 0, $table, $depth, $tcaCtrl, $filter) {
136  $id = (int)$id;
137  if (array_key_exists('delete', $tcaCtrl)) {
138  // find the 'deleted' field for this table
140  // create the filter WHERE-clause
141  if (trim($filter) != '') {
142  $filterWhere = ' AND (' . (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($filter) ? 'uid = ' . $filter . ' OR pid = ' . $filter . ' OR ' : '') . $tcaCtrl['label'] . ' LIKE "%' . $this->escapeValueForLike($filter, $table) . '%"' . ')';
143  }
144  // get the limit
145  if ($this->limit != '') {
146  // count the number of deleted records for this pid
147  $deletedCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, $deletedField . '<>0 AND pid = ' . $id . $filterWhere);
148  // split the limit
149  $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->limit);
150  $offset = $parts[0];
151  $rowCount = $parts[1];
152  // subtract the number of deleted records from the limit's offset
153  $result = $offset - $deletedCount;
154  // if the result is >= 0
155  if ($result >= 0) {
156  // store the new offset in the limit and go into the next depth
157  $offset = $result;
158  $this->limit = implode(',', array($offset, $rowCount));
159  // do NOT query this depth; limit also does not need to be set, we set it anyways
160  $allowQuery = FALSE;
161  $allowDepth = TRUE;
162  $limit = '';
163  } else {
164  // the offset for the temporary limit has to remain like the original offset
165  // in case the original offset was just crossed by the amount of deleted records
166  if ($offset != 0) {
167  $tempOffset = $offset;
168  } else {
169  $tempOffset = 0;
170  }
171  // set the offset in the limit to 0
172  $newOffset = 0;
173  // convert to negative result to the positive equivalent
174  $absResult = abs($result);
175  // if the result now is > limit's row count
176  if ($absResult > $rowCount) {
177  // use the limit's row count as the temporary limit
178  $limit = implode(',', array($tempOffset, $rowCount));
179  // set the limit's row count to 0
180  $this->limit = implode(',', array($newOffset, 0));
181  // do not go into new depth
182  $allowDepth = FALSE;
183  } else {
184  // if the result now is <= limit's row count
185  // use the result as the temporary limit
186  $limit = implode(',', array($tempOffset, $absResult));
187  // subtract the result from the row count
188  $newCount = $rowCount - $absResult;
189  // store the new result in the limit's row count
190  $this->limit = implode(',', array($newOffset, $newCount));
191  // if the new row count is > 0
192  if ($newCount > 0) {
193  // go into new depth
194  $allowDepth = TRUE;
195  } else {
196  // if the new row count is <= 0 (only =0 makes sense though)
197  // do not go into new depth
198  $allowDepth = FALSE;
199  }
200  }
201  // allow query for this depth
202  $allowQuery = TRUE;
203  }
204  } else {
205  $limit = '';
206  $allowDepth = TRUE;
207  $allowQuery = TRUE;
208  }
209  // query for actual deleted records
210  if ($allowQuery) {
211  $recordsToCheck = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordsByField($table, $deletedField, '1', ' AND pid = ' . $id . $filterWhere, '', '', $limit, FALSE);
212  if ($recordsToCheck) {
213  $this->checkRecordAccess($table, $recordsToCheck);
214  }
215  }
216  // go into depth
217  if ($allowDepth && $depth >= 1) {
218  // check recursively for elements beneath this page
219  $resPages = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'pid=' . $id, '', 'sorting');
220  if ($resPages) {
221  while ($rowPages = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resPages)) {
222  $this->setData($rowPages['uid'], $table, $depth - 1, $tcaCtrl, $filter);
223  // some records might have been added, check if we still have the limit for further queries
224  if ('' != $this->limit) {
225  $parts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->limit);
226  // abort loop if LIMIT 0,0
227  if ($parts[0] == 0 && $parts[1] == 0) {
228  break;
229  }
230  }
231  }
232  $GLOBALS['TYPO3_DB']->sql_free_result($resPages);
233  }
234  }
235  $this->label[$table] = $tcaCtrl['label'];
236  $this->title[$table] = $tcaCtrl['title'];
237  }
238  }
239 
247  protected function checkRecordAccess($table, array $rows) {
248  foreach ($rows as $key => $row) {
249  if (\TYPO3\CMS\Recycler\Utility\RecyclerUtility::checkAccess($table, $row)) {
250  $this->setDeletedRows($table, $row);
251  }
252  }
253  }
254 
263  protected function escapeValueForLike($value, $tableName) {
264  return $GLOBALS['TYPO3_DB']->escapeStrForLike($GLOBALS['TYPO3_DB']->quoteStr($value, $tableName), $tableName);
265  }
266 
267  /************************************************************
268  * DELETE FUNCTIONS
269  ************************************************************/
276  public function deleteData($recordsArray) {
277  $recordsArray = json_decode($recordsArray);
278  if (is_array($recordsArray)) {
279  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
280  $tce->start('', '');
281  $tce->disableDeleteClause();
282  foreach ($recordsArray as $key => $record) {
283  $tce->deleteEl($record[0], $record[1], TRUE, TRUE);
284  }
285  return TRUE;
286  }
287  return FALSE;
288  }
289 
290  /************************************************************
291  * UNDELETE FUNCTIONS
292  ************************************************************/
301  public function undeleteData($recordsArray, $recursive = FALSE) {
302  $result = FALSE;
303  $depth = 999;
304  $recordsArray = json_decode($recordsArray);
305  if (is_array($recordsArray)) {
306  $this->deletedRows = array();
307  $cmd = array();
308  foreach ($recordsArray as $key => $row) {
309  $cmd[$row[0]][$row[1]]['undelete'] = 1;
310  if ($row[0] == 'pages' && $recursive == TRUE) {
311  $this->loadData($row[1], '', $depth, '');
312  $childRecords = $this->getDeletedRows();
313  if (count($childRecords) > 0) {
314  foreach ($childRecords as $table => $childRows) {
315  foreach ($childRows as $childKey => $childRow) {
316  $cmd[$table][$childRow['uid']]['undelete'] = 1;
317  }
318  }
319  }
320  }
321  }
322  if ($cmd) {
323  $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
324  $tce->start(array(), $cmd);
325  $tce->process_cmdmap();
326  $result = TRUE;
327  }
328  }
329  return $result;
330  }
331 
332  /************************************************************
333  * SETTER FUNCTIONS
334  ************************************************************/
342  public function setDeletedRows($table, array $row) {
343  $this->deletedRows[$table][] = $row;
344  }
345 
346  /************************************************************
347  * GETTER FUNCTIONS
348  ************************************************************/
354  public function getDeletedRows() {
355  return $this->deletedRows;
356  }
357 
363  public function getTable() {
364  return $this->table;
365  }
366 
367 }
static getRecordsByField($theTable, $theField, $theValue, $whereClause='', $groupBy='', $orderBy='', $limit='', $useDeleteClause=TRUE)
undeleteData($recordsArray, $recursive=FALSE)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
getTotalCount($id, $table, $depth, $filter)
setData($id=0, $table, $depth, $tcaCtrl, $filter)
loadData($id, $table, $depth, $limit='', $filter='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]