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

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. private $segments = [];
  15. public function __construct(ContainerInterface $container, RequestStack $requestStack, RouterInterface $router, UserService $userService, TranslatorInterface $translator)
  16. {
  17. $this->router = $router;
  18. $this->container = $container;
  19. $this->requestStack = $requestStack;
  20. $this->userService = $userService;
  21. $this->translator = $translator;
  22. }
  23. public function appendNavigation(NavigationInterface $segment, $tags = [])
  24. {
  25. $this->segments[] = $segment;
  26. }
  27. public function render()
  28. {
  29. $router = $this->router;
  30. $request = $this->requestStack->getCurrentRequest();
  31. $route = $this->requestStack->getCurrentRequest()->get('_route');
  32. // Compile accessible segments by end-user
  33. $accessibleSegments = [];
  34. foreach ($this->segments as $item) {
  35. if (null == $item::getRoles()) {
  36. $accessibleSegments[] = $item;
  37. } else {
  38. foreach ($item::getRoles() as $requiredPermission) {
  39. if ($this->userService->isAccessAuthorized($requiredPermission)) {
  40. $accessibleSegments[] = $item;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. if (array_key_exists(2, $accessibleSegments))
  47. {
  48. $temp = $accessibleSegments[1];
  49. $accessibleSegments[1] = $accessibleSegments[2];
  50. $accessibleSegments[2] = $temp;
  51. }
  52. // Reduce the accessible segments into injectible html snippet
  53. $html = array_reduce($accessibleSegments, function($html, $segment) use ($router, $request, $route) {
  54. $isActive = '';
  55. if($segment::getRouteName() == $route) {
  56. $isActive = "uv-item-active";
  57. }
  58. $html .= strtr(self::TEMPLATE_ITEM, [
  59. '[[ SVG ]]' => $segment::getIcon(),
  60. '[[ NAME ]]' => $this->translator->trans($segment::getTitle()),
  61. '[[ URL ]]' => $router->generate($segment::getRouteName()),
  62. '[[ isActive ]]' => $isActive,
  63. ]);
  64. return $html;
  65. }, '');
  66. return strtr(self::TEMPLATE, ['[[ COLLECTION ]]' => $html]);
  67. }
  68. }