Permalink
Browse files

Fixed post comments form generation

  • Loading branch information...
theUniC committed Dec 8, 2011
1 parent 84bfc78 commit c3a62f60346500b3abaf205f0eadafd7885ccebc
@@ -40,7 +40,7 @@ public function indexAction()
*/
public function postAction($year, $month, $day, $slug)
{
- $post = $this->getDoctrine()->getRepository('ChristianSoronellasBlogBundle:Post')->findBySlug($slug);
+ $post = $this->getDoctrine()->getRepository('ChristianSoronellasBlogBundle:Post')->findOneBySlug($slug);
$date = new \DateTime();
$date->setDate($year, $month, $day);
$date->setTime(0, 0, 0);
@@ -64,13 +64,13 @@ public function postAction($year, $month, $day, $slug)
* Adds a new comment to a given post
*
* @var \ChristianSoronellas\BlogBundle\Entity\Post $post
- * @Route("/post/{id}/comment", name="post_comment")
- * @ParamConverter("post", class="ChristianSoronellasBlogBundle:Post")
+ * @Route("/post/{slug}/comment", name="post_comment")
* @Template("ChristianSoronellasBlogBundle:Posts:post.html.twig")
* @Method("post")
*/
- public function commentAction(Post $post)
+ public function commentAction($slug)
{
+ $post = $this->getDoctrine()->getRepository('ChristianSoronellasBlogBundle:Post')->findOneBySlug($slug);
$form = $this->createForm(new CommentType())->bindRequest($this->getRequest());
if ($form->isValid()) {
// OK! Proceed to save the new comment to the database!
@@ -80,10 +80,7 @@ public function commentAction(Post $post)
$comment->setPost($post);
// Akismet filtering
- $data = $form->getNormData();
- var_dump($data);
- exit;
- if ($this->get('akismet')->isSpam(array('comment_author' => $data['email'], 'comment_content' => $data['body']))) {
+ if ($this->get('ornicar_akismet')->isSpam(array('comment_author' => $comment->getName(), 'comment_content' => $comment->getBody()))) {
$comment->setState(Comment::STATE_IS_SPAM);
}
@@ -94,7 +91,19 @@ public function commentAction(Post $post)
$em->persist($post);
$em->flush();
- return $this->redirect($this->generateUrl('post', array('id' => $post->getId())));
+ $this->get('session')->setFlash('notice', 'Your comment is awaiting moderation!');
+
+ return $this->redirect(
+ $this->generateUrl(
+ 'post',
+ array(
+ 'day' => $post->getCreatedAt()->format('d'),
+ 'month' => $post->getCreatedAt()->format('m'),
+ 'year' => $post->getCreatedAt()->format('Y'),
+ 'slug' => $post->getSlug()
+ )
+ )
+ );
}
return array('post' => $post, 'form' => $form->createView());
@@ -7,7 +7,7 @@
/**
* ChristianSoronellas\BlogBundle\Entity\Page
*
- * @ORM\Table()
+ * @ORM\Table(name="page")
* @ORM\Entity(repositoryClass="ChristianSoronellas\BlogBundle\Entity\PageRepository")
*/
class Page extends Content
@@ -40,13 +40,12 @@ class Post extends Content
private $tags;
/**
- * Get id
- *
- * @return integer
+ * Class constructor
*/
- public function getId()
+ public function __construct()
{
- return $this->id;
+ $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
+ $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
@@ -125,8 +124,23 @@ public function getTags()
return $this->tags;
}
+ /**
+ * Renders the instance as a string
+ *
+ * @return string
+ */
public function __toString()
{
- return $this->id . '';
+ return $this->getId() . '';
+ }
+
+ /**
+ * Get id
+ *
+ * @return integer
+ */
+ public function getId()
+ {
+ return $this->id;
}
}
@@ -12,15 +12,5 @@
*/
class PostRepository extends EntityRepository
{
- /**
- * Finds a post by its slug
- *
- * @param string $slug
- * @return ChristianSoronellas\BlogBundle\Entity\Post
- */
- public function findBySlug($slug)
- {
- $posts = $this->findBy(array('slug' => $slug));
- return array_shift($posts);
- }
+
}
@@ -22,5 +22,5 @@
</div>
</div>
{% if showComments and post.parentComments|length > 0 %}
-{% include 'ChristianSoronellasBlogBundle:Posts:_comments.html.twig' with { 'comments': post.parentComments, 'post_url': post_url, 'depth': 1, 'total_comments': post.comments|length } %}
+{% include 'ChristianSoronellasBlogBundle:Posts:_comments.html.twig' with { 'comments': post.parentComments, 'post_url': post_url, 'depth': 1, 'total_comments': post.approvedComments|length } %}
{% endif %}
@@ -8,7 +8,10 @@
{% include 'ChristianSoronellasBlogBundle:Posts:_post.html.twig' with { 'post': post, 'showComments': true } %}
<div class="post-comments comment-form" id="respond">
<h2><a name="comments">Leave your comment</a></h2>
- <form action="{{ path('post_comment', {'id': post.id}) }}" method="post" novalidate="novalidate" {{ form_enctype(form) }}>
+ {% if app.session.hasFlash('notice') %}
+ <div class="flash-notice">{{ app.session.flash('notice') }}</div>
+ {% endif %}
+ <form action="{{ path('post_comment', { 'slug': post.slug }) }}" method="post" novalidate="novalidate" {{ form_enctype(form) }}>
<table>
<tbody>
<tr>

0 comments on commit c3a62f6

Please sign in to comment.