217 lines
6.1 KiB
PHP
217 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace Mmi\Bundle\EnigmaBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
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;
|
|
use DateTime;
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
use Mmi\Bundle\EnigmaBundle\Entity\Timer;
|
|
use Mmi\Bundle\EnigmaBundle\Entity\Awnser;
|
|
|
|
class DefaultController extends Controller
|
|
{
|
|
/**
|
|
* @Route("/enigma/{enigma}", name="enigma", requirements={"enigma": "\w{32}"})
|
|
* @Template()
|
|
*/
|
|
public function indexAction($enigma = null)
|
|
{
|
|
if ($this->getUser() === null) {
|
|
$this->get('session')->set(
|
|
'redirect_to',
|
|
$this->generateUrl('enigma', ['enigma' => $enigma !== null ? $enigma : null])
|
|
);
|
|
|
|
return $this->redirectToRoute('enigma_login');
|
|
}
|
|
|
|
if ($enigma !== null) {
|
|
return $this->redirectToRoute('enigma_show', ['enigma' => $enigma]);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
->getDoctrine()
|
|
->getRepository('MmiEnigmaBundle:Timer')
|
|
->findOneBy([
|
|
'team' => $this->getUser(),
|
|
'enigma' => $enigma,
|
|
]);
|
|
|
|
if ($timer !== null) {
|
|
if (time() - $timer->getDate()->getTimestamp() >= 60 * 30) {
|
|
return $this->redirectToRoute('enigma_too_late');
|
|
}
|
|
} else {
|
|
$timer = new Timer();
|
|
$timer
|
|
->setTeam($this->getUser())
|
|
->setEnigma($enigma)
|
|
->setDate(new DateTime('now'));
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->merge($timer);
|
|
$em->flush();
|
|
}
|
|
|
|
$answer = $this
|
|
->getDoctrine()
|
|
->getRepository('MmiEnigmaBundle:Awnser')
|
|
->findOneBy([
|
|
'team' => $this->getUser(),
|
|
'enigma' => $enigma,
|
|
]);
|
|
|
|
if ($answer !== null) {
|
|
return $this->redirectToRoute('enigma_answered');
|
|
}
|
|
|
|
$form = $this->createFormBuilder($answer = new Awnser())
|
|
->add('content', TextareaType::class, [
|
|
'required' => true,
|
|
'constraints' => [
|
|
new NotBlank(),
|
|
],
|
|
])
|
|
->add('Soumettre', SubmitType::class)
|
|
->getForm();
|
|
|
|
if ($request->isMethod('post')) {
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isValid()) {
|
|
// $answer = $form->getData();
|
|
$answer
|
|
->setTeam($this->getUser())
|
|
->setDate(new DateTime('now'))
|
|
->setEnigma($enigma);
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->merge($answer);
|
|
$em->flush();
|
|
}
|
|
|
|
return $this->redirectToRoute('enigma_ok');
|
|
}
|
|
|
|
return [
|
|
'form' => $form->createView(),
|
|
'enigma' => $enigma,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @Route("/enigma/ok", name="enigma_ok")
|
|
* @Template()
|
|
*/
|
|
public function okAction()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @Route("/enigma/oklm", name="enigma_too_late")
|
|
* @Template()
|
|
*/
|
|
public function tooLateAction()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @Route("/enigma/ahah", name="enigma_answered")
|
|
* @Template()
|
|
*/
|
|
public function answeredAction()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @Route("/enigma/login", name="enigma_login")
|
|
* @Template()
|
|
*/
|
|
public function loginAction(Request $request)
|
|
{
|
|
$redirectTo = $this->get('session')->get('redirect_to');
|
|
$redirect = $redirectTo ? $this->redirect($redirectTo) : $this->redirectToRoute('enigma');
|
|
|
|
if ($this->getUser() !== null) {
|
|
return $redirect;
|
|
}
|
|
|
|
$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()) {
|
|
$user = $this
|
|
->getDoctrine()
|
|
->getRepository('MmiEnigmaBundle:Team')
|
|
->findOneBy([
|
|
'username' => $form->getData()['username'],
|
|
'password' => $form->getData()['password'],
|
|
]);
|
|
|
|
if ($user === null) {
|
|
$invalid = true;
|
|
} else {
|
|
$this->get('session')->set('user', $user);
|
|
|
|
return $redirect;
|
|
}
|
|
}
|
|
}
|
|
|
|
return [
|
|
'form' => $form->createView(),
|
|
'invalid' => !empty($invalid),
|
|
];
|
|
}
|
|
|
|
public function getUser()
|
|
{
|
|
return $this->get('session')->get('user');
|
|
}
|
|
}
|