First Widget Bar + Number of notification unread. All is hardcoded

This commit is contained in:
Lucas Silva 2023-05-16 02:16:49 +02:00
parent 1be91bb392
commit 790c7f6724
7 changed files with 235 additions and 85 deletions

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Widget;
interface WidgetHandlerInterface
{
/**
* Return true if the handler supports the handling for this widget.
*/
public function supports(array $config,array $context = []): bool;
/**
* Return an array which will be passed as data for the chart in the Vue element.
*/
public function getDataForWidget(array $config,array $context = []): array;
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Widget;
use Chill\MainBundle\Widget\WidgetHandlerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
final class WidgetHandlerManager
{
private iterable $handlers;
public function __construct(
iterable $handlers
) {
$this->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);
}
}

View File

@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Widget;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
final class WidgetManager
{
private WidgetProviderInterface $widgetProvider;
public function __construct(WidgetProviderInterface $widgetProvider)
{
$this->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.');
}
}

View File

@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Widget;
interface WidgetProviderInterface{
public function supports(array $data, array $context);
public function getDataForWidget($data);
}

View File

@ -11,28 +11,95 @@ declare(strict_types=1);
namespace Chill\MainBundle\Widget\Widgets; 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 Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
use Chill\MainBundle\Widget\Widgets\DataFetcher\WidgetBarDataFetcher;
class WidgetBar implements WidgetProviderInterface class WidgetBar implements WidgetHandlerInterface
{ {
public function supports($data, $context): bool
private Security $security;
private AccompanyingPeriodRepository $acpRepository;
private WidgetBarDataFetcher $dataFetcher;
private NotificationRepository $notificationRepository;
private EntityManagerInterface $em;
public function __construct(
AccompanyingPeriodRepository $accompanyingPeriodRepository,
Security $security,
NotificationRepository $notificationRepository,
EntityManagerInterface $em,
WidgetBarDataFetcher $dataFetcher
)
{ {
// Check if the context is "bar" $this->acpRepository = $accompanyingPeriodRepository;
return $context === 'bar'; $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 // Check if the context is "bar"
// In this example, we assume the data is an array of numbers return $config['alias'] === 'bar';
// 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'];
// 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 [ return [
'data' => $dataForWidget,
'options' => [
'responsive' => 'true',
'maintainAspectRatio' => 'false',
'position' => 'relative'
],
'type' => 'bar', 'type' => 'bar',
'data' => $barData,
]; ];
} }
} }

View File

@ -11,27 +11,96 @@ declare(strict_types=1);
namespace Chill\MainBundle\Widget\Widgets; 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{ class WidgetNumber implements WidgetHandlerInterface
public function supports($data, $context): bool {
private Security $security;
//private AccompanyingPeriodRepository $acpRepository;
private NotificationRepository $notificationRepository;
public function __construct(
// AccompanyingPeriodRepository $accompanyingPeriodRepository,
Security $security,
NotificationRepository $notificationRepository,
)
{ {
// Check if the context is "number" // $this->acpRepository = $accompanyingPeriodRepository;
return $context === 'number'; $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 return $config['alias'] === 'number';
// 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']; 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 [ return [
'data' => $dataForWidget,
'type' => 'number', 'type' => 'number',
'data' => $numberData,
]; ];
} }
} }

View File

@ -112,6 +112,24 @@ services:
autowire: true autowire: true
autoconfigure: 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: Chill\MainBundle\Cron\CronManager:
autoconfigure: true autoconfigure: true
autowire: true autowire: true