MenuBuilder.php 3.9 KB

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