Skip to content

Commit

Permalink
[New Future] Remember Last Path
Browse files Browse the repository at this point in the history
  • Loading branch information
xDeSwa committed Aug 31, 2024
1 parent e5e49f4 commit 23b904e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
43 changes: 42 additions & 1 deletion Controller/ManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion Helpers/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions Resources/doc/book/1-basic-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| :--- |:--------:|:--------:|:--------:|
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 23b904e

Please sign in to comment.