FriendsOfSymfony / FOSCommentBundle

Threaded comments for Symfony
http://friendsofsymfony.github.com/
MIT License
460 stars 231 forks source link

Moderate the comments #496

Open anboo opened 9 years ago

anboo commented 9 years ago

How to make that were comments where field is_moderate is null not displayed? It's impossible? I found nothing like filters for query to database But this feature could be best of the best!

anboo commented 9 years ago

Okey, i think that it's really impossible. Because twig templates of bundle has follow:

{% set count = thread.numComments %} //count of comments

This means that the your business logic can not be implemented to it without rewrite templates.

I create service for getting comments by filter and it was like this:

$comments = $this->container->get('fos_comment.manager.comment')->findCommentTreeByThread($thread);

And it now like this:

$comments = $this->get('comment')->findCommentsByThread($thread, function(EntityComment $comment)
{
     return $comment->getIsPublished() == 1;
});

Method findCommentsByThread:

    public function findCommentsByThread(ThreadInterface $thread, \Closure $closure)
    {
        $resultArray = [];
        $manager = $this->container->get('fos_comment.manager.comment');
        $arrayComments = $manager->findCommentTreeByThread($thread);

        foreach($arrayComments as $comment)
        {
            $status = call_user_func_array($closure, [$comment['comment']]);
            if($status)
            {
                $resultArray[] = $comment;
            }
        }

        return $resultArray;
    }

You should not forget to add $isPublished field in the entity.

But if you want make moderate system you should be rewrite FOSCommentBundle:Thread:comments.html.twig in your app/Resources directory and change one string:

{% set count = comments|length %}

Because vendor template print thread.numComments result and thread.numComments maybe !== publishedArrayComments|length.