AdminListBlockService.php 2.4 KB

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