コード例 #1
0
 /**
  *	@fn post_comment
  *	@short Action method that receives a comment for a software product.
  */
 public function post_comment()
 {
     $conn = Db::get_connection();
     if (!$this->request->is_post()) {
         $this->redirect_to(array('action' => 'index'));
     }
     $software = new Software();
     if ($software->find_by_id($_POST['software_id']) === FALSE) {
         $this->flash(l('No such software'), '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($software->comments_permalink());
     }
     if (!Antispam::check_math()) {
         $this->flash(Antispam::random_comment(), 'error');
         $this->redirect_to($software->comments_permalink());
     }
     // A static class method would be infinitely better...
     $comment = new SoftwareComment($_POST);
     $comment->created_at = date('Y-m-d H:i:s');
     $comment->save();
     // Send an email to notify this comment
     $email = new SoftwareCommentEmail(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 of Comments feed
     $this->expire_cached_page(array('controller' => 'feed', 'action' => 'software_comments', 'id' => $_POST['software_id']));
     $this->redirect_to_software_page(array('id' => $_POST['software_id'], 'subview' => 'comments', 'hash' => 'comment-' . $comment->id));
     Db::close_connection($conn);
 }