forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminSearchBlockService.php
107 lines (93 loc) · 2.98 KB
/
AdminSearchBlockService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Block;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Thomas Rabaix <[email protected]>
*/
class AdminSearchBlockService extends AbstractBlockService
{
/**
* @var Pool
*/
protected $pool;
/**
* @var SearchHandler
*/
protected $searchHandler;
/**
* @param string $name
* @param EngineInterface $templating
* @param Pool $pool
* @param SearchHandler $searchHandler
*/
public function __construct($name, EngineInterface $templating, Pool $pool, SearchHandler $searchHandler)
{
parent::__construct($name, $templating);
$this->pool = $pool;
$this->searchHandler = $searchHandler;
}
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
try {
$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code'));
} catch (ServiceNotFoundException $e) {
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
}
if (!$admin instanceof AdminInterface) {
throw new \RuntimeException('The requested service is not an Admin instance');
}
$admin->checkAccess('list');
$pager = $this->searchHandler->search(
$admin,
$blockContext->getSetting('query'),
$blockContext->getSetting('page'),
$blockContext->getSetting('per_page')
);
return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), array(
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
'admin_pool' => $this->pool,
'pager' => $pager,
'admin' => $admin,
), $response);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Admin Search Result';
}
/**
* {@inheritdoc}
*/
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'admin_code' => false,
'query' => '',
'page' => 0,
'per_page' => 10,
'icon' => '<i class="fa fa-list"></i>',
));
}
}