comments: form controller

This commit is contained in:
Mathieu Jaumotte 2021-12-15 23:13:20 +01:00
parent ecbe30ae53
commit f01b87b348
5 changed files with 147 additions and 62 deletions

View File

@ -0,0 +1,86 @@
<?php
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\Controller;
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\Routing\Annotation\Route;
class AccompanyingCourseCommentController extends Controller
{
/**
* 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');
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();
}
}
if ($request->getMethod() === Request::METHOD_POST) {
if (array_key_exists('edit', $request->request->all()[$editForm->getName()])) {
$currentForm = $editForm->handleRequest($request);
$isEditingNew = false;
} else {
$currentForm = $form->handleRequest($request);
$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' => isset($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;
}
}

View File

@ -14,7 +14,6 @@ namespace Chill\PersonBundle\Controller;
use Chill\ActivityBundle\Entity\Activity;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\AccompanyingCourseCommentType;
use Chill\PersonBundle\Form\AccompanyingCourseType;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
@ -110,34 +109,6 @@ class AccompanyingCourseController extends Controller
]);
}
/**
* 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
{
$comment = new AccompanyingPeriod\Comment();
$form = $this->createForm(AccompanyingCourseCommentType::class, $comment, []);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
return $this->redirectToRoute($route);
}
return $this->render('@ChillPerson/AccompanyingCourse/comment_list.html.twig', [
'accompanyingCourse' => $accompanyingCourse,
'form' => $form->createView(),
]);
}
/**
* Edit page of Accompanying Course section.
*

View File

@ -73,14 +73,12 @@ class AccompanyingCourseMenuBuilder implements LocalMenuBuilderInterface
], ])
->setExtras(['order' => 40]);
/*
$menu->addChild($this->translator->trans('Accompanying Course Comment'), [
'route' => 'chill_person_accompanying_period_comment_list',
'routeParameters' => [
'accompanying_period_id' => $period->getId(),
], ])
->setExtras(['order' => 50]);
*/
$workflow = $this->registry->get($period, 'accompanying_period_lifecycle');

View File

@ -1,63 +1,92 @@
{% extends '@ChillPerson/AccompanyingCourse/layout.html.twig' %}
{% block title %}
{{ 'Accompanying Course Comment list'|trans }}
{% endblock %}
{% macro show_comment(comment, options) %}
<div class="item-bloc">
<div class="item-row">
<div>
{% if options.pinned is defined %}
<i class="fa fa-flag fa-fw" title="{{ 'pinned'|trans }}"></i>
<i class="fa fa-flag fa-fw fa-lg" title="{{ 'pinned'|trans }}"></i>
{% endif %}
<a id="comment{{ comment.id }}" href="{{ '#comment' ~ comment.id }}" class="fa fa-pencil-square-o fa-fw"></a>
{{ 'by'|trans }}<b>{{ comment.creator }}</b>{{ ', ' ~ 'on'|trans ~ ' ' ~ comment.createdAt|format_date('long') }}<br>
<i>{{ 'Last updated on'|trans ~ ' ' ~ comment.updatedAt|format_datetime('long', 'short') }}</i>
</div>
<ul class="record_actions">
<li><a class="btn btn-edit" title="{{ 'Edit'|trans }}" href="{{ '?edit=' ~ comment.id ~ '#comment' ~ comment.id }}"></a></li>
<li><a class="btn btn-delete" title="{{ 'Delete'|trans }}" href=""></a></li>
{% if options.pinned is not defined %}
<li>
<button class="btn btn-sm btn-misc" type="button">
<i class="fa fa-flag fa-fw"></i>
Épingler
</button>
</li>
{% endif %}
<li>
<a class="btn btn-sm btn-edit" title="{{ 'Edit'|trans }}" href="{{ path('chill_person_accompanying_period_comment_list', {
'accompanying_period_id': comment.accompanyingPeriod.id,
'edit': comment.id
}) ~ '#comment' ~ comment.id }}"></a>
</li>
<li>
<a class="btn btn-sm btn-delete" title="{{ 'Delete'|trans }}" href=""></a>
</li>
</ul>
</div>
<div class="item-row separator">
<blockquote class="chill-user-quote col">{{ comment.content }}</blockquote>
<blockquote class="chill-user-quote col">{{ comment.content|chill_markdown_to_html }}</blockquote>
</div>
</div>
{% endmacro %}
{% block title %}
{{ 'Accompanying Course Comment list'|trans }}
{% endblock %}
{% macro form_comment(type, form) %}
{% if type == 'edit' %}
<div class="item-bloc">
<div class="item-row row">
{% endif %}
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_widget(form.content) }}
<ul class="record_actions">
<li>
{% if type == 'new' %}
<button class="btn btn-create" type="submit">{{ 'Post a new comment'|trans }}</button>
{% elseif type == 'edit' %}
<button class="btn btn-save" type="submit">{{ 'Save'|trans }}</button>
{% endif %}
</li>
</ul>
{{ form_end(form) }}
{% if type == 'edit' %}
</div>
</div>
{% endif %}
{% endmacro %}
{% block content %}
<div class="accompanyingcourse-comment-list">
<h1>{{ block('title') }}</h1>
<div class="form my-5">
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_widget(form.content) }}
<ul class="record_actions">
<li>
<button class="btn btn-create" type="submit">{{ 'Post a new comment'|trans }}</button>
{#
<button class="btn btn-update" type="submit">{{ 'Edit a comment'|trans }}</button>
#}
</li>
</ul>
{{ form_end(form) }}
</div>
<div class="flex-table">
{% if accompanyingCourse.pinnedComment %}
{{ _self.show_comment(accompanyingCourse.pinnedComment, {'pinned': 'true'}) }}
{% if commentEditId == accompanyingCourse.pinnedComment.id %}
{{ _self.form_comment('edit', edit_form) }}
{% else %}
{{ _self.show_comment(accompanyingCourse.pinnedComment, {'pinned': 'true'}) }}
{% endif %}
{% endif %}
{% for c in accompanyingCourse.comments %}
{{ _self.show_comment(c) }}
{% if commentEditId == c.id %}
{{ _self.form_comment('edit', edit_form) }}
{% else %}
{{ _self.show_comment(c) }}
{% endif %}
{% endfor %}
</div>
<div class="new-comment my-5">
<h2 class="chill-blue">{{ 'Write a new comment'|trans }}</h2>
{{ _self.form_comment('new', form) }}
</div>
</div>
{% endblock %}

View File

@ -413,6 +413,7 @@ Accompanying Course Comment: Commentaire
Accompanying Course Comment list: Commentaires du parcours
pinned: épinglé
Post a new comment: Poster un nouveau commentaire
Write a new comment: Écrire un nouveau commentaire
Edit a comment: Modifier le commentaire
# Household