From 8b43aa7d54cf79ea285fcb7f0ee30fdf4e370d40 Mon Sep 17 00:00:00 2001
From: Steffen Ritter <info@rs-websystems.de>
Date: Mon, 3 Sep 2012 11:06:40 +0200
Subject: [PATCH] [BUGFIX] Make FAL Folder not found handling more user
 friendly

If an user moves the folder he currently views in the file
list module, after reload he sees only a red FlashMessage
error because the viewed folder is gone.

As the JavaScript cannot determine what might be the
identifier of the moved folder (after moving) it cannot
redirect there. Thus the file module needs to react more
user friendly in such a situation.

As a solution the module tries parent folders in, too.

Change-Id: Id9e8d988b159c3f0a21b41c4d1782101146ad23c
Fixes: #40547
Releases: 6.0
Reviewed-on: http://review.typo3.org/14301
Tested-by: Marcel Burkhalter
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Benjamin Mack
Tested-by: Benjamin Mack
---
 .../Classes/Controller/FileListController.php | 66 +++++++++++--------
 typo3/sysext/lang/locallang_mod_file_list.xlf |  4 +-
 2 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/typo3/sysext/filelist/Classes/Controller/FileListController.php b/typo3/sysext/filelist/Classes/Controller/FileListController.php
index dd708d82410f..2e67c0279b70 100644
--- a/typo3/sysext/filelist/Classes/Controller/FileListController.php
+++ b/typo3/sysext/filelist/Classes/Controller/FileListController.php
@@ -51,7 +51,7 @@ class FileListController {
 	/* @var \TYPO3\CMS\Core\Resource\Folder $folderObject */
 	protected $folderObject;
 
-	/* @var t3lib_FlashMessage $errorMessage */
+	/* @var \TYPO3\CMS\Core\Messaging\FlashMessage $errorMessage */
 	protected $errorMessage;
 
 	// Pointer to listing
@@ -99,32 +99,45 @@ class FileListController {
 		$this->overwriteExistingFiles = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('overwriteExistingFiles');
 		// Setting module name:
 		$this->MCONF = $GLOBALS['MCONF'];
-		// Create the folder object
-		try {
-			if ($combinedIdentifier) {
-				$fileFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
-				$this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
-				// Disallow the rendering of the processing folder (e.g. could be called manually)
-				// and all folders without any defined storage
-				if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') == trim($this->folderObject->getIdentifier(), '/'))) {
-					$this->folderObject = NULL;
-				}
-			} else {
-				// Take the first object of the first storage
-				$fileStorages = $GLOBALS['BE_USER']->getFileStorages();
-				$fileStorage = reset($fileStorages);
-				if ($fileStorage) {
-					// Validating the input "id" (the path, directory!) and
-					// checking it against the mounts of the user. - now done in the controller
-					$this->folderObject = $fileStorage->getRootLevelFolder();
+			// Create the folder object, even try parent folders
+		while ($this->folderObject === NULL && count(\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('/', $combinedIdentifier, TRUE)) > 1) {
+			try {
+				if ($combinedIdentifier) {
+					$fileFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
+					$this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
+					// Disallow the rendering of the processing folder (e.g. could be called manually)
+					// and all folders without any defined storage
+					if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') == trim($this->folderObject->getIdentifier(), '/'))) {
+						$this->folderObject = NULL;
+					}
 				} else {
-					$this->folderObject = NULL;
+					// Take the first object of the first storage
+					$fileStorages = $GLOBALS['BE_USER']->getFileStorages();
+					$fileStorage = reset($fileStorages);
+					if ($fileStorage) {
+						// Validating the input "id" (the path, directory!) and
+						// checking it against the mounts of the user. - now done in the controller
+						$this->folderObject = $fileStorage->getRootLevelFolder();
+					} else {
+						$this->folderObject = NULL;
+					}
 				}
+			} catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
+				// Set folder object to null and throw a message later on
+				$this->folderObject = NULL;
+				if ($this->id == $combinedIdentifier) {
+					$this->errorMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
+						sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE),
+								htmlspecialchars($this->id)
+						),
+						$GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE),
+						\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
+				}
+
+				$combinedIdentifierParts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('/', $combinedIdentifier, TRUE);
+				array_pop($combinedIdentifierParts);
+				$combinedIdentifier = implode('/', $combinedIdentifierParts) . '/';
 			}
-		} catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
-			// Set folder object to null and throw a message later on
-			$this->folderObject = NULL;
-			$this->errorMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE), htmlspecialchars($this->id)), $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
 		}
 		// Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
 		$this->menuConfig();
@@ -354,7 +367,7 @@ class FileListController {
 			$markerArray = array(
 				'CSH' => $docHeaderButtons['csh'],
 				'FUNC_MENU' => \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
-				'CONTENT' => $pageContent
+				'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent
 			);
 			$this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
 			// Renders the module page
@@ -362,6 +375,7 @@ class FileListController {
 		} else {
 			$content = '';
 			if ($this->errorMessage) {
+				$this->errorMessage->setSeverity(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
 				$content = $this->doc->moduleBody(array(), array_merge(array('LEVEL_UP' => '', 'REFRESH' => ''), $this->getButtons()), array('CSH' => '', 'TITLE' => '', 'FOLDER_INFO' => '', 'PAGE_ICON' => '', 'FUNC_MENU' => '', 'CONTENT' => $this->errorMessage->render()));
 			}
 			// Create output - no access (no warning though)
@@ -412,4 +426,4 @@ class FileListController {
 }
 
 
-?>
\ No newline at end of file
+?>
diff --git a/typo3/sysext/lang/locallang_mod_file_list.xlf b/typo3/sysext/lang/locallang_mod_file_list.xlf
index 02c2bf4af739..6e247083dd72 100644
--- a/typo3/sysext/lang/locallang_mod_file_list.xlf
+++ b/typo3/sysext/lang/locallang_mod_file_list.xlf
@@ -97,10 +97,10 @@
 				<source>You are trying to access a folder in a storage that is not browsable.</source>
 			</trans-unit>
 			<trans-unit id="folderNotFoundTitle" xml:space="preserve">
-				<source>Folder access denied.</source>
+				<source>Folder not found.</source>
 			</trans-unit>
 			<trans-unit id="folderNotFoundMessage" xml:space="preserve">
-				<source>The folder "%s" cannot be accessed.</source>
+				<source>The folder "%s" cannot be accessed. Trying to use parent folder(s).</source>
 			</trans-unit>
 		</body>
 	</file>
-- 
GitLab