/**
  * Creates a new version of a record
  * (Requires support in the table)
  *
  * @param	string		Table name
  * @param	integer		Record uid to versionize
  * @param	string		Version label
  * @param	boolean		If true, the version is created to delete the record.
  * @param	integer		Indicating "treeLevel" - or versioning type - "element" (-1), "page" (0) or "branch" (>=1)
  * @return	integer		Returns the id of the new version (if any)
  * @see copyRecord()
  */
 function versionizeRecord($table, $id, $label, $delete = FALSE, $versionizeTree = -1)
 {
     global $TCA;
     $id = intval($id);
     if ($TCA[$table] && $TCA[$table]['ctrl']['versioningWS'] && $id > 0) {
         if ($this->doesRecordExist($table, $id, 'show')) {
             if ($this->BE_USER->workspaceVersioningTypeAccess($versionizeTree)) {
                 // Select main record:
                 $row = $this->recordInfo($table, $id, 'pid,t3ver_id,t3ver_state');
                 if (is_array($row)) {
                     if ($row['pid'] >= 0) {
                         // record must be online record
                         if ($row['t3ver_state'] != 3) {
                             // record must not be placeholder for moving.
                             if (!$delete || !$this->cannotDeleteRecord($table, $id)) {
                                 // Look for next version number:
                                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('t3ver_id', $table, '((pid=-1 && t3ver_oid=' . $id . ') OR uid=' . $id . ')' . $this->deleteClause($table), '', 't3ver_id DESC', '1');
                                 list($highestVerNumber) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
                                 $GLOBALS['TYPO3_DB']->sql_free_result($res);
                                 // Look for version number of the current:
                                 $subVer = $row['t3ver_id'] . '.' . ($highestVerNumber + 1);
                                 // Set up the values to override when making a raw-copy:
                                 $overrideArray = array('t3ver_id' => $highestVerNumber + 1, 't3ver_oid' => $id, 't3ver_label' => $label ? $label : $subVer . ' / ' . date('d-m-Y H:m:s'), 't3ver_wsid' => $this->BE_USER->workspace, 't3ver_state' => $delete ? 2 : 0, 't3ver_count' => 0, 't3ver_stage' => 0, 't3ver_tstamp' => 0);
                                 if ($TCA[$table]['ctrl']['editlock']) {
                                     $overrideArray[$TCA[$table]['ctrl']['editlock']] = 0;
                                 }
                                 if ($table === 'pages') {
                                     $overrideArray['t3ver_swapmode'] = $versionizeTree;
                                 }
                                 // Checking if the record already has a version in the current workspace of the backend user
                                 $workspaceCheck = TRUE;
                                 if ($this->BE_USER->workspace !== 0) {
                                     // Look for version already in workspace:
                                     $workspaceCheck = t3lib_BEfunc::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $id, 'uid') ? FALSE : TRUE;
                                 }
                                 if ($workspaceCheck) {
                                     // Create raw-copy and return result:
                                     return $this->copyRecord_raw($table, $id, -1, $overrideArray);
                                 } else {
                                     $this->newlog('Record "' . $table . ':' . $id . '" you wanted to versionize was already a version in the workspace (wsid=' . $this->BE_USER->workspace . ')!', 1);
                                 }
                             } else {
                                 $this->newlog('Record cannot be deleted: ' . $this->cannotDeleteRecord($table, $id), 1);
                             }
                         } else {
                             $this->newlog('Record cannot be versioned because it is a placeholder for a moving operation', 1);
                         }
                     } else {
                         $this->newlog('Record "' . $table . ':' . $id . '" you wanted to versionize was already a version in archive (pid=-1)!', 1);
                     }
                 } else {
                     $this->newlog('Record "' . $table . ':' . $id . '" you wanted to versionize did not exist!', 1);
                 }
             } else {
                 $this->newlog('The versioning type ' . $versionizeTree . ' mode you requested was not allowed', 1);
             }
         } else {
             $this->newlog('You didnt have correct permissions to make a new version (copy) of this record "' . $table . '" / ' . $id, 1);
         }
     } else {
         $this->newlog('Versioning is not supported for this table "' . $table . '" / ' . $id, 1);
     }
 }
 /**
  * Creates a new version of a page including content and possible subpages.
  *
  * @param	integer		Page uid to create new version of.
  * @param	string		Version label
  * @param	integer		Indicating "treeLevel" - "page" (0) or "branch" (>=1) ["element" type must call versionizeRecord() directly]
  * @return	void
  * @see copyPages()
  */
 function versionizePages($uid, $label, $versionizeTree)
 {
     global $TCA;
     $uid = intval($uid);
     $brExist = $this->doesBranchExist('', $uid, $this->pMap['show'], 1);
     // returns the branch
     if ($brExist != -1) {
         // Checks if we had permissions
         // Finding list of tables ALLOWED to be copied
         $allowedTablesArray = $this->admin ? $this->compileAdminTables() : explode(',', $this->BE_USER->groupData['tables_modify']);
         // These are the tables, the user may modify
         $allowedTablesArray = $this->compileAdminTables();
         // These are ALL tables because a new version should be ALL of them regardless of permission of the user executing the request.
         // Make list of tables that should come along with a new version of the page:
         $verTablesArray = array();
         $allTables = array_keys($TCA);
         foreach ($allTables as $tN) {
             if ($tN != 'pages' && ($versionizeTree > 0 || $TCA[$tN]['ctrl']['versioning_followPages']) && ($this->admin || in_array($tN, $allowedTablesArray))) {
                 $verTablesArray[] = $tN;
             }
         }
         // Begin to copy pages if we're allowed to:
         if ($this->admin || in_array('pages', $allowedTablesArray)) {
             if ($this->BE_USER->workspaceVersioningTypeAccess($versionizeTree)) {
                 // Versionize this page:
                 $theNewRootID = $this->versionizeRecord('pages', $uid, $label, FALSE, $versionizeTree);
                 if ($theNewRootID) {
                     $this->rawCopyPageContent($uid, $theNewRootID, $verTablesArray);
                     // If we're going to copy recursively...:
                     if ($versionizeTree > 0) {
                         // Get ALL subpages to copy (read permissions respected - they should NOT be...):
                         $CPtable = $this->int_pageTreeInfo(array(), $uid, intval($versionizeTree), $theNewRootID);
                         // Now copying the subpages:
                         foreach ($CPtable as $thePageUid => $thePagePid) {
                             $newPid = $this->copyMappingArray['pages'][$thePagePid];
                             if (isset($newPid)) {
                                 $theNewRootID = $this->copyRecord_raw('pages', $thePageUid, $newPid);
                                 $this->rawCopyPageContent($thePageUid, $theNewRootID, $verTablesArray);
                             } else {
                                 $this->newlog('Something went wrong during copying branch (for versioning)', 1);
                                 break;
                             }
                         }
                     }
                     // else the page was not copied. Too bad...
                 } else {
                     $this->newlog('The root version could not be created!', 1);
                 }
             } else {
                 $this->newlog('Versioning type "' . $versionizeTree . '" was not allowed in workspace', 1);
             }
         } else {
             $this->newlog('Attempt to versionize page without permission to this table', 1);
         }
     } else {
         $this->newlog('Could not read all subpages to versionize.', 1);
     }
 }