/**
  * Get tables for menu example
  *
  * @param	string		$format: Return format (example: json)
  * @param	boolean		$withAllOption: 0 no, 1 return tables with a "all" option
  * @param	integer		$id: UID from selected page
  * @param	integer		$depth: How many levels recursive
  * @return	string		The tables to be displayed
  */
 public function getTables($format, $withAllOption = 0, $startUid, $depth = 0)
 {
     $deletedRecordsTotal = 0;
     $tables = array();
     foreach (array_keys($GLOBALS['TCA']) as $tableName) {
         $deletedField = tx_recycler_helper::getDeletedField($tableName);
         if ($deletedField) {
             // Determine whether the table has deleted records:
             $deletedCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $tableName, $deletedField . '!=0');
             if ($deletedCount) {
                 $deletedDataObject = t3lib_div::makeInstance('tx_recycler_model_deletedRecords');
                 $deletedData = $deletedDataObject->loadData($startUid, $tableName, $depth)->getDeletedRows();
                 if (isset($deletedData[$tableName])) {
                     if ($deletedRecordsInTable = count($deletedData[$tableName])) {
                         $deletedRecordsTotal += $deletedRecordsInTable;
                         $tables[] = array($tableName, $deletedRecordsInTable, $tableName, tx_recycler_helper::getUtf8String($GLOBALS['LANG']->sL($GLOBALS['TCA'][$tableName]['ctrl']['title'])));
                     }
                 }
             }
         }
     }
     $jsonArray = $tables;
     if ($withAllOption) {
         array_unshift($jsonArray, array('', $deletedRecordsTotal, '', $GLOBALS['LANG']->sL('LLL:EXT:recycler/mod1/locallang.xml:label_alltables')));
     }
     $output = json_encode($jsonArray);
     return $output;
 }
 /**
  * Set all deleted rows
  *
  * @param	integer		$id: UID from record
  * @param	string		$table: Tablename from record
  * @param	integer		$depth: How many levels recursive
  * @param	array		$ctrl: TCA CTRL Array
  * @param	string		$filter: Filter text
  * @return	void
  */
 protected function setData($id = 0, $table, $depth, $tcaCtrl, $filter)
 {
     $id = intval($id);
     if (array_key_exists('delete', $tcaCtrl)) {
         // find the 'deleted' field for this table
         $deletedField = tx_recycler_helper::getDeletedField($table);
         // create the filter WHERE-clause
         if (trim($filter) != '') {
             $filterWhere = ' AND (' . (t3lib_div::testInt($filter) ? 'uid = ' . $filter . ' OR pid = ' . $filter . ' OR ' : '') . $tcaCtrl['label'] . ' LIKE "%' . $this->escapeValueForLike($filter, $table) . '%"' . ')';
         }
         // get the limit
         if ($this->limit != '') {
             // count the number of deleted records for this pid
             $deletedCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', $table, $deletedField . '!=0 AND pid = ' . $id . $filterWhere);
             // split the limit
             $parts = t3lib_div::trimExplode(',', $this->limit);
             $offset = $parts[0];
             $rowCount = $parts[1];
             // subtract the number of deleted records from the limit's offset
             $result = $offset - $deletedCount;
             // if the result is >= 0
             if ($result >= 0) {
                 // store the new offset in the limit and go into the next depth
                 $offset = $result;
                 $this->limit = implode(',', array($offset, $rowCount));
                 // do NOT query this depth; limit also does not need to be set, we set it anyways
                 $allowQuery = false;
                 $allowDepth = true;
                 $limit = '';
                 // won't be queried anyways
                 // if the result is < 0
             } else {
                 // the offset for the temporary limit has to remain like the original offset
                 // in case the original offset was just crossed by the amount of deleted records
                 if ($offset != 0) {
                     $tempOffset = $offset;
                 } else {
                     $tempOffset = 0;
                 }
                 // set the offset in the limit to 0
                 $newOffset = 0;
                 // convert to negative result to the positive equivalent
                 $absResult = abs($result);
                 // if the result now is > limit's row count
                 if ($absResult > $rowCount) {
                     // use the limit's row count as the temporary limit
                     $limit = implode(',', array($tempOffset, $rowCount));
                     // set the limit's row count to 0
                     $this->limit = implode(',', array($newOffset, 0));
                     // do not go into new depth
                     $allowDepth = false;
                 } else {
                     // if the result now is <= limit's row count
                     // use the result as the temporary limit
                     $limit = implode(',', array($tempOffset, $absResult));
                     // subtract the result from the row count
                     $newCount = $rowCount - $absResult;
                     // store the new result in the limit's row count
                     $this->limit = implode(',', array($newOffset, $newCount));
                     // if the new row count is > 0
                     if ($newCount > 0) {
                         // go into new depth
                         $allowDepth = true;
                     } else {
                         // if the new row count is <= 0 (only =0 makes sense though)
                         // do not go into new depth
                         $allowDepth = false;
                     }
                 }
                 // allow query for this depth
                 $allowQuery = true;
             }
         } else {
             $limit = '';
             $allowDepth = true;
             $allowQuery = true;
         }
         // query for actual deleted records
         if ($allowQuery) {
             $recordsToCheck = t3lib_BEfunc::getRecordsByField($table, $deletedField, '1', ' AND pid = ' . $id . $filterWhere, '', '', $limit, false);
             if ($recordsToCheck) {
                 $this->checkRecordAccess($table, $recordsToCheck);
             }
         }
         // go into depth
         if ($allowDepth && $depth >= 1) {
             // check recursively for elements beneath this page
             $resPages = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'pid=' . $id, '', 'sorting');
             if (is_resource($resPages)) {
                 while ($rowPages = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($resPages)) {
                     $this->setData($rowPages['uid'], $table, $depth - 1, $tcaCtrl, $filter);
                     // some records might have been added, check if we still have the limit for further queries
                     if ('' != $this->limit) {
                         $parts = t3lib_div::trimExplode(',', $this->limit);
                         // abort loop if LIMIT 0,0
                         if ($parts[0] == 0 && $parts[1] == 0) {
                             break;
                         }
                     }
                 }
                 $GLOBALS['TYPO3_DB']->sql_free_result($resPages);
             }
         }
         $this->label[$table] = $tcaCtrl['label'];
         $this->title[$table] = $tcaCtrl['title'];
     }
 }