From 790c7f6724c9cafdc8254cbe388a3d9922924e6d Mon Sep 17 00:00:00 2001 From: Lucas Silva Date: Tue, 16 May 2023 02:16:49 +0200 Subject: [PATCH] First Widget Bar + Number of notification unread. All is hardcoded --- .../Widget/WidgetHandlerInterface.php | 18 ++++ .../Widget/WidgetHandlerManager.php | 37 ++++++++ .../ChillMainBundle/Widget/WidgetManager.php | 37 -------- .../Widget/WidgetProviderInterface.php | 22 ----- .../Widget/Widgets/WidgetBar.php | 93 +++++++++++++++--- .../Widget/Widgets/WidgetNumber.php | 95 ++++++++++++++++--- .../ChillMainBundle/config/services.yaml | 18 ++++ 7 files changed, 235 insertions(+), 85 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Widget/WidgetHandlerInterface.php create mode 100644 src/Bundle/ChillMainBundle/Widget/WidgetHandlerManager.php delete mode 100644 src/Bundle/ChillMainBundle/Widget/WidgetManager.php delete mode 100644 src/Bundle/ChillMainBundle/Widget/WidgetProviderInterface.php diff --git a/src/Bundle/ChillMainBundle/Widget/WidgetHandlerInterface.php b/src/Bundle/ChillMainBundle/Widget/WidgetHandlerInterface.php new file mode 100644 index 000000000..8e2b977e4 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Widget/WidgetHandlerInterface.php @@ -0,0 +1,18 @@ +handlers = $handlers; + dump($this->handlers); + } + + public function getHandler(array $config,): WidgetHandlerInterface + { + foreach ($this->handlers as $widget) { + if ($widget->supports($config)) { + return $widget; + } + } + + // Handle unsupported context + throw new \InvalidArgumentException('Unsupported widget.'); + } + + public function getDataForWidget(array $config, array $context=[]): array + { + return $this->getHandler($config)->getDataForWidget($config,$context); + } +} diff --git a/src/Bundle/ChillMainBundle/Widget/WidgetManager.php b/src/Bundle/ChillMainBundle/Widget/WidgetManager.php deleted file mode 100644 index c160bb54f..000000000 --- a/src/Bundle/ChillMainBundle/Widget/WidgetManager.php +++ /dev/null @@ -1,37 +0,0 @@ -widgetProvider = $widgetProvider; - } - - public function getDataForWidget($data,$context) - { - //Check if the WidgetProvider supports the given context - if ($this->widgetProvider->supports($data,$context)) { - // Get the widget data using specific Widget implementation - $widgetData = $this->widgetProvider->getDataForWidget($data); - return $widgetData; - } - - // Handle unsupported context - throw new \InvalidArgumentException('Unsupported context.'); - } -} diff --git a/src/Bundle/ChillMainBundle/Widget/WidgetProviderInterface.php b/src/Bundle/ChillMainBundle/Widget/WidgetProviderInterface.php deleted file mode 100644 index d48cf430c..000000000 --- a/src/Bundle/ChillMainBundle/Widget/WidgetProviderInterface.php +++ /dev/null @@ -1,22 +0,0 @@ -acpRepository = $accompanyingPeriodRepository; + $this->security = $security; + $this->notificationRepository = $notificationRepository; + $this->em = $em; + $this->dataFetcher = $dataFetcher; } - public function getDataForWidget($data): array + public function supports(array $config, array $context = []): bool { - // Process the data for the bar widget - // In this example, we assume the data is an array of numbers - // Here, I think we suppose to make a query to the database to get some number - // For now just sending a number - $barData = $data['bar']; + // Check if the context is "bar" + return $config['alias'] === 'bar'; + } - // Return the widget data as an associative array + /** + * @throws \Exception + */ + public function getDataForWidget(array $config, array $context = []): array + { + $user = $this->security->getUser(); + //$userId = $user->getId(); + //Hardcoder pour résultat + $userId = 19; + $dataForWidget = []; + if (!$user instanceof User) { + throw new AccessDeniedException(); + } + + if ($context !== []) { + switch ($context['what']) { + case 'accompanying_period_by_month': + + $data = $this->dataFetcher->fetchDataForWidget($userId); + $dataForWidget = [ + 'labels' => $data['months'], + 'datasets' => [ + [ + 'label' => 'Number of accompanying periods opened', + 'backgroundColor' => ['#41B883'], + 'data' => $data['counts'] + ] + ] + ]; + break; + default: + throw new \InvalidArgumentException('Invalid Context.'); + + } + } return [ + 'data' => $dataForWidget, + 'options' => [ + + 'responsive' => 'true', + 'maintainAspectRatio' => 'false', + 'position' => 'relative' + + ], 'type' => 'bar', - 'data' => $barData, ]; } } diff --git a/src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php b/src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php index d20998cee..1ee41df67 100644 --- a/src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php +++ b/src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php @@ -11,27 +11,96 @@ declare(strict_types=1); namespace Chill\MainBundle\Widget\Widgets; -use Chill\MainBundle\Widget\WidgetProviderInterface; +use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Repository\NotificationRepository; +use Chill\MainBundle\Widget\WidgetHandlerInterface; +use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; +use Chill\PersonBundle\Repository\Person; +use DateInterval; +use DateTimeImmutable; +use Symfony\Component\Security\Core\Exception\AccessDeniedException; +use Symfony\Component\Security\Core\Security; -class WidgetNumber implements WidgetProviderInterface{ - public function supports($data, $context): bool +class WidgetNumber implements WidgetHandlerInterface +{ + + private Security $security; + + //private AccompanyingPeriodRepository $acpRepository; + + private NotificationRepository $notificationRepository; + + + public function __construct( + // AccompanyingPeriodRepository $accompanyingPeriodRepository, + Security $security, + NotificationRepository $notificationRepository, + + ) { - // Check if the context is "number" - return $context === 'number'; + // $this->acpRepository = $accompanyingPeriodRepository; + $this->security = $security; + $this->notificationRepository = $notificationRepository; } - public function getDataForWidget($data): array + public function supports(array $config, array $context = []): bool { - // Process the data for the number widget - // We suppose that its only a single number - // Here, I think we suppose to make a query to the database to get some number - // For now just sending a number - $numberData = $data['number']; + return $config['alias'] === 'number'; + } + + + public function getDataForWidget(array $config, array $context = []): array + { + $user = $this->security->getUser(); + $dataForWidget = []; + if (!$user instanceof User) { + throw new AccessDeniedException(); + } + + if ($context !== []) { + switch ($context['what']) { + + /*case 'accompanying_period_history' : + //Send back a data that need to be serialize in order to see + $since = (new DateTimeImmutable('now'))->sub(new DateInterval('P1Y')); + $data = $this->acpRepository->countByRecentUserHistory($user, $since); + $what = $this->acpRepository->findConfirmedByUser($user); + break;*/ + case 'notification_sender': + //Count the number of notification the sender send + $countSend = $this->notificationRepository->countAllForSender($user); + $dataForWidget = [ + 'labels' => ['Notification Send'], + 'datasets' => [ + [ + 'backgroundColor' => ['#41B883'], + 'data' => [$countSend] + ] + ] + ]; + break; + + case 'notification_unread': + //Count the number of unread notification by the current User + $countUnread = $this->notificationRepository->countUnreadByUser($user); + $dataForWidget = [ + 'labels' => ['Notification Unread'], + 'datasets' => [ + [ + 'backgroundColor' => ['#41B883'], + 'data' => [$countUnread] + ] + ] + ]; + break; + default : throw new \InvalidArgumentException('Invalid Context.'); + } + } - // Return the widget data as an associative array return [ + 'data' => $dataForWidget, 'type' => 'number', - 'data' => $numberData, ]; } } + diff --git a/src/Bundle/ChillMainBundle/config/services.yaml b/src/Bundle/ChillMainBundle/config/services.yaml index 6248c508e..0dae1f853 100644 --- a/src/Bundle/ChillMainBundle/config/services.yaml +++ b/src/Bundle/ChillMainBundle/config/services.yaml @@ -112,6 +112,24 @@ services: autowire: true autoconfigure: true + Chill\MainBundle\Widget\: + resource: '../Widget/' + autowire: true + autoconfigure: true + + Chill\MainBundle\Widget\WidgetHandlerManager: + arguments: + $handlers: !tagged_iterator chill_main.widget_handler + + Chill\MainBundle\Widget\Widgets\WidgetNumber: + autoconfigure: true + autowire: true + + Chill\MainBundle\Widget\Widgets\WidgetBar: + autoconfigure: true + autowire: true + + Chill\MainBundle\Cron\CronManager: autoconfigure: true autowire: true