vendor/uvdesk/core-framework/Dashboard/NavigationTemplate.php line 23

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Dashboard;
  3. use Symfony\Component\Routing\RouterInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Dashboard\Segments\NavigationInterface;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Framework\ExtendableComponentInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class NavigationTemplate implements ExtendableComponentInterface
  11. {
  12.     CONST TEMPLATE '<ul class="uv-menubar">[[ COLLECTION ]]</ul>';
  13.     CONST TEMPLATE_ITEM '<li title = "[[ NAME ]]" class = "[[ ATTRIBUTES ]]" data-toggle = "tooltip" data-placement = "right"><a class="[[ isActive ]]" href="[[ URL ]]"><span class="uv-icon">[[ SVG ]]</span><span class="uv-menu-item">[[ NAME ]]</span></a></li>';
  14.     
  15.     private $segments = [];
  16.     public function __construct(ContainerInterface $containerRequestStack $requestStackRouterInterface $routerUserService $userServiceTranslatorInterface $translator)
  17.     {
  18.         $this->router $router;
  19.         $this->container $container;
  20.         $this->requestStack $requestStack;
  21.         $this->userService $userService;
  22.         $this->translator $translator;
  23.     }
  24.     public function appendNavigation(NavigationInterface $segment$tags = [])
  25.     {
  26.         $this->segments[] = $segment;
  27.     }
  28.     public function render()
  29.     {
  30.         $router $this->router;
  31.         $request $this->requestStack->getCurrentRequest();
  32.         
  33.         $route $this->requestStack->getCurrentRequest()->get('_route');
  34.         // Compile accessible segments by end-user
  35.         $accessibleSegments = [];
  36.         foreach ($this->segments as $item) {
  37.             if (null == $item::getRoles()) {
  38.                 $accessibleSegments[] = $item;
  39.             } else {
  40.                 foreach ($item::getRoles() as $requiredPermission) {
  41.                     if ($this->userService->isAccessAuthorized($requiredPermission)) {
  42.                         $accessibleSegments[] = $item;
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.         if (array_key_exists(2$accessibleSegments))
  49.         {
  50.             $temp $accessibleSegments[1];
  51.             $accessibleSegments[1] = $accessibleSegments[2];
  52.             $accessibleSegments[2] = $temp;
  53.         }
  54.         // Reduce the accessible segments into injectible html snippet
  55.         $html array_reduce($accessibleSegments, function($html$segment) use ($router$request$route) {
  56.             $isActive '';
  57.             if($segment::getRouteName() == $route) {
  58.                 $isActive "uv-item-active";
  59.             }
  60.             $html .= strtr(self::TEMPLATE_ITEM, [
  61.                 '[[ SVG ]]' => $segment::getIcon(),
  62.                 '[[ NAME ]]' => $this->translator->trans($segment::getTitle()),
  63.                 '[[ URL ]]' => $router->generate($segment::getRouteName()),
  64.                 '[[ isActive ]]' => $isActive,
  65.             ]);
  66.             return $html;
  67.         }, '');
  68.         return strtr(self::TEMPLATE, ['[[ COLLECTION ]]' => $html]);
  69.     }
  70. }