Made forum posting possible.

It has some limitations, though, as
ForumTopic.last_post and ForumTopicGroup.last_post is not updated
automagically...
This commit is contained in:
Polonkai Gergely 2012-07-07 20:25:54 +02:00
parent adba578db8
commit 301db68281
11 changed files with 333 additions and 5 deletions

View File

@ -3,6 +3,10 @@
namespace KekRozsak\FrontBundle\Controller; namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use KekRozsak\FrontBundle\Entity\ForumPost;
use KekRozsak\FrontBundle\Form\Type\ForumPostType;
class ForumController extends Controller class ForumController extends Controller
{ {
@ -31,15 +35,56 @@ class ForumController extends Controller
public function postListAction($topicGroupSlug, $topicSlug) public function postListAction($topicGroupSlug, $topicSlug)
{ {
$request = $this->getRequest();
// Get the topic group based on slug
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup'); $groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup');
if (!($topicGroup = $groupRepo->findOneBySlug($topicGroupSlug))) if (!($topicGroup = $groupRepo->findOneBySlug($topicGroupSlug)))
throw $this->createNotFoundException('A kért témakör nem létezik!'); throw $this->createNotFoundException('A kért témakör nem létezik!');
// Get the topic based on slug
$topicRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopic'); $topicRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopic');
if (!($topic = $topicRepo->findOneBy(array('topic_group' => $topicGroup, 'slug' => $topicSlug)))) if (!($topic = $topicRepo->findOneBy(array('topic_group' => $topicGroup, 'slug' => $topicSlug))))
throw $this->createNotFoundException('A kért téma nem létezik!'); throw $this->createNotFoundException('A kért téma nem létezik!');
// Get the list of posts in the requested topic
$postRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumPost');
$posts = $postRepo->findBy(array('topic' => $topic), array('created_at' => 'DESC') /* TODO: , limit, offset */);
// Create an empty post object for posting
$post = new ForumPost();
$form = $this->createForm(new ForumPostType($topicGroup->getId(), $topic->getId()), $post);
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);
$topicGroup->setLastPost($post);
$topic->setLastPost($post);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($post);
// FIXME: Make this next 2 lines work!
$em->persist($topic);
$em->persist($topicGroup);
$em->flush();
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_forum_post_list', array(
'topicGroupSlug' => $topicGroupSlug,
'topicSlug' => $topicSlug,
)));
}
}
return $this->render('KekRozsakFrontBundle:Forum:post_list.html.twig', array( return $this->render('KekRozsakFrontBundle:Forum:post_list.html.twig', array(
'topicGroup' => $topicGroup, 'topicGroup' => $topicGroup,
'topic' => $topic, 'topic' => $topic,
'posts' => $posts,
'form' => $form->createView(),
)); ));
} }
} }

View File

@ -271,4 +271,37 @@ class ForumTopic
{ {
return $this->posts; return $this->posts;
} }
/**
* @var KekRozsak\FrontBundle\Entity\ForumPost
*/
private $lastPost;
/**
* Set lastPost
*
* @param KekRozsak\FrontBundle\Entity\ForumPost $lastPost
* @return ForumTopic
*/
public function setLastPost(\KekRozsak\FrontBundle\Entity\ForumPost $lastPost = null)
{
$this->lastPost = $lastPost;
return $this;
}
/**
* Get lastPost
*
* @return KekRozsak\FrontBundle\Entity\ForumPost
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* @var KekRozsak\FrontBundle\Entity\ForumPost
*/
private $last_post;
} }

View File

@ -259,4 +259,37 @@ class ForumTopicGroup
{ {
return $this->topics; return $this->topics;
} }
/**
* @var KekRozsak\FrontBundle\Entity\ForumPost
*/
private $lastPost;
/**
* Set lastPost
*
* @param KekRozsak\FrontBundle\Entity\ForumPost $lastPost
* @return ForumTopicGroup
*/
public function setLastPost(\KekRozsak\FrontBundle\Entity\ForumPost $lastPost = null)
{
$this->lastPost = $lastPost;
return $this;
}
/**
* Get lastPost
*
* @return KekRozsak\FrontBundle\Entity\ForumPost
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* @var KekRozsak\FrontBundle\Entity\ForumPost
*/
private $last_post;
} }

