AdminListBlockService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 getName()
  61. {
  62. return 'Admin List';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setDefaultSettings(OptionsResolverInterface $resolver)
  68. {
  69. $resolver->setDefaults(array(
  70. 'groups' => false
  71. ));
  72. $resolver->setAllowedTypes(array(
  73. 'groups' => array('bool', 'array')
  74. ));
  75. }
  76. }