AdminListBlockService.php 2.4 KB

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