AdminSearchBlockService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  21. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  22. */
  23. class AdminSearchBlockService extends AbstractBlockService
  24. {
  25. /**
  26. * @var Pool
  27. */
  28. protected $pool;
  29. /**
  30. * @var SearchHandler
  31. */
  32. protected $searchHandler;
  33. /**
  34. * @param string $name
  35. * @param EngineInterface $templating
  36. * @param Pool $pool
  37. * @param SearchHandler $searchHandler
  38. */
  39. public function __construct($name, EngineInterface $templating, Pool $pool, SearchHandler $searchHandler)
  40. {
  41. parent::__construct($name, $templating);
  42. $this->pool = $pool;
  43. $this->searchHandler = $searchHandler;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function execute(BlockContextInterface $blockContext, Response $response = null)
  49. {
  50. try {
  51. $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code'));
  52. } catch (ServiceNotFoundException $e) {
  53. throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
  54. }
  55. if (!$admin instanceof AdminInterface) {
  56. throw new \RuntimeException('The requested service is not an Admin instance');
  57. }
  58. $admin->checkAccess('list');
  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 getName()
  77. {
  78. return 'Admin Search Result';
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function configureSettings(OptionsResolver $resolver)
  84. {
  85. $resolver->setDefaults(array(
  86. 'admin_code' => false,
  87. 'query' => '',
  88. 'page' => 0,
  89. 'per_page' => 10,
  90. 'icon' => '<i class="fa fa-list"></i>',
  91. ));
  92. }
  93. }