AdminListBlockService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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\BlockBundle\Block\BlockContextInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\BlockBundle\Block\BaseBlockService;
  16. use Symfony\Component\HttpKernel\Kernel;
  17. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  18. /**
  19. * Class AdminListBlockService
  20. *
  21. * @package Sonata\AdminBundle\Block
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class AdminListBlockService extends BaseBlockService
  25. {
  26. protected $pool;
  27. /**
  28. * @param string $name
  29. * @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
  30. * @param \Sonata\AdminBundle\Admin\Pool $pool
  31. */
  32. public function __construct($name, EngineInterface $templating, Pool $pool)
  33. {
  34. parent::__construct($name, $templating);
  35. $this->pool = $pool;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function execute(BlockContextInterface $blockContext, Response $response = null)
  41. {
  42. $dashboardGroups = $this->pool->getDashboardGroups();
  43. $settings = $blockContext->getSettings();
  44. $visibleGroups = array();
  45. foreach ($dashboardGroups as $name => $dashboardGroup) {
  46. if (!$settings['groups'] || in_array($name, $settings['groups'])) {
  47. $visibleGroups[] = $dashboardGroup;
  48. }
  49. }
  50. return $this->renderPrivateResponse($this->pool->getTemplate('list_block'), array(
  51. 'block' => $blockContext->getBlock(),
  52. 'settings' => $settings,
  53. 'admin_pool' => $this->pool,
  54. 'groups' => $visibleGroups
  55. ), $response);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getName()
  61. {
  62. return 'Admin List';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setDefaultSettings(OptionsResolverInterface $resolver)
  68. {
  69. $resolver->setDefaults(array(
  70. 'groups' => false
  71. ));
  72. if (version_compare(Kernel::VERSION, '2.6', '<')) {
  73. $resolver->setAllowedTypes(array(
  74. 'groups' => array('bool', 'array')
  75. ));
  76. } else {
  77. $resolver->setAllowedTypes('groups', array('bool', 'array'));
  78. }
  79. }
  80. }