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

@@ -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,
];
}
}