1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CMS Kernel package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Infrastructure\BenGorFileBundle\HttpAction; |
13
|
|
|
|
14
|
|
|
use BenGorFile\File\Application\Query\CountFilesHandler; |
15
|
|
|
use BenGorFile\File\Application\Query\CountFilesQuery; |
16
|
|
|
use BenGorFile\File\Application\Query\FilterFilesHandler; |
17
|
|
|
use BenGorFile\File\Application\Query\FilterFilesQuery; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Beñat Espiña <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class AjaxFileGalleryAction |
26
|
|
|
{ |
27
|
|
|
private $filterFilesHandler; |
28
|
|
|
private $countFilesHandler; |
29
|
|
|
private $urlGenerator; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
FilterFilesHandler $filterFilesHandler, |
33
|
|
|
CountFilesHandler $countFilesHandler, |
34
|
|
|
UrlGeneratorInterface $urlGenerator |
35
|
|
|
) { |
36
|
|
|
$this->filterFilesHandler = $filterFilesHandler; |
37
|
|
|
$this->countFilesHandler = $countFilesHandler; |
38
|
|
|
$this->urlGenerator = $urlGenerator; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __invoke(Request $request, $fileType) |
42
|
|
|
{ |
43
|
|
|
$query = $request->query->get('q'); |
44
|
|
|
$limit = $request->query->get('limit'); |
45
|
|
|
$offset = $request->query->get('page'); |
46
|
|
|
|
47
|
|
|
$files = $this->filterFilesHandler->__invoke( |
48
|
|
|
new FilterFilesQuery( |
49
|
|
|
$query, |
50
|
|
|
$offset, |
51
|
|
|
$limit |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
foreach ($files as $key => $file) { |
56
|
|
|
$files[$key]['preview_path'] = $this->previewPath($file['file_name'], $fileType); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$filesTotalCount = $this->countFilesHandler->__invoke( |
60
|
|
|
new CountFilesQuery(null) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return new JsonResponse([ |
64
|
|
|
'files' => $files, |
65
|
|
|
'files_total_count' => $filesTotalCount, |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function previewPath($filename, $fileType) |
70
|
|
|
{ |
71
|
|
|
return $this->urlGenerator->generate( |
72
|
|
|
'bengor_file_' . $fileType . '_download', |
73
|
|
|
[ |
74
|
|
|
'filename' => $filename, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|