2012-07-03 12:11:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace KekRozsak\FrontBundle\Controller;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2012-07-15 12:56:31 +00:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2012-07-07 18:25:54 +00:00
|
|
|
|
|
|
|
use KekRozsak\FrontBundle\Entity\ForumPost;
|
|
|
|
use KekRozsak\FrontBundle\Form\Type\ForumPostType;
|
2012-07-03 12:11:18 +00:00
|
|
|
|
2012-07-13 10:07:21 +00:00
|
|
|
/**
|
|
|
|
* @Route("/forum")
|
|
|
|
*/
|
2012-07-03 12:11:18 +00:00
|
|
|
class ForumController extends Controller
|
|
|
|
{
|
2012-07-13 10:07:21 +00:00
|
|
|
/**
|
|
|
|
* @Route("", name="KekRozsakFrontBundle_forum_main")
|
2012-07-15 12:56:31 +00:00
|
|
|
* @Template("KekRozsakFrontBundle:Forum:topic_group_list.html.twig")
|
2012-07-13 10:07:21 +00:00
|
|
|
*/
|
2012-07-03 12:11:18 +00:00
|
|
|
public function mainAction()
|
|
|
|
{
|
|
|
|
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup');
|
|
|
|
|
2012-07-15 12:56:31 +00:00
|
|
|
// TODO: ORDER the topic list by last post date
|
2012-07-03 12:11:18 +00:00
|
|
|
$topicGroups = $groupRepo->findAll();
|
|
|
|
|
2012-07-15 12:56:31 +00:00
|
|
|
return array(
|
2012-07-03 12:11:18 +00:00
|
|
|
'topicGroups' => $topicGroups,
|
2012-07-15 12:56:31 +00:00
|
|
|
);
|
2012-07-03 12:11:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 10:07:21 +00:00
|
|
|
/**
|
|
|
|
* @Route("/{topicGroupSlug}", name="KekRozsakFrontBundle_forum_topic_list")
|
2012-07-15 12:56:31 +00:00
|
|
|
* @Template("KekRozsakFrontBundle:Forum:topic_list.html.twig")
|
2012-07-13 10:07:21 +00:00
|
|
|
*/
|
2012-07-03 12:11:18 +00:00
|
|
|
public function topicListAction($topicGroupSlug)
|
|
|
|
{
|
|
|
|
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup');
|
2012-07-15 12:56:31 +00:00
|
|
|
|
2012-07-03 12:11:18 +00:00
|
|
|
if (!($topicGroup = $groupRepo->findOneBySlug($topicGroupSlug)))
|
|
|
|
throw $this->createNotFoundException('A kért témakör nem létezik!');
|
|
|
|
|
2012-07-15 12:56:31 +00:00
|
|
|
return array(
|
2012-07-03 12:11:18 +00:00
|
|
|
'topicGroup' => $topicGroup,
|
2012-07-15 12:56:31 +00:00
|
|
|
);
|
2012-07-03 12:11:18 +00:00
|
|
|
}
|
|
|
|
|
2012-07-13 10:07:21 +00:00
|
|
|
/**
|
|
|
|
* @Route("/{topicGroupSlug}/{topicSlug}", name="KekRozsakFrontBundle_forum_post_list")
|
2012-07-15 12:56:31 +00:00
|
|
|
* @Template("KekRozsakFrontBundle:Forum:post_list.html.twig")
|
2012-07-13 10:07:21 +00:00
|
|
|
*/
|
2012-07-03 12:11:18 +00:00
|
|
|
public function postListAction($topicGroupSlug, $topicSlug)
|
|
|
|
{
|
2012-07-07 18:25:54 +00:00
|
|
|
// Get the topic group based on slug
|
2012-07-03 12:11:18 +00:00
|
|
|
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup');
|
|
|
|
if (!($topicGroup = $groupRepo->findOneBySlug($topicGroupSlug)))
|
|
|
|
throw $this->createNotFoundException('A kért témakör nem létezik!');
|
2012-07-07 18:25:54 +00:00
|
|
|
|
|
|
|
// Get the topic based on slug
|
2012-07-03 12:11:18 +00:00
|
|
|
$topicRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopic');
|
2012-07-13 10:07:21 +00:00
|
|
|
if (!($topic = $topicRepo->findOneBy(array('topicGroup' => $topicGroup, 'slug' => $topicSlug))))
|
2012-07-03 12:11:18 +00:00
|
|
|
throw $this->createNotFoundException('A kért téma nem létezik!');
|
2012-07-07 18:25:54 +00:00
|
|
|
|
|
|
|
// Get the list of posts in the requested topic
|
|
|
|
$postRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumPost');
|
2012-07-13 10:07:21 +00:00
|
|
|
$posts = $postRepo->findBy(array('topic' => $topic), array('createdAt' => 'DESC') /* TODO: , limit, offset */);
|
2012-07-07 18:25:54 +00:00
|
|
|
|
|
|
|
// Create an empty post object for posting
|
|
|
|
$post = new ForumPost();
|
|
|
|
$form = $this->createForm(new ForumPostType($topicGroup->getId(), $topic->getId()), $post);
|
|
|
|
|
2012-07-15 12:56:31 +00:00
|
|
|
$request = $this->getRequest();
|
2012-07-07 18:25:54 +00:00
|
|
|
if ($request->getMethod() == 'POST')
|
|
|
|
{
|
|
|
|
$form->bindRequest($request);
|
|
|
|
if ($form->isValid())
|
|
|
|
{
|
|
|
|
$post->setCreatedAt(new \DateTime('now'));
|
|
|
|
$post->setCreatedBy($this->get('security.context')->getToken()->getUser());
|
|
|
|
$post->setTopic($topic);
|
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getEntityManager();
|
|
|
|
$em->persist($post);
|
|
|
|
$em->persist($topic);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_forum_post_list', array(
|
|
|
|
'topicGroupSlug' => $topicGroupSlug,
|
|
|
|
'topicSlug' => $topicSlug,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-15 12:56:31 +00:00
|
|
|
return array(
|
2012-07-03 12:11:18 +00:00
|
|
|
'topicGroup' => $topicGroup,
|
|
|
|
'topic' => $topic,
|
2012-07-07 18:25:54 +00:00
|
|
|
'posts' => $posts,
|
|
|
|
'form' => $form->createView(),
|
2012-07-15 12:56:31 +00:00
|
|
|
);
|
2012-07-03 12:11:18 +00:00
|
|
|
}
|
|
|
|
}
|