mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
327 pinned comment
This commit is contained in:
@@ -13,91 +13,181 @@ namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Form\AccompanyingCourseCommentType;
|
||||
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodCommentVoter;
|
||||
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use LogicException;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
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;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class AccompanyingCourseCommentController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private FormFactoryInterface $formFactory;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $entityManager,
|
||||
FormFactoryInterface $formFactory,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->formFactory = $formFactory;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comments page of Accompanying Course section.
|
||||
* Page of comments in 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);
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::SEE_DETAILS, $accompanyingCourse);
|
||||
|
||||
$form = $this->createCommentForm($newComment, 'new');
|
||||
$editForm = null;
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$afterSuccessfulRedirectResponse = $this->redirectToRoute('chill_person_accompanying_period_comment_list', [
|
||||
'accompanying_period_id' => $accompanyingCourse->getId(),
|
||||
]);
|
||||
|
||||
if ($request->query->has('edit')) {
|
||||
if ($request->query->has('edit') && $this->isGranted(AccompanyingPeriodVoter::EDIT, $accompanyingCourse)) {
|
||||
foreach ($accompanyingCourse->getComments() as $comment) {
|
||||
if ($comment->getId() === $request->query->getInt('edit')) {
|
||||
$editForm = $this->createCommentForm($comment, 'edit');
|
||||
$commentEditId = $comment->getId();
|
||||
$commentEdited = $comment;
|
||||
}
|
||||
}
|
||||
$pinnedComment = $accompanyingCourse->getPinnedComment();
|
||||
|
||||
if ($pinnedComment->getId() === $request->query->getInt('edit')) {
|
||||
if (null !== $pinnedComment && $pinnedComment->getId() === $request->query->getInt('edit')) {
|
||||
$editForm = $this->createCommentForm($pinnedComment, 'edit');
|
||||
$commentEditId = $pinnedComment->getId();
|
||||
$commentEdited = $pinnedComment;
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $editForm) {
|
||||
throw new NotFoundHttpException('Unable to find an edit form.');
|
||||
}
|
||||
if (!isset($editForm)) {
|
||||
throw new NotFoundHttpException('comment with id ' . $request->query->getInt('edit') . ' is not found');
|
||||
}
|
||||
|
||||
if ($request->getMethod() === Request::METHOD_POST) {
|
||||
$currentForm = $editForm->handleRequest($request);
|
||||
|
||||
if (array_key_exists('edit', $request->request->all()[$editForm->getName()])) {
|
||||
$isEditingNew = false;
|
||||
if (isset($commentEdited)) {
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodCommentVoter::EDIT, $commentEdited);
|
||||
} else {
|
||||
$isEditingNew = true;
|
||||
throw new LogicException('at this step, commentEdited should be set');
|
||||
}
|
||||
|
||||
if ($currentForm->isSubmitted() && $currentForm->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($isEditingNew) {
|
||||
$em->persist($newComment);
|
||||
}
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
return $afterSuccessfulRedirectResponse;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_person_accompanying_period_comment_list', [
|
||||
'accompanying_period_id' => $accompanyingCourse->getId(),
|
||||
]);
|
||||
if ($editForm->isSubmitted() && !$editForm->isValid()) {
|
||||
$this->addFlash('error', $this->translator->trans('This form contains errors'));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingCourse/comment_list.html.twig', [
|
||||
if ($this->isGranted(AccompanyingPeriodVoter::EDIT, $accompanyingCourse)) {
|
||||
$newComment = new AccompanyingPeriod\Comment();
|
||||
$newComment->setAccompanyingPeriod($accompanyingCourse);
|
||||
|
||||
$newForm = $this->createCommentForm($newComment, 'new');
|
||||
$newForm->handleRequest($request);
|
||||
|
||||
if ($newForm->isSubmitted() && $newForm->isValid()) {
|
||||
$em->persist($newComment);
|
||||
$em->flush();
|
||||
|
||||
return $afterSuccessfulRedirectResponse;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingCourse/Comment/index.html.twig', [
|
||||
'accompanyingCourse' => $accompanyingCourse,
|
||||
'form' => $form->createView(),
|
||||
'edit_form' => null !== $editForm ? $editForm->createView() : null,
|
||||
'form' => isset($newForm) ? $newForm->createView() : null,
|
||||
'edit_form' => isset($editForm) ? $editForm->createView() : null,
|
||||
'commentEditId' => $commentEditId ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing comment.
|
||||
*
|
||||
* @Route(
|
||||
* "/{_locale}/parcours/comment/{id}/delete",
|
||||
* name="chill_person_accompanying_period_comment_delete"
|
||||
* )
|
||||
*/
|
||||
public function deleteAction(AccompanyingPeriod\Comment $comment, Request $request): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodCommentVoter::DELETE, $comment);
|
||||
|
||||
$form = $this->createForm(FormType::class, []);
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Confirm']);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->entityManager->remove($comment);
|
||||
|
||||
if ($comment->getAccompanyingPeriod()->getPinnedComment() === $comment) {
|
||||
$comment->getAccompanyingPeriod()->setPinnedComment(null);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator->trans('accompanying_course_comment.Comment removed'));
|
||||
|
||||
return $this->redirect(
|
||||
$request->query->has('returnPath') ? $request->query->get('returnPath') :
|
||||
$this->generateUrl('chill_person_accompanying_period_comment_list', [
|
||||
'accompanying_period_id' => $comment->getAccompanyingPeriod()->getId(),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingCourse/Comment/confirm_delete.html.twig', [
|
||||
'comment' => $comment,
|
||||
'delete_form' => $form->createView(),
|
||||
'accompanyingCourse' => $comment->getAccompanyingPeriod(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{_locale}/parcours/comment/{id}/pin", name="chill_person_accompanying_period_comment_pin")
|
||||
*/
|
||||
public function pinComment(AccompanyingPeriod\Comment $comment): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::EDIT, $comment->getAccompanyingPeriod());
|
||||
|
||||
$comment->getAccompanyingPeriod()->setPinnedComment($comment);
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator->trans('accompanying_course.comment is pinned'));
|
||||
|
||||
return $this->redirectToRoute('chill_person_accompanying_period_comment_list', [
|
||||
'accompanying_period_id' => $comment->getAccompanyingPeriod()->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
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;
|
||||
return $this->formFactory->createNamed($step, AccompanyingCourseCommentType::class, $comment);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user