AdminListBlockService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\CoreBundle\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\OptionsResolverInterface;
  20. /**
  21. *
  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 validateBlock(ErrorElement $errorElement, BlockInterface $block)
  61. {
  62. // TODO: Implement validateBlock() method.
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
  68. {
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getName()
  74. {
  75. return 'Admin List';
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function setDefaultSettings(OptionsResolverInterface $resolver)
  81. {
  82. $resolver->setDefaults(array(
  83. 'groups' => false
  84. ));
  85. $resolver->setAllowedTypes(array(
  86. 'groups' => array('bool', 'array')
  87. ));
  88. }
  89. }