MenuBuilder.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $extras = array(
  61. 'icon' => $group['icon'],
  62. 'label_catalogue' => $group['label_catalogue'],
  63. 'roles' => $group['roles'],
  64. );
  65. // Check if the menu group is built by a menu provider
  66. if (isset($group['provider'])) {
  67. $subMenu = $this->provider->get($group['provider']);
  68. $menu
  69. ->addChild($subMenu)
  70. ->setExtras(array_merge($subMenu->getExtras(), $extras))
  71. ->setAttributes(array_merge($subMenu->getAttributes(), $attributes))
  72. ;
  73. continue;
  74. }
  75. // The menu group is built by config
  76. $menu->addChild($name, array(
  77. 'label' => $group['label'],
  78. 'attributes' => $attributes,
  79. 'extras' => $extras,
  80. ));
  81. foreach ($group['items'] as $item) {
  82. if (isset($item['admin']) && !empty($item['admin'])) {
  83. $admin = $this->pool->getInstance($item['admin']);
  84. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  85. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  86. continue;
  87. }
  88. $label = $admin->getLabel();
  89. $options = $admin->generateMenuUrl('list');
  90. $options['extras'] = array(
  91. 'translation_domain' =>$admin->getTranslationDomain(),
  92. 'admin' => $admin,
  93. );
  94. } else {
  95. $label = $item['label'];
  96. $options = array(
  97. 'route' => $item['route'],
  98. 'routeParameters' => $item['route_params'],
  99. 'extras' => array(
  100. 'translation_domain' => $group['label_catalogue'],
  101. )
  102. );
  103. }
  104. $menu[$name]->addChild($label, $options);
  105. }
  106. if (0 === count($menu[$name]->getChildren())) {
  107. $menu->removeChild($name);
  108. }
  109. }
  110. $event = new ConfigureMenuEvent($this->factory, $menu);
  111. $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
  112. return $event->getMenu();
  113. }
  114. /**
  115. * Sets the request the service
  116. *
  117. * @param Request $request
  118. */
  119. public function setRequest(Request $request = null)
  120. {
  121. $this->request = $request;
  122. }
  123. }