defis48/src/Mmi/Bundle/EnigmaBundle/Controller/DefaultController.php

217 lines
6.1 KiB
PHP
Raw Normal View History

2016-01-12 19:57:14 +01:00
<?php
namespace Mmi\Bundle\EnigmaBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
2016-01-12 20:31:48 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
2016-01-12 23:51:17 +01:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
2016-01-13 19:47:16 +01:00
use DateTime;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Constraints\NotBlank;
2016-01-13 22:55:59 +01:00
use Mmi\Bundle\EnigmaBundle\Entity\Timer;
use Mmi\Bundle\EnigmaBundle\Entity\Awnser;
2016-01-12 19:57:14 +01:00
class DefaultController extends Controller
{
/**
2016-01-13 19:25:20 +01:00
* @Route("/enigma/{enigma}", name="enigma", requirements={"enigma": "\w{32}"})
2016-01-12 20:31:48 +01:00
* @Template()
2016-01-12 19:57:14 +01:00
*/
2016-01-13 19:25:20 +01:00
public function indexAction($enigma = null)
2016-01-12 19:57:14 +01:00
{
2016-01-13 19:47:16 +01:00
if ($this->getUser() === null) {
2016-01-13 19:25:20 +01:00
$this->get('session')->set(
'redirect_to',
2016-01-13 19:47:16 +01:00
$this->generateUrl('enigma', ['enigma' => $enigma !== null ? $enigma : null])
2016-01-13 19:25:20 +01:00
);
2016-01-12 23:51:17 +01:00
return $this->redirectToRoute('enigma_login');
}
2016-01-13 19:47:16 +01:00
if ($enigma !== null) {
return $this->redirectToRoute('enigma_show', ['enigma' => $enigma]);
}
2016-01-13 19:25:20 +01:00
2016-01-12 20:31:48 +01:00
return [];
2016-01-12 19:57:14 +01:00
}
2016-01-12 23:51:17 +01:00
2016-01-13 19:47:16 +01:00
/**
* @Route("/enigma/{enigma}/resoudre", name="enigma_show", requirements={"enigma": "\w{32}"})
* @Template()
*/
public function showEnigmaAction($enigma, Request $request)
{
if ($this->getUser() === null) {
$this->get('session')->set(
'redirect_to',
$this->generateUrl('enigma_show', ['enigma' => $enigma])
);
return $this->redirectToRoute('enigma_login');
}
$enigma = $this
->getDoctrine()
->getRepository('MmiEnigmaBundle:Enigma')
->findOneBy([
'hash' => $enigma,
]);
if ($enigma === null) {
return $this->redirect('enigma');
}
$timer = $this
2016-01-13 22:55:59 +01:00
->getDoctrine()
2016-01-13 19:47:16 +01:00
->getRepository('MmiEnigmaBundle:Timer')
->findOneBy([
2016-01-13 22:55:59 +01:00
'team' => $this->getUser(),
'enigma' => $enigma,
2016-01-13 19:47:16 +01:00
]);
if ($timer !== null) {
2016-01-13 22:55:59 +01:00
if (time() - $timer->getDate()->getTimestamp() >= 60 * 30) {
2016-01-13 19:47:16 +01:00
return $this->redirectToRoute('enigma_too_late');
}
} else {
$timer = new Timer();
$timer
->setTeam($this->getUser())
->setEnigma($enigma)
2016-01-13 22:55:59 +01:00
->setDate(new DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->merge($timer);
$em->flush();
2016-01-13 19:47:16 +01:00
}
$answer = $this
->getDoctrine()
->getRepository('MmiEnigmaBundle:Awnser')
->findOneBy([
2016-01-13 22:55:59 +01:00
'team' => $this->getUser(),
'enigma' => $enigma,
2016-01-13 19:47:16 +01:00
]);
2016-01-13 21:27:24 +01:00
if ($answer !== null) {
return $this->redirectToRoute('enigma_answered');
}
2016-01-13 19:47:16 +01:00
2016-01-13 22:55:59 +01:00
$form = $this->createFormBuilder($answer = new Awnser())
2016-01-13 19:47:16 +01:00
->add('content', TextareaType::class, [
'required' => true,
'constraints' => [
new NotBlank(),
],
])
->add('Soumettre', SubmitType::class)
->getForm();
if ($request->isMethod('post')) {
$form->handleRequest($request);
if ($form->isValid()) {
2016-01-13 22:55:59 +01:00
// $answer = $form->getData();
$answer
->setTeam($this->getUser())
->setDate(new DateTime('now'))
->setEnigma($enigma);
$em = $this->getDoctrine()->getManager();
$em->merge($answer);
$em->flush();
2016-01-13 19:47:16 +01:00
}
2016-01-13 22:55:59 +01:00
return $this->redirectToRoute('enigma_ok');
2016-01-13 19:47:16 +01:00
}
2016-01-13 22:55:59 +01:00
2016-01-13 19:47:16 +01:00
return [
'form' => $form->createView(),
'enigma' => $enigma,
];
}
2016-01-13 21:27:24 +01:00
2016-01-13 22:55:59 +01:00
/**
* @Route("/enigma/ok", name="enigma_ok")
* @Template()
*/
public function okAction()
{
return [];
}
2016-01-13 21:27:24 +01:00
/**
* @Route("/enigma/oklm", name="enigma_too_late")
* @Template()
*/
public function tooLateAction()
{
return [];
}
/**
* @Route("/enigma/ahah", name="enigma_answered")
* @Template()
*/
public function answeredAction()
{
return [];
}
2016-01-13 19:47:16 +01:00
2016-01-12 23:51:17 +01:00
/**
* @Route("/enigma/login", name="enigma_login")
* @Template()
*/
public function loginAction(Request $request)
{
2016-01-13 19:25:20 +01:00
$redirectTo = $this->get('session')->get('redirect_to');
$redirect = $redirectTo ? $this->redirect($redirectTo) : $this->redirectToRoute('enigma');
2016-01-13 19:47:16 +01:00
if ($this->getUser() !== null) {
2016-01-13 19:25:20 +01:00
return $redirect;
2016-01-12 23:51:17 +01:00
}
$form = $this->createFormBuilder()
->add('username', TextType::class, ['required' => true])
->add('password', PasswordType::class, ['required' => true])
->add('Se connecter', SubmitType::class)
->getForm();
if ($request->isMethod('post')) {
$form->handleRequest($request);
if ($form->isValid()) {
2016-01-13 19:47:16 +01:00
$user = $this
->getDoctrine()
2016-01-12 23:51:17 +01:00
->getRepository('MmiEnigmaBundle:Team')
->findOneBy([
'username' => $form->getData()['username'],
'password' => $form->getData()['password'],
]);
if ($user === null) {
$invalid = true;
} else {
$this->get('session')->set('user', $user);
2016-01-13 19:25:20 +01:00
return $redirect;
2016-01-12 23:51:17 +01:00
}
}
}
return [
'form' => $form->createView(),
'invalid' => !empty($invalid),
];
}
2016-01-13 19:47:16 +01:00
public function getUser()
{
return $this->get('session')->get('user');
}
2016-01-12 19:57:14 +01:00
}