AdminListBlockService.php 2.8 KB

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