Merge branch 'master' into notification/completion

This commit is contained in:
2022-01-11 10:18:02 +01:00
9 changed files with 230 additions and 113 deletions

View File

@@ -13,20 +13,41 @@ 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;
}
/**
* Page of comments in Accompanying Course section.
*
@@ -35,107 +56,139 @@ class AccompanyingCourseCommentController extends AbstractController
*/
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 (!isset($editForm)) {
throw new NotFoundHttpException('comment with id ' . $request->query->getInt('edit') . ' is not found');
}
if (isset($commentEdited)) {
$this->denyAccessUnlessGranted(AccompanyingPeriodCommentVoter::EDIT, $commentEdited);
} else {
throw new LogicException('at this step, commentEdited should be set');
}
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em->flush();
return $afterSuccessfulRedirectResponse;
}
if ($editForm->isSubmitted() && !$editForm->isValid()) {
$this->addFlash('error', $this->translator->trans('This form contains errors'));
}
}
dump($editForm);
//if (null === $editForm) {
// throw new NotFoundHttpException('Unable to find an edit form.');
//}
if ($this->isGranted(AccompanyingPeriodVoter::EDIT, $accompanyingCourse)) {
$newComment = new AccompanyingPeriod\Comment();
$newComment->setAccompanyingPeriod($accompanyingCourse);
if ($request->getMethod() === Request::METHOD_POST) {
$currentForm = $editForm->handleRequest($request);
$newForm = $this->createCommentForm($newComment, 'new');
$newForm->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);
}
if ($newForm->isSubmitted() && $newForm->isValid()) {
$em->persist($newComment);
$em->flush();
}
return $this->redirectToRoute('chill_person_accompanying_period_comment_list', [
'accompanying_period_id' => $accompanyingCourse->getId(),
]);
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);
}
////////////// building
///
// /**
// * Delete an existing comment
// *
// * @Route(
// * "/{_locale}/parcours/{accompanying_period_id}/comment/{comment_id}/delete",
// * name="chill_person_accompanying_period_comment_delete"
// * )
// * @ParamConverter("accompanyingCourse", options={"id": "accompanying_period_id"})
// * @ParamConverter("comment", options={"id": "comment_id"})
// */
// public function deleteAction(AccompanyingPeriod $accompanyingCourse, AccompanyingPeriod\Comment $comment, Request $request)
// {
// $form = $this->createDeleteForm($comment->getId(), $accompanyingCourse);
//
// return $this->render('@ChillPerson/AccompanyingCourse/Comment/confirm_delete.html.twig', [
// 'comment' => $comment,
// 'delete_form' => $form->createView(),
// 'accompanyingCourse' => $accompanyingCourse,
// ]);
// }
//
// private function createDeleteForm(int $id, ?AccompanyingPeriod $accompanyingCourse): FormInterface
// {
// $form = $this->createForm($type);
//
// return $this->createFormBuilder()
// ->setAction($this->generateUrl('chill_person_accompanying_period_comment_delete', $params))
// ->setMethod('DELETE')
// ->add('submit', SubmitType::class, ['label' => 'Delete'])
// ->getForm();
// }
//
//// end
}