AdminSearchBlockService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\BaseBlockService;
  15. use Sonata\BlockBundle\Block\BlockContextInterface;
  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 BaseBlockService
  25. {
  26. protected $pool;
  27. protected $searchHandler;
  28. /**
  29. * @param string $name
  30. * @param EngineInterface $templating
  31. * @param Pool $pool
  32. * @param SearchHandler $searchHandler
  33. */
  34. public function __construct($name, EngineInterface $templating, Pool $pool, SearchHandler $searchHandler)
  35. {
  36. parent::__construct($name, $templating);
  37. $this->pool = $pool;
  38. $this->searchHandler = $searchHandler;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function execute(BlockContextInterface $blockContext, Response $response = null)
  44. {
  45. try {
  46. $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code'));
  47. } catch (ServiceNotFoundException $e) {
  48. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  49. }
  50. if (!$admin instanceof AdminInterface) {
  51. throw new \RuntimeException('The requested service is not an Admin instance');
  52. }
  53. if (!$admin->isGranted('LIST')) {
  54. throw new AccessDeniedException();
  55. }
  56. $pager = $this->searchHandler->search(
  57. $admin,
  58. $blockContext->getSetting('query'),
  59. $blockContext->getSetting('page'),
  60. $blockContext->getSetting('per_page')
  61. );
  62. return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), array(
  63. 'block' => $blockContext->getBlock(),
  64. 'settings' => $blockContext->getSettings(),
  65. 'admin_pool' => $this->pool,
  66. 'pager' => $pager,
  67. 'admin' => $admin,
  68. ), $response);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getName()
  74. {
  75. return 'Admin Search Result';
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function configureSettings(OptionsResolver $resolver)
  81. {
  82. $resolver->setDefaults(array(
  83. 'admin_code' => false,
  84. 'query' => '',
  85. 'page' => 0,
  86. 'per_page' => 10,
  87. 'icon' => '<i class="fa fa-list"></i>',
  88. ));
  89. }
  90. }