AdminSearchBlockService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Form\FormMapper;
  14. use Sonata\AdminBundle\Search\SearchHandler;
  15. use Sonata\AdminBundle\Validator\ErrorElement;
  16. use Sonata\BlockBundle\Block\BaseBlockService;
  17. use Sonata\BlockBundle\Block\BlockContextInterface;
  18. use Sonata\BlockBundle\Model\BlockInterface;
  19. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  20. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  23. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  24. /**
  25. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  26. */
  27. class AdminSearchBlockService extends BaseBlockService
  28. {
  29. protected $pool;
  30. protected $searchHandler;
  31. /**
  32. * @param string $name
  33. * @param EngineInterface $templating
  34. * @param Pool $pool
  35. * @param SearchHandler $searchHandler
  36. */
  37. public function __construct($name, EngineInterface $templating, Pool $pool, SearchHandler $searchHandler)
  38. {
  39. parent::__construct($name, $templating);
  40. $this->pool = $pool;
  41. $this->searchHandler = $searchHandler;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function execute(BlockContextInterface $blockContext, Response $response = null)
  47. {
  48. try {
  49. $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code'));
  50. } catch (ServiceNotFoundException $e) {
  51. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  52. }
  53. if (!$admin instanceof AdminInterface) {
  54. throw new \RuntimeException('The requested service is not an Admin instance');
  55. }
  56. if (!$admin->isGranted('LIST')) {
  57. throw new AccessDeniedException();
  58. }
  59. $pager = $this->searchHandler->search(
  60. $admin,
  61. $blockContext->getSetting('query'),
  62. $blockContext->getSetting('page'),
  63. $blockContext->getSetting('per_page')
  64. );
  65. return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), array(
  66. 'block' => $blockContext->getBlock(),
  67. 'settings' => $blockContext->getSettings(),
  68. 'admin_pool' => $this->pool,
  69. 'pager' => $pager,
  70. 'admin' => $admin,
  71. ), $response);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
  77. {
  78. // TODO: Implement validateBlock() method.
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
  84. {
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getName()
  90. {
  91. return 'Admin Search Result';
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function setDefaultSettings(OptionsResolverInterface $resolver)
  97. {
  98. $resolver->setDefaults(array(
  99. 'admin_code' => false,
  100. 'query' => '',
  101. 'page' => 0,
  102. 'per_page' => 10,
  103. ));
  104. }
  105. }