chill-bundles/src/Bundle/ChillDocStoreBundle/Controller/DocumentAccompanyingCourseController.php

215 lines
7.0 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);
/**
* 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\DocStoreBundle\Controller;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Form\AccompanyingCourseDocumentType;
use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/{_locale}/parcours/{course}/document")
*/
class DocumentAccompanyingCourseController extends AbstractController
{
/**
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* DocumentAccompanyingCourseController constructor.
*/
public function __construct(
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher,
AuthorizationHelper $authorizationHelper
) {
$this->translator = $translator;
$this->eventDispatcher = $eventDispatcher;
$this->authorizationHelper = $authorizationHelper;
}
/**
* @Route("/{id}", name="accompanying_course_document_delete", methods="DELETE")
*/
public function delete(Request $request, AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response
{
$this->denyAccessUnlessGranted(AccompanyingCourseDocumentVoter::DELETE, $document);
if ($this->isCsrfTokenValid('delete' . $document->getId(), $request->request->get('_token'))) {
$em = $this->getDoctrine()->getManager();
$em->remove($document);
$em->flush();
}
return $this->redirectToRoute(
'accompanying_course_document_index',
['accompanyingCourse' => $course->getId()]
);
}
/**
* @Route("/{id}/edit", name="accompanying_course_document_edit", methods="GET|POST")
*/
public function edit(Request $request, AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response
{
$this->denyAccessUnlessGranted(AccompanyingCourseDocumentVoter::UPDATE, $document);
$document->setUser($this->getUser());
$document->setDate(new DateTime('Now'));
$form = $this->createForm(
AccompanyingCourseDocumentType::class,
$document
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', $this->translator->trans('The document is successfully updated'));
return $this->redirectToRoute(
'accompanying_course_document_edit',
['id' => $document->getId(), 'course' => $course->getId()]
);
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
return $this->render(
'ChillDocStoreBundle:AccompanyingCourseDocument:edit.html.twig',
[
'document' => $document,
'form' => $form->createView(),
'accompanyingCourse' => $course,
]
);
}
/**
* @Route("/", name="accompanying_course_document_index", methods="GET")
*/
public function index(AccompanyingPeriod $course): Response
{
$em = $this->getDoctrine()->getManager();
if (null === $course) {
throw $this->createNotFoundException('Accompanying period not found');
}
$this->denyAccessUnlessGranted(AccompanyingCourseDocumentVoter::SEE, $course);
$documents = $em
->getRepository('ChillDocStoreBundle:AccompanyingCourseDocument')
->findBy(
['course' => $course],
['date' => 'DESC']
);
return $this->render(
'ChillDocStoreBundle:AccompanyingCourseDocument:index.html.twig',
[
'documents' => $documents,
'accompanyingCourse' => $course,
]
);
}
/**
* @Route("/new", name="accompanying_course_document_new", methods="GET|POST")
*/
public function new(Request $request, AccompanyingPeriod $course): Response
{
if (null === $course) {
throw $this->createNotFoundException('Accompanying period not found');
}
$document = new AccompanyingCourseDocument();
$document->setUser($this->getUser());
$document->setCourse($course);
$document->setDate(new DateTime('Now'));
$this->denyAccessUnlessGranted(AccompanyingCourseDocumentVoter::CREATE, $document);
$form = $this->createForm(AccompanyingCourseDocumentType::class, $document);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->denyAccessUnlessGranted(
'CHILL_ACCOMPANYING_COURSE_DOCUMENT_CREATE',
$document,
'creation of this activity not allowed'
);
$em = $this->getDoctrine()->getManager();
$em->persist($document);
$em->flush();
$this->addFlash('success', $this->translator->trans('The document is successfully registered'));
return $this->redirectToRoute('accompanying_course_document_index', ['course' => $course->getId()]);
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
return $this->render('ChillDocStoreBundle:AccompanyingCourseDocument:new.html.twig', [
'document' => $document,
'form' => $form->createView(),
'accompanyingCourse' => $course,
]);
}
/**
* @Route("/{id}", name="accompanying_course_document_show", methods="GET")
*/
public function show(AccompanyingPeriod $course, AccompanyingCourseDocument $document): Response
{
$this->denyAccessUnlessGranted(AccompanyingCourseDocumentVoter::SEE_DETAILS, $document);
return $this->render(
'ChillDocStoreBundle:AccompanyingCourseDocument:show.html.twig',
['document' => $document, 'accompanyingCourse' => $course]
);
}
}