Question & response controllers

pull/10/head
root 2018-06-05 17:09:52 +02:00
parent e78e174fdb
commit 6f91d1209b
17 changed files with 427 additions and 0 deletions

View File

@ -13,4 +13,5 @@ return [
ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
];

View File

@ -0,0 +1,90 @@
<?php
namespace App\Controller;
use App\Entity\Question;
use App\Form\QuestionType;
use App\Repository\QuestionRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/admin/question")
*/
class QuestionController extends Controller
{
/**
* @Route("/", name="question_index", methods="GET")
*/
public function index(QuestionRepository $questionRepository): Response
{
return $this->render('question/index.html.twig', ['questions' => $questionRepository->findAll()]);
}
/**
* @Route("/new", name="question_new", methods="GET|POST")
*/
public function new(Request $request): Response
{
$question = new Question();
$form = $this->createForm(QuestionType::class, $question);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($question);
$em->flush();
return $this->redirectToRoute('question_index');
}
return $this->render('question/new.html.twig', [
'question' => $question,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="question_show", methods="GET")
*/
public function show(Question $question): Response
{
return $this->render('question/show.html.twig', ['question' => $question]);
}
/**
* @Route("/{id}/edit", name="question_edit", methods="GET|POST")
*/
public function edit(Request $request, Question $question): Response
{
$form = $this->createForm(QuestionType::class, $question);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('question_edit', ['id' => $question->getId()]);
}
return $this->render('question/edit.html.twig', [
'question' => $question,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="question_delete", methods="DELETE")
*/
public function delete(Request $request, Question $question): Response
{
if ($this->isCsrfTokenValid('delete'.$question->getId(), $request->request->get('_token'))) {
$em = $this->getDoctrine()->getManager();
$em->remove($question);
$em->flush();
}
return $this->redirectToRoute('question_index');
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace App\Controller;
use App\Entity\Response;
use App\Form\ResponseType;
use App\Repository\ResponseRepository;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/admin/response")
*/
class ResponseController extends Controller
{
/**
* @Route("/", name="response_index", methods="GET")
*/
public function index(ResponseRepository $responseRepository): HttpResponse
{
return $this->render('response/index.html.twig', ['responses' => $responseRepository->findAll()]);
}
/**
* @Route("/new", name="response_new", methods="GET|POST")
*/
public function new(Request $request): HttpResponse
{
$response = new Response();
$form = $this->createForm(ResponseType::class, $response);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($response);
$em->flush();
return $this->redirectToRoute('response_index');
}
return $this->render('response/new.html.twig', [
'response' => $response,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="response_show", methods="GET")
*/
public function show(Response $response): HttpResponse
{
return $this->render('response/show.html.twig', ['response' => $response]);
}
/**
* @Route("/{id}/edit", name="response_edit", methods="GET|POST")
*/
public function edit(Request $request, Response $response): HttpResponse
{
$form = $this->createForm(ResponseType::class, $response);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('response_edit', ['id' => $response->getId()]);
}
return $this->render('response/edit.html.twig', [
'response' => $response,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="response_delete", methods="DELETE")
*/
public function delete(Request $request, Response $response): HttpResponse
{
if ($this->isCsrfTokenValid('delete'.$response->getId(), $request->request->get('_token'))) {
$em = $this->getDoctrine()->getManager();
$em->remove($response);
$em->flush();
}
return $this->redirectToRoute('response_index');
}
}

25
src/Form/QuestionType.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Form;
use App\Entity\Question;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class QuestionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Question::class,
]);
}
}

27
src/Form/ResponseType.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Form;
use App\Entity\Response;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ResponseType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text')
->add('question')
->add('child')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Response::class,
]);
}
}

View File

@ -0,0 +1,5 @@
<form method="post" action="{{ path('question_delete', {'id': question.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ question.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Question{% endblock %}
{% block body %}
<h1>Edit Question</h1>
{{ include('question/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('question_index') }}">back to list</a>
{{ include('question/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,37 @@
{% extends 'base.html.twig' %}
{% block title %}Question index{% endblock %}
{% block body %}
<h1>Question index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Text</th>
<th>Date</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for question in questions %}
<tr>
<td>{{ question.id }}</td>
<td>{{ question.text }}</td>
<td>{{ question.date ? question.date|date('Y-m-d H:i:s') : '' }}</td>
<td>
<a href="{{ path('question_show', {'id': question.id}) }}">show</a>
<a href="{{ path('question_edit', {'id': question.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="5">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('question_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Question{% endblock %}
{% block body %}
<h1>Create new Question</h1>
{{ include('question/_form.html.twig') }}
<a href="{{ path('question_index') }}">back to list</a>
{% endblock %}

View File

@ -0,0 +1,30 @@
{% extends 'base.html.twig' %}
{% block title %}Question{% endblock %}
{% block body %}
<h1>Question</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ question.id }}</td>
</tr>
<tr>
<th>Text</th>
<td>{{ question.text }}</td>
</tr>
<tr>
<th>Date</th>
<td>{{ question.date ? question.date|date('Y-m-d H:i:s') : '' }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('question_index') }}">back to list</a>
<a href="{{ path('question_edit', {'id': question.id}) }}">edit</a>
{{ include('question/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,5 @@
<form method="post" action="{{ path('response_delete', {'id': response.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ response.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Response{% endblock %}
{% block body %}
<h1>Edit Response</h1>
{{ include('response/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('response_index') }}">back to list</a>
{{ include('response/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,35 @@
{% extends 'base.html.twig' %}
{% block title %}Response index{% endblock %}
{% block body %}
<h1>Response index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Text</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for response in responses %}
<tr>
<td>{{ response.id }}</td>
<td>{{ response.text }}</td>
<td>
<a href="{{ path('response_show', {'id': response.id}) }}">show</a>
<a href="{{ path('response_edit', {'id': response.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="3">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('response_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Response{% endblock %}
{% block body %}
<h1>Create new Response</h1>
{{ include('response/_form.html.twig') }}
<a href="{{ path('response_index') }}">back to list</a>
{% endblock %}

View File

@ -0,0 +1,26 @@
{% extends 'base.html.twig' %}
{% block title %}Response{% endblock %}
{% block body %}
<h1>Response</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ response.id }}</td>
</tr>
<tr>
<th>Text</th>
<td>{{ response.text }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('response_index') }}">back to list</a>
<a href="{{ path('response_edit', {'id': response.id}) }}">edit</a>
{{ include('response/_delete_form.html.twig') }}
{% endblock %}