|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Block; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
17
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
18
|
|
|
use Sonata\AdminBundle\Search\SearchHandler; |
|
19
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
20
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
25
|
|
|
use Twig\Environment; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
|
29
|
|
|
* |
|
30
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class AdminSearchBlockService extends AbstractBlockService |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Pool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $pool; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var SearchHandler |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $searchHandler; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* NEXT_MAJOR: Remove `$templating` argument. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Environment|string $twigOrName |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($twigOrName, ?EngineInterface $templating, Pool $pool, SearchHandler $searchHandler) |
|
50
|
|
|
{ |
|
51
|
|
|
// NEXT_MAJOR: Remove conditions, only pass the Environment |
|
52
|
|
|
if ($templating instanceof EngineInterface) { |
|
53
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
54
|
|
|
'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x' |
|
55
|
|
|
. ' and will throw a \TypeError in version 4.0. You must pass null instead.', |
|
56
|
|
|
EngineInterface::class, |
|
57
|
|
|
__METHOD__ |
|
58
|
|
|
), E_USER_DEPRECATED); |
|
59
|
|
|
|
|
60
|
|
|
if (!\is_string($twigOrName)) { |
|
61
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
62
|
|
|
'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.' |
|
63
|
|
|
. ' You must pass an string instead.', |
|
64
|
|
|
__METHOD__, |
|
65
|
|
|
EngineInterface::class |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
parent::__construct($twigOrName, $templating); |
|
|
|
|
|
|
70
|
|
|
} elseif ($twigOrName instanceof Environment) { |
|
71
|
|
|
if (!\is_null($templating)) { |
|
72
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
73
|
|
|
'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.', |
|
74
|
|
|
__METHOD__, |
|
75
|
|
|
Environment::class, |
|
76
|
|
|
EngineInterface::class |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
parent::__construct($twigOrName); |
|
81
|
|
|
} else { |
|
82
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
83
|
|
|
'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.', |
|
84
|
|
|
__METHOD__, |
|
85
|
|
|
Environment::class |
|
86
|
|
|
)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->pool = $pool; |
|
90
|
|
|
$this->searchHandler = $searchHandler; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response |
|
94
|
|
|
{ |
|
95
|
|
|
try { |
|
96
|
|
|
$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code')); |
|
97
|
|
|
} catch (ServiceNotFoundException $e) { |
|
98
|
|
|
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (!$admin instanceof AdminInterface) { |
|
102
|
|
|
throw new \RuntimeException('The requested service is not an Admin instance'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$admin->checkAccess('list'); |
|
106
|
|
|
|
|
107
|
|
|
$pager = $this->searchHandler->search( |
|
108
|
|
|
$admin, |
|
109
|
|
|
$blockContext->getSetting('query'), |
|
110
|
|
|
$blockContext->getSetting('page'), |
|
111
|
|
|
$blockContext->getSetting('per_page') |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
if (false === $pager) { |
|
115
|
|
|
$response = $response ?: new Response(); |
|
116
|
|
|
|
|
117
|
|
|
return $response->setContent('')->setStatusCode(204); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), [ |
|
|
|
|
|
|
121
|
|
|
'block' => $blockContext->getBlock(), |
|
122
|
|
|
'settings' => $blockContext->getSettings(), |
|
123
|
|
|
'admin_pool' => $this->pool, |
|
124
|
|
|
'pager' => $pager, |
|
125
|
|
|
'admin' => $admin, |
|
126
|
|
|
], $response); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getName() |
|
130
|
|
|
{ |
|
131
|
|
|
return 'Admin Search Result'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
|
135
|
|
|
{ |
|
136
|
|
|
$resolver |
|
137
|
|
|
->setDefaults([ |
|
138
|
|
|
'admin_code' => '', |
|
139
|
|
|
'query' => '', |
|
140
|
|
|
'page' => 0, |
|
141
|
|
|
'per_page' => 10, |
|
142
|
|
|
'icon' => '<i class="fa fa-list"></i>', |
|
143
|
|
|
]) |
|
144
|
|
|
->setRequired('admin_code') |
|
145
|
|
|
->setAllowedTypes('admin_code', ['string']); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: