MenuBuilder.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. private $pool;
  26. private $factory;
  27. private $provider;
  28. private $request;
  29. private $eventDispatcher;
  30. /**
  31. * Constructor.
  32. *
  33. * @param Pool $pool
  34. * @param FactoryInterface $factory
  35. * @param MenuProviderInterface $provider
  36. * @param EventDispatcherInterface $eventDispatcher
  37. */
  38. public function __construct(Pool $pool, FactoryInterface $factory, MenuProviderInterface $provider, EventDispatcherInterface $eventDispatcher)
  39. {
  40. $this->pool = $pool;
  41. $this->factory = $factory;
  42. $this->provider = $provider;
  43. $this->eventDispatcher = $eventDispatcher;
  44. }
  45. /**
  46. * Builds sidebar menu.
  47. *
  48. * @return ItemInterface
  49. */
  50. public function createSidebarMenu()
  51. {
  52. $menu = $this->factory->createItem('root', array(
  53. 'extras' => array(
  54. 'request' => $this->request,
  55. ),
  56. ));
  57. foreach ($this->pool->getAdminGroups() as $name => $group) {
  58. $attributes = array();
  59. $extras = array(
  60. 'icon' => $group['icon'],
  61. 'label_catalogue' => $group['label_catalogue'],
  62. 'roles' => $group['roles'],
  63. );
  64. // Check if the menu group is built by a menu provider
  65. if (isset($group['provider'])) {
  66. $subMenu = $this->provider->get($group['provider']);
  67. $menu
  68. ->addChild($subMenu)
  69. ->setExtras(array_merge($subMenu->getExtras(), $extras))
  70. ->setAttributes(array_merge($subMenu->getAttributes(), $attributes))
  71. ;
  72. continue;
  73. }
  74. // The menu group is built by config
  75. $menu->addChild($name, array(
  76. 'label' => $group['label'],
  77. 'attributes' => $attributes,
  78. 'extras' => $extras,
  79. ));
  80. foreach ($group['items'] as $item) {
  81. if (isset($item['admin']) && !empty($item['admin'])) {
  82. $admin = $this->pool->getInstance($item['admin']);
  83. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  84. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  85. continue;
  86. }
  87. $label = $admin->getLabel();
  88. $options = $admin->generateMenuUrl('list');
  89. $options['extras'] = array(
  90. 'translation_domain' => $admin->getTranslationDomain(),
  91. 'admin' => $admin,
  92. );
  93. } else {
  94. $label = $item['label'];
  95. $options = array(
  96. 'route' => $item['route'],
  97. 'routeParameters' => $item['route_params'],
  98. 'extras' => array(
  99. 'translation_domain' => $group['label_catalogue'],
  100. ),
  101. );
  102. }
  103. $menu[$name]->addChild($label, $options);
  104. }
  105. if (0 === count($menu[$name]->getChildren())) {
  106. $menu->removeChild($name);
  107. }
  108. }
  109. $event = new ConfigureMenuEvent($this->factory, $menu);
  110. $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
  111. return $event->getMenu();
  112. }
  113. /**
  114. * Sets the request the service.
  115. *
  116. * @param Request $request
  117. */
  118. public function setRequest(Request $request = null)
  119. {
  120. $this->request = $request;
  121. }
  122. }