1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[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 Sonata\AdminBundle\Action; |
13
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
15
|
|
|
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface; |
16
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
17
|
|
|
use Sonata\AdminBundle\Search\SearchHandler; |
18
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
20
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
21
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
|
25
|
|
|
final class SearchAction extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Pool |
29
|
|
|
*/ |
30
|
|
|
private $pool; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var SearchHandler |
34
|
|
|
*/ |
35
|
|
|
private $searchHandler; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TemplateRegistryInterface |
39
|
|
|
*/ |
40
|
|
|
private $templateRegistry; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var BreadcrumbsBuilderInterface |
44
|
|
|
*/ |
45
|
|
|
private $breadcrumbsBuilder; |
46
|
|
|
|
47
|
|
|
public function __construct( |
48
|
|
|
Pool $pool, |
49
|
|
|
SearchHandler $searchHandler, |
50
|
|
|
TemplateRegistryInterface $templateRegistry, |
51
|
|
|
BreadcrumbsBuilderInterface $breadcrumbsBuilder |
52
|
|
|
) { |
53
|
|
|
$this->pool = $pool; |
54
|
|
|
$this->searchHandler = $searchHandler; |
55
|
|
|
$this->templateRegistry = $templateRegistry; |
56
|
|
|
$this->breadcrumbsBuilder = $breadcrumbsBuilder; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The search action first render an empty page, if the query is set, then the template generates |
61
|
|
|
* some ajax request to retrieve results for each admin. The Ajax query returns a JSON response. |
62
|
|
|
* |
63
|
|
|
* @throws \RuntimeException |
64
|
|
|
* |
65
|
|
|
* @return JsonResponse|Response |
66
|
|
|
*/ |
67
|
|
|
public function __invoke(Request $request) |
68
|
|
|
{ |
69
|
|
|
if (!$request->get('admin') || !$request->isXmlHttpRequest()) { |
70
|
|
|
return $this->render($this->templateRegistry->getTemplate('search'), [ |
71
|
|
|
'base_template' => $request->isXmlHttpRequest() ? |
72
|
|
|
$this->templateRegistry->getTemplate('ajax') : |
73
|
|
|
$this->templateRegistry->getTemplate('layout'), |
74
|
|
|
'breadcrumbs_builder' => $this->breadcrumbsBuilder, |
75
|
|
|
'admin_pool' => $this->pool, |
76
|
|
|
'query' => $request->get('q'), |
77
|
|
|
'groups' => $this->pool->getDashboardGroups(), |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$admin = $this->pool->getAdminByAdminCode($request->get('admin')); |
83
|
|
|
} catch (ServiceNotFoundException $e) { |
84
|
|
|
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!$admin instanceof AdminInterface) { |
88
|
|
|
throw new \RuntimeException('The requested service is not an Admin instance'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$results = []; |
92
|
|
|
|
93
|
|
|
$page = false; |
94
|
|
|
$total = false; |
95
|
|
|
if ($pager = $this->searchHandler->search( |
96
|
|
|
$admin, |
97
|
|
|
$request->get('q'), |
98
|
|
|
$request->get('page'), |
99
|
|
|
$request->get('offset') |
100
|
|
|
)) { |
101
|
|
|
foreach ($pager->getResults() as $result) { |
102
|
|
|
$results[] = [ |
103
|
|
|
'label' => $admin->toString($result), |
104
|
|
|
'link' => $admin->generateObjectUrl('edit', $result), |
105
|
|
|
'id' => $admin->id($result), |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
$page = (int) $pager->getPage(); |
109
|
|
|
$total = (int) $pager->getNbResults(); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$response = new JsonResponse([ |
113
|
|
|
'results' => $results, |
114
|
|
|
'page' => $page, |
115
|
|
|
'total' => $total, |
116
|
|
|
]); |
117
|
|
|
$response->setPrivate(); |
118
|
|
|
|
119
|
|
|
return $response; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.