コード例 #1
0
 /**
  *	@fn post_comment
  *	@short Action method to receive a comment for an article.
  */
 public function post_comment()
 {
     if (!$this->request->is_post()) {
         $this->redirect_to(array('action' => 'index'));
     }
     $post = new DiarioPost();
     if ($post->find_by_id($_POST['post_id']) === FALSE) {
         $this->flash(l('No such article'), 'error');
         $this->redirect_to(array('action' => 'index'));
     }
     if (!Email::is_valid($_POST['email'])) {
         $this->flash(l('Please enter a valid email address'), 'error');
         $this->redirect_to($post->permalink(FALSE));
     }
     if (!Antispam::check_math()) {
         $this->flash(Antispam::random_comment(), 'error');
         $this->redirect_to($post->permalink(FALSE));
     }
     // A static class method would be infinitely better...
     $comment = new DiarioComment($_POST);
     $comment->created_at = date("Y-m-d H:i:s");
     $comment->save();
     // Send an email to notify this comment
     $email = new DiarioCommentEmail(array('comment' => $comment, 'name' => $_POST['author'], 'email' => $_POST['email'], 'URL' => $_POST['URL']));
     $email->send();
     if (isset($_POST['remember_me'])) {
         $this->set_credentials($_POST['author'], $_POST['email'], $_POST['URL']);
     }
     // Expires the cache
     $this->expire_cached_page(array('action' => 'index'));
     $this->expire_cached_page(array('action' => 'read', 'id' => $_POST['post_id']));
     // Expires the cache of Comments feed
     $this->expire_cached_page(array('controller' => 'feed', 'action' => 'diario_comments', 'id' => $_POST['post_id']));
     $this->redirect_to(array('action' => 'read', 'id' => $_POST['post_id'], 'hash' => "comment-{$comment->id}"));
 }