106 lines
3.1 KiB
PHP

<?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\Widgets;
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 WidgetHandlerInterface
{
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
)
{
// $this->acpRepository = $accompanyingPeriodRepository;
$this->security = $security;
// $this->notificationRepository = $notificationRepository;
// $this->em = $em;
$this->dataFetcher = $dataFetcher;
}
public function supports(array $config, array $context = []): bool
{
// Check if the context is "bar"
return $config['alias'] === 'bar';
}
/**
* @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',
];
}
}