AdminSearchBlockService.php 3.1 KB

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