1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Controller; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Admin\MediaAdmin; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Twig\Environment; |
11
|
|
|
|
12
|
|
|
class HelperController |
13
|
|
|
{ |
14
|
|
|
const QUERY_MINIMUM_LENGTH = 3; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var MediaAdmin |
18
|
|
|
*/ |
19
|
|
|
private $mediaAdmin; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Environment |
23
|
|
|
*/ |
24
|
|
|
private $twig; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param MediaAdmin $mediaAdmin |
28
|
|
|
* @param Environment $twig |
29
|
|
|
*/ |
30
|
1 |
|
public function __construct(MediaAdmin $mediaAdmin, Environment $twig) |
31
|
|
|
{ |
32
|
1 |
|
$this->mediaAdmin = $mediaAdmin; |
33
|
1 |
|
$this->twig = $twig; |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Request $request |
38
|
|
|
* @return JsonResponse |
39
|
|
|
*/ |
40
|
1 |
|
public function getAutocompleteItemsAction(Request $request): JsonResponse |
41
|
|
|
{ |
42
|
1 |
|
$this->mediaAdmin->checkAccess('list'); |
43
|
|
|
|
44
|
1 |
|
if (mb_strlen($request->get('q'), 'UTF-8') < self::QUERY_MINIMUM_LENGTH) { |
45
|
1 |
|
return new JsonResponse(['status' => 'KO', 'message' => 'Search string too short'], Response::HTTP_FORBIDDEN); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return new JsonResponse([ |
49
|
1 |
|
'status' => 'OK', |
50
|
|
|
'more' => false, |
51
|
1 |
|
'items' => $this->transformResults($this->getPagerResults($request)) |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Request $request |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
1 |
|
protected function getPagerResults(Request $request): array |
60
|
|
|
{ |
61
|
1 |
|
$this->mediaAdmin->setPersistFilters(false); |
|
|
|
|
62
|
|
|
|
63
|
1 |
|
$datagrid = $this->mediaAdmin->getDatagrid(); |
64
|
1 |
|
$datagrid->setValue('title', null, $request->get('q')); |
65
|
1 |
|
$datagrid->setValue('_per_page', null, $request->query->get('_per_page', 10)); |
66
|
1 |
|
$datagrid->setValue('_page', null, $request->query->get('_page', 1)); |
67
|
1 |
|
if ($request->query->has('type')) { |
68
|
1 |
|
$datagrid->setValue('type', null, $request->query->get('type')); |
69
|
|
|
} |
70
|
1 |
|
if ($request->query->has('provider')) { |
71
|
1 |
|
$datagrid->setValue('provider', null, $request->query->get('provider')); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$datagrid->buildPager(); |
75
|
|
|
|
76
|
1 |
|
$pager = $datagrid->getPager(); |
77
|
1 |
|
return $pager->getResults(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param MediaInterface[] $results |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
1 |
|
protected function transformResults(array $results): array |
85
|
|
|
{ |
86
|
1 |
|
$items = []; |
87
|
1 |
|
foreach($results as $media) { |
88
|
1 |
|
$items[] = [ |
89
|
1 |
|
'id' => $media->getId(), |
90
|
1 |
|
'label' => $this->twig->render('@MediaMonksSonataMedia/CRUD/autocomplete.html.twig', [ |
91
|
1 |
|
'media' => $media |
92
|
|
|
]) |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $items; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.