Removed the tree filter toggle
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_tcemain.php
index b67697e..e94c908 100755 (executable)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2007 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -231,7 +231,7 @@ require_once (PATH_t3lib.'class.t3lib_flexformtools.php');
  *
  * Dependencies:
  * - $GLOBALS['TCA'] must exist
- * - $GLOBALS['LANG'] (languageobject) may be preferred, but not fatal.
+ * - $GLOBALS['LANG'] must exist
  *
  * tce_db.php for further comments and SYNTAX! Also see document 'TYPO3 Core API' for details.
  *
@@ -256,6 +256,8 @@ class t3lib_TCEmain {
        var $deleteTree = FALSE;                                // Boolean. If this is set, then a page is deleted by deleting the whole branch under it (user must have deletepermissions to it all). If not set, then the page is deleted ONLY if it has no branch
        var $neverHideAtCopy = FALSE;                   // Boolean. If set, then the 'hideAtCopy' flag for tables will be ignored.
        var $dontProcessTransformations = FALSE;        // Boolean: If set, then transformations are NOT performed on the input.
+       var $clear_flexFormData_vDEFbase = FALSE;       // Boolean: If set, .vDEFbase values are unset in flexforms.
+       var $updateModeL10NdiffData = TRUE;             // Boolean/Mixed: TRUE: (traditional) Updates when record is saved. For flexforms, updates if change is made to the localized value. FALSE: Will not update anything. "FORCE_FFUPD" (string): Like TRUE, but will force update to the FlexForm Field
        var $bypassWorkspaceRestrictions = FALSE;       // Boolean: If true, workspace restrictions are bypassed on edit an create actions (process_datamap()). YOU MUST KNOW what you do if you use this feature!
        var $bypassFileHandling = FALSE;                        // Boolean: If true, file handling of attached files (addition, deletion etc) is bypassed - the value is saved straight away. YOU MUST KNOW what you are doing with this feature!
        var $bypassAccessCheckForRecords = FALSE;       // Boolean: If true, access check, check for deleted etc. for records is bypassed. YOU MUST KNOW what you are doing if you use this feature!
@@ -285,7 +287,9 @@ class t3lib_TCEmain {
        var $newRelatedIDs = Array();                           // Holds the tables and there the ids of newly created child records from IRRE
        var $copyMappingArray_merged = Array();         // This array is the sum of all copying operations in this class. May be READ from outside, thus partly public.
        var $copiedFileMap = Array();                           // A map between input file name and final destination for files being attached to records.
-       var     $errorLog = Array();                                    // Errors are collected in this variable.
+       var $RTEmagic_copyIndex = Array();                      // Contains [table][id][field] of fiels where RTEmagic images was copied. Holds old filename as key and new filename as value.
+       var $errorLog = Array();                                        // Errors are collected in this variable.
+       var $accumulateForNotifEmail = Array();         // For accumulating information about workspace stages raised on elements so a single mail is sent as notification.
 
 
 
@@ -294,7 +298,12 @@ class t3lib_TCEmain        {
                // *********************
 
                // Variables set in init() function:
-       var $BE_USER;           // The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
+       /**
+        * The user-object the script uses. If not set from outside, this is set to the current global $BE_USER.
+        *
+        * @var t3lib_beUserAuth
+        */
+       var $BE_USER;
        var $userid;            // will be set to uid of be_user executing this script
        var $username;          // will be set to username of be_user executing this script
        var $admin;                     // will be set if user is admin
@@ -333,12 +342,20 @@ class t3lib_TCEmain       {
        var $removeFilesStore=array();                          // For accumulation of files which must be deleted after processing of all input content
        var $uploadedFileArray = array();                       // Uploaded files, set by process_uploads()
        var $registerDBList=array();                            // Used for tracking references that might need correction after operations
+       var $registerDBPids=array();                            // Used for tracking references that might need correction in pid field after operations (e.g. IRRE)
        var $copyMappingArray = Array();                        // Used by the copy action to track the ids of new pages so subpages are correctly inserted! THIS is internally cleared for each executed copy operation! DO NOT USE THIS FROM OUTSIDE! Read from copyMappingArray_merged instead which is accumulating this information.
        var $remapStack = array();                                      // array used for remapping uids and values at the end of process_datamap
+       var $remapStackRecords = array();                       // array used for remapping uids and values at the end of process_datamap (e.g. $remapStackRecords[<table>][<uid>] = <index in $remapStack>)
        var $updateRefIndexStack = array();                     // array used for additional calls to $this->updateRefIndex
        var $callFromImpExp = false;                            // tells, that this TCEmain was called from tx_impext - this variable is set by tx_impexp
+       var $newIndexMap = array();                                     // Array for new flexform index mapping
 
                // Various
+       /**
+        * basicFileFunctions object
+        *
+        * @var t3lib_basicFileFunctions
+        */
        var $fileFunc;                                                          // For "singleTon" file-manipulation object
        var $checkValue_currentRecord=array();          // Set to "currentRecord" during checking of values.
        var $autoVersioningUpdate = FALSE;                      // A signal flag used to tell file processing that autoversioning has happend and hence certain action should be applied.
@@ -375,6 +392,10 @@ class t3lib_TCEmain        {
                if ($GLOBALS['BE_USER']->uc['recursiveDelete'])    {
                        $this->deleteTree = 1;
                }
+               
+               if ($GLOBALS['TYPO3_CONF_VARS']['BE']['explicitConfirmationOfTranslation'] && $this->updateModeL10NdiffData===TRUE)     {
+                       $this->updateModeL10NdiffData = FALSE;
+               }
 
                        // Initializing default permissions for pages
                $defaultPermissions = $GLOBALS['TYPO3_CONF_VARS']['BE']['defaultPermissions'];
@@ -515,6 +536,57 @@ class t3lib_TCEmain        {
 
        /*********************************************
         *
+        * HOOKS
+        *
+        *********************************************/
+
+       /**
+        * Hook: processDatamap_afterDatabaseOperations
+        * (calls $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);)
+        *
+        * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
+        *               but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
+        *
+        * @param       object          $hookObjectsArr: (reference) Array with hook objects
+        * @param       string          $status: (reference) Status of the current operation, 'new' or 'update
+        * @param       string          $table: (refrence) The table currently processing data for
+        * @param       string          $id: (reference) The record uid currently processing data for, [integer] or [string] (like 'NEW...')
+        * @param       array           $fieldArray: (reference) The field array of a record
+        * @return      void
+        */
+       function hook_processDatamap_afterDatabaseOperations(&$hookObjectsArr, &$status, &$table, &$id, &$fieldArray) {
+                       // Process hook directly:
+               if (!isset($this->remapStackRecords[$table][$id])) {
+                       foreach($hookObjectsArr as $hookObj)    {
+                               if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
+                                       $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
+                               }
+                       }
+                       // If this record is in remapStack (e.g. when using IRRE), values will be updated/remapped later on. So the hook will also be called later:
+               } else {
+                       $this->remapStackRecords[$table][$id]['processDatamap_afterDatabaseOperations'] = array(
+                               'status' => $status,
+                               'fieldArray' => $fieldArray,
+                               'hookObjectsArr' => $hookObjectsArr,
+                       );
+               }
+       }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+       /*********************************************
+        *
         * PROCESSING DATA
         *
         *********************************************/
@@ -701,7 +773,7 @@ class t3lib_TCEmain {
                                                                                $id = $this->autoVersionIdMap[$table][$id];
                                                                                $recordAccess = TRUE;
                                                                                $this->autoVersioningUpdate = TRUE;
-                                                                               
+
                                                                                // Checking access in case of offline workspace:
                                                                        } elseif (!$this->bypassWorkspaceRestrictions && $errorCode = $this->BE_USER->workspaceCannotEditRecord($table,$tempdata)) {
                                                                                $recordAccess = FALSE;          // Versioning is required and it must be offline version!
@@ -709,13 +781,14 @@ class t3lib_TCEmain       {
                                                                                        // Auto-creation of version: In offline workspace, test if versioning is enabled and look for workspace version of input record. If there is no versionized record found we will create one and save to that.
                                                                                if ($this->BE_USER->workspaceAllowAutoCreation($table,$id,$theRealPid)) {
                                                                                        $tce = t3lib_div::makeInstance('t3lib_TCEmain');
+                                                                                       /* @var $tce t3lib_TCEmain  */
                                                                                        $tce->stripslashes_values = 0;
 
                                                                                                // Setting up command for creating a new version of the record:
                                                                                        $cmd = array();
                                                                                        $cmd[$table][$id]['version'] = array(
                                                                                                'action' => 'new',
-                                                                                               'treeLevels' => -1,     // Default is to create a version of the individual records...
+                                                                                               'treeLevels' => -1,     // Default is to create a version of the individual records... element versioning that is.
                                                                                                'label' => 'Auto-created for WS #'.$this->BE_USER->workspace
                                                                                        );
                                                                                        $tce->start(array(),$cmd);
@@ -730,13 +803,14 @@ class t3lib_TCEmain       {
                                                                                                                $this->autoVersionIdMap[$origTable][$origId] = $newId;
                                                                                                        }
                                                                                                }
+                                                                                               $this->RTEmagic_copyIndex = t3lib_div::array_merge_recursive_overrule($this->RTEmagic_copyIndex, $tce->RTEmagic_copyIndex);             // See where RTEmagic_copyIndex is used inside fillInFieldArray() for more information...
 
                                                                                                        // Update registerDBList, that holds the copied relations to child records:
                                                                                                $registerDBList = array_merge($registerDBList, $tce->registerDBList);
                                                                                                        // For the reason that creating a new version of this record, automatically
                                                                                                        // created related child records (e.g. "IRRE"), update the accordant field:
                                                                                                $this->getVersionizedIncomingFieldArray($table, $id, $incomingFieldArray, $registerDBList);
-                                                                                               
+
                                                                                                        // Use the new id of the copied/versionized record:
                                                                                                $id = $this->autoVersionIdMap[$table][$id];
                                                                                                $recordAccess = TRUE;
@@ -845,11 +919,7 @@ class t3lib_TCEmain        {
                                                                 * Note: When using the hook after INSERT operations, you will only get the temporary NEW... id passed to your hook as $id,
                                                                 *               but you can easily translate it to the real uid of the inserted record using the $this->substNEWwithIDs array.
                                                                 */
-                                                       foreach($hookObjectsArr as $hookObj)    {
-                                                               if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
-                                                                       $hookObj->processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, $this);
-                                                               }
-                                                       }
+                                                       $this->hook_processDatamap_afterDatabaseOperations($hookObjectsArr, $status, $table, $id, $fieldArray);
                                                }       // if ($recordAccess)   {
                                        }       // if (is_array($incomingFieldArray))   {
                                }
@@ -858,9 +928,19 @@ class t3lib_TCEmain        {
 
                        // Process the stack of relations to remap/correct
                $this->processRemapStack();
-
                $this->dbAnalysisStoreExec();
                $this->removeRegisteredFiles();
+
+               /*
+                * Hook: processDatamap_afterAllOperations
+                * 
+                * Note: When this hook gets called, all operations on the submitted data have been finished.
+                */
+               foreach($hookObjectsArr as $hookObj) {
+                       if (method_exists($hookObj, 'processDatamap_afterAllOperations')) {
+                               $hookObj->processDatamap_afterAllOperations($this);
+                       }
+               }
        }
 
        /**
@@ -875,7 +955,7 @@ class t3lib_TCEmain {
 
                t3lib_div::loadTCA($table);
                if ($liveRec = t3lib_BEfunc::getLiveVersionOfRecord($table,$id,'*'))    {
-                       if ((int)$liveRec['t3ver_state']===1)   {
+                       if ((int)$liveRec['t3ver_state']>0)     {
                                $justStoredRecord = t3lib_BEfunc::getRecord($table,$id);
                                $newRecord = array();
 
@@ -905,13 +985,13 @@ class t3lib_TCEmain       {
         * $this->exclude_array is used to filter fields if needed.
         *
         * @param       string          Table name
-        * @param       [type]          $id: ...
+        * @param       integer         Record ID
         * @param       array           Default values, Preset $fieldArray with 'pid' maybe (pid and uid will be not be overridden anyway)
         * @param       array           $incomingFieldArray is which fields/values you want to set. There are processed and put into $fieldArray if OK
         * @param       integer         The real PID value of the record. For updates, this is just the pid of the record. For new records this is the PID of the page where it is inserted.
         * @param       string          $status = 'new' or 'update'
-        * @param       [type]          $tscPID: ...
-        * @return      [type]          ...
+        * @param       integer         $tscPID: TSconfig PID
+        * @return      array           Field Array
         */
        function fillInFieldArray($table,$id,$fieldArray,$incomingFieldArray,$realPid,$status,$tscPID)  {
                global $TCA;
@@ -1021,11 +1101,25 @@ class t3lib_TCEmain     {
                                                                        $fieldArray[$field]=$res['value'];
 
                                                                                // Add the value of the original record to the diff-storage content:
-                                                                       if ($TCA[$table]['ctrl']['transOrigDiffSourceField'])   {
+                                                                       if ($this->updateModeL10NdiffData && $TCA[$table]['ctrl']['transOrigDiffSourceField'])  {
                                                                                $originalLanguage_diffStorage[$field] = $originalLanguageRecord[$field];
                                                                                $diffStorageFlag = TRUE;
                                                                        }
                                                                }
+
+
+                                                                       // If autoversioning is happening we need to perform a nasty hack. The case is parallel to a similar hack inside checkValue_group_select_file().
+                                                                       // When a copy or version is made of a record, a search is made for any RTEmagic* images in fields having the "images" soft reference parser applied. That should be true for RTE fields. If any are found they are duplicated to new names and the file reference in the bodytext is updated accordingly.
+                                                                       // However, with auto-versioning the submitted content of the field will just overwrite the corrected values. This leaves a) lost RTEmagic files and b) creates a double reference to the old files.
+                                                                       // The only solution I can come up with is detecting when auto versioning happens, then see if any RTEmagic images was copied and if so make a stupid string-replace of the content !
+                                                               if ($this->autoVersioningUpdate===TRUE) {
+                                                                       if (is_array($this->RTEmagic_copyIndex[$table][$id][$field]))   {
+                                                                               foreach($this->RTEmagic_copyIndex[$table][$id][$field] as $oldRTEmagicName => $newRTEmagicName) {
+                                                                                       $fieldArray[$field] = str_replace(' src="'.$oldRTEmagicName.'"',' src="'.$newRTEmagicName.'"',$fieldArray[$field]);
+                                                                               }
+                                                                       }
+                                                               }
+
                                                        } elseif ($TCA[$table]['ctrl']['origUid']===$field) {   // Allow value for original UID to pass by...
                                                                $fieldArray[$field] = $fieldValue;
                                                        }
@@ -1034,7 +1128,6 @@ class t3lib_TCEmain       {
                                }       // Checking language.
                        }       // Check exclude fields / disabled fields...
                }
-
                        // Add diff-storage information:
                if ($diffStorageFlag && !isset($fieldArray[$TCA[$table]['ctrl']['transOrigDiffSourceField']]))  {       // If the field is set it would probably be because of an undo-operation - in which case we should not update the field of course...
                         $fieldArray[$TCA[$table]['ctrl']['transOrigDiffSourceField']] = serialize($originalLanguage_diffStorage);
@@ -1078,6 +1171,7 @@ class t3lib_TCEmain       {
                                        $mixedRec = array_merge($currentRecord,$fieldArray);
                                        $SW_fileContent = t3lib_div::getUrl($eFile['editFile']);
                                        $parseHTML = t3lib_div::makeInstance('t3lib_parsehtml_proc');
+                                       /* @var $parseHTML t3lib_parsehtml_proc */
                                        $parseHTML->init('','');
 
                                        $eFileMarker = $eFile['markerField']&&trim($mixedRec[$eFile['markerField']]) ? trim($mixedRec[$eFile['markerField']]) : '###TYPO3_STATICFILE_EDIT###';
@@ -1406,6 +1500,7 @@ class t3lib_TCEmain       {
                if ($tcaFieldConf['type']=='select' && $tcaFieldConf['foreign_table'])  {
                                // check, if there is a NEW... id in the value, that should be substituded later
                        if (strpos($value, 'NEW') !== false) {
+                               $this->remapStackRecords[$table][$id] = array('remapStackIndex' => count($this->remapStack));
                                $this->remapStack[] = array(
                                        'func' => 'checkValue_group_select_processDBdata',
                                        'args' => array($valueArray,$tcaFieldConf,$id,$status,'select',$table),
@@ -1485,7 +1580,7 @@ class t3lib_TCEmain       {
                                                // Actually, the same problem applies to database references in case auto-versioning would include sub-records since in such a case references are remapped - and they would be overridden due to the same principle then.
                                                // Illustration of the problem comes here:
                                                // We have a record 123 with a file logo.gif. We open and edit the files header in a workspace. So a new version is automatically made.
-                                               // The versions uid is 456 and the file is copied to "logo_01.gif". But the form data that we sents was based on uid 123 and hence contains the filename "logo.gif" from the original.
+                                               // The versions uid is 456 and the file is copied to "logo_01.gif". But the form data that we sent was based on uid 123 and hence contains the filename "logo.gif" from the original.
                                                // The file management code below will do two things: First it will blindly accept "logo.gif" as a file attached to the record (thus creating a double reference) and secondly it will find that "logo_01.gif" was not in the incoming filelist and therefore should be deleted.
                                                // If we prefix the incoming file "logo.gif" with its absolute path it will be seen as a new file added. Thus it will be copied to "logo_02.gif". "logo_01.gif" will still be deleted but since the files are the same the difference is zero - only more processing and file copying for no reason. But it will work.
                                        if ($this->autoVersioningUpdate===TRUE) {
@@ -1500,6 +1595,7 @@ class t3lib_TCEmain       {
                                        $theFileValues=array();
                                        if ($tcaFieldConf['MM'])        {       // If MM relations for the files also!
                                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                                               /* @var $dbAnalysis t3lib_loadDBGroup */
                                                $dbAnalysis->start('','files',$tcaFieldConf['MM'],$id);
                                                reset($dbAnalysis->itemArray);
                                                while (list($somekey,$someval)=each($dbAnalysis->itemArray))    {
@@ -1585,6 +1681,7 @@ class t3lib_TCEmain       {
                                // If MM relations for the files, we will set the relations as MM records and change the valuearray to contain a single entry with a count of the number of files!
                        if ($tcaFieldConf['MM'])        {
                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                               /* @var $dbAnalysis t3lib_loadDBGroup */
                                $dbAnalysis->tableArray['files']=array();       // dummy
 
                                reset($valueArray);
@@ -1652,27 +1749,11 @@ class t3lib_TCEmain     {
                                $xmlValue = $this->checkValue_flexArray2Xml($arrValue,TRUE);
                        }
 
-                               // Temporary fix to delete flex form elements:
-                       $deleteCMDs = t3lib_div::_GP('_DELETE_FLEX_FORMdata');
-                       if (is_array($deleteCMDs[$table][$id][$field]['data'])) {
-                               $arrValue = t3lib_div::xml2array($xmlValue);
-                               $this->_DELETE_FLEX_FORMdata($arrValue['data'],$deleteCMDs[$table][$id][$field]['data']);
-                               $xmlValue = $this->checkValue_flexArray2Xml($arrValue,TRUE);
-                       }
-
-                               // Temporary fix to move flex form elements up:
-                       $moveCMDs = t3lib_div::_GP('_MOVEUP_FLEX_FORMdata');
-                       if (is_array($moveCMDs[$table][$id][$field]['data']))   {
-                               $arrValue = t3lib_div::xml2array($xmlValue);
-                               $this->_MOVE_FLEX_FORMdata($arrValue['data'],$moveCMDs[$table][$id][$field]['data'], 'up');
-                               $xmlValue = $this->checkValue_flexArray2Xml($arrValue,TRUE);
-                       }
-
-                               // Temporary fix to move flex form elements down:
-                       $moveCMDs = t3lib_div::_GP('_MOVEDOWN_FLEX_FORMdata');
-                       if (is_array($moveCMDs[$table][$id][$field]['data']))   {
+                               // Action commands (sorting order and removals of elements)
+                       $actionCMDs = t3lib_div::_GP('_ACTION_FLEX_FORMdata');
+                       if (is_array($actionCMDs[$table][$id][$field]['data'])) {
                                $arrValue = t3lib_div::xml2array($xmlValue);
-                               $this->_MOVE_FLEX_FORMdata($arrValue['data'],$moveCMDs[$table][$id][$field]['data'], 'down');
+                               $this->_ACTION_FLEX_FORMdata($arrValue['data'],$actionCMDs[$table][$id][$field]['data']);
                                $xmlValue = $this->checkValue_flexArray2Xml($arrValue,TRUE);
                        }
 
@@ -1695,62 +1776,38 @@ class t3lib_TCEmain     {
         */
        function checkValue_flexArray2Xml($array, $addPrologue=FALSE)   {
                $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
+               /* @var $flexObj t3lib_flexformtools */
                return $flexObj->flexArray2Xml($array, $addPrologue);
        }
 
        /**
-        * Deletes a flex form element
+        * Actions for flex form element (move, delete)
         *
         * @param       array           &$valueArrayToRemoveFrom: by reference
         * @param       array           $deleteCMDS: ...         *
         * @return      void
         */
-       function _DELETE_FLEX_FORMdata(&$valueArrayToRemoveFrom,$deleteCMDS)    {
-               if (is_array($valueArrayToRemoveFrom) && is_array($deleteCMDS)) {
-                       foreach($deleteCMDS as $key => $value)  {
-                               if (is_array($deleteCMDS[$key]))        {
-                                       $this->_DELETE_FLEX_FORMdata($valueArrayToRemoveFrom[$key],$deleteCMDS[$key]);
-                               } else {
-                                       unset($valueArrayToRemoveFrom[$key]);
-                               }
-                       }
-               }
-       }
-
-       /**
-        * Deletes a flex form element
-        *
-        * TODO: Like _DELETE_FLEX_FORMdata, this is only a temporary solution!
-        *
-        * @param       array           &$valueArrayToMoveIn: by reference
-        * @param       array           $moveCMDS: ...   *
-        * @param       string          $direction: 'up' or 'down'
-        * @return      void
-        */
-       function _MOVE_FLEX_FORMdata(&$valueArrayToMoveIn, $moveCMDS, $direction)       {
-               if (is_array($valueArrayToMoveIn) && is_array($moveCMDS))       {
-
-                               // Only execute the first move command:
-                       list ($key, $value) = each ($moveCMDS);
-
-                       if (is_array($moveCMDS[$key]))  {
-                               $this->_MOVE_FLEX_FORMdata($valueArrayToMoveIn[$key],$moveCMDS[$key], $direction);
-                       } else {
-                               switch ($direction) {
-                                       case 'up':
-                                               if ($key > 1) {
-                                                       $tmpArr = $valueArrayToMoveIn[$key];
-                                                       $valueArrayToMoveIn[$key] = $valueArrayToMoveIn[$key-1];
-                                                       $valueArrayToMoveIn[$key-1] = $tmpArr;
-                                               }
-                                       break;
-                                       case 'down':
-                                               if ($key < count($valueArrayToMoveIn)) {
-                                                       $tmpArr = $valueArrayToMoveIn[$key];
-                                                       $valueArrayToMoveIn[$key] = $valueArrayToMoveIn[$key+1];
-                                                       $valueArrayToMoveIn[$key+1] = $tmpArr;
+       function _ACTION_FLEX_FORMdata(&$valueArray,$actionCMDs)        {
+               if (is_array($valueArray) && is_array($actionCMDs))     {
+                       foreach($actionCMDs as $key => $value)  {
+                               if ($key=='_ACTION')    {
+                                               // First, check if there are "commands":
+                                       if (current($actionCMDs[$key])!=="")    {
+                                               asort($actionCMDs[$key]);
+                                               $newValueArray = array();
+                                               foreach($actionCMDs[$key] as $idx => $order)    {
+                                                       if (substr($idx,0,3)=="ID-")    {
+                                                               $idx = $this->newIndexMap[$idx];
+                                                       }
+                                                       if ($order!="DELETE")   {       // Just one reflection here: It is clear that when removing elements from a flexform, then we will get lost files unless we act on this delete operation by traversing and deleting files that were referred to.
+                                                               $newValueArray[$idx] = $valueArray[$idx];
+                                                       }
+                                                       unset($valueArray[$idx]);
                                                }
-                                       break;
+                                               $valueArray = t3lib_div::array_merge($newValueArray,$valueArray);
+                                       }
+                               } elseif (is_array($actionCMDs[$key]) && isset($valueArray[$key]))      {
+                                       $this->_ACTION_FLEX_FORMdata($valueArray[$key],$actionCMDs[$key]);
                                }
                        }
                }
@@ -1768,7 +1825,7 @@ class t3lib_TCEmain       {
         * @return      array           Modified $res array
         */
        function checkValue_inline($res,$value,$tcaFieldConf,$PP,$field)        {
-               list($table,$id,$curValue,$status,$realPid,$recFID) = $PP;
+               list($table, $id, $curValue, $status, $realPid, $recFID) = $PP;
 
                if (!$tcaFieldConf['foreign_table'])    {
                        return false;   // Fatal error, inline fields should always have a foreign_table defined
@@ -1784,18 +1841,16 @@ class t3lib_TCEmain     {
                        // $value = 45,NEW4555fdf59d154,12,123
                        // We need to decide whether we use the stack or can save the relation directly.
                if(strpos($value, 'NEW') !== false || !t3lib_div::testInt($id)) {
+                       $this->remapStackRecords[$table][$id] = array('remapStackIndex' => count($this->remapStack));
                        $this->remapStack[] = array(
-                               'func' => 'checkValue_group_select_processDBdata',
-                               'args' => array($valueArray,$tcaFieldConf,$id,$status,'inline',$table),
-                               'pos' => array('valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 5),
+                               'func' => 'checkValue_inline_processDBdata',
+                               'args' => array($valueArray, $tcaFieldConf, $id, $status, $table, $field),
+                               'pos' => array('valueArray' => 0, 'tcaFieldConf' => 1, 'id' => 2, 'table' => 4),
                                'field' => $field
                        );
                        unset($res['value']);
                } elseif($value || t3lib_div::testInt($id)) {
-                       $newValueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'inline', $table);
-                               // Checking that the number of items is correct
-                       $newVal = $this->checkValue_checkMax($tcaFieldConf, $newValueArray);
-                       $res['value'] = implode(',',$newVal);
+                       $res['value'] = $this->checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field);
                }
 
                return $res;
@@ -1908,12 +1963,17 @@ class t3lib_TCEmain     {
                        switch($func)   {
                                case 'int':
                                case 'year':
-                               case 'date':
-                               case 'datetime':
                                case 'time':
                                case 'timesec':
                                        $value = intval($value);
                                break;
+                               case 'date':
+                               case 'datetime':
+                                       $value = intval($value);
+                                       if ($value>0)   {
+                                               $value -= date('Z');
+                                       }
+                               break;
                                case 'double2':
                                        $theDec = 0;
                                        for ($a=strlen($value); $a>0; $a--)     {
@@ -1933,12 +1993,10 @@ class t3lib_TCEmain     {
                                        $value = trim($value);
                                break;
                                case 'upper':
-                                       $value = strtoupper($value);
-#                                      $value = strtr($value, '', ''); // WILL make trouble with other charsets than ISO-8859-1, so what do we do here? PHP-function which can handle this for other charsets? Currently the browsers JavaScript will fix it.
+                                       $value = $GLOBALS['LANG']->csConvObj->conv_case($GLOBALS['LANG']->charSet, $value, 'toUpper');
                                break;
                                case 'lower':
-                                       $value = strtolower($value);
-#                                      $value = strtr($value, '', ''); // WILL make trouble with other charsets than ISO-8859-1, so what do we do here? PHP-function which can handle this for other charsets? Currently the browsers JavaScript will fix it.
+                                       $value = $GLOBALS['LANG']->csConvObj->conv_case($GLOBALS['LANG']->charSet, $value, 'toLower');
                                break;
                                case 'required':
                                        if (!$value)    {$set=0;}
@@ -2001,6 +2059,7 @@ class t3lib_TCEmain       {
                $prep = $type=='group'?$tcaFieldConf['prepend_tname']:$tcaFieldConf['neg_foreign_table'];
 
                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+               /* @var $dbAnalysis t3lib_loadDBGroup */
                $dbAnalysis->registerNonTableValues=$tcaFieldConf['allowNonIdValues'] ? 1 : 0;
                $dbAnalysis->start(implode(',',$valueArray),$tables, '', 0, $currentTable, $tcaFieldConf);
 
@@ -2008,23 +2067,9 @@ class t3lib_TCEmain      {
                        if ($status=='update')  {
                                $dbAnalysis->writeMM($tcaFieldConf['MM'],$id,$prep);
                        } else {
-                               $this->dbAnalysisStore[] = array($dbAnalysis,$tcaFieldConf['MM'],$id,$prep);    // This will be traversed later to execute the actions
+                               $this->dbAnalysisStore[] = array($dbAnalysis,$tcaFieldConf['MM'],$id,$prep,$currentTable);      // This will be traversed later to execute the actions
                        }
                        $valueArray = $dbAnalysis->countItems();
-               } elseif ($type == 'inline') {
-                       if ($tcaFieldConf['foreign_field']) {
-                                       // if the record was imported, sorting was also imported, so skip this
-                               $skipSorting = $this->callFromImpExp ? true : false;
-                                       // update record in intermediate table (sorting & pointer uid to parent record)
-                               $dbAnalysis->writeForeignField($tcaFieldConf, $id, 0, $skipSorting);
-                               $valueArray = $dbAnalysis->countItems();
-                       } else {
-                               $valueArray = $dbAnalysis->getValueArray($prep);
-                               if ($prep) {
-                                               // @TODO: Do we want to support relations to multiple tables in Comma Separated Lists?
-                                       $valueArray = $dbAnalysis->convertPosNeg($valueArray,$tcaFieldConf['foreign_table'],$tcaFieldConf['neg_foreign_table']);
-                               }
-                       }
                } else {
                        $valueArray = $dbAnalysis->getValueArray($prep);
                        if ($type=='select' && $prep)   {
@@ -2113,18 +2158,34 @@ class t3lib_TCEmain     {
                                if ($DSelements[$key]['type']=='array') {
                                        if (is_array($dataValues[$key]['el']))  {
                                                if ($DSelements[$key]['section'])       {
+                                                       $newIndexCounter=0;
                                                        foreach($dataValues[$key]['el'] as $ik => $el)  {
-                                                               $theKey = key($el);
-                                                               if (is_array($dataValues[$key]['el'][$ik][$theKey]['el']))      {
-                                                                       $this->checkValue_flex_procInData_travDS(
-                                                                                       $dataValues[$key]['el'][$ik][$theKey]['el'],
-                                                                                       $dataValues_current[$key]['el'][$ik][$theKey]['el'],
-                                                                                       $uploadedFiles[$key]['el'][$ik][$theKey]['el'],
-                                                                                       $DSelements[$key]['el'][$theKey]['el'],
-                                                                                       $pParams,
-                                                                                       $callBackFunc,
-                                                                                       $structurePath.$key.'/el/'.$ik.'/'.$theKey.'/el/'
-                                                                               );
+                                                               if (is_array($el))      {
+                                                                       if (!is_array($dataValues_current[$key]['el'])) $dataValues_current[$key]['el']=array();
+                                                                       
+                                                                       $theKey = key($el);
+
+                                                                       if (is_array($dataValues[$key]['el'][$ik][$theKey]['el']))      {
+                                                                               $this->checkValue_flex_procInData_travDS(
+                                                                                               $dataValues[$key]['el'][$ik][$theKey]['el'],
+                                                                                               is_array($dataValues_current[$key]['el'][$ik]) ? $dataValues_current[$key]['el'][$ik][$theKey]['el'] : array(),
+                                                                                               $uploadedFiles[$key]['el'][$ik][$theKey]['el'],
+                                                                                               $DSelements[$key]['el'][$theKey]['el'],
+                                                                                               $pParams,
+                                                                                               $callBackFunc,
+                                                                                               $structurePath.$key.'/el/'.$ik.'/'.$theKey.'/el/'
+                                                                                       );
+                                                                               
+                                                                                       // If element is added dynamically in the flexform of TCEforms, we map the ID-string to the next numerical index we can have in that particular section of elements:
+                                                                                       // The fact that the order changes is not important since order is controlled by a separately submitted index.
+
+                                                                               if (substr($ik,0,3)=="ID-")     {
+                                                                                       $newIndexCounter++;
+                                                                                       $this->newIndexMap[$ik] = (is_array($dataValues_current[$key]['el'])&&count($dataValues_current[$key]['el'])?max(array_keys($dataValues_current[$key]['el'])):0)+$newIndexCounter;      // Set mapping index
+                                                                                       $dataValues[$key]['el'][$this->newIndexMap[$ik]] = $dataValues[$key]['el'][$ik];        // Transfer values
+                                                                                       unset($dataValues[$key]['el'][$ik]);    // Unset original
+                                                                               }
+                                                                       }
                                                                }
                                                        }
                                                } else {
@@ -2160,7 +2221,8 @@ class t3lib_TCEmain       {
                                                                                                $dsConf['TCEforms']['config'],
                                                                                                $dataValues[$key][$vKey],
                                                                                                $dataValues_current[$key][$vKey],
-                                                                                               $uploadedFiles[$key][$vKey]
+                                                                                               $uploadedFiles[$key][$vKey],
+                                                                                               $structurePath.$key.'/'.$vKey.'/'
                                                                                        );
                                                                }
                                                        } else {        // Default
@@ -2214,6 +2276,23 @@ class t3lib_TCEmain      {
                                                        if (isset($res['value']))       {
                                                                $dataValues[$key][$vKey] = $res['value'];
                                                        }
+
+                                                               // Finally, check if new and old values are different (or no .vDEFbase value is found) and if so, we record the vDEF value for diff'ing.
+                                                               // We do this after $dataValues has been updated since I expect that $dataValues_current holds evaluated values from database (so this must be the right value to compare with).
+                                                       if (substr($vKey,-9)!='.vDEFbase')      {
+                                                               if ($this->clear_flexFormData_vDEFbase) {
+                                                                       $dataValues[$key][$vKey.'.vDEFbase'] = '';
+                                                               } elseif ($this->updateModeL10NdiffData && $GLOBALS['TYPO3_CONF_VARS']['BE']['flexFormXMLincludeDiffBase'] && $vKey!=='vDEF' && (strcmp($dataValues[$key][$vKey],$dataValues_current[$key][$vKey]) || !isset($dataValues_current[$key][$vKey.'.vDEFbase']) || $this->updateModeL10NdiffData==='FORCE_FFUPD'))   {
+                                                                               // Now, check if a vDEF value is submitted in the input data, if so we expect this has been processed prior to this operation (normally the case since those fields are higher in the form) and we can use that:
+                                                                       if (isset($dataValues[$key]['vDEF']))   {
+                                                                               $diffValue = $dataValues[$key]['vDEF'];
+                                                                       } else {        // If not found (for translators with no access to the default language) we use the one from the current-value data set:
+                                                                               $diffValue = $dataValues_current[$key]['vDEF'];
+                                                                       }
+                                                                               // Setting the reference value for vDEF for this translation. This will be used for translation tools to make a diff between the vDEF and vDEFbase to see if an update would be fitting.
+                                                                       $dataValues[$key][$vKey.'.vDEFbase'] = $diffValue;
+                                                               }
+                                                       }
                                                }
                                        }
                                }
@@ -2221,7 +2300,66 @@ class t3lib_TCEmain      {
                }
        }
 
+       /**
+        * Returns data for inline fields.
+        *
+        * @param       array           Current value array
+        * @param       array           TCA field config
+        * @param       integer         Record id
+        * @param       string          Status string ('update' or 'new')
+        * @param       string          Table name, needs to be passed to t3lib_loadDBGroup
+        * @param       string          The current field the values are modified for
+        * @return      string          Modified values
+        */
+       protected function checkValue_inline_processDBdata($valueArray, $tcaFieldConf, $id, $status, $table, $field)    {
+               $newValue = '';
+               $foreignTable = $tcaFieldConf['foreign_table'];
 
+               /*
+                * Fetch the related child records by using t3lib_loadDBGroup:
+                * @var $dbAnalysis t3lib_loadDBGroup
+                */
+               $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+               $dbAnalysis->start(implode(',', $valueArray), $foreignTable, '', 0, $table, $tcaFieldConf);
+                       // If the localizazionMode is set to 'keep', the children for the localized parent are kept as in the original untranslated record:
+               $localizationMode = t3lib_BEfunc::getInlineLocalizationMode($table, $tcaFieldConf);
+               if ($localizationMode=='keep' && $status=='update') {
+                               // Fetch the current record and determine the original record:
+                       $row = t3lib_BEfunc::getRecordWSOL($table, $id);
+                       if (is_array($row)) {
+                               $language = intval($row[$GLOBALS['TCA'][$table]['ctrl']['languageField']]);
+                               $transOrigPointer = intval($row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]);
+                                       // If language is set (e.g. 1) and also transOrigPointer (e.g. 123), use transOrigPointer as uid:
+                               if ($language>0 && $transOrigPointer) {
+                                       $id = $transOrigPointer;
+                                               // If we're in active localizationMode 'keep', prevent from writing data to the field of the parent record:
+                                               // (on removing the localized parent, the original (untranslated) children would then also be removed)
+                                       $keepTranslation = true;
+                               }
+                       }
+               }
+                       // IRRE with a pointer field (database normalization): 
+               if ($tcaFieldConf['foreign_field']) {
+                               // if the record was imported, sorting was also imported, so skip this
+                       $skipSorting = ($this->callFromImpExp ? true : false);
+                               // update record in intermediate table (sorting & pointer uid to parent record)
+                       $dbAnalysis->writeForeignField($tcaFieldConf, $id, 0, $skipSorting);
+                       $newValue = ($keepTranslation ? 0 : $dbAnalysis->countItems(false));
+                       // IRRE with comma separated values:
+               } else {
+                       $valueArray = $dbAnalysis->getValueArray();
+                               // Checking that the number of items is correct:
+                       $valueArray = $this->checkValue_checkMax($tcaFieldConf, $valueArray);
+                               // If a valid translation of the 'keep' mode is active, update relations in the original(!) record:
+                       if ($keepTranslation) {
+                               $this->updateDB($table, $transOrigPointer, array($field => implode(',', $valueArray)));
+                       } else {
+                               $newValue = implode(',', $valueArray);
+                       }
+               }
+
+               return $newValue;
+       }
 
 
 
@@ -2265,6 +2403,9 @@ class t3lib_TCEmain       {
                                $hookObjectsArr[] = &t3lib_div::getUserObj($classRef);
                        }
                }
+#debug($this->cmdmap);
+               
+               $this->accumulateForNotifEmail = array();       // Reset notification array
 
                        // Traverse command map:
                reset($this->cmdmap);
@@ -2313,6 +2454,9 @@ class t3lib_TCEmain       {
                                                        case 'localize':
                                                                $this->localize($table,$id,$value);
                                                        break;
+                                                       case 'inlineLocalizeSynchronize':
+                                                               $this->inlineLocalizeSynchronize($table, $id, $value);
+                                                       break;
                                                        case 'version':
                                                                switch ((string)$value['action'])       {
                                                                        case 'new':
@@ -2344,7 +2488,11 @@ class t3lib_TCEmain      {
                                                                        case 'clearWSID':
                                                                                $this->version_clearWSID($table,$id);
                                                                        break;
+                                                                       case 'flush':
+                                                                               $this->version_clearWSID($table,$id,TRUE);
+                                                                       break;
                                                                        case 'setStage':
+                                                                               $elementList = array();
                                                                                $idList = $elementList[$table] = t3lib_div::trimExplode(',',$id,1);
                                                                                $setStageMode = $GLOBALS['BE_USER']->getTSConfigVal('options.workspaces.changeStageMode');
                                                                                if ($setStageMode == 'any' || $setStageMode == 'page') {
@@ -2369,9 +2517,10 @@ class t3lib_TCEmain      {
                                                                                                $this->findPageElementsForVersionStageChange($idList, $workspaceId, $elementList);
                                                                                        }
                                                                                }
+                                                                               
                                                                                foreach ($elementList as $tbl => $elementIdList) {
                                                                                        foreach($elementIdList as $id)  {
-                                                                                               $this->version_setStage($tbl,$id,$value['stageId'],$value['comment']?$value['comment']:$this->generalComment);
+                                                                                               $this->version_setStage($tbl,$id,$value['stageId'],$value['comment']?$value['comment']:$this->generalComment, TRUE);
                                                                                        }
                                                                                }
                                                                        break;
@@ -2400,6 +2549,16 @@ class t3lib_TCEmain      {
 
                        // Finally, before exit, check if there are ID references to remap. This might be the case if versioning or copying has taken place!
                $this->remapListedDBRecords();
+
+
+                       // Empty accumulation array:
+               foreach($this->accumulateForNotifEmail as $notifItem)   {
+                       $this->notifyStageChange($notifItem['shared'][0],$notifItem['shared'][1],implode(', ',$notifItem['elements']),0,$notifItem['shared'][2]);
+               }
+
+               $this->accumulateForNotifEmail = array();       // Reset notification array
+               
+#              die("REMOVE ME");
        }
 
 
@@ -2427,13 +2586,15 @@ class t3lib_TCEmain     {
         * @param       boolean         $first is a flag set, if the record copied is NOT a 'slave' to another record copied. That is, if this record was asked to be copied in the cmd-array
         * @param       array           Associative array with field/value pairs to override directly. Notice; Fields must exist in the table record and NOT be among excluded fields!
         * @param       string          Commalist of fields to exclude from the copy process (might get default values)
+        * @param       integer         Language ID (from sys_language table)
         * @return      integer         ID of new record, if any
         */
-       function copyRecord($table,$uid,$destPid,$first=0,$overrideValues=array(),$excludeFields='')    {
+       function copyRecord($table, $uid, $destPid, $first=0, $overrideValues=array(), $excludeFields='', $language=0) {
                global $TCA;
 
                $uid = $origUid = intval($uid);
-               if ($TCA[$table] && $uid)       {
+                       // Only copy if the table is defined in TCA, a uid is given and the record wasn't copied before:
+               if ($TCA[$table] && $uid && !$this->isRecordCopied($table, $uid))       {
                        t3lib_div::loadTCA($table);
 /*
                                // In case the record to be moved turns out to be an offline version, we have to find the live version and work on that one (this case happens for pages with "branch" versioning type)
@@ -2464,7 +2625,6 @@ class t3lib_TCEmain       {
                                        $defaultData = $this->newFieldArray($table);
 
                                                // Getting "copy-after" fields if applicable:
-                                               // origDestPid is retrieve before it may possibly be converted to resolvePid if the table is not sorted anyway. In this way, copying records to after another records which are not sorted still lets you use this function in order to copy fields from the one before.
                                        $copyAfterFields = $destPid<0 ? $this->fixCopyAfterDuplFields($table,$uid,abs($destPid),0) : array();
 
                                                // Page TSconfig related:
@@ -2498,7 +2658,7 @@ class t3lib_TCEmain       {
                                                                        $value = $this->getCopyHeader($table,$this->resolvePid($table,$destPid),$field,$this->clearPrefixFromValue($table,$value),0);
                                                                }
                                                                        // Processing based on the TCA config field type (files, references, flexforms...)
-                                                               $value = $this->copyRecord_procBasedOnFieldType($table,$uid,$field,$value,$row,$conf,$tscPID);
+                                                               $value = $this->copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $tscPID, $language);
                                                        }
 
                                                                // Add value to array.
@@ -2518,6 +2678,7 @@ class t3lib_TCEmain       {
 
                                                // Do the copy by simply submitting the array through TCEmain:
                                        $copyTCE = t3lib_div::makeInstance('t3lib_TCEmain');
+                                       /* @var $copyTCE t3lib_TCEmain  */
                                        $copyTCE->stripslashes_values = 0;
                                        $copyTCE->copyTree = $this->copyTree;
                                        $copyTCE->cachedTSconfig = $this->cachedTSconfig;       // Copy forth the cached TSconfig
@@ -2529,6 +2690,7 @@ class t3lib_TCEmain       {
                                                // Getting the new UID:
                                        $theNewSQLID = $copyTCE->substNEWwithIDs[$theNewID];
                                        if ($theNewSQLID)       {
+                                               $this->copyRecord_fixRTEmagicImages($table,t3lib_BEfunc::wsMapId($table,$theNewSQLID));
                                                $this->copyMappingArray[$table][$origUid] = $theNewSQLID;
                                        }
 
@@ -2643,7 +2805,8 @@ class t3lib_TCEmain       {
                global $TCA;
 
                $uid = intval($uid);
-               if ($TCA[$table] && $uid)       {
+                       // Only copy if the table is defined in TCA, a uid is given and the record wasn't copied before:
+               if ($TCA[$table] && $uid && !$this->isRecordCopied($table, $uid))       {
                        t3lib_div::loadTCA($table);
                        if ($this->doesRecordExist($table,$uid,'show')) {
 
@@ -2686,6 +2849,7 @@ class t3lib_TCEmain       {
                                        if ($theNewSQLID)       {
                                                $this->dbAnalysisStoreExec();
                                                $this->dbAnalysisStore = array();
+                                               $this->copyRecord_fixRTEmagicImages($table,t3lib_BEfunc::wsMapId($table,$theNewSQLID));
                                                return $this->copyMappingArray[$table][$uid] = $theNewSQLID;
                                        }
                                } else $this->log($table,$uid,3,0,1,'Attempt to rawcopy/versionize record that did not exist!');
@@ -2717,6 +2881,7 @@ class t3lib_TCEmain       {
                                                        $this->copyRecord_raw($table,$row['uid'],$new_pid);
                                                }
                                        }
+                                       $GLOBALS['TYPO3_DB']->sql_free_result($mres);
                                }
                        }
                }
@@ -2781,11 +2946,12 @@ class t3lib_TCEmain     {
         * @param       array           Record array
         * @param       array           TCA field configuration
         * @param       integer         Real page id (pid) the record is copied to
+        * @param       integer         Language ID (from sys_language table)
         * @return      mixed           Processed value. Normally a string/integer, but can be an array for flexforms!
         * @access private
         * @see copyRecord()
         */
-       function copyRecord_procBasedOnFieldType($table,$uid,$field,$value,$row,$conf,$realDestPid) {
+       function copyRecord_procBasedOnFieldType($table, $uid, $field, $value, $row, $conf, $realDestPid, $language=0) {
                global $TCA;
 
                        // Process references and files, currently that means only the files, prepending absolute paths (so the TCEmain engine will detect the file as new and one that should be made into a copy)
@@ -2796,35 +2962,79 @@ class t3lib_TCEmain     {
                if ($this->isReferenceField($conf) || $inlineSubType == 'mm')   {
                        $allowedTables = $conf['type']=='group' ? $conf['allowed'] : $conf['foreign_table'].','.$conf['neg_foreign_table'];
                        $prependName = $conf['type']=='group' ? $conf['prepend_tname'] : $conf['neg_foreign_table'];
-                       if ($conf['MM'])        {
+                       $localizeReferences = (isset($conf['foreign_table']) && t3lib_BEfunc::isTableLocalizable($conf['foreign_table']) && isset($conf['localizeReferencesAtParentLocalization']) && $conf['localizeReferencesAtParentLocalization']);
+                       if ($conf['MM'] || $language>0 && $localizeReferences) {
                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
-                               $dbAnalysis->start('', $allowedTables, $conf['MM'], $uid, $table, $conf);
+                               /* @var $dbAnalysis t3lib_loadDBGroup */
+                               $dbAnalysis->start($value, $allowedTables, $conf['MM'], $uid, $table, $conf);
+                               if (!$conf['MM']) {
+                                               // Localize referenced records of select fields:
+                                       foreach ($dbAnalysis->itemArray as $index => $item) {
+                                                       // Since select fields can reference many records, check whether there's already a localization:
+                                               $recordLocalization = t3lib_BEfunc::getRecordLocalization($item['table'], $item['id'], $language);
+                                               if (!$recordLocalization) {
+                                                       $dbAnalysis->itemArray[$index]['id'] = $this->localize($item['table'], $item['id'], $language);
+                                               } else {
+                                                       $dbAnalysis->itemArray[$index]['id'] = $recordLocalization[0]['uid'];
+                                               }
+                                       }
+                               }
                                $value = implode(',',$dbAnalysis->getValueArray($prependName));
                        }
                        if ($value)     {       // Setting the value in this array will notify the remapListedDBRecords() function that this field MAY need references to be corrected
                                $this->registerDBList[$table][$uid][$field] = $value;
                        }
 
-                       // if another inline subtype is used (foreign_field, mm with attributes or simply item list)
+                       // If another inline subtype is used (comma-separated-values or the foreign_field property):
                } elseif ($inlineSubType !== false) {
-                       $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
-                       $dbAnalysis->start($value, $conf['foreign_table'], $conf['MM'], $uid, $table, $conf);
-
-                               // walk through the items, copy them and remember the new id
-                       foreach ($dbAnalysis->itemArray as $k => $v) {
-                               if (!t3lib_div::testInt($realDestPid)) {
-                                       $newId = $this->copyRecord($v['table'], $v['id'], -$v['id']);
-                               } elseif ($realDestPid == -1) {
-                                       $newId = $this->versionizeRecord($v['table'], $v['id'], 'Auto-created for WS #'.$this->BE_USER->workspace);
-                               } else {
-                                       $newId = $this->copyRecord_raw($v['table'], $v['id'], $realDestPid);
+                               // Get the localization mode for the current (parent) record (keep|select|all):
+                       $localizazionMode = t3lib_BEfunc::getInlineLocalizationMode($table, $field);
+                               // Localization in mode 'keep', isn't a real localization, but keeps the children of the original parent record:
+                       if ($language>0 && $localizazionMode=='keep') {
+                               $value = ($inlineSubType=='field' ? 0 : '');
+                               // Execute copy or localization actions:
+                       } else {
+                               /*
+                                * Fetch the related child records by using t3lib_loadDBGroup:
+                                * @var $dbAnalysis t3lib_loadDBGroup
+                                */
+                               $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                               $dbAnalysis->start($value, $conf['foreign_table'], '', $uid, $table, $conf);
+
+                                       // Walk through the items, copy them and remember the new id:
+                               foreach ($dbAnalysis->itemArray as $k => $v) {
+                                               // If language is set, this isn't a copy action but a localization of our parent/ancestor:
+                                       if ($language>0) {
+                                                       // If children should be localized when the parent gets localized the first time, just do it:
+                                               if ($localizazionMode!=false && isset($conf['behaviour']['localizeChildrenAtParentLocalization']) && $conf['behaviour']['localizeChildrenAtParentLocalization']) {
+                                                       $newId = $this->localize($v['table'], $v['id'], $language);
+                                               }
+                                               // If no language it set, this is a regular copy action:
+                                       } else {
+                                               if (!t3lib_div::testInt($realDestPid)) {
+                                                       $newId = $this->copyRecord($v['table'], $v['id'], -$v['id']);
+                                               } elseif ($realDestPid == -1) {
+                                                       $newId = $this->versionizeRecord($v['table'], $v['id'], 'Auto-created for WS #'.$this->BE_USER->workspace);
+                                               } else {
+                                                       $newId = $this->copyRecord_raw($v['table'], $v['id'], $realDestPid);
+                                               }
+                                       }
+
+                                               // If the current field is set on a page record, update the pid of related child records:
+                                       if ($table == 'pages') {
+                                               $this->registerDBPids[$v['table']][$v['id']] = $uid;
+                                               // If the current field has ancestors that have a field on a page record, update the pid of related child records:
+                                       } elseif (isset($this->registerDBPids[$table][$uid])) {
+                                               $this->registerDBPids[$v['table']][$v['id']] = $this->registerDBPids[$table][$uid];
+                                       }
+
+                                       $dbAnalysis->itemArray[$k]['id'] = $newId;
                                }
-                               $dbAnalysis->itemArray[$k]['id'] = $newId;
-                       }
 
-                               // store the new values, we will set up the uids for the subtype later on
-                       $value = implode(',',$dbAnalysis->getValueArray());
-                       $this->registerDBList[$table][$uid][$field] = $value;
+                                       // Store the new values, we will set up the uids for the subtype later on (exception keep localization from original record):
+                               $value = implode(',',$dbAnalysis->getValueArray());
+                               $this->registerDBList[$table][$uid][$field] = $value;
+                       }
                }
 
                        // For "flex" fieldtypes we need to traverse the structure for two reasons: If there are file references they have to be prepended with absolute paths and if there are database reference they MIGHT need to be remapped (still done in remapListedDBRecords())
@@ -2900,6 +3110,7 @@ class t3lib_TCEmain       {
                                $theFileValues = array();
 
                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                               /* @var $dbAnalysis t3lib_loadDBGroup */
                                $dbAnalysis->start('', 'files', $conf['MM'], $uid);
 
                                foreach($dbAnalysis->itemArray as $somekey => $someval) {
@@ -2933,6 +3144,94 @@ class t3lib_TCEmain      {
                return $value;
        }
 
+       /**
+        * Copies any "RTEmagic" image files found in record with table/id to new names.
+        * Usage: After copying a record this function should be called to search for "RTEmagic"-images inside the record. If such are found they should be duplicated to new names so all records have a 1-1 relation to them.
+        * Reason for copying RTEmagic files: a) if you remove an RTEmagic image from a record it will remove the file - any other record using it will have a lost reference! b) RTEmagic images keeps an original and a copy. The copy always is re-calculated to have the correct physical measures as the HTML tag inserting it defines. This is calculated from the original. Two records using the same image could have difference HTML-width/heights for the image and the copy could only comply with one of them. If you don't want a 1-1 relation you should NOT use RTEmagic files but just insert it as a normal file reference to a file inside fileadmin/ folder
+        *
+        * @param       string          Table name
+        * @param       integer         Record UID
+        * @return      void
+        */
+       function copyRecord_fixRTEmagicImages($table,$theNewSQLID)      {
+               global $TYPO3_DB;
+
+                       // Creating fileFunc object.
+               if (!$this->fileFunc)   {
+                       $this->fileFunc = t3lib_div::makeInstance('t3lib_basicFileFunctions');
+                       $this->include_filefunctions=1;
+               }
+
+                       // Select all RTEmagic files in the reference table from the table/ID
+               /* @var $TYPO3_DB t3lib_DB */
+               $recs = $TYPO3_DB->exec_SELECTgetRows(
+                       '*',
+                       'sys_refindex',
+                       'ref_table='.$TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex').
+                               ' AND ref_string LIKE '.$TYPO3_DB->fullQuoteStr('%/RTEmagic%', 'sys_refindex').
+                               ' AND softref_key='.$TYPO3_DB->fullQuoteStr('images', 'sys_refindex').
+                               ' AND tablename='.$TYPO3_DB->fullQuoteStr($table, 'sys_refindex').
+                               ' AND recuid='.intval($theNewSQLID),
+                       '',
+                       'sorting DESC'
+               );
+
+
+                       // Traverse the files found and copy them:
+               if (is_array($recs)) {
+                       foreach($recs as $rec)  {
+                               $filename = basename($rec['ref_string']);
+                               $fileInfo = array();
+                               if (t3lib_div::isFirstPartOfStr($filename,'RTEmagicC_'))        {
+
+                                       $fileInfo['exists'] = @is_file(PATH_site.$rec['ref_string']);
+                                       $fileInfo['original'] = substr($rec['ref_string'],0,-strlen($filename)).'RTEmagicP_'.ereg_replace('\.[[:alnum:]]+$','',substr($filename,10));
+                                       $fileInfo['original_exists'] = @is_file(PATH_site.$fileInfo['original']);
+
+                                       // CODE from tx_impexp and class.rte_images.php adapted for use here:
+
+                                       if ($fileInfo['exists'] && $fileInfo['original_exists'])        {
+
+                                                       // Initialize; Get directory prefix for file and set the original name:
+                                               $dirPrefix = dirname($rec['ref_string']).'/';
+                                               $rteOrigName = basename($fileInfo['original']);
+
+                                                       // If filename looks like an RTE file, and the directory is in "uploads/", then process as a RTE file!
+                                               if ($rteOrigName && t3lib_div::isFirstPartOfStr($dirPrefix,'uploads/') && @is_dir(PATH_site.$dirPrefix))        {       // RTE:
+
+                                                               // From the "original" RTE filename, produce a new "original" destination filename which is unused.
+                                                       $origDestName = $this->fileFunc->getUniqueName($rteOrigName, PATH_site.$dirPrefix);
+
+                                                               // Create copy file name:
+                                                       $pI = pathinfo($rec['ref_string']);
+                                                       $copyDestName = dirname($origDestName).'/RTEmagicC_'.substr(basename($origDestName),10).'.'.$pI['extension'];
+                                                       if (!@is_file($copyDestName) && !@is_file($origDestName)
+                                                               && $origDestName===t3lib_div::getFileAbsFileName($origDestName) && $copyDestName===t3lib_div::getFileAbsFileName($copyDestName))        {
+
+                                                                       // Making copies:
+                                                               t3lib_div::upload_copy_move(PATH_site.$fileInfo['original'],$origDestName);
+                                                               t3lib_div::upload_copy_move(PATH_site.$rec['ref_string'],$copyDestName);
+                                                               clearstatcache();
+
+                                                                       // Register this:
+                                                               $this->RTEmagic_copyIndex[$rec['tablename']][$rec['recuid']][$rec['field']][$rec['ref_string']] = substr($copyDestName,strlen(PATH_site));
+
+                                                                       // Check and update the record using the t3lib_refindex class:
+                                                               if (@is_file($copyDestName))    {
+                                                                       $sysRefObj = t3lib_div::makeInstance('t3lib_refindex');
+                                                                       $error = $sysRefObj->setReferenceValue($rec['hash'],substr($copyDestName,strlen(PATH_site)),FALSE,TRUE);
+                                                                       if ($error)     {
+                                                                               echo $this->newlog('t3lib_refindex::setReferenceValue(): '.$error,1);
+                                                                       }
+                                                               } else $this->newlog('File "'.$copyDestName.'" was not created!',1);
+                                                       } else $this->newlog('Could not construct new unique names for file!',1);
+                                               } else $this->newlog('Maybe directory of file was not within "uploads/"?',1);
+                                       } else $this->newlog('Trying to copy RTEmagic files ('.$rec['ref_string'].' / '.$fileInfo['original'].') but one or both were missing',1);
+                               }
+                       }
+               }
+       }
+
 
 
 
@@ -2960,30 +3259,17 @@ class t3lib_TCEmain     {
         * @return      void
         */
        function moveRecord($table,$uid,$destPid)       {
-               global $TCA, $TYPO3_CONF_VARS;
+               global $TCA;
 
                if ($TCA[$table])       {
 
-                               // Prepare user defined objects (if any) for hooks which extend this function:
-                       $hookObjectsArr = array();
-                       if (is_array ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'])) {
-                               foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'] as $classRef) {
-                                       $hookObjectsArr[] = &t3lib_div::getUserObj($classRef);
-                               }
-                       }
-
                                // In case the record to be moved turns out to be an offline version, we have to find the live version and work on that one (this case happens for pages with "branch" versioning type)
                        if ($lookForLiveVersion = t3lib_BEfunc::getLiveVersionOfRecord($table,$uid,'uid'))      {
                                $uid = $lookForLiveVersion['uid'];
                        }
 
-                               // Get workspace version of the source record, if any:
-                       $WSversion = t3lib_BEfunc::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $uid, 'uid,t3ver_oid');
-
                                // Initialize:
-                       $sortRow = $TCA[$table]['ctrl']['sortby'];
                        $destPid = intval($destPid);
-                       $origDestPid = $destPid;
 
                        $propArr = $this->getRecordProperties($table,$uid);     // Get this before we change the pid (for logging)
                        $moveRec = $this->getRecordProperties($table,$uid,TRUE);
@@ -3004,143 +3290,256 @@ class t3lib_TCEmain   {
                                $mayInsertAccess = $this->checkRecordUpdateAccess($table,$uid);
                        }
 
-                               // Check workspace permissions:
-                       $workspaceAccessBlocked = array();
-                       $recIsNewVersion = !strcmp($moveRec['_ORIG_pid'],'') && (int)$moveRec['t3ver_state']==1;        // Element was an online version AND it was in "New state" so it can be moved...
-                       $destRes = $this->BE_USER->workspaceAllowLiveRecordsInPID($resolvedPid,$table);
-                               // Workspace source check:
-                       if ($errorCode = $this->BE_USER->workspaceCannotEditRecord($table, $WSversion['uid'] ? $WSversion['uid'] : $uid))       {
-                               $workspaceAccessBlocked['src1']='Record could not be edited in workspace: '.$errorCode.' ';
-                       } else {
-                               if (!$recIsNewVersion && $this->BE_USER->workspaceAllowLiveRecordsInPID($moveRec['pid'],$table)<=0)     {
-                                       $workspaceAccessBlocked['src2']='Could not remove record from table "'.$table.'" from its page "'.$moveRec['pid'].'" ';
-                               }
-                       }
-                               // Workspace destination check:
-                       if (!($destRes>0 || ($recIsNewVersion && !$destRes)))   {       // All records can be inserted if $destRes is greater than zero. Only new versions can be inserted if $destRes is false. NO RECORDS can be inserted if $destRes is negative which indicates a stage not allowed for use.
-                               $workspaceAccessBlocked['dest1']='Could not insert record from table "'.$table.'" in destination PID "'.$resolvedPid.'" ';
-                       } elseif ($destRes==1 && $WSversion['uid'])     {
-                               $workspaceAccessBlocked['dest2']='Could not insert other versions in destination PID ';
-                       }
-
-                               // Checking if the pid is negative, but no sorting row is defined. In that case, find the correct pid. Basically this check make the error message 4-13 meaning less... But you can always remove this check if you prefer the error instead of a no-good action (which is to move the record to its own page...)
-                       if (($destPid<0 && !$sortRow) || $destPid>=0)   {       // $destPid>=0 because we must correct pid in case of versioning "page" types.
-                               $destPid = $resolvedPid;
-                       }
+                               // Checking if there is anything else disallowing moving the record by checking if editing is allowed
+                       $mayEditAccess = $this->BE_USER->recordEditAccessInternals($table,$uid);
 
-                               // Timestamp field:
-                       $updateFields = array();
-                       if ($TCA[$table]['ctrl']['tstamp'])     {
-                               $updateFields[$TCA[$table]['ctrl']['tstamp']] = time();
-                       }
-
-                                       // If moving is allowed, begin the processing:
-                       if (!count($workspaceAccessBlocked))    {
+                               // If moving is allowed, begin the processing:
+                       if ($mayEditAccess)     {
                                if ($mayMoveAccess)     {
-                                       if ($destPid>=0)        {       // insert as first element on page (where uid = $destPid)
-                                               if ($mayInsertAccess)   {
-                                                       if ($table!='pages' || $this->destNotInsideSelf($destPid,$uid)) {
-                                                               $this->clear_cache($table,$uid);        // clear cache before moving
+                                       if ($mayInsertAccess)   {
 
-                                                               $updateFields['pid'] = $destPid;        // Setting PID
+                                               if ($this->BE_USER->workspace!==0)      {       // Draft workspace...:
+                                                               // Get workspace version of the source record, if any:
+                                                       $WSversion = t3lib_BEfunc::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $uid, 'uid,t3ver_oid');
 
-                                                                       // table is sorted by 'sortby'
-                                                               if ($sortRow)   {
-                                                                       $sortNumber = $this->getSortNumber($table,$uid,$destPid);
-                                                                       $updateFields[$sortRow] = $sortNumber;
-                                                               }
+                                                               // If no version exists and versioningWS is in version 2, a new placeholder is made automatically:
+                                                       if (!$WSversion['uid'] && (int)$TCA[$table]['ctrl']['versioningWS']>=2 && (int)$moveRec['t3ver_state']!=3)      {
+                                                               $this->versionizeRecord($table,$uid,'Placeholder version for moving record');
+                                                               $WSversion = t3lib_BEfunc::getWorkspaceVersionOfRecord($this->BE_USER->workspace, $table, $uid, 'uid,t3ver_oid');       // Will not create new versions in live workspace though...
+                                                       }
 
-                                                                       // check for child records that have also to be moved
-                                                               $this->moveRecord_procFields($table,$uid,$destPid);
-                                                                       // Create query for update:
-                                                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
+                                                               // Check workspace permissions:
+                                                       $workspaceAccessBlocked = array();
+                                                       $recIsNewVersion = (int)$moveRec['t3ver_state']>0;      // Element was in "New/Deleted/Moved" so it can be moved...
+                                                       $destRes = $this->BE_USER->workspaceAllowLiveRecordsInPID($resolvedPid,$table);
+                                                       $canMoveRecord = $recIsNewVersion || (int)$TCA[$table]['ctrl']['versioningWS']>=2;
 
-                                                                       // Call post processing hooks:
-                                                               foreach($hookObjectsArr as $hookObj) {
-                                                                       if (method_exists($hookObj, 'moveRecord_firstElementPostProcess')) {
-                                                                               $hookObj->moveRecord_firstElementPostProcess($table, $uid, $destPid, $moveRec, $updateFields, $this);
+                                                               // Workspace source check:
+                                                       if (!$recIsNewVersion)  {
+                                                               if ($errorCode = $this->BE_USER->workspaceCannotEditRecord($table, $WSversion['uid'] ? $WSversion['uid'] : $uid))       {
+                                                                       $workspaceAccessBlocked['src1']='Record could not be edited in workspace: '.$errorCode.' ';
+                                                               } else {
+                                                                       if (!$canMoveRecord && $this->BE_USER->workspaceAllowLiveRecordsInPID($moveRec['pid'],$table)<=0)       {
+                                                                               $workspaceAccessBlocked['src2']='Could not remove record from table "'.$table.'" from its page "'.$moveRec['pid'].'" ';
                                                                        }
                                                                }
+                                                       }
 
-                                                                       // Logging...
-                                                               $newPropArr = $this->getRecordProperties($table,$uid);
-                                                               $oldpagePropArr = $this->getRecordProperties('pages',$propArr['pid']);
-                                                               $newpagePropArr = $this->getRecordProperties('pages',$destPid);
+                                                               // Workspace destination check:
+                                                       if (!($destRes>0 || ($canMoveRecord && !$destRes)))     {       // All records can be inserted if $destRes is greater than zero. Only new versions can be inserted if $destRes is false. NO RECORDS can be inserted if $destRes is negative which indicates a stage not allowed for use. If "versioningWS" is version 2, moving can take place of versions.
+                                                               $workspaceAccessBlocked['dest1']='Could not insert record from table "'.$table.'" in destination PID "'.$resolvedPid.'" ';
+                                                       } elseif ($destRes==1 && $WSversion['uid'])     {
+                                                               $workspaceAccessBlocked['dest2']='Could not insert other versions in destination PID ';
+                                                       }
 
-                                                               if ($destPid!=$propArr['pid'])  {
-                                                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) to page '%s' (%s)",2,array($propArr['header'],$table.':'.$uid, $newpagePropArr['header'], $newPropArr['pid']),$propArr['pid']);     // Logged to old page
-                                                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) from page '%s' (%s)",3,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);     // Logged to new page
+                                                       if (!count($workspaceAccessBlocked))    {
+                                                               if ($WSversion['uid'] && !$recIsNewVersion && (int)$TCA[$table]['ctrl']['versioningWS']>=2)     { // If the move operation is done on a versioned record, which is NOT new/deletd placeholder and versioningWS is in version 2, then...
+                                                                       $this->moveRecord_wsPlaceholders($table,$uid,$destPid,$WSversion['uid']);
                                                                } else {
-                                                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) on page '%s' (%s)",4,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);       // Logged to new page
+                                                                       $this->moveRecord_raw($table,$uid,$destPid);
                                                                }
-                                                               $this->clear_cache($table,$uid);        // clear cache after moving
-                                                               $this->fixUniqueInPid($table,$uid);
-                                                                       // fixCopyAfterDuplFields
-                                                               if ($origDestPid<0)     {$this->fixCopyAfterDuplFields($table,$uid,abs($origDestPid),1);}       // origDestPid is retrieve before it may possibly be converted to resolvePid if the table is not sorted anyway. In this way, copying records to after another records which are not sorted still lets you use this function in order to copy fields from the one before.
                                                        } else {
-                                                               $destPropArr = $this->getRecordProperties('pages',$destPid);
-                                                               $this->log($table,$uid,4,0,1,"Attempt to move page '%s' (%s) to inside of its own rootline (at page '%s' (%s))",10,array($propArr['header'],$uid, $destPropArr['header'], $destPid),$propArr['pid']);
+                                                               $this->newlog("Move attempt failed due to workspace restrictions: ".implode(' // ',$workspaceAccessBlocked),1);
                                                        }
+                                               } else {        // Live workspace - move it!
+                                                       $this->moveRecord_raw($table,$uid,$destPid);
                                                }
-                                       } else {        // Put after another record
-                                               if ($sortRow)   {       // table is being sorted
-                                                       $sortInfo = $this->getSortNumber($table,$uid,$destPid);
-                                                       $destPid = $sortInfo['pid'];    // Setting the destPid to the new pid of the record.
-                                                       if (is_array($sortInfo))        {       // If not an array, there was an error (which is already logged)
-                                                               if ($mayInsertAccess)   {
-                                                                       if ($table!='pages' || $this->destNotInsideSelf($destPid,$uid)) {
-                                                                               $this->clear_cache($table,$uid);        // clear cache before moving
-
-                                                                                       // We now update the pid and sortnumber
-                                                                               $updateFields['pid'] = $destPid;
-                                                                               $updateFields[$sortRow] = $sortInfo['sortNumber'];
-
-                                                                                       // check for child records that have also to be moved
-                                                                               $this->moveRecord_procFields($table,$uid,$destPid);
-                                                                                       // Create query for update:
-                                                                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
-
-                                                                                       // Call post processing hooks:
-                                                                               foreach($hookObjectsArr as $hookObj) {
-                                                                                       if (method_exists($hookObj, 'moveRecord_afterAnotherElementPostProcess')) {
-                                                                                               $hookObj->moveRecord_afterAnotherElementPostProcess($table, $uid, $destPid, $origDestPid, $moveRec, $updateFields, $this);
-                                                                                       }
-                                                                               }
+                                       } else {
+                                               $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) without having permissions to insert.",14,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
+                                       }
+                               } else {
+                                       $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) without having permissions to do so.",14,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
+                               }
+                       } else {
+                               $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) without having permissions to do so. [".$this->BE_USER->errorMsg."]",14,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
+                       }
+               }
+       }
 
-                                                                                       // Logging...
-                                                                               $newPropArr = $this->getRecordProperties($table,$uid);
-                                                                               $oldpagePropArr = $this->getRecordProperties('pages',$propArr['pid']);
-                                                                               if ($destPid!=$propArr['pid'])  {
-                                                                                       $newpagePropArr = $this->getRecordProperties('pages',$destPid);
-                                                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) to page '%s' (%s)",2,array($propArr['header'],$table.':'.$uid, $newpagePropArr['header'], $newPropArr['pid']),$propArr['pid']);    // Logged to old page
-                                                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) from page '%s' (%s)",3,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);    // Logged to new page
-                                                                               } else {
-                                                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) on page '%s' (%s)",4,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);      // Logged to new page
-                                                                               }
+       /**
+        * Creates a move placeholder for workspaces.
+        * USE ONLY INTERNALLY
+        * Moving placeholder: Can be done because the system sees it as a placeholder for NEW elements like t3ver_state=1
+        * Moving original: Will either create the placeholder if it doesn't exist or move existing placeholder in workspace.
+        *
+        * @param       string          Table name to move
+        * @param       integer         Record uid to move (online record)
+        * @param       integer         Position to move to: $destPid: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
+        * @param       integer         UID of offline version of online record
+        * @return      void
+        * @see moveRecord()
+        */
+       function moveRecord_wsPlaceholders($table,$uid,$destPid,$wsUid) {
+               global $TCA;
+
+               if ($plh = t3lib_BEfunc::getMovePlaceholder($table,$uid,'uid')) {
+                               // If already a placeholder exists, move it:
+                       $this->moveRecord_raw($table,$plh['uid'],$destPid);
+               } else {
+                               // First, we create a placeholder record in the Live workspace that represents the position to where the record is eventually moved to.
+                       $newVersion_placeholderFieldArray = array();
+                       if ($TCA[$table]['ctrl']['crdate'])     {
+                               $newVersion_placeholderFieldArray[$TCA[$table]['ctrl']['crdate']] = time();
+                       }
+                       if ($TCA[$table]['ctrl']['cruser_id'])  {
+                               $newVersion_placeholderFieldArray[$TCA[$table]['ctrl']['cruser_id']] = $this->userid;
+                       }
+                       if ($TCA[$table]['ctrl']['tstamp'] && count($fieldArray))       {
+                               $newVersion_placeholderFieldArray[$TCA[$table]['ctrl']['tstamp']] = time();
+                       }
 
-                                                                                       // clear cache after moving
-                                                                               $this->clear_cache($table,$uid);
+                       $newVersion_placeholderFieldArray['t3ver_label'] = 'MOVE-TO PLACEHOLDER for #'.$uid;
+                       $newVersion_placeholderFieldArray['t3ver_move_id'] = $uid;
+                       $newVersion_placeholderFieldArray['t3ver_state'] = 3;   // Setting placeholder state value for temporary record
+                       $newVersion_placeholderFieldArray['t3ver_wsid'] = $this->BE_USER->workspace;    // Setting workspace - only so display of place holders can filter out those from other workspaces.
+                       $newVersion_placeholderFieldArray[$TCA[$table]['ctrl']['label']] = '[MOVE-TO PLACEHOLDER for #'.$uid.', WS#'.$this->BE_USER->workspace.']';
 
-                                                                                       // fixUniqueInPid
-                                                                               $this->fixUniqueInPid($table,$uid);
+                       $newVersion_placeholderFieldArray['pid'] = 0;   // Initially, create at root level.
+                       $id = 'NEW_MOVE_PLH';
+                       $this->insertDB($table,$id,$newVersion_placeholderFieldArray,FALSE);    // Saving placeholder as 'original'
 
-                                                                                       // fixCopyAfterDuplFields
-                                                                               if ($origDestPid<0)     {$this->fixCopyAfterDuplFields($table,$uid,abs($origDestPid),1);}
-                                                                       } else {
-                                                                               $destPropArr = $this->getRecordProperties('pages',$destPid);
-                                                                               $this->log($table,$uid,4,0,1,"Attempt to move page '%s' (%s) to inside of its own rootline (at page '%s' (%s))",10,array($propArr['header'],$uid, $destPropArr['header'], $destPid),$propArr['pid']);
-                                                                       }
-                                                               }
+                               // Move the new placeholder from temporary root-level to location:
+                       $this->moveRecord_raw($table,$this->substNEWwithIDs[$id],$destPid);
+
+                               // Move the workspace-version of the original to be the version of the move-to-placeholder:
+                       $updateFields = array();
+                       $updateFields['t3ver_state'] = 4;       // Setting placeholder state value for version (so it can know it is currently a new version...)
+                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($wsUid), $updateFields);
+               }
+       }
+
+       /**
+        * Moves a record without checking security of any sort.
+        * USE ONLY INTERNALLY
+        *
+        * @param       string          Table name to move
+        * @param       integer         Record uid to move
+        * @param       integer         Position to move to: $destPid: >=0 then it points to a page-id on which to insert the record (as the first element). <0 then it points to a uid from its own table after which to insert it (works if
+        * @return      void
+        * @see moveRecord()
+        */
+       function moveRecord_raw($table,$uid,$destPid)   {
+               global $TCA, $TYPO3_CONF_VARS;
+
+               $sortRow = $TCA[$table]['ctrl']['sortby'];
+               $origDestPid = $destPid;
+               $resolvedPid = $this->resolvePid($table,$destPid);      // This is the actual pid of the moving to destination
+
+                       // Checking if the pid is negative, but no sorting row is defined. In that case, find the correct pid. Basically this check make the error message 4-13 meaning less... But you can always remove this check if you prefer the error instead of a no-good action (which is to move the record to its own page...)
+               if (($destPid<0 && !$sortRow) || $destPid>=0)   {       // $destPid>=0 because we must correct pid in case of versioning "page" types.
+                       $destPid = $resolvedPid;
+               }
+
+               $propArr = $this->getRecordProperties($table,$uid);     // Get this before we change the pid (for logging)
+               $moveRec = $this->getRecordProperties($table,$uid,TRUE);
+
+                       // Prepare user defined objects (if any) for hooks which extend this function:
+               $hookObjectsArr = array();
+               if (is_array ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'])) {
+                       foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['moveRecordClass'] as $classRef) {
+                               $hookObjectsArr[] = &t3lib_div::getUserObj($classRef);
+                       }
+               }
+
+                       // Timestamp field:
+               $updateFields = array();
+               if ($TCA[$table]['ctrl']['tstamp'])     {
+                       $updateFields[$TCA[$table]['ctrl']['tstamp']] = time();
+               }
+
+               if ($destPid>=0)        {       // insert as first element on page (where uid = $destPid)
+                       if ($table!='pages' || $this->destNotInsideSelf($destPid,$uid)) {
+                               $this->clear_cache($table,$uid);        // clear cache before moving
+
+                               $updateFields['pid'] = $destPid;        // Setting PID
+
+                                       // table is sorted by 'sortby'
+                               if ($sortRow)   {
+                                       $sortNumber = $this->getSortNumber($table,$uid,$destPid);
+                                       $updateFields[$sortRow] = $sortNumber;
+                               }
+
+                                       // check for child records that have also to be moved
+                               $this->moveRecord_procFields($table,$uid,$destPid);
+                                       // Create query for update:
+                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
+
+                                       // Call post processing hooks:
+                               foreach($hookObjectsArr as $hookObj) {
+                                       if (method_exists($hookObj, 'moveRecord_firstElementPostProcess')) {
+                                               $hookObj->moveRecord_firstElementPostProcess($table, $uid, $destPid, $moveRec, $updateFields, $this);
+                                       }
+                               }
+
+                                       // Logging...
+                               $newPropArr = $this->getRecordProperties($table,$uid);
+                               $oldpagePropArr = $this->getRecordProperties('pages',$propArr['pid']);
+                               $newpagePropArr = $this->getRecordProperties('pages',$destPid);
+
+                               if ($destPid!=$propArr['pid'])  {
+                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) to page '%s' (%s)",2,array($propArr['header'],$table.':'.$uid, $newpagePropArr['header'], $newPropArr['pid']),$propArr['pid']);     // Logged to old page
+                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) from page '%s' (%s)",3,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);     // Logged to new page
+                               } else {
+                                       $this->log($table,$uid,4,$destPid,0,"Moved record '%s' (%s) on page '%s' (%s)",4,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);       // Logged to new page
+                               }
+                               $this->clear_cache($table,$uid);        // clear cache after moving
+                               $this->fixUniqueInPid($table,$uid);
+                                       // fixCopyAfterDuplFields
+                               if ($origDestPid<0)     {$this->fixCopyAfterDuplFields($table,$uid,abs($origDestPid),1);}       // origDestPid is retrieve before it may possibly be converted to resolvePid if the table is not sorted anyway. In this way, copying records to after another records which are not sorted still lets you use this function in order to copy fields from the one before.
+                       } else {
+                               $destPropArr = $this->getRecordProperties('pages',$destPid);
+                               $this->log($table,$uid,4,0,1,"Attempt to move page '%s' (%s) to inside of its own rootline (at page '%s' (%s))",10,array($propArr['header'],$uid, $destPropArr['header'], $destPid),$propArr['pid']);
+                       }
+               } else {        // Put after another record
+                       if ($sortRow)   {       // table is being sorted
+                               $sortInfo = $this->getSortNumber($table,$uid,$destPid);
+                               $destPid = $sortInfo['pid'];    // Setting the destPid to the new pid of the record.
+                               if (is_array($sortInfo))        {       // If not an array, there was an error (which is already logged)
+                                       if ($table!='pages' || $this->destNotInsideSelf($destPid,$uid)) {
+                                               $this->clear_cache($table,$uid);        // clear cache before moving
+
+                                                       // We now update the pid and sortnumber
+                                               $updateFields['pid'] = $destPid;
+                                               $updateFields[$sortRow] = $sortInfo['sortNumber'];
+
+                                                       // check for child records that have also to be moved
+                                               $this->moveRecord_procFields($table,$uid,$destPid);
+                                                       // Create query for update:
+                                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
+
+                                                       // Call post processing hooks:
+                                               foreach($hookObjectsArr as $hookObj) {
+                                                       if (method_exists($hookObj, 'moveRecord_afterAnotherElementPostProcess')) {
+                                                               $hookObj->moveRecord_afterAnotherElementPostProcess($table, $uid, $destPid, $origDestPid, $moveRec, $updateFields, $this);
                                                        }
+                                               }
+
+                                                       // Logging...
+                                               $newPropArr = $this->getRecordProperties($table,$uid);
+                                               $oldpagePropArr = $this->getRecordProperties('pages',$propArr['pid']);
+                                               if ($destPid!=$propArr['pid'])  {
+                                                       $newpagePropArr = $this->getRecordProperties('pages',$destPid);
+                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) to page '%s' (%s)",2,array($propArr['header'],$table.':'.$uid, $newpagePropArr['header'], $newPropArr['pid']),$propArr['pid']);    // Logged to old page
+                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) from page '%s' (%s)",3,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);    // Logged to new page
                                                } else {
-                                                       $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) to after another record, although the table has no sorting row.",13,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
+                                                       $this->log($table,$uid,4,0,0,"Moved record '%s' (%s) on page '%s' (%s)",4,array($propArr['header'],$table.':'.$uid, $oldpagePropArr['header'], $propArr['pid']),$destPid);      // Logged to new page
                                                }
+
+                                                       // clear cache after moving
+                                               $this->clear_cache($table,$uid);
+
+                                                       // fixUniqueInPid
+                                               $this->fixUniqueInPid($table,$uid);
+
+                                                       // fixCopyAfterDuplFields
+                                               if ($origDestPid<0)     {$this->fixCopyAfterDuplFields($table,$uid,abs($origDestPid),1);}
+                                       } else {
+                                               $destPropArr = $this->getRecordProperties('pages',$destPid);
+                                               $this->log($table,$uid,4,0,1,"Attempt to move page '%s' (%s) to inside of its own rootline (at page '%s' (%s))",10,array($propArr['header'],$uid, $destPropArr['header'], $destPid),$propArr['pid']);
                                        }
-                               } else {
-                                       $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) without having permissions to do so",14,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
                                }
                        } else {
-                               $this->newlog("Move attempt failed due to workspace restrictions: ".implode(' / ',$workspaceAccessBlocked),1);
+                               $this->log($table,$uid,4,0,1,"Attempt to move record '%s' (%s) to after another record, although the table has no sorting row.",13,array($propArr['header'],$table.':'.$uid),$propArr['event_pid']);
                        }
                }
        }
@@ -3152,14 +3551,16 @@ class t3lib_TCEmain     {
         * @param       string          $table: Record Table
         * @param       string          $uid: Record UID
         * @param       string          $destPid: Position to move to
-        * @return      [type]          ...
+        * @return      void
         */
        function moveRecord_procFields($table,$uid,$destPid) {
                t3lib_div::loadTCA($table);
                $conf = $GLOBALS['TCA'][$table]['columns'];
                $row = t3lib_BEfunc::getRecordWSOL($table,$uid);
-               foreach ($row as $field => $value) {
-                       $this->moveRecord_procBasedOnFieldType($table,$uid,$destPid,$field,$value,$conf[$field]['config']);
+               if (is_array($row))     {
+                       foreach ($row as $field => $value) {
+                               $this->moveRecord_procBasedOnFieldType($table,$uid,$destPid,$field,$value,$conf[$field]['config']);
+                       }
                }
        }
 
@@ -3180,8 +3581,9 @@ class t3lib_TCEmain       {
 
                if ($conf['type'] == 'inline')  {
                        $foreign_table = $conf['foreign_table'];
+                       $moveChildrenWithParent = (!isset($conf['behaviour']['disableMovingChildrenWithParent']) || !$conf['behaviour']['disableMovingChildrenWithParent']);
 
-                       if ($foreign_table) {
+                       if ($foreign_table && $moveChildrenWithParent) {
                                $inlineType = $this->getInlineFieldType($conf);
                                if ($inlineType == 'list' || $inlineType == 'field') {
                                        $moveTable = $foreign_table;
@@ -3206,11 +3608,12 @@ class t3lib_TCEmain     {
         * @param       string          Table name
         * @param       integer         Record uid (to be localized)
         * @param       integer         Language ID (from sys_language table)
-        * @return      void
+        * @return      mixed           The uid (integer) of the new translated record or false (boolean) if something went wrong
         */
-       function localize($table,$uid,$language)        {
+       function localize($table, $uid, $language) {
                global $TCA;
 
+               $newId = false;
                $uid = intval($uid);
 
                if ($TCA[$table] && $uid)       {
@@ -3229,7 +3632,7 @@ class t3lib_TCEmain       {
                                                                                $Ttable = 'pages_language_overlay';
                                                                                t3lib_div::loadTCA($Ttable);
                                                                        } else {
-                                                                               $pass = !t3lib_BEfunc::getRecordsByField($table,$TCA[$table]['ctrl']['transOrigPointerField'],$uid,'AND pid='.intval($row['pid']).' AND '.$TCA[$table]['ctrl']['languageField'].'='.intval($langRec['uid']));
+                                                                               $pass = !t3lib_BEfunc::getRecordLocalization($table, $uid, $langRec['uid'], 'AND pid='.intval($row['pid']));
                                                                                $Ttable = $table;
                                                                        }
 
@@ -3267,11 +3670,12 @@ class t3lib_TCEmain     {
                                                                                if ($Ttable === $table) {
 
                                                                                                // Execute the copy:
-                                                                                       $this->copyRecord($table,$uid,-$uid,1,$overrideValues,implode(',',$excludeFields));
+                                                                                       $newId = $this->copyRecord($table, $uid, -$uid, 1, $overrideValues, implode(',', $excludeFields), $language);
                                                                                } else {
 
                                                                                                // Create new record:
                                                                                        $copyTCE = t3lib_div::makeInstance('t3lib_TCEmain');
+                                                                                       /* @var $copyTCE t3lib_TCEmain  */
                                                                                        $copyTCE->stripslashes_values = 0;
                                                                                        $copyTCE->cachedTSconfig = $this->cachedTSconfig;       // Copy forth the cached TSconfig
                                                                                        $copyTCE->dontProcessTransformations=1;         // Transformations should NOT be carried out during copy
@@ -3284,6 +3688,7 @@ class t3lib_TCEmain       {
                                                                                        if ($theNewSQLID)       {
                                                                                                        // If is by design that $Ttable is used and not $table! See "l10nmgr" extension. Could be debated, but this is what I chose for this "pseudo case"
                                                                                                $this->copyMappingArray[$Ttable][$uid] = $theNewSQLID;
+                                                                                               $newId = $theNewSQLID;
                                                                                        }
                                                                                }
                                                                        } else $this->newlog('Localization failed; There already was a localization for this language of the record!',1);
@@ -3294,11 +3699,106 @@ class t3lib_TCEmain    {
                                } else $this->newlog('Sys language UID "'.$language.'" not found valid!',1);
                        } else $this->newlog('Localization failed; "languageField" and "transOrigPointerField" must be defined for the table!',1);
                }
+               return $newId;
        }
 
 
-
-
+       /**
+        * Performs localization or synchronization of child records.
+        *
+        * @param       string          $table: The table of the localized parent record
+        * @param       integer         $id: The uid of the localized parent record
+        * @param       mixed           $command: Defines the type 'localize' or 'synchronize' (string) or a single uid to be localized (integer)
+        * @return      void
+        */
+       protected function inlineLocalizeSynchronize($table, $id, $command) {
+                       // <field>, (localize | synchronize | <uid>): 
+               $parts = t3lib_div::trimExplode(',', $command);
+               $field = $parts[0];
+               $type = $parts[1];
+
+               if ($field && (t3lib_div::inList('localize,synchronize', $type) || t3lib_div::testInt($type)) && isset($GLOBALS['TCA'][$table]['columns'][$field]['config'])) {
+                       $config = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
+                       $foreignTable = $config['foreign_table'];
+                       $localizationMode = t3lib_BEfunc::getInlineLocalizationMode($table, $config);
+
+                       if ($localizationMode=='select') {
+                               $parentRecord = t3lib_BEfunc::getRecordWSOL($table, $id);
+                               $language = intval($parentRecord[$GLOBALS['TCA'][$table]['ctrl']['languageField']]);
+                               $transOrigPointer = intval($parentRecord[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']]);
+                               $childTransOrigPointerField = $GLOBALS['TCA'][$foreignTable]['ctrl']['transOrigPointerField'];
+
+                               if ($parentRecord && is_array($parentRecord) && $language>0 && $transOrigPointer) {
+                                       $inlineSubType = $this->getInlineFieldType($config);
+                                       $transOrigRecord = t3lib_BEfunc::getRecordWSOL($table, $transOrigPointer);
+
+                                       if ($inlineSubType!==false) {
+                                               $removeArray = array();
+                                                       // Fetch children from original language parent:
+                                                       // @var $dbAnalysisOriginal t3lib_loadDBGroup
+                                               $dbAnalysisOriginal = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                                               $dbAnalysisOriginal->start($transOrigRecord[$field], $foreignTable, '', $transOrigRecord['uid'], $table, $config);
+                                               $elementsOriginal = array();
+                                               foreach ($dbAnalysisOriginal->itemArray as $item) {
+                                                       $elementsOriginal[$item['id']] = $item;
+                                               }
+                                               unset($dbAnalysisOriginal);
+                                                       // Fetch children from current localized parent:
+                                                       // @var $dbAnalysisCurrent t3lib_loadDBGroup
+                                               $dbAnalysisCurrent = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                                               $dbAnalysisCurrent->start($parentRecord[$field], $foreignTable, '', $id, $table, $config);
+                                                       // Perform synchronization: Possibly removal of already localized records:
+                                               if ($type=='synchronize') {
+                                                       foreach ($dbAnalysisCurrent->itemArray as $index => $item) {
+                                                               $childRecord = t3lib_BEfunc::getRecordWSOL($item['table'], $item['id']);
+                                                               if (isset($childRecord[$childTransOrigPointerField]) && $childRecord[$childTransOrigPointerField]>0) {
+                                                                       $childTransOrigPointer = $childRecord[$childTransOrigPointerField];
+                                                                               // If snychronization is requested, child record was translated once, but original record does not exist anymore, remove it:
+                                                                       if (!isset($elementsOriginal[$childTransOrigPointer])) {
+                                                                               unset($dbAnalysisCurrent->itemArray[$index]);
+                                                                               $removeArray[$item['table']][$item['id']]['delete'] = 1;
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                                       // Perform synchronization/localization: Possibly add unlocalized records for original language: 
+                                               if (t3lib_div::testInt($type) && isset($elementsOriginal[$type])) {
+                                                       $item = $elementsOriginal[$type];
+                                                       $item['id'] = $this->localize($item['table'], $item['id'], $language);
+                                                       $dbAnalysisCurrent->itemArray[] = $item; 
+                                               } elseif (t3lib_div::inList('localize,synchronize', $type)) {
+                                                       foreach ($elementsOriginal as $originalId => $item) {
+                                                               $item['id'] = $this->localize($item['table'], $item['id'], $language);
+                                                               $dbAnalysisCurrent->itemArray[] = $item; 
+                                                       }
+                                               }
+                                                       // Store the new values, we will set up the uids for the subtype later on (exception keep localization from original record):
+                                               $value = implode(',', $dbAnalysisCurrent->getValueArray());
+                                               $this->registerDBList[$table][$id][$field] = $value;
+                                                       // Remove child records (if synchronization requested it):
+                                               if (is_array($removeArray) && count($removeArray)) {
+                                                       $tce = t3lib_div::makeInstance('t3lib_TCEmain');
+                                                       $tce->stripslashes_values = false;
+                                                       $tce->start(array(), $removeArray);
+                                                       $tce->process_cmdmap();
+                                                       unset($tce);
+                                               }
+                                                       // Handle, reorder and store relations:
+                                               if ($inlineSubType=='list') {
+                                                       $updateFields = array($field => $value);
+                                               } elseif ($inlineSubType=='field') {
+                                                       $dbAnalysisCurrent->writeForeignField($config, $id);
+                                                       $updateFields = array($field => $dbAnalysisCurrent->countItems(false));
+                                               }
+                                                       // Update field referencing to child records of localized parent record:
+                                               if (is_array($updateFields) && count($updateFields)) {
+                                                       $this->updateDB($table, $id, $updateFields);
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
 
 
 
@@ -3342,9 +3842,9 @@ class t3lib_TCEmain       {
                                        if ($this->BE_USER->workspace==0 || (int)$delRec['t3ver_wsid']==$this->BE_USER->workspace)      {       // In Live workspace, delete any. In other workspaces there must be match.
                                                $liveRec = t3lib_BEfunc::getLiveVersionOfRecord($table,$id,'uid,t3ver_state');
 
-                                               if ($delRec['t3ver_wsid']==0 || (int)$liveRec['t3ver_state']!==1)       {       // Delete those in WS 0 + if their live records state was not "Placeholder".
+                                               if ($delRec['t3ver_wsid']==0 || (int)$liveRec['t3ver_state']<=0)        {       // Delete those in WS 0 + if their live records state was not "Placeholder".
                                                        $this->deleteEl($table, $id);
-                                               } else {        // If live record was placeholder, rather clear it from workspace (because it clears both version and placeholder).
+                                               } else {        // If live record was placeholder (new/deleted), rather clear it from workspace (because it clears both version and placeholder).
                                                        $this->version_clearWSID($table,$id);
                                                }
                                        } else $this->newlog('Tried to delete record from another workspace',1);
@@ -3353,8 +3853,18 @@ class t3lib_TCEmain      {
                                if ($res>0)     {
                                        $this->deleteEl($table, $id);
                                } else $this->newlog('Stage of root point did not allow for deletion',1);
+                       } elseif ((int)$delRec['t3ver_state']===3) {    // Placeholders for moving operations are deletable directly.
+
+                                       // Get record which its a placeholder for and reset the t3ver_state of that:
+                               if ($wsRec = t3lib_BEfunc::getWorkspaceVersionOfRecord($delRec['t3ver_wsid'], $table, $delRec['t3ver_move_id'], 'uid')) {
+                                               // Clear the state flag of the workspace version of the record
+                                       $updateFields = array();
+                                       $updateFields['t3ver_state'] = 0;       // Setting placeholder state value for version (so it can know it is currently a new version...)
+                                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($wsRec['uid']), $updateFields);
+                               }
+                               $this->deleteEl($table, $id);
                        } else {
-                               // Otherwise, try to delete by versionization:
+                               // Otherwise, try to delete by versioning:
                                $this->versionizeRecord($table,$id,'DELETED!',TRUE);
                        }
                }
@@ -3428,103 +3938,108 @@ class t3lib_TCEmain   {
        function deleteRecord($table,$uid, $noRecordCheck=FALSE, $forceHardDelete=FALSE,$undeleteRecord=FALSE)  {
                global $TCA;
 
+                       // Checking if there is anything else disallowing deleting the record by checking if editing is allowed
+               $mayEditAccess = $this->BE_USER->recordEditAccessInternals($table,$uid);
+
                $uid = intval($uid);
                if ($TCA[$table] && $uid)       {
-                       if ($noRecordCheck || $this->doesRecordExist($table,$uid,'delete'))     {
-                               $this->clear_cache($table,$uid);        // clear cache before deleting the record, else the correct page cannot be identified by clear_cache
-
-                               $propArr = $this->getRecordProperties($table, $uid);
-                               $pagePropArr = $this->getRecordProperties('pages', $propArr['pid']);
-
-                               $deleteRow = $TCA[$table]['ctrl']['delete'];
-                               if ($deleteRow && !$forceHardDelete)    {
-                                       $value = $undeleteRecord ? 0 : 1;
-                                       $updateFields = array(
-                                               $deleteRow => $value
-                                       );
+                       if ($mayEditAccess)     {
+                               if ($noRecordCheck || $this->doesRecordExist($table,$uid,'delete'))     {
+                                       $this->clear_cache($table,$uid);        // clear cache before deleting the record, else the correct page cannot be identified by clear_cache
+
+                                       $propArr = $this->getRecordProperties($table, $uid);
+                                       $pagePropArr = $this->getRecordProperties('pages', $propArr['pid']);
+
+                                       $deleteRow = $TCA[$table]['ctrl']['delete'];
+                                       if ($deleteRow && !$forceHardDelete)    {
+                                               $value = $undeleteRecord ? 0 : 1;
+                                               $updateFields = array(
+                                                       $deleteRow => $value
+                                               );
 
-                                       if ($TCA[$table]['ctrl']['tstamp']) {
-                                               $updateFields[$TCA[$table]['ctrl']['tstamp']] = time();
-                                       }
+                                               if ($TCA[$table]['ctrl']['tstamp']) {
+                                                       $updateFields[$TCA[$table]['ctrl']['tstamp']] = time();
+                                               }
 
-                                               // If the table is sorted, then the sorting number is set very high
-                                       if ($TCA[$table]['ctrl']['sortby'] && !$undeleteRecord) {
-                                               $updateFields[$TCA[$table]['ctrl']['sortby']] = 1000000000;
-                                       }
+                                                       // If the table is sorted, then the sorting number is set very high
+                                               if ($TCA[$table]['ctrl']['sortby'] && !$undeleteRecord) {
+                                                       $updateFields[$TCA[$table]['ctrl']['sortby']] = 1000000000;
+                                               }
 
-                                               // before (un-)deleting this record, check for child records or references
-                                       $this->deleteRecord_procFields($table, $uid, $undeleteRecord);
-                                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
-                               } else {
+                                                       // before (un-)deleting this record, check for child records or references
+                                               $this->deleteRecord_procFields($table, $uid, $undeleteRecord);
+                                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid='.intval($uid), $updateFields);
+                                       } else {
 
-                                               // Fetches all fields with flexforms and look for files to delete:
-                                       t3lib_div::loadTCA($table);
-                                       foreach($TCA[$table]['columns'] as $fieldName => $cfg)  {
-                                               $conf = $cfg['config'];
+                                                       // Fetches all fields with flexforms and look for files to delete:
+                                               t3lib_div::loadTCA($table);
+                                               foreach($TCA[$table]['columns'] as $fieldName => $cfg)  {
+                                                       $conf = $cfg['config'];
 
-                                               switch($conf['type'])   {
-                                                       case 'flex':
-                                                               $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
-                                                               $flexObj->traverseFlexFormXMLData($table,$fieldName,t3lib_BEfunc::getRecordRaw($table,'uid='.intval($uid)),$this,'deleteRecord_flexFormCallBack');
-                                                       break;
+                                                       switch($conf['type'])   {
+                                                               case 'flex':
+                                                                       $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
+                                                                       $flexObj->traverseFlexFormXMLData($table,$fieldName,t3lib_BEfunc::getRecordRaw($table,'uid='.intval($uid)),$this,'deleteRecord_flexFormCallBack');
+                                                               break;
+                                                       }
                                                }
-                                       }
 
-                                               // Fetches all fields that holds references to files
-                                       $fileFieldArr = $this->extFileFields($table);
-                                       if (count($fileFieldArr))       {
-                                               $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',$fileFieldArr), $table, 'uid='.intval($uid));
-                                               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))        {
-                                                       $fArray = $fileFieldArr;
-                                                       foreach($fArray as $theField)   {       // MISSING: Support for MM file relations!
-                                                               $this->extFileFunctions($table,$theField,$row[$theField],'deleteAll');          // This deletes files that belonged to this record.
+                                                       // Fetches all fields that holds references to files
+                                               $fileFieldArr = $this->extFileFields($table);
+                                               if (count($fileFieldArr))       {
+                                                       $mres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(implode(',',$fileFieldArr), $table, 'uid='.intval($uid));
+                                                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($mres))        {
+                                                               $fArray = $fileFieldArr;
+                                                               foreach($fArray as $theField)   {       // MISSING: Support for MM file relations!
+                                                                       $this->extFileFunctions($table,$theField,$row[$theField],'deleteAll');          // This deletes files that belonged to this record.
+                                                               }
+                                                       } else {
+                                                               $this->log($table,$uid,3,0,100,'Delete: Zero rows in result when trying to read filenames from record which should be deleted');
                                                        }
-                                               } else {
-                                                       $this->log($table,$uid,3,0,100,'Delete: Zero rows in result when trying to read filenames from record which should be deleted');
                                                }
+
+                                                       // Delete the hard way...:
+                                               $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, 'uid='.intval($uid));
                                        }
 
-                                               // Delete the hard way...:
-                                       $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, 'uid='.intval($uid));
-                               }
+                                       $state = $undeleteRecord ? 1 : 3;       // 1 means insert, 3 means delete
+                                       if (!$GLOBALS['TYPO3_DB']->sql_error()) {
+                                               if ($forceHardDelete) {
+                                                       $message = "Record '%s' (%s) was deleted unrecoverable from page '%s' (%s)";
+                                               }
+                                               else {
+                                                       $message = $state == 1 ?
+                                                               "Record '%s' (%s) was restored on page '%s' (%s)" :
+                                                               "Record '%s' (%s) was deleted from page '%s' (%s)";
+                                               }
+                                               $this->log($table, $uid, $state, 0, 0,
+                                                                       $message, 0,
+                                                                       array(
+                                                                               $propArr['header'],
+                                                                               $table.':'.$uid,
+                                                                               $pagePropArr['header'],
+                                                                               $propArr['pid']
+                                                                               ),
+                                                                       $propArr['pid']);
 
-                               $state = $undeleteRecord ? 1 : 3;       // 1 means insert, 3 means delete
-                               if (!$GLOBALS['TYPO3_DB']->sql_error()) {
-                                       if ($forceHardDelete) {
-                                               $message = "Record '%s' (%s) was deleted unrecoverable from page '%s' (%s)";
-                                       }
-                                       else {
-                                               $message = $state == 1 ?
-                                                       "Record '%s' (%s) was restored on page '%s' (%s)" :
-                                                       "Record '%s' (%s) was deleted from page '%s' (%s)";
+                                       } else {
+                                               $this->log($table,$uid,$state,0,100,$GLOBALS['TYPO3_DB']->sql_error());
                                        }
-                                       $this->log($table, $uid, $state, 0, 0,
-                                                               $message, 0,
-                                                               array(
-                                                                       $propArr['header'],
-                                                                       $table.':'.$uid,
-                                                                       $pagePropArr['header'],
-                                                                       $propArr['pid']
-                                                                       ),
-                                                               $propArr['pid']);
 
-                               } else {
-                                       $this->log($table,$uid,$state,0,100,$GLOBALS['TYPO3_DB']->sql_error());
-                               }
-
-                                       // Update reference index:
-                               $this->updateRefIndex($table,$uid);
+                                               // Update reference index:
+                                       $this->updateRefIndex($table,$uid);
 
-                                       // if there are entries in the updateRefIndexStack
-                               if (is_array($this->updateRefIndexStack[$table]) && is_array($this->updateRefIndexStack[$table][$uid])) {
-                                       while ($args = array_pop($this->updateRefIndexStack[$table][$uid])) {
-                                                       // $args[0]: table, $args[1]: uid
-                                               $this->updateRefIndex($args[0], $args[1]);
+                                               // if there are entries in the updateRefIndexStack
+                                       if (is_array($this->updateRefIndexStack[$table]) && is_array($this->updateRefIndexStack[$table][$uid])) {
+                                               while ($args = array_pop($this->updateRefIndexStack[$table][$uid])) {
+                                                               // $args[0]: table, $args[1]: uid
+                                                       $this->updateRefIndex($args[0], $args[1]);
+                                               }
+                                               unset($this->updateRefIndexStack[$table][$uid]);
                                        }
-                                       unset($this->updateRefIndexStack[$table][$uid]);
-                               }
 
-                       } else $this->log($table,$uid,3,0,1,'Attempt to delete record without delete-permissions');
+                               } else $this->log($table,$uid,3,0,1,'Attempt to delete record without delete-permissions');
+                       } else $this->log($table,$uid,3,0,1,'Attempt to delete record without delete-permissions. ['.$this->BE_USER->errorMsg.']');
                }
        }
 
@@ -3758,57 +4273,59 @@ class t3lib_TCEmain     {
                                if ($this->BE_USER->workspaceVersioningTypeAccess($versionizeTree))     {
 
                                                // Select main record:
-                                       $row = $this->recordInfo($table,$id,'pid,t3ver_id');
+                                       $row = $this->recordInfo($table,$id,'pid,t3ver_id,t3ver_state');
                                        if (is_array($row))     {
                                                if ($row['pid']>=0)     {       // record must be online record
-                                                       if (!$delete || !$this->cannotDeleteRecord($table,$id)) {
-
-                                                                       // Look for next version number:
-                                                               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-                                                                       't3ver_id',
-                                                                       $table,
-                                                                       '(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;
-                                                               }
+                                                       if ($row['t3ver_state']!=3)     {       // record must not be placeholder for moving.
+                                                               if (!$delete || !$this->cannotDeleteRecord($table,$id)) {
 
-                                                                       // 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;
-                                                               }
+                                                                               // 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;
+                                                                       }
 
-                                                               if ($workspaceCheck)    {
+                                                                               // 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);
+                                                                                       // 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);
@@ -3891,35 +4408,14 @@ class t3lib_TCEmain     {
        function version_swap($table,$id,$swapWith,$swapIntoWS=0)       {
                global $TCA;
 
-               /*
-               Version ID swapping principles:
-                       - Version from archive (future/past, called "swap version") will get the uid of the "t3ver_oid", the official element with uid = "t3ver_oid" will get the new versions old uid. PIDs are swapped also
-
-                       uid             pid                     uid             t3ver_oid       pid
-               1:      13              123      -->    -13             247                     123             (Original has negated UID, and sets t3ver_oid to the final UID (which is nice to know for recovery). PID is unchanged at this point)
-               2:      247             -1       -->    13              13                      123             (Swap version gets original UID, correct t3ver_oid (not required for online version) and is moved to the final PID (123))
-               3:      -13             123      -->    247             13                      -1              (Original gets the swap versions old UID, has t3ver_oid set correctly (important) and the ver. repository PID set right.)
-
-                       13 is online UID,
-                       247 is specific versions UID
-                       123 is the PID of the original record
-                       -1 is the versioning repository PID
-
-                       Recovery Process:
-                               Search for negative UID (here "-13"):
-                                       YES: Step 1 completed, but at least step 3 didn't.
-                                               Search for the negativ UIDs positive (here: "13")
-                                                       YES: Step 2 completed: Rollback: "t3ver_oid" of the -uid record shows the original UID of the swap record. Use that to change back UID and pid to -1. After that, proceed with recovery for step 1 (see below)
-                                                       NO: Only Step 1 completed! Rollback: Just change uid "-13" to "13" and "t3ver_oid" to "13" (not important)
-                                       NO: No problems.
-               */
-
                        // First, check if we may actually edit the online record
                if ($this->checkRecordUpdateAccess($table,$id)) {
 
                                // Select the two versions:
                        $curVersion = t3lib_BEfunc::getRecord($table,$id,'*');
                        $swapVersion = t3lib_BEfunc::getRecord($table,$swapWith,'*');
+                       $movePlh = array();
+                       $movePlhID = 0;
 
                        if (is_array($curVersion) && is_array($swapVersion))    {
                                if ($this->BE_USER->workspacePublishAccess($swapVersion['t3ver_wsid'])) {
@@ -3965,10 +4461,28 @@ class t3lib_TCEmain     {
                                                                                        // Modify offline version to become online:
                                                                                $tmp_wsid = $swapVersion['t3ver_wsid'];
                                                                                $swapVersion['pid'] = intval($curVersion['pid']);       // Set pid for ONLINE
-                                                                               $swapVersion['t3ver_oid'] = intval($id);
-                                                                               $swapVersion['t3ver_wsid'] = $swapIntoWS ? intval($curVersion['t3ver_wsid']) : 0;
+                                                                               $swapVersion['t3ver_oid'] = 0;  // We clear this because t3ver_oid only make sense for offline versions and we want to prevent unintentional misuse of this value for online records.
+                                                                               $swapVersion['t3ver_wsid'] = $swapIntoWS ? ($t3ver_state['swapVersion']>0 ? $this->BE_USER->workspace : intval($curVersion['t3ver_wsid'])) : 0; // In case of swapping and the offline record has a state (like 2 or 4 for deleting or move-pointer) we set the current workspace ID so the record is not deselected in the interface by t3lib_BEfunc::versioningPlaceholderClause()
                                                                                $swapVersion['t3ver_tstamp'] = time();
-                                                                               $swapVersion['t3ver_stage'] = $swapVersion['t3ver_state'] = 0;
+                                                                               $swapVersion['t3ver_stage'] = 0;
+                                                                               if (!$swapIntoWS)       $swapVersion['t3ver_state'] = 0;
+
+                                                                                       // Moving element.
+                                                                               if ((int)$TCA[$table]['ctrl']['versioningWS']>=2)       {               //  && $t3ver_state['swapVersion']==4   // Maybe we don't need this?
+                                                                                       if ($plhRec = t3lib_BEfunc::getMovePlaceholder($table,$id,'t3ver_state,pid,uid'.($TCA[$table]['ctrl']['sortby']?','.$TCA[$table]['ctrl']['sortby']:'')))        {
+                                                                                               $movePlhID = $plhRec['uid'];
+                                                                                               $movePlh['pid'] = $swapVersion['pid'];
+                                                                                               $swapVersion['pid'] = intval($plhRec['pid']);
+
+                                                                                               $curVersion['t3ver_state'] = intval($swapVersion['t3ver_state']);
+                                                                                               $swapVersion['t3ver_state'] = 0;
+
+                                                                                               if ($TCA[$table]['ctrl']['sortby'])     {
+                                                                                                       $movePlh[$TCA[$table]['ctrl']['sortby']] = $swapVersion[$TCA[$table]['ctrl']['sortby']];        // sortby is a "keepFields" which is why this will work...
+                                                                                                       $swapVersion[$TCA[$table]['ctrl']['sortby']] = $plhRec[$TCA[$table]['ctrl']['sortby']];
+                                                                                               }
+                                                                                       }
+                                                                               }
 
                                                                                        // Take care of relations in each field (e.g. IRRE):
                                                                                if (is_array($GLOBALS['TCA'][$table]['columns'])) {
@@ -3979,7 +4493,7 @@ class t3lib_TCEmain       {
                                                                                        }
                                                                                }
                                                                                unset($swapVersion['uid']);
-                                                                               
+
                                                                                        // Modify online version to become offline:
                                                                                unset($curVersion['uid']);
                                                                                $curVersion['pid'] = -1;        // Set pid for OFFLINE
@@ -3987,12 +4501,16 @@ class t3lib_TCEmain     {
                                                                                $curVersion['t3ver_wsid'] = $swapIntoWS ? intval($tmp_wsid) : 0;
                                                                                $curVersion['t3ver_tstamp'] = time();
                                                                                $curVersion['t3ver_count'] = $curVersion['t3ver_count']+1;      // Increment lifecycle counter
-                                                                               $curVersion['t3ver_stage'] = $curVersion['t3ver_state'] = 0;
+                                                                               $curVersion['t3ver_stage'] = 0;
+                                                                               if (!$swapIntoWS)       $curVersion['t3ver_state'] = 0;
 
                                                                                if ($table==='pages') {         // Keeping the swapmode state
                                                                                                $curVersion['t3ver_swapmode'] = $swapVersion['t3ver_swapmode'];
                                                                                }
 
+                                                                                       // Registering and swapping MM relations in current and swap records:
+                                                                               $this->version_remapMMForVersionSwap($table,$id,$swapWith);
+
                                                                                        // Execute swapping:
                                                                                $sqlErrors = array();
                                                                                $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($id),$swapVersion);
@@ -4006,11 +4524,21 @@ class t3lib_TCEmain     {
                                                                                                unlink($lockFileName);
                                                                                        }
                                                                                }
-
+                                                                               
                                                                                if (!count($sqlErrors)) {
+                                                                                       
+                                                                                               // If a moving operation took place...:
+                                                                                       if ($movePlhID) {
+                                                                                               if (!$swapIntoWS)       {       // Remove, if normal publishing:
+                                                                                                       $this->deleteEl($table, $movePlhID, TRUE, TRUE);        // For delete + completely delete!
+                                                                                               } else {        // Otherwise update the movePlaceholder:
+                                                                                                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($movePlhID),$movePlh);
+                                                                                                       $this->updateRefIndex($table,$movePlhID);
+                                                                                               }
+                                                                                       }
 
                                                                                                // Checking for delete:
-                                                                                       if ($t3ver_state['swapVersion']==2)     {
+                                                                                       if (!$swapIntoWS && ((int)$t3ver_state['swapVersion']===1 || (int)$t3ver_state['swapVersion']===2))     {       // Delete only if new/deleted placeholders are there.
                                                                                                $this->deleteEl($table,$id,TRUE);       // Force delete
                                                                                        }
 
@@ -4047,7 +4575,7 @@ class t3lib_TCEmain       {
                                                                                        $this->clear_cache($table,$id);
 
                                                                                                // Checking for "new-placeholder" and if found, delete it (BUT FIRST after swapping!):
-                                                                                       if ($t3ver_state['curVersion']==1)      {
+                                                                                       if (!$swapIntoWS && $t3ver_state['curVersion']>0)       {
                                                                                                $this->deleteEl($table, $swapWith, TRUE, TRUE);         // For delete + completely delete!
                                                                                        }
                                                                                } else $this->newlog('During Swapping: SQL errors happend: '.implode('; ',$sqlErrors),2);
@@ -4066,9 +4594,12 @@ class t3lib_TCEmain      {
         *
         * @param       string          Table name
         * @param       integer         Record UID
+        * @param       boolean         If set, will completely delete element
         * @return      void
         */
-       function version_clearWSID($table,$id)  {
+       function version_clearWSID($table,$id,$flush=FALSE)     {
+               global $TCA;
+
                if ($errorCode = $this->BE_USER->workspaceCannotEditOfflineVersion($table, $id))        {
                        $this->newlog('Attempt to reset workspace for record failed: '.$errorCode,1);
                } elseif ($this->checkRecordUpdateAccess($table,$id)) {
@@ -4079,16 +4610,23 @@ class t3lib_TCEmain     {
                                $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($id),$sArray);
 
                                        // Clear workspace ID for live version AND DELETE IT as well because it is a new record!
-                               if ((int)$liveRec['t3ver_state']===1)   {
+                               if ((int)$liveRec['t3ver_state']==1 || (int)$liveRec['t3ver_state']==2) {
                                        $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.intval($liveRec['uid']),$sArray);
                                        $this->deleteEl($table, $liveRec['uid'], TRUE); // THIS assumes that the record was placeholder ONLY for ONE record (namely $id)
                                }
 
                                        // If "deleted" flag is set for the version that got released it doesn't make sense to keep that "placeholder" anymore and we delete it completly.
                                $wsRec = t3lib_BEfunc::getRecord($table,$id);
-                               if ((int)$wsRec['t3ver_state']===2)     {
+                               if ($flush || ((int)$wsRec['t3ver_state']==1 || (int)$wsRec['t3ver_state']==2)) {
                                        $this->deleteEl($table, $id, TRUE, TRUE);
                                }
+
+                                       // Remove the move-placeholder if found for live record.
+                               if ((int)$TCA[$table]['ctrl']['versioningWS']>=2)       {
+                                       if ($plhRec = t3lib_BEfunc::getMovePlaceholder($table,$liveRec['uid'],'uid'))   {
+                                               $this->deleteEl($table, $plhRec['uid'], TRUE, TRUE);
+                                       }
+                               }
                        }
                } else $this->newlog('Attempt to reset workspace for record failed because you do not have edit access',1);
        }
@@ -4100,9 +4638,10 @@ class t3lib_TCEmain      {
         * @param       integer         Record UID
         * @param       integer         Stage ID to set
         * @param       string          Comment that goes into log
+        * @param       boolean         Accumulate state changes in memory for compiled notification email?
         * @return      void
         */
-       function version_setStage($table,$id,$stageId,$comment='')      {
+       function version_setStage($table,$id,$stageId,$comment='',$accumulateForNotifEmail=FALSE)       {
                if ($errorCode = $this->BE_USER->workspaceCannotEditOfflineVersion($table, $id))        {
                        $this->newlog('Attempt to set stage for record failed: '.$errorCode,1);
                } elseif ($this->checkRecordUpdateAccess($table,$id)) {
@@ -4118,7 +4657,12 @@ class t3lib_TCEmain      {
 $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stage'=>$stageId));
 
                                if ((int)$stat['stagechg_notification']>0)      {
-                                       $this->notifyStageChange($stat,$stageId,$table,$id,$comment);
+                                       if ($accumulateForNotifEmail)   {
+                                               $this->accumulateForNotifEmail[$stat['uid'].':'.$stageId.':'.$comment]['shared'] = array($stat,$stageId,$comment);
+                                               $this->accumulateForNotifEmail[$stat['uid'].':'.$stageId.':'.$comment]['elements'][] = $table.':'.$id;
+                                       } else {
+                                               $this->notifyStageChange($stat,$stageId,$table,$id,$comment);
+                                       }
                                }
                        } else $this->newlog('The member user tried to set a stage value "'.$stageId.'" that was not allowed',1);
                } else $this->newlog('Attempt to set stage for record failed because you do not have edit access',1);
@@ -4136,7 +4680,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         */
        function version_swap_procBasedOnFieldType($table,$field,$conf,&$curVersion,&$swapVersion) {
                $inlineType = $this->getInlineFieldType($conf);
-               
+
                        // Process pointer fields on normalized database:
                if ($inlineType == 'field') {
                                // Read relations that point to the current record (e.g. live record):
@@ -4148,7 +4692,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                // Update relations for both (workspace/versioning) sites:
                        $dbAnalysisCur->writeForeignField($conf,$curVersion['uid'],$swapVersion['uid']);
                        $dbAnalysisSwap->writeForeignField($conf,$swapVersion['uid'],$curVersion['uid']);
-                       
+
                        // Swap field values (CSV):
                        // BUT: These values will be swapped back in the next steps, when the *CHILD RECORD ITSELF* is swapped!
                } elseif ($inlineType == 'list') {
@@ -4157,7 +4701,144 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                        $swapVersion[$field] = $tempValue;
                }
        }
+       
+       /**
+        * Swaps MM-relations for current/swap record, see version_swap()
+        *
+        * @param       string  Table for the two input records
+        * @param       integer Current record (about to go offline)
+        * @param       integer Swap record (about to go online)
+        * @return void
+        * @see version_swap()
+        */
+       function version_remapMMForVersionSwap($table,$id,$swapWith)    {
+               global $TCA;
+               
+                       // Actually, selecting the records fully is only need if flexforms are found inside... This could be optimized ...
+               $currentRec = t3lib_BEfunc::getRecord($table,$id);
+               $swapRec = t3lib_BEfunc::getRecord($table,$swapWith);
+               
+               $this->version_remapMMForVersionSwap_reg = array();
+               
+               foreach($TCA[$table]['columns'] as $field => $fConf) {
+                       $conf = $fConf['config'];
+                       
+                       if ($this->isReferenceField($conf))     {
+                               $allowedTables = $conf['type']=='group' ? $conf['allowed'] : $conf['foreign_table'].','.$conf['neg_foreign_table'];
+                               $prependName = $conf['type']=='group' ? $conf['prepend_tname'] : $conf['neg_foreign_table'];
+                               if ($conf['MM'])        {
+                                       
+                                       $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                                       /* @var $dbAnalysis t3lib_loadDBGroup */
+                                       $dbAnalysis->start('', $allowedTables, $conf['MM'], $id, $table, $conf);
+                                       if (count($dbAnalysis->getValueArray($prependName)))    {       
+                                               $this->version_remapMMForVersionSwap_reg[$id][$field] = array($dbAnalysis, $conf['MM'], $prependName);
+                                       }
+                                                                               
+                                       $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                                       /* @var $dbAnalysis t3lib_loadDBGroup */
+                                       $dbAnalysis->start('', $allowedTables, $conf['MM'], $swapWith, $table, $conf);
+                                       if (count($dbAnalysis->getValueArray($prependName)))    {       
+                                               $this->version_remapMMForVersionSwap_reg[$swapWith][$field] = array($dbAnalysis, $conf['MM'], $prependName);
+                                       }
+                               }
+                       } elseif($conf['type']=='flex') {
+
+                                       // Current record
+                               $dataStructArray = t3lib_BEfunc::getFlexFormDS($conf, $currentRec, $table);
+                               $currentValueArray = t3lib_div::xml2array($currentRec[$field]);
+
+                               if (is_array($currentValueArray))       {
+                                       $this->checkValue_flex_procInData(
+                                               $currentValueArray['data'],
+                                               array(),        // Not used.
+                                               array(),        // Not used.
+                                               $dataStructArray,
+                                               array($table,$id,$field),       // Parameters.
+                                               'version_remapMMForVersionSwap_flexFormCallBack'
+                                       );
+                               }
 
+                                       // Swap record
+                               $dataStructArray = t3lib_BEfunc::getFlexFormDS($conf, $swapRec, $table);
+                               $currentValueArray = t3lib_div::xml2array($swapRec[$field]);
+
+                               if (is_array($currentValueArray))       {
+                                       $this->checkValue_flex_procInData(
+                                               $currentValueArray['data'],
+                                               array(),        // Not used.
+                                               array(),        // Not used.
+                                               $dataStructArray,
+                                               array($table,$swapWith,$field), // Parameters.
+                                               'version_remapMMForVersionSwap_flexFormCallBack'
+                                       );
+                               }
+                       }
+               }
+               
+                       // Execute:
+               $this->version_remapMMForVersionSwap_execSwap($table,$id,$swapWith);
+       }
+
+       /**
+        * Callback function for traversing the FlexForm structure in relation to ...
+        *
+        * @param       array           Array of parameters in num-indexes: table, uid, field
+        * @param       array           TCA field configuration (from Data Structure XML)
+        * @param       string          The value of the flexForm field
+        * @param       string          Not used.
+        * @param       string          Not used.
+        * @param       string          Path in flexforms
+        * @return      array           Result array with key "value" containing the value of the processing.
+        * @see version_remapMMForVersionSwap(), checkValue_flex_procInData_travDS()
+        */
+       function version_remapMMForVersionSwap_flexFormCallBack($pParams, $dsConf, $dataValue, $dataValue_ext1, $dataValue_ext2, $path) {
+
+                       // Extract parameters:
+               list($table, $uid, $field) = $pParams;
+
+               if ($this->isReferenceField($dsConf)) {
+                       $allowedTables = $dsConf['type']=='group' ? $dsConf['allowed'] : $dsConf['foreign_table'].','.$dsConf['neg_foreign_table'];
+                       $prependName = $dsConf['type']=='group' ? $dsConf['prepend_tname'] : $dsConf['neg_foreign_table'];
+                       if ($dsConf['MM'])      {
+                               $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
+                               /* @var $dbAnalysis t3lib_loadDBGroup */
+                               $dbAnalysis->start('', $allowedTables, $dsConf['MM'], $uid, $table, $dsConf);                           
+                               $this->version_remapMMForVersionSwap_reg[$uid][$field.'/'.$path] = array($dbAnalysis, $dsConf['MM'], $prependName);
+                       }
+               }
+       }
+
+       /**
+        * Performing the remapping operations found necessary in version_remapMMForVersionSwap()
+        * It must be done in three steps with an intermediate "fake" uid. The UID can be something else than -$id (fx. 9999999+$id if you dare... :-)- as long as it is unique.
+        *
+        * @param       string  Table for the two input records
+        * @param       integer Current record (about to go offline)
+        * @param       integer Swap record (about to go online)
+        * @return void
+        * @see version_remapMMForVersionSwap()
+        */
+       function version_remapMMForVersionSwap_execSwap($table,$id,$swapWith)   {
+               
+               if (is_array($this->version_remapMMForVersionSwap_reg[$id]))    {
+                       foreach($this->version_remapMMForVersionSwap_reg[$id] as $field => $str)        {
+                               $str[0]->remapMM($str[1],$id,-$id,$str[2]);
+                       }
+               }
+
+               if (is_array($this->version_remapMMForVersionSwap_reg[$swapWith]))      {
+                       foreach($this->version_remapMMForVersionSwap_reg[$swapWith] as $field => $str)  {
+                               $str[0]->remapMM($str[1],$swapWith,$id,$str[2]);
+                       }
+               }
+
+               if (is_array($this->version_remapMMForVersionSwap_reg[$id]))    {
+                       foreach($this->version_remapMMForVersionSwap_reg[$id] as $field => $str)        {
+                               $str[0]->remapMM($str[1],-$id,$swapWith,$str[2]);
+                       }
+               }
+       }
 
 
 
@@ -4313,8 +4994,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                        // If a change has been done, set the new value(s)
                if ($set)       {
                        if ($conf['MM'])        {
-// FIXME $theUidToUpdate is undefined
-                               $dbAnalysis->writeMM($conf['MM'], $theUidToUpdate, $prependName);
+                               $dbAnalysis->writeMM($conf['MM'], $MM_localUid, $prependName);
                        } else {
                                $vArray = $dbAnalysis->getValueArray($prependName);
                                if ($conf['type']=='select')    {
@@ -4332,7 +5012,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         * @param       string          $value: Field value
         * @param       integer         $uid: The uid of the ORIGINAL record
         * @param       string          $table: Table name
-        * @return      string          The value to be updated on the table field in the database
+        * @return      void
         */
        function remapListedDBRecords_procInline($conf, $value, $uid, $table) {
                $theUidToUpdate = $this->copyMappingArray_merged[$table][$uid];
@@ -4340,13 +5020,36 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                if ($conf['foreign_table']) {
                        $inlineType = $this->getInlineFieldType($conf);
 
-                       if ($inlineType == 'field') {
+                       if ($inlineType == 'mm') {
+                               $this->remapListedDBRecords_procDBRefs($conf, $value, $theUidToUpdate, $table);
+
+                       } elseif ($inlineType !== false) {
                                $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
-                               $dbAnalysis->start($value, $conf['foreign_table'], $conf['MM'], 0, $table, $conf);
+                               $dbAnalysis->start($value, $conf['foreign_table'], '', 0, $table, $conf);
+
+                                       // If the current field is set on a page record, update the pid of related child records:
+                               if ($table == 'pages') {
+                                       $thePidToUpdate = $theUidToUpdate;
+                                       // If the current field has ancestors that have a field on a page record, update the pid of related child records:
+                               } elseif (isset($this->registerDBPids[$table][$uid])) {
+                                       $thePidToUpdate = $this->registerDBPids[$table][$uid];
+                                       $thePidToUpdate = $this->copyMappingArray_merged['pages'][$thePidToUpdate];
+                               }
+
+                                       // Update child records if using pointer fields ('foreign_field'):
+                               if ($inlineType == 'field') {
+                                       $dbAnalysis->writeForeignField($conf, $uid, $theUidToUpdate);
+                               }
 
-                               $dbAnalysis->writeForeignField($conf, $uid, $theUidToUpdate);
-                       } elseif ($inlineType == 'mm') {
-                               $vArray = $this->remapListedDBRecords_procDBRefs($conf, $value, $theUidToUpdate, $table);
+                                       // Update child records if change to pid is required:
+                               if ($thePidToUpdate) {
+                                       $updateValues = array('pid' => $thePidToUpdate);
+                                       foreach ($dbAnalysis->itemArray as $v) {
+                                               if ($v['id'] && $v['table']) {
+                                                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery($v['table'], 'uid='.intval($v['id']), $updateValues);
+                                               }
+                                       }
+                               }
                        }
                }
        }
@@ -4360,12 +5063,13 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
        function processRemapStack() {
                if(is_array($this->remapStack)) {
                        foreach($this->remapStack as $remapAction) {
-                                       // if no position index for the arguments was set, skip this remap action
+                                       // If no position index for the arguments was set, skip this remap action:
                                if (!is_array($remapAction['pos'])) continue;
 
-                                       // load values from the argument array in remapAction
+                                       // Load values from the argument array in remapAction:
                                $field = $remapAction['field'];
                                $id = $remapAction['args'][$remapAction['pos']['id']];
+                               $rawId = $id;
                                $table = $remapAction['args'][$remapAction['pos']['table']];
                                $valueArray = $remapAction['args'][$remapAction['pos']['valueArray']];
                                $tcaFieldConf = $remapAction['args'][$remapAction['pos']['tcaFieldConf']];
@@ -4393,26 +5097,42 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                                                $value = $this->autoVersionIdMap[$foreign_table][$value];
                                                        }
                                                                // Set a hint that this was a new child record:
-                                                       $this->newRelatedIDs[$table][] = $value;
+                                                       $this->newRelatedIDs[$foreign_table][] = $value;
                                                        $valueArray[$key] = $value;
                                                }
                                        }
                                        $remapAction['args'][$remapAction['pos']['valueArray']] = $valueArray;
                                }
 
-                                       // process the arguments with the defined function
-                               $remapAction['args'][$remapAction['pos']['valueArray']] = call_user_func_array(
+                                       // Process the arguments with the defined function:
+                               $newValue = call_user_func_array(
                                        array($this, $remapAction['func']),
                                        $remapAction['args']
                                );
-
-                                       // @TODO: Add option to disable count-field
-                               $newVal = $this->checkValue_checkMax($tcaFieldConf, $remapAction['args'][$remapAction['pos']['valueArray']]);
-                               $this->updateDB($table,$id,array($field => implode(',', $newVal)));
+                                       // If array is returned, check for maxitems condition, if string is returned this was already done:
+                               if (is_array($newValue)) {
+                                       $newValue = implode(',', $this->checkValue_checkMax($tcaFieldConf, $newValue));
+                               }
+                                       // Update in database (list of children (csv) or number of relations (foreign_field)):
+                               $this->updateDB($table, $id, array($field => $newValue));
+                                       // Process waiting Hook: processDatamap_afterDatabaseOperations:
+                               if (isset($this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'])) {
+                                       $hookArgs = $this->remapStackRecords[$table][$rawId]['processDatamap_afterDatabaseOperations'];
+                                               // Update field with remapped data:
+                                       $hookArgs['fieldArray'][$field] = $newValue;
+                                               // Process waiting hook objects:
+                                       $hookObjectsArr = $hookArgs['hookObjectsArr'];
+                                       foreach($hookObjectsArr as $hookObj)    {
+                                               if (method_exists($hookObj, 'processDatamap_afterDatabaseOperations')) {
+                                                       $hookObj->processDatamap_afterDatabaseOperations($hookArgs['status'], $table, $rawId, $hookArgs['fieldArray'], $this);
+                                               }
+                                       }
+                               }
                        }
                }
                        // Reset:
                $this->remapStack = array();
+               $this->remapStackRecords = array();
        }
 
        /**
@@ -4447,7 +5167,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                // Clean up the $registerDBList array:
                        unset($registerDBList[$table][$id]);
                        if (!count($registerDBList[$table])) unset($registerDBList[$table]);
-               }                                                                                               
+               }
        }
 
 
@@ -4560,7 +5280,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                }
                return $res;
        }
-
+       
        /**
         * Checks if a table is allowed on a certain page id according to allowed tables set for the page "doktype" and its [ctrl][rootLevel]-settings if any.
         *
@@ -4881,7 +5601,9 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                if (is_array($TCA[$table]))     {
                        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fieldList, $table, 'uid='.intval($id));
                        if ($GLOBALS['TYPO3_DB']->sql_num_rows($res))   {
-                               return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $result = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
+                               $GLOBALS['TYPO3_DB']->sql_free_result($res);
+                               return $result;
                        }
                }
        }
@@ -5122,7 +5844,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         * @param       integer         Log entry ID, important for linking between log and history views
         * @return      void
         */
-       function setHistory($table,$id,$logId)          {
+       function setHistory($table,$id,$logId)  {
                if (isset($this->historyRecords[$table.':'.$id]))       {
 
                                // Initialize settings:
@@ -5136,17 +5858,17 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                        $this->clearHistory($maxAgeSeconds, $table);
 
                                // Set history data:
-                               $fields_values = array();
-                               $fields_values['history_data'] = serialize($this->historyRecords[$table.':'.$id]);
-                               $fields_values['fieldlist'] = implode(',',array_keys($this->historyRecords[$table.':'.$id]['newRecord']));
-                               $fields_values['tstamp'] = time();
-                               $fields_values['tablename'] = $table;
-                               $fields_values['recuid'] = $id;
-                               $fields_values['sys_log_uid'] = $logId;
+                       $fields_values = array();
+                       $fields_values['history_data'] = serialize($this->historyRecords[$table.':'.$id]);
+                       $fields_values['fieldlist'] = implode(',',array_keys($this->historyRecords[$table.':'.$id]['newRecord']));
+                       $fields_values['tstamp'] = time();
+                       $fields_values['tablename'] = $table;
+                       $fields_values['recuid'] = $id;
+                       $fields_values['sys_log_uid'] = $logId;
 
-                               $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_history', $fields_values);
-                       }
+                       $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_history', $fields_values);
                }
+       }
 
        /**
         * Clearing sys_history table from older entries that are expired.
@@ -5171,6 +5893,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         */
        function updateRefIndex($table,$id)     {
                $refIndexObj = t3lib_div::makeInstance('t3lib_refindex');
+               /* @var $refIndexObj t3lib_refindex */
                $result = $refIndexObj->updateRefIndexTable($table,$id);
        }
 
@@ -5229,18 +5952,18 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                                $row =  $lookForLiveVersion;
                                        }
 
-                                               // If the record happends to be it self
+                                               // If the record should be inserted after itself, keep the current sorting information:
                                        if ($row['uid']==$uid)  {
                                                $sortNumber = $row[$sortRow];
                                        } else {
                                                $subres = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
-                                                                               $sortRow.',pid,uid',
-                                                                               $table,
-                                                                               'pid='.intval($row['pid']).' AND '.$sortRow.'>='.intval($row[$sortRow]).$this->deleteClause($table),
-                                                                               '',
-                                                                               $sortRow.' ASC',
-                                                                               '2'
-                                                                       );              // Fetches the next record in order to calculate the in between sortNumber
+                                                       $sortRow.',pid,uid',
+                                                       $table,
+                                                       'pid='.intval($row['pid']).' AND '.$sortRow.'>='.intval($row[$sortRow]).$this->deleteClause($table),
+                                                       '',
+                                                       $sortRow.' ASC',
+                                                       '2'
+                                               );              // Fetches the next record in order to calculate the in-between sortNumber
                                                if ($GLOBALS['TYPO3_DB']->sql_num_rows($subres)==2)     {       // There was a record afterwards
                                                        $GLOBALS['TYPO3_DB']->sql_fetch_assoc($subres);                         // Forward to the second result...
                                                        $subrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($subres);       // There was a record afterwards
@@ -5251,6 +5974,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                                } else {        // If after the last record in the list, we just add the sortInterval to the last sortvalue
                                                        $sortNumber = $row[$sortRow]+$this->sortIntervals;
                                                }
+                                               $GLOBALS['TYPO3_DB']->sql_free_result($subres);
                                        }
                                        return Array('pid' => $row['pid'], 'sortNumber' => $sortNumber);
                                } else {
@@ -5559,7 +6283,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
        function dbAnalysisStoreExec()  {
                reset($this->dbAnalysisStore);
                while(list($k,$v)=each($this->dbAnalysisStore)) {
-                       $id = $this->substNEWwithIDs[$v[2]];
+                       $id = t3lib_BEfunc::wsMapId($v[4],$this->substNEWwithIDs[$v[2]]);
                        if ($id)        {
                                $v[2] = $id;
                                $v[0]->writeMM($v[1],$v[2],$v[3]);
@@ -5744,7 +6468,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         * @return      boolean         True if DB reference field (group/db or select with foreign-table)
         */
        function isReferenceField($conf)        {
-               return ($conf['type']=='group' && $conf['internal_type']=='db') ||      ($conf['type']=='select' && $conf['foreign_table']);
+               return ($conf['type']=='group' && $conf['internal_type']=='db' || $conf['type']=='select' && $conf['foreign_table']);
        }
 
        /**
@@ -5766,6 +6490,7 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                return false;
        }
 
+
        /**
         * Get modified header for a copied record
         *
@@ -5919,13 +6644,14 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
         *
         * @param       array           Workspace access array (from t3lib_userauthgroup::checkWorkspace())
         * @param       integer         New Stage number: 0 = editing, 1= just ready for review, 10 = ready for publication, -1 = rejected!
-        * @param       string          Table name of element
-        * @param       integer         Record uid of element
+        * @param       string          Table name of element (or list of element names if $id is zero)
+        * @param       integer         Record uid of element (if zero, then $table is used as reference to element(s) alone)
         * @param       string          User comment sent along with action
         * @return      void
         */
        function notifyStageChange($stat,$stageId,$table,$id,$comment)  {
                $workspaceRec = t3lib_BEfunc::getRecord('sys_workspace', $stat['uid']);
+               $elementName = $id ? $table.':'.$id : $table;   // So, if $id is not set, then $table is taken to be the complete element name!
 
                if (is_array($workspaceRec))    {
 
@@ -5978,13 +6704,24 @@ $this->log($table,$id,6,0,0,'Stage raised...',30,array('comment'=>$comment,'stag
                                break;
                        }
                        $emails = array_unique($emails);
-
-                               // Send email:
-                       if (count($emails))     {
-                               $message = sprintf('
+                       
+                               // Path to record is found:
+                       list($eTable,$eUid) = explode(':',$elementName);
+                       $eUid = intval($eUid);
+                       $rr = t3lib_BEfunc::getRecord($eTable,$eUid);
+                       $recTitle = t3lib_BEfunc::getRecordTitle($eTable,$rr);                  
+                       if ($eTable!='pages')   {
+                               t3lib_BEfunc::fixVersioningPid($eTable,$rr);
+                               $eUid=$rr['pid'];
+                       }
+                       $path = t3lib_BEfunc::getRecordPath($eUid,'',20);
+                       
+                               // ALternative messages:
+                       $TSConfig = $this->getTCEMAIN_TSconfig($eUid);
+                       $body = trim($TSConfig['notificationEmail_body']) ? trim($TSConfig['notificationEmail_body']) : '
 At the TYPO3 site "%s" (%s)
 in workspace "%s" (#%s)
-the stage has changed for the element "%s":
+the stage has changed for the element(s) "%11$s" (%s) at location "%10$s" in the page tree:
 
 ==> %s
 
@@ -5992,20 +6729,27 @@ User Comment:
 "%s"
 
 State was change by %s (username: %s)
-                               ',
+                       ';
+                       $subject = trim($TSConfig['notificationEmail_subject']) ? trim($TSConfig['notificationEmail_subject']) : 'TYPO3 Workspace Note: Stage Change for %s';
+                       
+                               // Send email:
+                       if (count($emails))     {
+                               $message = sprintf($body,
                                $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
                                t3lib_div::getIndpEnv('TYPO3_SITE_URL').TYPO3_mainDir,
                                $workspaceRec['title'],
                                $workspaceRec['uid'],
-                               $table.':'.$id,
+                               $elementName,
                                $newStage,
                                $comment,
                                $this->BE_USER->user['realName'],
-                               $this->BE_USER->user['username']);
+                               $this->BE_USER->user['username'],
+                               $path,
+                               $recTitle);
 
                                t3lib_div::plainMailEncoded(
                                        implode(',',$emails),
-                                       'TYPO3 Workspace Note: Stage Change for '.$table.':'.$id,
+                                       sprintf($subject,$elementName),
                                        trim($message)
                                );
                        }
@@ -6039,6 +6783,24 @@ State was change by %s (username: %s)
                return $emails;
        }
 
+       /**
+        * Determine if a record was copied or if a record is the result of a copy action.
+        *
+        * @param       string          $table: The tablename of the record
+        * @param       integer         $uid: The uid of the record
+        * @return      boolean         Returns true if the record is copied or is the result of a copy action
+        */
+       function isRecordCopied($table, $uid) {
+                       // If the record was copied:
+               if (isset($this->copyMappingArray[$table][$uid])) {
+                       return true;
+                       // If the record is the result of a copy action:
+               } elseif (isset($this->copyMappingArray[$table]) && in_array($uid, array_values($this->copyMappingArray[$table]))) {
+                       return true;
+               }
+               return false;
+       }
+
 
 
 
@@ -6452,7 +7214,7 @@ State was change by %s (username: %s)
                        }
                }
        }
-       
+
        /**
         * Finds real page IDs for state change.
         *
@@ -6467,7 +7229,7 @@ State was change by %s (username: %s)
                        }
                }
        }
-       
+
        /**
         * Finds all elements for swapping versions in workspace
         *