GroupMenuProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Provider;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\Provider\MenuProviderInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. /**
  15. * Menu provider based on group options.
  16. *
  17. * @author Alexandru Furculita <alex@furculita.net>
  18. */
  19. class GroupMenuProvider implements MenuProviderInterface
  20. {
  21. /**
  22. * @var FactoryInterface
  23. */
  24. private $menuFactory;
  25. /**
  26. * @var Pool
  27. */
  28. private $pool;
  29. /**
  30. * @param FactoryInterface $menuFactory
  31. * @param Pool $pool
  32. */
  33. public function __construct(FactoryInterface $menuFactory, Pool $pool)
  34. {
  35. $this->menuFactory = $menuFactory;
  36. $this->pool = $pool;
  37. }
  38. /**
  39. * Retrieves the menu based on the group options.
  40. *
  41. * @param string $name
  42. * @param array $options
  43. *
  44. * @return \Knp\Menu\ItemInterface
  45. *
  46. * @throws \InvalidArgumentException if the menu does not exists
  47. */
  48. public function get($name, array $options = array())
  49. {
  50. $group = $options['group'];
  51. $menuItem = $this->menuFactory->createItem(
  52. $options['name'],
  53. array(
  54. 'label' => $group['label'],
  55. )
  56. );
  57. if (empty($group['on_top'])) {
  58. foreach ($group['items'] as $item) {
  59. if (isset($item['admin']) && !empty($item['admin'])) {
  60. $admin = $this->pool->getInstance($item['admin']);
  61. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  62. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  63. continue;
  64. }
  65. $label = $admin->getLabel();
  66. $options = $admin->generateMenuUrl('list', array(), $item['route_absolute']);
  67. $options['extras'] = array(
  68. 'translation_domain' => $admin->getTranslationDomain(),
  69. 'admin' => $admin,
  70. );
  71. } else {
  72. $label = $item['label'];
  73. $options = array(
  74. 'route' => $item['route'],
  75. 'routeParameters' => $item['route_params'],
  76. 'routeAbsolute' => $item['route_absolute'],
  77. 'extras' => array(
  78. 'translation_domain' => $group['label_catalogue'],
  79. ),
  80. );
  81. }
  82. $menuItem->addChild($label, $options);
  83. }
  84. if (false === $menuItem->hasChildren()) {
  85. $menuItem->setDisplay(false);
  86. }
  87. } else {
  88. foreach ($group['items'] as $item) {
  89. if (isset($item['admin']) && !empty($item['admin'])) {
  90. $admin = $this->pool->getInstance($item['admin']);
  91. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  92. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  93. continue;
  94. }
  95. $options = $admin->generateUrl('list');
  96. $menuItem->setExtra('route', $admin->getBaseRouteName().'_list');
  97. $menuItem->setExtra('on_top', $group['on_top']);
  98. $menuItem->setUri($options);
  99. } else {
  100. $menuItem->setUri($item['route']);
  101. }
  102. }
  103. }
  104. return $menuItem;
  105. }
  106. /**
  107. * Checks whether a menu exists in this provider.
  108. *
  109. * @param string $name
  110. * @param array $options
  111. *
  112. * @return bool
  113. */
  114. public function has($name, array $options = array())
  115. {
  116. return 'sonata_group_menu' === $name;
  117. }
  118. }