Commit 6b6a7f51 authored by ericksho-ipre's avatar ericksho-ipre

falta agregar seguridad, punto de entrada y css

parent 3e68fba6
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
<ul id="main-menu" class="nav navbar-nav"> <ul id="main-menu" class="nav navbar-nav">
{% if is_granted('ROLE_ADMIN') %} {% if is_granted('ROLE_ADMIN') %}
<li><a href="{{path('user_index')}}">Usuarios</a></li> <li><a href="{{path('user_index')}}">Usuarios</a></li>
<li><a href="{{path('meeting_index')}}">Meetings</a></li> <li><a href="{{path('event_index')}}">Eventos</a></li>
{% endif %} {% endif %}
{% if is_granted('ROLE_STUDENT') and not is_granted('ROLE_ADMIN') %} {% if is_granted('ROLE_STUDENT') and not is_granted('ROLE_ADMIN') %}
......
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block body %} {% block body %}
<h1>Meeting edit</h1> <h1>Editar evento</h1>
{{ form_start(edit_form) }} {{ form_start(edit_form) }}
{{ form_widget(edit_form) }} {{ form_widget(edit_form) }}
<input type="submit" value="Edit" /> <input type="submit" value="Editar" class="btn btn1 btn-primary"/>
{{ form_end(edit_form) }} {{ form_end(edit_form) }}
<ul> <a href="{{ path('event_index') }}" class="btn btn2 btn-primary">Listar eventos</a>
<li>
<a href="{{ path('meeting_index') }}">Back to the list</a> {{ form_start(delete_form) }}
</li> <input type="submit" value="Elminar" class="btn btn3 btn-primary">
<li> {{ form_end(delete_form) }}
{{ form_start(delete_form) }}
<input type="submit" value="Delete">
{{ form_end(delete_form) }}
</li>
</ul>
{% endblock %} {% endblock %}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Eventos</h1>
<table class="table">
<thead>
<tr>
<th>Nombre</th>
<th>Hashtag</th>
<th>Abierto</th>
<th></th>
</tr>
</thead>
<tbody>
{% for event in events %}
<tr>
<td>{{ event.name }}</td>
<td>{{ event.hashtag }}</td>
<td>{% if event.open %}Si{% else %}No{% endif %}</td>
<td>
<a href="{{ path('event_show', { 'id': event.id }) }}" class="btn btn-xs btn-primary">ver</a>
<a href="{{ path('event_edit', { 'id': event.id }) }}" class="btn btn-xs btn-primary">editar</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('event_new') }}" class="btn btn-primary">Crear Evento</a>
{% endblock %}
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
<input type="submit" value="Crear" class="btn btn1 btn-primary" /> <input type="submit" value="Crear" class="btn btn1 btn-primary" />
{{ form_end(form) }} {{ form_end(form) }}
<a href="{{ path('meeting_index') }}" class="btn btn2 btn-primary">Listar Eventos</a> <a href="{{ path('event_index') }}" class="btn btn2 btn-primary">Listar Eventos</a>
{% endblock %} {% endblock %}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Meeting</h1>
<table class="table">
<tbody>
<tr>
<th>Nombre</th>
<td>{{ event.name }}</td>
</tr>
<tr>
<th>Hashtag</th>
<td>{{ event.hashtag }}</td>
</tr>
<tr>
<th>Abierto</th>
<td>{% if event.open %}Si{% else %}No{% endif %}</td>
</tr>
</tbody>
</table>
<a href="{{ path('event_index') }}" class="btn btn1 btn-primary" >Listar Eventos</a>
<a href="{{ path('event_edit', { 'id': event.id }) }}" class="btn btn2 btn-primary">Editar</a>
{{ form_start(delete_form) }}
<input type="submit" value="Eliminar" class="btn btn3 btn-primary">
{{ form_end(delete_form) }}
{% endblock %}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Meetingos</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Hashtag</th>
<th>Open</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for meeting in meetings %}
<tr>
<td><a href="{{ path('meeting_show', { 'id': meeting.id }) }}">{{ meeting.id }}</a></td>
<td>{{ meeting.name }}</td>
<td>{{ meeting.hashtag }}</td>
<td>{% if meeting.open %}Yes{% else %}No{% endif %}</td>
<td>
<ul>
<li>
<a href="{{ path('meeting_show', { 'id': meeting.id }) }}">show</a>
</li>
<li>
<a href="{{ path('meeting_edit', { 'id': meeting.id }) }}">edit</a>
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul>
<li>
<a href="{{ path('meeting_new') }}">Create a new meeting</a>
</li>
</ul>
{% endblock %}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Meeting</h1>
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ meeting.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ meeting.name }}</td>
</tr>
<tr>
<th>Hashtag</th>
<td>{{ meeting.hashtag }}</td>
</tr>
<tr>
<th>Open</th>
<td>{% if meeting.open %}Yes{% else %}No{% endif %}</td>
</tr>
</tbody>
</table>
<ul>
<li>
<a href="{{ path('meeting_index') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('meeting_edit', { 'id': meeting.id }) }}">Edit</a>
</li>
<li>
{{ form_start(delete_form) }}
<input type="submit" value="Delete">
{{ form_end(delete_form) }}
</li>
</ul>
{% endblock %}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Evento {{ event.name }}</h1>
<div id="questions">
{% for question in questions %}
<div data-sort="{{ question.dataSort() }}" id="q{{ question.id }}" class="question-div">
<h5>{{ question.question }}</h5>
<p>{% if question.asked %}{{ question.asked|date('Y-m-d') }}{% endif %}
<button type="button" class="btn btn-default btn-xs" qid="{{question.id}}" onclick="upvote(this)"><div class="n-lovers">{{ question.nLovers }}</div><span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span></button>
<button type="button" class="btn btn-default btn-xs" qid="{{question.id}}" onclick="downvote(this)"><div class="n-haters">{{ question.nhaters }}</div><span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span></button>
</p>
</div>
{% endfor %}
</div>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Nueva pregunta</label>
<input type="text" class="form-control" id="new_question">
</div>
<input type="button" class="btn btn-primary" value="Preguntar" onclick="submitQuestion()">
</form>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
setInterval(function() {
updateQuestions;
}, 3000);
var allQuestions = null;
function updateQuestions()
{
$.post("{{ path('question_get', { 'id': event.id }) }}",
{question: null},
function(response)
{
$.each(response, function(index, value){
if($('#q'+index).length == 0)
{
$('#questions').append('<div data-sort="'+value[2]+'" id="q'+index+'" class="question-div">'+
'<h5>'+value[3]+'</h5>'+
'<p>'+value[4]+
'<button type="button" class="btn btn-default btn-xs" qid="'+index+'" onclick="upvote(this)"><div class="n-lovers">'+value[0]+'</div><span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span></button>'+
'<button type="button" class="btn btn-default btn-xs" qid="'+index+'" onclick="downvote(this)"><div class="n-haters">'+value[1]+'</div><span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span></button>'+
'</p>'+
'</div>');
}
else
{
var existingQuestion = $('#q'+index);
existingQuestion.attr('data-sort',value[2]);
existingQuestion.find('.n-lovers').text(value[0]);
existingQuestion.find('.n-haters').text(value[1]);
}
});
sortQuestions();
},
"json");
}
function submitQuestion()
{
var question = $('#new_question').val();
$.post("{{ path('question_ask', { 'id': event.id }) }}",
{question: question},
function(response)
{
updateQuestions();
$('#new_question').val(null);
},
"json");
}
function upvote(question)
{
var questionId = $(question).attr("qid");
$.post("/question/love/"+questionId,
{question: questionId},
function(response)
{
updateQuestions();
},
"json");
}
function downvote(question)
{
var questionId = $(question).attr("qid");
$.post("/question/hate/"+questionId,
{question: questionId},
function(response)
{
updateQuestions();
},
"json");
}
function sortQuestions()
{
var $wrapper = $('#questions');
$wrapper.find('.question-div').sort(function (a, b) {
return +b.dataset.sort - +a.dataset.sort;
})
.appendTo( $wrapper );
}
</script>
{% endblock %}
...@@ -2,137 +2,137 @@ ...@@ -2,137 +2,137 @@
namespace AppBundle\Controller; namespace AppBundle\Controller;
use AppBundle\Entity\Meeting; use AppBundle\Entity\Event;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
* Meeting controller. * Event controller.
* *
* @Route("meeting") * @Route("event")
*/ */
class MeetingController extends Controller class EventController extends Controller
{ {
/** /**
* Lists all meeting entities. * Lists all event entities.
* *
* @Route("/", name="meeting_index") * @Route("/", name="event_index")
* @Method("GET") * @Method("GET")
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$meetings = $em->getRepository('AppBundle:Meeting')->findAll(); $events = $em->getRepository('AppBundle:Event')->findAll();
return $this->render('meeting/index.html.twig', array( return $this->render('event/index.html.twig', array(
'meetings' => $meetings, 'events' => $events,
)); ));
} }
/** /**
* Creates a new meeting entity. * Creates a new event entity.
* *
* @Route("/new", name="meeting_new") * @Route("/new", name="event_new")
* @Method({"GET", "POST"}) * @Method({"GET", "POST"})
*/ */
public function newAction(Request $request) public function newAction(Request $request)
{ {
$currentUser = $this->get('security.token_storage')->getToken()->getUser(); $currentUser = $this->get('security.token_storage')->getToken()->getUser();
$meeting = new Meeting(); $event = new Event();
$form = $this->createForm('AppBundle\Form\MeetingType', $meeting); $form = $this->createForm('AppBundle\Form\EventType', $event);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$meeting->setOwner($currentUser); $event->setOwner($currentUser);
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->persist($meeting); $em->persist($event);
$em->flush(); $em->flush();
return $this->redirectToRoute('meeting_show', array('id' => $meeting->getId())); return $this->redirectToRoute('event_show', array('id' => $event->getId()));
} }
return $this->render('meeting/new.html.twig', array( return $this->render('event/new.html.twig', array(
'meeting' => $meeting, 'event' => $event,
'form' => $form->createView(), 'form' => $form->createView(),
)); ));
} }
/** /**
* Finds and displays a meeting entity. * Finds and displays a event entity.
* *
* @Route("/{id}", name="meeting_show") * @Route("/{id}", name="event_show")
* @Method("GET") * @Method("GET")
*/ */
public function showAction(Meeting $meeting) public function showAction(Event $event)
{ {
$deleteForm = $this->createDeleteForm($meeting); $deleteForm = $this->createDeleteForm($event);
return $this->render('meeting/show.html.twig', array( return $this->render('event/show.html.twig', array(
'meeting' => $meeting, 'event' => $event,
'delete_form' => $deleteForm->createView(), 'delete_form' => $deleteForm->createView(),
)); ));
} }
/** /**
* Displays a form to edit an existing meeting entity. * Displays a form to edit an existing event entity.
* *
* @Route("/{id}/edit", name="meeting_edit") * @Route("/{id}/edit", name="event_edit")
* @Method({"GET", "POST"}) * @Method({"GET", "POST"})
*/ */
public function editAction(Request $request, Meeting $meeting) public function editAction(Request $request, Event $event)
{ {
$deleteForm = $this->createDeleteForm($meeting); $deleteForm = $this->createDeleteForm($event);
$editForm = $this->createForm('AppBundle\Form\MeetingType', $meeting); $editForm = $this->createForm('AppBundle\Form\EventType', $event);
$editForm->handleRequest($request); $editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) { if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush(); $this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('meeting_edit', array('id' => $meeting->getId())); return $this->redirectToRoute('event_edit', array('id' => $event->getId()));
} }
return $this->render('meeting/edit.html.twig', array( return $this->render('event/edit.html.twig', array(
'meeting' => $meeting, 'event' => $event,
'edit_form' => $editForm->createView(), 'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(), 'delete_form' => $deleteForm->createView(),
)); ));
} }
/** /**
* Deletes a meeting entity. * Deletes a event entity.
* *
* @Route("/{id}", name="meeting_delete") * @Route("/{id}", name="event_delete")
* @Method("DELETE") * @Method("DELETE")
*/ */
public function deleteAction(Request $request, Meeting $meeting) public function deleteAction(Request $request, Event $event)
{ {
$form = $this->createDeleteForm($meeting); $form = $this->createDeleteForm($event);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$em->remove($meeting); $em->remove($event);
$em->flush(); $em->flush();
} }
return $this->redirectToRoute('meeting_index'); return $this->redirectToRoute('event_index');
} }
/** /**
* Creates a form to delete a meeting entity. * Creates a form to delete a event entity.
* *
* @param Meeting $meeting The meeting entity * @param Event $event The event entity
* *
* @return \Symfony\Component\Form\Form The form * @return \Symfony\Component\Form\Form The form
*/ */
private function createDeleteForm(Meeting $meeting) private function createDeleteForm(Event $event)
{ {
return $this->createFormBuilder() return $this->createFormBuilder()
->setAction($this->generateUrl('meeting_delete', array('id' => $meeting->getId()))) ->setAction($this->generateUrl('event_delete', array('id' => $event->getId())))
->setMethod('DELETE') ->setMethod('DELETE')
->getForm() ->getForm()
; ;
......
...@@ -5,7 +5,10 @@ namespace AppBundle\Controller; ...@@ -5,7 +5,10 @@ namespace AppBundle\Controller;
use AppBundle\Entity\Question; use AppBundle\Entity\Question;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Event;
use Symfony\Component\HttpFoundation\Response;
/** /**
* Question controller. * Question controller.
...@@ -31,6 +34,152 @@ class QuestionController extends Controller ...@@ -31,6 +34,152 @@ class QuestionController extends Controller
)); ));
} }
/**
* event/questions view.
*
* @Route("/event/{id}/", name="question_event")
* @Method("GET")
*/
public function eventAction(Event $event)
{
$em = $this->getDoctrine()->getManager();
$questions = $em->getRepository('AppBundle:Question')->findByEvent($event);
return $this->render('question/event.html.twig', array(
'questions' => $questions,
'event' => $event,
));
}
/**
* Ajax, Creates a new question entity.
*
* @Route("/ask/{id}", name="question_ask")
* @Method({"POST"})
*/
public function askAction(Request $request, Event $event)
{
$question = new Question();
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$questionText = $request->get("question");
$question->setQuestion($questionText);
$question->setEvent($event);
$question->setAsker($currentUser);
$ip = $request->getClientIp();
$question->setAskerip($ip);
$em = $this->getDoctrine()->getManager();
$em->persist($question);
$em->flush();
return new Response(json_encode(true));
}
/**
* Ajax, upvote.
*
* @Route("/love/{id}", name="question_love")
* @Method({"POST"})
*/
public function loveAction(Request $request, Question $question)
{
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$isLover = false;
foreach ($question->getLovers() as $lover)
{
if($currentUser->getId() == $lover->getId())
{
$isLover = true;
}
}
if(!$isLover)
{
$isHater = false;
foreach ($question->getHaters() as $hater)
{
if($currentUser->getId() == $hater->getId())
{
$isHater = true;
}
}
if($isHater)
{
$question->removeHater($currentUser);
}
$question->addLover($currentUser);
$em = $this->getDoctrine()->getManager();
$em->persist($question);
$em->flush();
return new Response(json_encode(true));
}
return new Response(json_encode(false));
}
/**
* Ajax, downvote.
*
* @Route("/hate/{id}", name="question_hate")
* @Method({"POST"})
*/
public function hateAction(Request $request, Question $question)
{
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$isHater = false;
foreach ($question->getHaters() as $hater)
{
if($currentUser->getId() == $hater->getId())
{
$isHater = true;