eventDispatcher = $dispatcher; $this->widgets = $service; $this->dashboard = $dashboard; } /** * @Route(path="/", defaults={}, name="dashboard", methods={"GET"}) */ public function indexAction() { $event = new DashboardEvent($this->getUser()); foreach ($this->dashboard as $widgetRow) { if (empty($widgetRow['widgets'])) { continue; } if (null !== $widgetRow['permission'] && !$this->isGranted($widgetRow['permission'])) { continue; } if (!isset($widgetRow['type'])) { $widgetRow['type'] = CompoundRow::class; } if (!class_exists($widgetRow['type'])) { throw new WidgetException(sprintf('Unknown widget type "%s"', $widgetRow['type'])); } $row = new $widgetRow['type'](); if (!($row instanceof AbstractContainer)) { throw new WidgetException( sprintf( 'Expected widget type to be an instanceof "%s", but found "%s"', AbstractContainer::class, $widgetRow['type'] ) ); } $row->setTitle($widgetRow['title'] ?? ''); $row->setOrder($widgetRow['order']); foreach ($widgetRow['widgets'] as $widgetName) { if (!$this->widgets->hasWidget($widgetName)) { throw new \Exception(sprintf('Unknown widget "%s"', $widgetName)); } $widget = $this->widgets->getWidget($widgetName); $add = true; if ($widget instanceof AuthorizedWidget) { $tmp = false; foreach ($widget->getPermissions() as $perm) { if ($this->isGranted($perm)) { $tmp = true; break; } } $add = $tmp; } if ($add) { $row->addWidget($widget); } } $event->addSection($row); } $this->eventDispatcher->dispatch($event); $sections = $event->getSections(); $clearedSections = []; /** @var WidgetContainerInterface $section */ foreach ($sections as $key => $section) { if (!empty($section->getWidgets())) { $clearedSections[] = $section; } } uasort( $clearedSections, function (WidgetContainerInterface $a, WidgetContainerInterface $b) { if ($a->getOrder() == $b->getOrder()) { return 0; } return ($a->getOrder() < $b->getOrder()) ? -1 : 1; } ); return $this->render('dashboard/index.html.twig', [ 'widgets' => $clearedSections ]); } }