123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- */
- namespace Sonata\AdminBundle\Menu;
- use Knp\Menu\FactoryInterface;
- use Knp\Menu\ItemInterface;
- use Knp\Menu\Provider\MenuProviderInterface;
- use Sonata\AdminBundle\Admin\Pool;
- use Symfony\Component\HttpFoundation\Request;
- /**
- * Sonata menu builder.
- *
- * @author Martin Hasoň <martin.hason@gmail.com>
- */
- class MenuBuilder
- {
- private $pool;
- private $factory;
- private $provider;
- private $request;
- /**
- * Constructor.
- *
- * @param Pool $pool
- * @param FactoryInterface $factory
- * @param MenuProviderInterface $provider
- */
- public function __construct(Pool $pool, FactoryInterface $factory, MenuProviderInterface $provider)
- {
- $this->pool = $pool;
- $this->factory = $factory;
- $this->provider = $provider;
- }
- /**
- * Builds sidebar menu.
- *
- * @return ItemInterface
- */
- public function createSidebarMenu()
- {
- $menu = $this->factory->createItem('root', array(
- 'extras' => array(
- 'request' => $this->request,
- ),
- ));
- foreach ($this->pool->getAdminGroups() as $name => $group) {
- $attributes = array(
- 'icon' => $group['icon'],
- 'label_catalogue' => $group['label_catalogue'],
- );
- $extras = array(
- 'roles' => $group['roles'],
- );
- // Check if the menu group is built by a menu provider
- if (isset($group['provider'])) {
- $subMenu = $this->provider->get($group['provider']);
- $menu
- ->addChild($subMenu)
- ->setExtras(array_merge($subMenu->getExtras(), $extras))
- ->setAttributes(array_merge($subMenu->getAttributes(), $attributes))
- ;
- continue;
- }
- // The menu group is built by config
- $menu->addChild($name, array(
- 'label' => $group['label'],
- 'attributes' => $attributes,
- 'extras' => $extras,
- ));
- foreach ($group['items'] as $item) {
- if (isset($item['admin']) && !empty($item['admin'])) {
- $admin = $this->pool->getInstance($item['admin']);
- // skip menu item if no `list` url is available or user doesn't have the LIST access rights
- if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
- continue;
- }
- $label = $admin->getLabel();
- $options = $admin->generateMenuUrl('list');
- $options['extras'] = array(
- 'translation_domain' =>$admin->getTranslationDomain(),
- 'admin' => $admin,
- );
- } else {
- $label = $item['label'];
- $options = array(
- 'route' => $item['route'],
- 'routeParameters' => $item['route_params'],
- 'extras' => array(
- 'translation_domain' => $group['label_catalogue'],
- )
- );
- }
- $menu[$name]->addChild($label, $options);
- }
- if (0 === count($menu[$name]->getChildren())) {
- $menu->removeChild($name);
- }
- }
- return $menu;
- }
- /**
- * Sets the request the service
- *
- * @param Request $request
- */
- public function setRequest(Request $request = null)
- {
- $this->request = $request;
- }
- }
|