MenuBuilder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Menu;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\ItemInterface;
  13. use Knp\Menu\Provider\MenuProviderInterface;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. /**
  19. * Sonata menu builder.
  20. *
  21. * @author Martin Hasoň <martin.hason@gmail.com>
  22. */
  23. class MenuBuilder
  24. {
  25. /**
  26. * @var Pool
  27. */
  28. private $pool;
  29. /**
  30. * @var FactoryInterface
  31. */
  32. private $factory;
  33. /**
  34. * @var MenuProviderInterface
  35. */
  36. private $provider;
  37. /**
  38. * @var Request
  39. */
  40. private $request;
  41. /**
  42. * @var EventDispatcherInterface
  43. */
  44. private $eventDispatcher;
  45. /**
  46. * Constructor.
  47. *
  48. * @param Pool $pool
  49. * @param FactoryInterface $factory
  50. * @param MenuProviderInterface $provider
  51. * @param EventDispatcherInterface $eventDispatcher
  52. */
  53. public function __construct(Pool $pool, FactoryInterface $factory, MenuProviderInterface $provider, EventDispatcherInterface $eventDispatcher)
  54. {
  55. $this->pool = $pool;
  56. $this->factory = $factory;
  57. $this->provider = $provider;
  58. $this->eventDispatcher = $eventDispatcher;
  59. }
  60. /**
  61. * Builds sidebar menu.
  62. *
  63. * @return ItemInterface
  64. */
  65. public function createSidebarMenu()
  66. {
  67. $menu = $this->factory->createItem('root', array(
  68. 'extras' => array(
  69. 'request' => $this->request,
  70. ),
  71. ));
  72. foreach ($this->pool->getAdminGroups() as $name => $group) {
  73. $attributes = array();
  74. $extras = array(
  75. 'icon' => $group['icon'],
  76. 'label_catalogue' => $group['label_catalogue'],
  77. 'roles' => $group['roles'],
  78. );
  79. // Check if the menu group is built by a menu provider
  80. if (isset($group['provider'])) {
  81. $subMenu = $this->provider->get($group['provider']);
  82. $menu
  83. ->addChild($subMenu)
  84. ->setExtras(array_merge($subMenu->getExtras(), $extras))
  85. ->setAttributes(array_merge($subMenu->getAttributes(), $attributes))
  86. ;
  87. continue;
  88. }
  89. // The menu group is built by config
  90. $menu->addChild($name, array(
  91. 'label' => $group['label'],
  92. 'attributes' => $attributes,
  93. 'extras' => $extras,
  94. ));
  95. foreach ($group['items'] as $item) {
  96. if (isset($item['admin']) && !empty($item['admin'])) {
  97. $admin = $this->pool->getInstance($item['admin']);
  98. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  99. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  100. continue;
  101. }
  102. $label = $admin->getLabel();
  103. $options = $admin->generateMenuUrl('list');
  104. $options['extras'] = array(
  105. 'translation_domain' => $admin->getTranslationDomain(),
  106. 'admin' => $admin,
  107. );
  108. } else {
  109. $label = $item['label'];
  110. $options = array(
  111. 'route' => $item['route'],
  112. 'routeParameters' => $item['route_params'],
  113. 'extras' => array(
  114. 'translation_domain' => $group['label_catalogue'],
  115. ),
  116. );
  117. }
  118. $menu[$name]->addChild($label, $options);
  119. }
  120. if (0 === count($menu[$name]->getChildren())) {
  121. $menu->removeChild($name);
  122. }
  123. }
  124. $event = new ConfigureMenuEvent($this->factory, $menu);
  125. $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
  126. return $event->getMenu();
  127. }
  128. /**
  129. * Sets the request the service.
  130. *
  131. * @param Request $request
  132. */
  133. public function setRequest(Request $request = null)
  134. {
  135. $this->request = $request;
  136. }
  137. }