From 301db68281e75df8785b253cba3b901ba917cab6 Mon Sep 17 00:00:00 2001 From: Polonkai Gergely Date: Sat, 7 Jul 2012 20:25:54 +0200 Subject: [PATCH] Made forum posting possible. It has some limitations, though, as ForumTopic.last_post and ForumTopicGroup.last_post is not updated automagically... --- .../Controller/ForumController.php | 45 +++++++++++ .../FrontBundle/Entity/ForumTopic.php | 33 ++++++++ .../FrontBundle/Entity/ForumTopicGroup.php | 33 ++++++++ .../FrontBundle/Extension/TwigBBExtension.php | 80 +++++++++++++++++++ .../FrontBundle/Form/Type/ForumPostType.php | 47 +++++++++++ .../config/doctrine/ForumTopic.orm.yml | 6 ++ .../config/doctrine/ForumTopicGroup.orm.yml | 6 ++ .../FrontBundle/Resources/config/routing.yml | 9 +++ .../FrontBundle/Resources/config/services.yml | 6 ++ .../Resources/views/Forum/post_list.html.twig | 32 +++++++- web/css/kekrozsak_front.css | 41 +++++++++- 11 files changed, 333 insertions(+), 5 deletions(-) create mode 100644 src/KekRozsak/FrontBundle/Extension/TwigBBExtension.php create mode 100644 src/KekRozsak/FrontBundle/Form/Type/ForumPostType.php diff --git a/src/KekRozsak/FrontBundle/Controller/ForumController.php b/src/KekRozsak/FrontBundle/Controller/ForumController.php index fdabe29..c9fc27c 100644 --- a/src/KekRozsak/FrontBundle/Controller/ForumController.php +++ b/src/KekRozsak/FrontBundle/Controller/ForumController.php @@ -3,6 +3,10 @@ namespace KekRozsak\FrontBundle\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 { @@ -31,15 +35,56 @@ class ForumController extends Controller public function postListAction($topicGroupSlug, $topicSlug) { + $request = $this->getRequest(); + + // Get the topic group based on slug $groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopicGroup'); if (!($topicGroup = $groupRepo->findOneBySlug($topicGroupSlug))) throw $this->createNotFoundException('A kért témakör nem létezik!'); + + // Get the topic based on slug $topicRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:ForumTopic'); if (!($topic = $topicRepo->findOneBy(array('topic_group' => $topicGroup, 'slug' => $topicSlug)))) 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( 'topicGroup' => $topicGroup, 'topic' => $topic, + 'posts' => $posts, + 'form' => $form->createView(), )); } } diff --git a/src/KekRozsak/FrontBundle/Entity/ForumTopic.php b/src/KekRozsak/FrontBundle/Entity/ForumTopic.php index 36a6dc7..fc28e76 100644 --- a/src/KekRozsak/FrontBundle/Entity/ForumTopic.php +++ b/src/KekRozsak/FrontBundle/Entity/ForumTopic.php @@ -271,4 +271,37 @@ class ForumTopic { 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; + + } \ No newline at end of file diff --git a/src/KekRozsak/FrontBundle/Entity/ForumTopicGroup.php b/src/KekRozsak/FrontBundle/Entity/ForumTopicGroup.php index 6310c1e..03da5f4 100644 --- a/src/KekRozsak/FrontBundle/Entity/ForumTopicGroup.php +++ b/src/KekRozsak/FrontBundle/Entity/ForumTopicGroup.php @@ -259,4 +259,37 @@ class ForumTopicGroup { 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; + + } \ No newline at end of file diff --git a/src/KekRozsak/FrontBundle/Extension/TwigBBExtension.php b/src/KekRozsak/FrontBundle/Extension/TwigBBExtension.php new file mode 100644 index 0000000..7371257 --- /dev/null +++ b/src/KekRozsak/FrontBundle/Extension/TwigBBExtension.php @@ -0,0 +1,80 @@ +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"), "
", $sentence); + $sentence = preg_replace('/\\[u\\](.*?)\\[\\/u\\]/im', '\\1', $sentence); + $sentence = preg_replace('/\\[b\\](.*?)\\[\\/b\\]/im', '\\1', $sentence); + $sentence = preg_replace('/\\[i\\](.*?)\\[\\/i\\]/im', '\\1', $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, '', $start, $len); + } + } + while (preg_match('/\\[link( (url)="[^"]+"){1,}\\](?P.*?)\\[\\/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, '' . $content . '', $start, $len); + } + } + return $sentence; + } + + public function getName() + { + return 'twig_bb'; + } +} + diff --git a/src/KekRozsak/FrontBundle/Form/Type/ForumPostType.php b/src/KekRozsak/FrontBundle/Form/Type/ForumPostType.php new file mode 100644 index 0000000..1aff774 --- /dev/null +++ b/src/KekRozsak/FrontBundle/Form/Type/ForumPostType.php @@ -0,0 +1,47 @@ +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; + } +} + diff --git a/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopic.orm.yml b/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopic.orm.yml index eb387e9..7a6df42 100644 --- a/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopic.orm.yml +++ b/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopic.orm.yml @@ -40,6 +40,12 @@ KekRozsak\FrontBundle\Entity\ForumTopic: targetEntity: ForumTopicGroup inversedBy: topics nullable: false + oneToOne: + last_post: + targetEntity: ForumPost + nullable: true + default: null + cascade: [ persist ] uniqueConstraint: uniqueSlugByGroup: columns: [ topic_group, slug ] diff --git a/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopicGroup.orm.yml b/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopicGroup.orm.yml index def4cc4..5e3aec8 100644 --- a/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopicGroup.orm.yml +++ b/src/KekRozsak/FrontBundle/Resources/config/doctrine/ForumTopicGroup.orm.yml @@ -32,6 +32,12 @@ KekRozsak\FrontBundle\Entity\ForumTopicGroup: targetEntity: User nullable: true default: null + oneToOne: + last_post: + targetEntity: ForumPost + nullable: true + default: null + cascade: [ persist ] oneToMany: topics: targetEntity: ForumTopic diff --git a/src/KekRozsak/FrontBundle/Resources/config/routing.yml b/src/KekRozsak/FrontBundle/Resources/config/routing.yml index bc24499..2e304b3 100644 --- a/src/KekRozsak/FrontBundle/Resources/config/routing.yml +++ b/src/KekRozsak/FrontBundle/Resources/config/routing.yml @@ -22,3 +22,12 @@ KekRozsakFrontBundle_forum_post_list: pattern: /forum/{topicGroupSlug}/{topicSlug} defaults: _controller: KekRozsakFrontBundle:Forum:postList + requirements: + _method: GET + +KekRozsakFrontBundle_forum_new_post: + pattern: /forum/{topicGroupSlug}/{topicSlug}/post + defaults: + _controller: KekRozsakFrontBundle:Forum:postList + requirements: + _method: POST diff --git a/src/KekRozsak/FrontBundle/Resources/config/services.yml b/src/KekRozsak/FrontBundle/Resources/config/services.yml index dda9023..c19fc24 100644 --- a/src/KekRozsak/FrontBundle/Resources/config/services.yml +++ b/src/KekRozsak/FrontBundle/Resources/config/services.yml @@ -15,3 +15,9 @@ services: class: KekRozsak\FrontBundle\Form\Extension\HelpMessageTypeExtension tags: - { name: "form.type_extension", alias: "field" } + bb.twig.extension: + class: KekRozsak\FrontBundle\Extension\TwigBBExtension + arguments: + - @service_container + tags: + - { name: "twig.extension" } diff --git a/src/KekRozsak/FrontBundle/Resources/views/Forum/post_list.html.twig b/src/KekRozsak/FrontBundle/Resources/views/Forum/post_list.html.twig index 757a7b5..9e33085 100644 --- a/src/KekRozsak/FrontBundle/Resources/views/Forum/post_list.html.twig +++ b/src/KekRozsak/FrontBundle/Resources/views/Forum/post_list.html.twig @@ -5,14 +5,38 @@ {% if topic.posts|length > 0 %} -{% for post in topic.posts %} - + + +{% for post in posts %} + + + {% endfor %} diff --git a/web/css/kekrozsak_front.css b/web/css/kekrozsak_front.css index 643b084..3580d49 100644 --- a/web/css/kekrozsak_front.css +++ b/web/css/kekrozsak_front.css @@ -140,7 +140,7 @@ h3 a { } .forum-lista tbody tr.odd td { - background-color: #001144; + background-color: #060c16; } .forum-lista tbody tr.even td { @@ -179,3 +179,42 @@ h3 a { 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; +} +
{{ post.text|raw }}
+
+ {{ form_widget(form) }} +

+ Súgó +

+
+
+
+
+ [avatar]
+ {{ app.user.displayName }}
+ Saját szint
+
+ Tagság kezdete:
+ {{ app.user.registeredAt|date('Y-m-d') }} +
{{ post.text|bbdecode }}
+ {{ post.createdAt|date('Y-m-d') }}
+ {{ post.createdAt|date('H:i') }}
+ [avatar]
{{ post.createdBy.displayName }}
- {{ post.createdAt|date('Y-m-d H:i') }}
Szint
- Tagság kezdete: {{ post.createdBy.RegisteredAt|date('Y-m-d') }} +
+ Tagság kezdete:
+ {{ post.createdBy.RegisteredAt|date('Y-m-d') }}