*/ private $widgets = []; /** * @var array */ private $widgetsPerWidgetGroup = []; public function __construct( protected readonly ContainerInterface $container, protected readonly WidgetSettingsFactory $widgetSettingsFactory, ) {} /** * @return WidgetConfigurationInterface[] */ public function getAvailableWidgets(): array { return $this->checkPermissionOfWidgets($this->widgets); } /** * @return WidgetConfigurationInterface[] */ public function getAllWidgets(): array { return $this->widgets; } /** * @throws \InvalidArgumentException If requested identifier does not exist. */ public function getAvailableWidget(ServerRequestInterface $request, string $identifier): WidgetRendererInterface|WidgetInterface { if (array_key_exists($identifier, $this->getAvailableWidgets())) { $widget = $this->container->get($this->widgets[$identifier]->getServiceName()); if ($widget instanceof RequestAwareWidgetInterface) { $widget->setRequest($request); } return $widget; } throw new \InvalidArgumentException('Requested widget "' . $identifier . '" does not exist.', 1584777201); } /** * @return WidgetConfigurationInterface[] */ public function getAvailableWidgetsForWidgetGroup(string $widgetGroupIdentifier): array { if (!array_key_exists($widgetGroupIdentifier, $this->widgetsPerWidgetGroup)) { return []; } return $this->checkPermissionOfWidgets($this->widgetsPerWidgetGroup[$widgetGroupIdentifier]); } public function registerWidget(string $serviceName): void { $widgetConfiguration = $this->container->get($serviceName); $this->widgets[$widgetConfiguration->getIdentifier()] = $widgetConfiguration; foreach ($widgetConfiguration->getGroupNames() as $groupIdentifier) { $this->widgetsPerWidgetGroup = ArrayUtility::setValueByPath( $this->widgetsPerWidgetGroup, $groupIdentifier . '/' . $widgetConfiguration->getIdentifier(), $widgetConfiguration ); } } /** * @param WidgetConfigurationInterface[] $widgets * @return WidgetConfigurationInterface[] */ protected function checkPermissionOfWidgets(array $widgets): array { return array_filter($widgets, function ($widget, $identifier) { return $this->getBackendUser()->check('available_widgets', $identifier) && (!$widget instanceof WidgetConfiguration || !$widget->isAdminOnly() || $this->getBackendUser()->isAdmin()); }, ARRAY_FILTER_USE_BOTH); } public function widgetItemsProcFunc(array &$parameters): void { foreach ($this->widgets as $widget) { if ($widget instanceof WidgetConfiguration && $widget->isAdminOnly()) { continue; } $parameters['items'][] = [ 'label' => $widget->getTitle(), 'value' => $widget->getIdentifier(), 'icon' => $widget->getIconIdentifier(), 'description' => $widget->getDescription(), ]; } } protected function getBackendUser(): BackendUserAuthentication { return $GLOBALS['BE_USER']; } }