MenuBuilder.php 4.3 KB

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