chill-bundles/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseCommentController.php

105 lines
3.7 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\PersonBundle\Controller;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Form\AccompanyingCourseCommentType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use function array_key_exists;
class AccompanyingCourseCommentController extends AbstractController
{
/**
* Comments page of Accompanying Course section.
*
* @Route("/{_locale}/parcours/{accompanying_period_id}/comments", name="chill_person_accompanying_period_comment_list")
* @ParamConverter("accompanyingCourse", options={"id": "accompanying_period_id"})
*/
public function commentAction(AccompanyingPeriod $accompanyingCourse, Request $request): Response
{
$newComment = new AccompanyingPeriod\Comment();
$newComment->setAccompanyingPeriod($accompanyingCourse);
$form = $this->createCommentForm($newComment, 'new');
$editForm = null;
if ($request->query->has('edit')) {
foreach ($accompanyingCourse->getComments() as $comment) {
if ($comment->getId() === $request->query->getInt('edit')) {
$editForm = $this->createCommentForm($comment, 'edit');
$commentEditId = $comment->getId();
}
}
$pinnedComment = $accompanyingCourse->getPinnedComment();
if ($pinnedComment->getId() === $request->query->getInt('edit')) {
$editForm = $this->createCommentForm($pinnedComment, 'edit');
$commentEditId = $pinnedComment->getId();
}
}
dump($editForm);
//if (null === $editForm) {
// throw new NotFoundHttpException('Unable to find an edit form.');
//}
if ($request->getMethod() === Request::METHOD_POST) {
$currentForm = $editForm->handleRequest($request);
if (array_key_exists('edit', $request->request->all()[$editForm->getName()])) {
$isEditingNew = false;
} else {
$isEditingNew = true;
}
if ($currentForm->isSubmitted() && $currentForm->isValid()) {
$em = $this->getDoctrine()->getManager();
if ($isEditingNew) {
$em->persist($newComment);
}
$em->flush();
}
return $this->redirectToRoute('chill_person_accompanying_period_comment_list', [
'accompanying_period_id' => $accompanyingCourse->getId(),
]);
}
return $this->render('@ChillPerson/AccompanyingCourse/comment_list.html.twig', [
'accompanyingCourse' => $accompanyingCourse,
'form' => $form->createView(),
'edit_form' => null !== $editForm ? $editForm->createView() : null,
'commentEditId' => $commentEditId ?? null,
]);
}
private function createCommentForm(AccompanyingPeriod\Comment $comment, string $step): FormInterface
{
$form = $this->createForm(AccompanyingCourseCommentType::class, $comment);
if ('edit' === $step) {
$form->add('edit', HiddenType::class, ['mapped' => false]);
}
return $form;
}
}