Skip to content

Commit

Permalink
Closed threads should not accept comments
Browse files Browse the repository at this point in the history
  • Loading branch information
merk committed Jan 31, 2012
1 parent 00d40cc commit 389c631
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
45 changes: 45 additions & 0 deletions EventListener/ClosedThreadListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the FOSCommentBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace FOS\CommentBundle\EventListener;

use FOS\CommentBundle\Events;
use FOS\CommentBundle\Event\CommentEvent;
use FOS\CommentBundle\Model\CommentInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Blames a comment using Symfony2 security component
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
*/
class ClosedThreadListener implements EventSubscriberInterface
{
/**
* Disallows comments in a closed thread.
*
* @param \FOS\CommentBundle\Event\CommentEvent $event
*/
public function onCommentPersist(CommentEvent $event)
{
$comment = $event->getComment();
$thread = $comment->getThread();

if (!$thread->isCommentable()) {
throw new \LogicException('Cannot add comment to a closed thread');
}
}

static public function getSubscribedEvents()
{
return array(Events::COMMENT_PRE_PERSIST => 'onCommentPersist');
}
}
2 changes: 1 addition & 1 deletion Model/CommentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function organiseComments($comments, SortingInterface $sorter, $ignore
* perform the saving of the comment to the backend.
*
* @param CommentInterface $comment
* @throws InvalidArumgnetException when the comment does not have a thread.
* @throws InvalidArgumentException when the comment does not have a thread.
*/
public function saveComment(CommentInterface $comment)
{
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@
<argument type="service" id="logger" />
</service>

<service id="fos_comment.listener.closed_threads" class="FOS\CommentBundle\EventListener\ClosedThreadListener">
<!-- TODO: 2.1 <tag name="kernel.event_subscriber" /> -->
<tag name="kernel.event_listener" event="fos_comment.comment.pre_persist" method="onCommentPersist" />
</service>
</services>
</container>
4 changes: 2 additions & 2 deletions Resources/views/Thread/comments.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{% set depth = depth|default(0) %}
{% set view = view|default('tree') %}

{% if depth == 0 and fos_comment_can_comment() %}
{% render "FOSCommentBundle:Thread:newThreadComments" with {"id": thread.id} %}
{% if depth == 0 and thread.commentable and fos_comment_can_comment() %}
{% render "FOSCommentBundle:Thread:newThreadComments" with {"id": thread.id} %}
{% endif %}

{% for commentinfo in comments %}
Expand Down

0 comments on commit 389c631

Please sign in to comment.