AdminListBlockService.php 2.8 KB

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