Discutea/DForumBundle

Quote & likes

marvin-SL opened this issue · 3 comments

Quoting button does add the quotation tag but when the comment is sent, the quotation remains raw tag
[quote=john_user]foobar[/quote].
I have no error in web console, I 've installed the assets. Any tips ?

Like button does't seem to work either.

Thanks for you work

same problem here 👍

edit: it seems quoting and liking its currently not supported as from
#23

Hello,
Where did you get that ?
Finaly I've implemented my own forum system.

I got a solution (not very clean but work for me)...

You need JBBCode : http://jbbcode.com
And a BBCode Editor (SCEditor for me) : https://www.sceditor.com

So first you need to extend the DForumBundle into your forum bundle, next create a PostController.php file into your controller directory, copy the content of the PostController.php file of the bundle into your PostController.php file (don't forget to change the namespace for your namespace namespace Yourbundle\ForumBundle\Controller, and then add these 2 functions :

use Discutea\DForumBundle\Controller\Base\BasePostController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Discutea\DForumBundle\Entity\Post;
use Discutea\DForumBundle\Form\Type\PostType;
use Discutea\DForumBundle\Entity\Topic;
use Symfony\Component\HttpFoundation\Request;
use JBBCode;

protected function generatePostForm(Request $request, Topic $topic) {

        if  ( $this->isGranted('CanReplyTopic', $topic) ) {
            $this->post = new Post();
            $this->post->setTopic($topic);
            $user = $this->get('security.token_storage')->getToken()->getUser();
            $this->post->setPoster($user);

            if ( ($quote = $request->query->getInt('quote')) ) {
                $this->post->setContent( $this->addQuote($quote) );
            }

            return $this->createForm(PostType::class, $this->post, array(
                'preview' => $this->container->getParameter('discutea_forum.preview')
            ));
        }

        return NULL;
    }
   protected function addQuote($quote) {

        $post = $this->getEm()->getRepository('DForumBundle:Post')->find($quote);
        if ($post === NULL) {
            return NULL;
        }
        return '[quote='.$post->getPoster()->getUsername().']'.$post->getContent().'[/quote]';
    }

Delete the PostAction function and replace it by this function :

public function postAction(Request $request, Topic $topic)
    {
        $preview = false;

        $posts = $this->getPaginator()->pagignate('posts', $topic->getPosts());

        if (( $form = $this->generatePostForm($request, $topic) ) !== NULL) {
            $form->handleRequest($request);

            if (($form->isSubmitted()) && ($form->isValid()))
            {
                if ( !$preview = $this->getPreview($request, $form, $this->post) )
                {
                    $postData = $this->get('request')->request->all();
                    $contentData = $postData["post"]["content"];

                    if (strpos($contentData, "<script") != false) {
                        $request->getSession()->getFlashBag()->add('error', "Le code Javascript n'est pas autorisé.");
                        return $this->redirectAfterPost($posts);
                    }
                    else{

                        require_once "./JBBCode/Parser.php"; // I have put the JBBCode folder in the "web" folder

                        $parser = new JBBCode\Parser();

                        $builder = new JBBCode\CodeDefinitionBuilder('quote', '<div class="quote">{option} say {param}</div>'); // Add your own style !
                        $builder->setUseOption(true);
                        $parser->addCodeDefinition($builder->build());

                        $contentParsed = $parser->parse($contentData)->getAsHTML();

                        $em = $this->getEm();
                        $this->post->setContent($contentParsed);
                        $em->persist($this->post);
                        $em->flush();
                        $request->getSession()->getFlashBag()->add('success', $this->getTranslator()->trans('discutea.forum.post.create'));
                        return $this->redirectAfterPost($posts);
                    }
                }
            }
            $form = $this->autorizedPostForm($posts, $request, $form);
        }

        return $this->render('DForumBundle::post.html.twig', array(
            'topic' => $topic,
            'posts' => $posts,
            'form'  => $form,
            'postpreview' => $preview
        ));
    }

This function add some tweaks, like <script> tag protection, the BBCode Parser, and the Quote Parser.
Extend the single-post.html.twig file and add
{% autoescape false %}{{ post.content }}{% endautoescape %} between the content of the post for allowing HTML.

Obviously you need to do the same for the Topic controller but without the quotes. At the end don't forget to clear the cache.

Let me know if it's work for you !