notification: create

This commit is contained in:
Julien Fastré 2021-12-25 22:41:25 +01:00
parent d62893827b
commit 11824adda4
10 changed files with 210 additions and 26 deletions

View File

@ -13,8 +13,9 @@ namespace Chill\ActivityBundle\Notification;
use Chill\ActivityBundle\Entity\Activity;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Notification\NotificationHandlerInterface;
final class ActivityNotificationRenderer
final class ActivityNotificationRenderer implements NotificationHandlerInterface
{
public function getTemplate()
{

View File

@ -11,41 +11,118 @@ declare(strict_types=1);
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Notification\NotificationRenderer;
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 Security $security;
private EntityManagerInterface $em;
private NotificationHandlerManager $notificationHandlerManager;
private NotificationRepository $notificationRepository;
private NotificationRenderer $notificationRenderer;
private PaginatorFactory $paginatorFactory;
private Security $security;
private TranslatorInterface $translator;
public function __construct(
EntityManagerInterface $em,
Security $security,
NotificationRepository $notificationRepository,
NotificationRenderer $notificationRenderer,
PaginatorFactory $paginatorFactory
NotificationHandlerManager $notificationHandlerManager,
PaginatorFactory $paginatorFactory,
TranslatorInterface $translator
) {
$this->em = $em;
$this->security = $security;
$this->notificationRepository = $notificationRepository;
$this->notificationRenderer = $notificationRenderer;
$this->notificationHandlerManager = $notificationHandlerManager;
$this->paginatorFactory = $paginatorFactory;
$this->translator = $translator;
}
/**
* @Route("/show", name="chill_main_notification_show")
* @Route("/create", name="chill_main_notification_create")
*/
public function showAction(): Response
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));
@ -61,8 +138,8 @@ class NotificationController extends AbstractController
foreach ($notifications as $notification) {
$data = [
'template' => $this->notificationRenderer->getTemplate($notification),
'template_data' => $this->notificationRenderer->getTemplateData($notification),
'template' => $this->notificationHandlerManager->getTemplate($notification),
'template_data' => $this->notificationHandlerManager->getTemplateData($notification),
'notification' => $notification,
];
$templateData[] = $data;

View File

@ -53,7 +53,7 @@ class Notification
/**
* @ORM\Column(type="json")
*/
private array $read;
private array $read = [];
/**
* @ORM\Column(type="string", length=255)
@ -74,6 +74,7 @@ class Notification
public function __construct()
{
$this->addressees = new ArrayCollection();
$this->setDate(new DateTimeImmutable());
}
public function addAddressee(User $addressee): self

View File

@ -0,0 +1,41 @@
<?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\Form;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillTextareaType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class NotificationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('addressees', EntityType::class, [
'class' => User::class,
'choice_label' => 'label',
'multiple' => true,
])
->add('message', ChillTextareaType::class, [
'required' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('class', Notification::class);
}
}

View File

@ -0,0 +1,18 @@
<?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\Notification\Exception;
use RuntimeException;
class NotificationHandlerNotFound extends RuntimeException
{
}

View File

@ -0,0 +1,16 @@
<?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\Notification;
interface NotificationHandlerInterface
{
}

View File

@ -13,8 +13,8 @@ namespace Chill\MainBundle\Notification;
use Chill\ActivityBundle\Notification\ActivityNotificationRenderer;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound;
use Chill\PersonBundle\Notification\AccompanyingPeriodNotificationRenderer;
use Exception;
final class NotificationHandlerManager
{
@ -31,17 +31,7 @@ final class NotificationHandlerManager
$this->renderers[] = $activityNotificationRenderer;
}
public function getTemplate(Notification $notification)
{
return $this->getRenderer($notification)->getTemplate();
}
public function getTemplateData(Notification $notification)
{
return $this->getRenderer($notification)->getTemplateData($notification);
}
private function getRenderer(Notification $notification)
public function getHandler(Notification $notification): NotificationHandlerInterface
{
foreach ($this->renderers as $renderer) {
if ($renderer->supports($notification)) {
@ -49,6 +39,16 @@ final class NotificationHandlerManager
}
}
throw new Exception('No renderer for ' . $notification);
throw new NotificationHandlerNotFound();
}
public function getTemplate(Notification $notification)
{
return $this->getHandler($notification)->getTemplate();
}
public function getTemplateData(Notification $notification)
{
return $this->getHandler($notification)->getTemplateData($notification);
}
}

View File

@ -0,0 +1,25 @@
{% extends "@ChillMain/layout.html.twig" %}
{% block content %}
<div class="row">
<div class="col-md-10">
{% include handler.template(notification) with handler.templateData(notification) %}
{{ form_start(form, { 'attr': { 'id': 'notification' }}) }}
{{ form_row(form.addressees) }}
{{ form_row(form.message) }}
{{ form_end(form) }}
<ul class="record_actions sticky-form-buttons">
<li class="cancel">
<a href="{{ chill_return_path_or('chill_main_homepage') }}" class="btn btn-cancel">{{ 'Cancel'|trans|chill_return_path_label }}</a>
</li>
<li>
<button type="submit" form="notification" class="btn btn-save">{{ 'Save'|trans }}</button>
</li>
</ul>
</div>
</div>
{% endblock %}

View File

@ -12,9 +12,10 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Notification;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Notification\NotificationHandlerInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
final class AccompanyingPeriodNotificationRenderer
final class AccompanyingPeriodNotificationRenderer implements NotificationHandlerInterface
{
public function getTemplate()
{

View File

@ -108,6 +108,10 @@
</div>
{% endif %}
<div class="col col-sm-4 col-lg-4 notify mb-4">
<a class="btn btn-notify" href="{{ chill_path_add_return_path('chill_main_notification_create', {'entityClass': 'Chill\\PersonBundle\\Entity\\AccompanyingPeriod', 'entityId': accompanyingCourse.id}) }}"><i class="fa fa-envelope"></i> {{ 'notification.Notify'|trans }}</a>
</div>
{% if accompanyingCourse.requestorPerson is not null or accompanyingCourse.requestorThirdParty is not null %}
<div class="col col-sm-6 col-lg-4 requestor mb-4">
{% if accompanyingCourse.requestorPerson is not null %}