mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
155 lines
5.3 KiB
PHP
155 lines
5.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Controller;
|
|
|
|
use Chill\MainBundle\Entity\Notification;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Form\NotificationType;
|
|
use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound;
|
|
use Chill\MainBundle\Notification\NotificationHandlerManager;
|
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
|
use Chill\MainBundle\Repository\NotificationRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Security\Core\Security;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
/**
|
|
* @Route("/{_locale}/notification")
|
|
*/
|
|
class NotificationController extends AbstractController
|
|
{
|
|
private EntityManagerInterface $em;
|
|
|
|
private NotificationHandlerManager $notificationHandlerManager;
|
|
|
|
private NotificationRepository $notificationRepository;
|
|
|
|
private PaginatorFactory $paginatorFactory;
|
|
|
|
private Security $security;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(
|
|
EntityManagerInterface $em,
|
|
Security $security,
|
|
NotificationRepository $notificationRepository,
|
|
NotificationHandlerManager $notificationHandlerManager,
|
|
PaginatorFactory $paginatorFactory,
|
|
TranslatorInterface $translator
|
|
) {
|
|
$this->em = $em;
|
|
$this->security = $security;
|
|
$this->notificationRepository = $notificationRepository;
|
|
$this->notificationHandlerManager = $notificationHandlerManager;
|
|
$this->paginatorFactory = $paginatorFactory;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
/**
|
|
* @Route("/create", name="chill_main_notification_create")
|
|
*/
|
|
public function createAction(Request $request): Response
|
|
{
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
|
|
|
|
if (!$this->security->getUser() instanceof User) {
|
|
throw new AccessDeniedHttpException('You must be authenticated and a user to create a notification');
|
|
}
|
|
|
|
if (!$request->query->has('entityClass')) {
|
|
throw new BadRequestHttpException('Missing entityClass parameter');
|
|
}
|
|
|
|
if (!$request->query->has('entityId')) {
|
|
throw new BadRequestHttpException('missing entityId parameter');
|
|
}
|
|
|
|
$notification = new Notification();
|
|
$notification
|
|
->setRelatedEntityClass($request->query->get('entityClass'))
|
|
->setRelatedEntityId($request->query->getInt('entityId'))
|
|
->setSender($this->security->getUser());
|
|
|
|
try {
|
|
$handler = $this->notificationHandlerManager->getHandler($notification);
|
|
} catch (NotificationHandlerNotFound $e) {
|
|
throw new BadRequestHttpException('no handler for this notification');
|
|
}
|
|
|
|
$form = $this->createForm(NotificationType::class, $notification);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->em->persist($notification);
|
|
$this->em->flush();
|
|
|
|
$this->addFlash('success', $this->translator->trans('notification.Notification created'));
|
|
|
|
if ($request->query->has('returnPath')) {
|
|
return new RedirectResponse($request->query->get('returnPath'));
|
|
}
|
|
|
|
return $this->redirectToRoute('chill_main_homepage');
|
|
}
|
|
|
|
return $this->render('@ChillMain/Notification/create.html.twig', [
|
|
'form' => $form->createView(),
|
|
'handler' => $handler,
|
|
'notification' => $notification,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @Route("/my", name="chill_main_notification_my")
|
|
*/
|
|
public function myAction(): Response
|
|
{
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
|
|
$currentUser = $this->security->getUser();
|
|
|
|
$notificationsNbr = $this->notificationRepository->countAllForAttendee(($currentUser));
|
|
$paginator = $this->paginatorFactory->create($notificationsNbr);
|
|
|
|
$notifications = $this->notificationRepository->findAllForAttendee(
|
|
$currentUser,
|
|
$limit = $paginator->getItemsPerPage(),
|
|
$offset = $paginator->getCurrentPage()->getFirstItemNumber()
|
|
);
|
|
|
|
$templateData = [];
|
|
|
|
foreach ($notifications as $notification) {
|
|
$data = [
|
|
'template' => $this->notificationHandlerManager->getTemplate($notification),
|
|
'template_data' => $this->notificationHandlerManager->getTemplateData($notification),
|
|
'notification' => $notification,
|
|
];
|
|
$templateData[] = $data;
|
|
}
|
|
|
|
return $this->render('@ChillMain/Notification/show.html.twig', [
|
|
'datas' => $templateData,
|
|
'notifications' => $notifications,
|
|
'paginator' => $paginator,
|
|
]);
|
|
}
|
|
}
|