View File

@ -0,0 +1,80 @@
<?php
namespace KekRozsak\FrontBundle\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TwigBBExtension extends \Twig_Extension
{
private $assets;
public function __construct(ContainerInterface $container)
{
$this->assets = $container->get('templating.helper.assets');
}
public function getFilters()
{
return array(
'bbdecode' => new \Twig_Filter_Method($this, 'bbdecode', array(
'is_safe' => array('html'),
)),
);
}
public function bbdecode($sentence)
{
$sentence = htmlspecialchars($sentence, ENT_NOQUOTES);
$sentence = str_replace(array("\r\n", "\n", "\r"), "<br />", $sentence);
$sentence = preg_replace('/\\[u\\](.*?)\\[\\/u\\]/im', '<span class="u">\\1</span>', $sentence);
$sentence = preg_replace('/\\[b\\](.*?)\\[\\/b\\]/im', '<span class="b">\\1</span>', $sentence);
$sentence = preg_replace('/\\[i\\](.*?)\\[\\/i\\]/im', '<span class="i">\\1</span>', $sentence);
while (preg_match('/\\[img( (ns|name)="[^"]+"){1,}\\]/i', $sentence, $m, PREG_OFFSET_CAPTURE))
{
$start = $m[0][1];
$len = strlen($m[0][0]);
$full_tag = $m[0][0];
$ns = (preg_match('/ ns="([^"]+)"/', $full_tag, $ns)) ? trim($ns[1]) : '';
$name = (preg_match('/ name="([^"]+)"/', $full_tag, $name)) ? trim($name[1]) : '';
if ($name == '')
{
$sentence = substr_replace($sentence, 'Hibás kép', $start, $len);
}
else
{
// TODO: Thumbnailing!
$sentence = substr_replace($sentence, '<img src="' . $this->assets->getUrl('upload/images/' . (($ns == '') ? '' : $ns . '/') . $name) . '" alt="" />', $start, $len);
}
}
while (preg_match('/\\[link( (url)="[^"]+"){1,}\\](?P<content>.*?)\\[\\/link\\]/i', $sentence, $m, PREG_OFFSET_CAPTURE))
{
$start = $m[0][1];
$len = strlen($m[0][0]);
$full_tag = $m[0][0];
$url = (preg_match('/ url="([^"]+)"/', $full_tag, $url)) ? trim($url[1]) : '';
$content = '';
if (array_key_exists('content', $m))
{
$content = trim($m['content'][0]);
}
if (($url == '') || ($content == ''))
{
$sentence = substr_replace($sentence, 'Hibás link', $start, $len);
}
else
{
$sentence = substr_replace($sentence, '<a href="' . $url . '" target="_blank">' . $content . '</a>', $start, $len);
}
}
return $sentence;
}
public function getName()
{
return 'twig_bb';
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace KekRozsak\FrontBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ForumPostType extends AbstractType
{
private $topicGroup;
private $topic;
public function __construct($topicGroup = null, $topic = null)
{
$this->topicGroup = $topicGroup;
$this->topic = $topic;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('created_at', 'hidden', array(
'label' => 'Időpont',
'data' => new \DateTime('now')
));
$builder->add('text', null, array(
'label' => ' ',
));
$builder->add('topic', 'hidden', array(
'property_path' => false,
'data' => $this->topic,
));
}
public function getName()
{
return 'forum_post';
}
public function getDefaultOptions()
{
$opts = array(
'data_class' => 'KekRozsak\FrontBundle\Entity\ForumPost',
);
return $opts;
}
}

View File

@ -40,6 +40,12 @@ KekRozsak\FrontBundle\Entity\ForumTopic:
targetEntity: ForumTopicGroup targetEntity: ForumTopicGroup
inversedBy: topics inversedBy: topics
nullable: false nullable: false
oneToOne:
last_post:
targetEntity: ForumPost
nullable: true
default: null
cascade: [ persist ]
uniqueConstraint: uniqueConstraint:
uniqueSlugByGroup: uniqueSlugByGroup:
columns: [ topic_group, slug ] columns: [ topic_group, slug ]

View File

@ -32,6 +32,12 @@ KekRozsak\FrontBundle\Entity\ForumTopicGroup:
targetEntity: User targetEntity: User
nullable: true nullable: true
default: null default: null
oneToOne:
last_post:
targetEntity: ForumPost
nullable: true
default: null
cascade: [ persist ]
oneToMany: oneToMany:
topics: topics:
targetEntity: ForumTopic targetEntity: ForumTopic

View File

@ -22,3 +22,12 @@ KekRozsakFrontBundle_forum_post_list:
pattern: /forum/{topicGroupSlug}/{topicSlug} pattern: /forum/{topicGroupSlug}/{topicSlug}
defaults: defaults:
_controller: KekRozsakFrontBundle:Forum:postList _controller: KekRozsakFrontBundle:Forum:postList
requirements:
_method: GET
KekRozsakFrontBundle_forum_new_post:
pattern: /forum/{topicGroupSlug}/{topicSlug}/post
defaults:
_controller: KekRozsakFrontBundle:Forum:postList
requirements:
_method: POST

View File

@ -15,3 +15,9 @@ services:
class: KekRozsak\FrontBundle\Form\Extension\HelpMessageTypeExtension class: KekRozsak\FrontBundle\Form\Extension\HelpMessageTypeExtension
tags: tags:
- { name: "form.type_extension", alias: "field" } - { name: "form.type_extension", alias: "field" }
bb.twig.extension:
class: KekRozsak\FrontBundle\Extension\TwigBBExtension
arguments:
- @service_container
tags:
- { name: "twig.extension" }

View File

@ -5,14 +5,38 @@
{% if topic.posts|length > 0 %} {% if topic.posts|length > 0 %}
<table class="post-lista"> <table class="post-lista">
<tbody> <tbody>
{% for post in topic.posts %}
<tr> <tr>
<td class="szoveg"><div>{{ post.text|raw }}</div></td> <td class="uj-post">
<form method="post" action="{{ path('KekRozsakFrontBundle_forum_new_post', { topicGroupSlug: topicGroup.slug, topicSlug: topic.slug } ) }}">
{{ form_widget(form) }}
<p>
<span class="eszkoztar">Súgó</span><span class="kuldes-gomb" /><button type="submit">Küldés</button>
</p>
</form>
</td>
<td class="felado"> <td class="felado">
<br />
<br />
[avatar]<br />
{{ app.user.displayName }}<br />
Saját szint<br />
<br />
Tagság kezdete:<br />
{{ app.user.registeredAt|date('Y-m-d') }}
</td>
</tr>
{% for post in posts %}
<tr>
<td class="szoveg"><div>{{ post.text|bbdecode }}</div></td>
<td class="felado">
{{ post.createdAt|date('Y-m-d') }}<br />
{{ post.createdAt|date('H:i') }}<br />
[avatar]<br />
{{ post.createdBy.displayName }}<br /> {{ post.createdBy.displayName }}<br />
{{ post.createdAt|date('Y-m-d H:i') }}<br />
Szint<br /> Szint<br />
Tagság kezdete: {{ post.createdBy.RegisteredAt|date('Y-m-d') }} <br />
Tagság kezdete:<br />
{{ post.createdBy.RegisteredAt|date('Y-m-d') }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -140,7 +140,7 @@ h3 a {
} }
.forum-lista tbody tr.odd td { .forum-lista tbody tr.odd td {
background-color: #001144; background-color: #060c16;
} }
.forum-lista tbody tr.even td { .forum-lista tbody tr.even td {
@ -179,3 +179,42 @@ h3 a {
overflow: auto; overflow: auto;
} }
td.uj-post {
}
td.uj-post textarea {
width: 500px;
background-color: #000000;
color: #3366ff;
border-style: solid;
border-color: #3366ff;
border-width: 0 0 2px 0;
height: 15em;
}
td.uj-post p {
clear: both;
float: none;
margin: 0;
}
td.uj-post p .eszkoztar {
float: left;
}
td.uj-post p .kuldes-gomb {
float: right;
}
.u {
text-decoration: underline;
}
.b {
font-weight: bold;
}
.i {
font-style: italic;
}