diff --git a/Controller/ManagerController.php b/Controller/ManagerController.php index 3da00fd..5b9dfab 100644 --- a/Controller/ManagerController.php +++ b/Controller/ManagerController.php @@ -35,6 +35,7 @@ use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Contracts\Translation\TranslatorInterface; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Exception; /** @@ -51,12 +52,20 @@ public function __construct(private FilemanagerService $filemanagerService, priv } #[Route('/', name: 'file_manager')] - public function indexAction(Request $request, FileTypeService $fileTypeService): JsonResponse|Response { + public function indexAction(Request $request, FileTypeService $fileTypeService, SessionInterface $session): JsonResponse|Response { $queryParameters = $request->query->all(); $isJson = $request->get('json'); if ($isJson) { unset($queryParameters['json']); } + + // Remember Last Path - 31.08.2024 + if(isset($this->getParameter('artgris_file_manager')['conf'][$queryParameters['conf']]['remember_last_path']) + && $this->getParameter('artgris_file_manager')['conf'][$queryParameters['conf']]['remember_last_path'] + ){ + $queryParameters = $this->rememberLastPath($queryParameters, $session); + } + $fileManager = $this->newFileManager($queryParameters); // Folder search @@ -458,4 +467,36 @@ protected function dispatch(string $eventName, array $arguments = []) { $event = new GenericEvent($subject, $arguments); $this->dispatcher->dispatch($event, $eventName); } + + private function rememberLastPath(array $queryParameters, $session): array + { + // GET Last Path + if ($session->has('file_manager_last_path') && empty($queryParameters['route'])) { + + $fileSystem = new Filesystem(); + $last_path = $this->getParameter('artgris_file_manager')['conf'][$queryParameters['conf']]['dir'].$session->get('file_manager_last_path'); + $exist = $fileSystem->exists($last_path); + + if (false === $exist) { + $queryParameters['route'] = '/'; // if directory not exist return home path and clear session + if ($session->has('file_manager_last_path')) { + $session->remove('file_manager_last_path'); + } + } else { + $queryParameters['route'] = $session->get('file_manager_last_path'); + } + } + + // SET Last Path + if(isset($queryParameters['route'])){ + if ($session->has('file_manager_last_path')) { + $session->remove('file_manager_last_path'); + } + $session->set('file_manager_last_path', $queryParameters['route']); + } else { + $queryParameters['route'] = '/'; + } + + return $queryParameters; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 9a20e15..900a210 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -32,6 +32,7 @@ public function getConfigTreeBuilder(): TreeBuilder { ->children() ->scalarNode('dir')->end() ->enumNode('type')->values(['file', 'image', 'media'])->end() + ->booleanNode('remember_last_path')->defaultValue(true)->end() ->booleanNode('tree')->end() ->booleanNode('show_file_count')->defaultValue(true)->end() ->scalarNode('root_name')->defaultValue(null)->end() diff --git a/Helpers/FileManager.php b/Helpers/FileManager.php index 26ffd3b..60887eb 100644 --- a/Helpers/FileManager.php +++ b/Helpers/FileManager.php @@ -74,7 +74,8 @@ public function getParent(): ?string if (\DIRECTORY_SEPARATOR !== $parentRoute) { $queryParentParameters['route'] = \dirname($this->getRoute()); } else { - unset($queryParentParameters['route']); +// unset($queryParentParameters['route']); + $queryParentParameters['route'] = '/'; } $parentRoute = $this->router->generate('file_manager', $queryParentParameters); diff --git a/Resources/doc/book/1-basic-configuration.md b/Resources/doc/book/1-basic-configuration.md index 7828cc6..05f958e 100644 --- a/Resources/doc/book/1-basic-configuration.md +++ b/Resources/doc/book/1-basic-configuration.md @@ -54,6 +54,23 @@ artgris_file_manager: >Browse the `/manager/?conf=private` URL to get access to this File Manager +## `remember_last_path` Last worked directory + +| Option | Type | Required | Default value | +| :--- |:---------:|:--------:|:-------------:| +| `remember_last_path` | `Boolean` | False | true | + +> Allows opening folders to the last working directory without clicking on them repeatedly +> +Example: + ```yml +artgris_file_manager: + conf: + default: + remember_last_path: true +``` + + ## `tree` Display Folder Tree | Option | Type | Required | Default value | | :--- |:--------:|:--------:|:--------:| diff --git a/composer.json b/composer.json index d1fb89a..9dee445 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ "symfony/translation": "^6.0||^7.0", "symfony/twig-bridge": "^6.0||^7.0", "symfony/twig-bundle": "^6.0||^7.0", - "symfony/validator": "^6.0||^7.0" + "symfony/validator": "^6.0||^7.0", + "symfony/http-foundation": "^6.0||^7.0" }, "require-dev": { "phpunit/phpunit": "^9.5",