Ejemplo n.º 1
0
 /**
  * add a comment from POST data to current blog post
  */
 public function addComment(\Base $f3, $params)
 {
     if (isset($params['slug'])) {
         // you may only comment published posts
         $this->resource->load(array('slug = ? and publish_date <= ? and published = ?', $params['slug'], date('Y-m-d'), true));
         if ($this->resource->dry()) {
             // invalid post ID
             $f3->error(404, 'Post not found.');
             return false;
         }
         if (!$this->resource->enable_comments && !$this->resource->enable_comments === NULL) {
             $f3->error(403, 'Comments are not allowed for this Post');
             return false;
         }
         $comment = new \Model\Comment();
         $comment->copyfrom('POST', 'author_name, author_email, message');
         $comment->post = $this->resource->_id;
         $comment->approved = \Config::instance()->get('auto_approve_comments') ? 1 : 0;
         $comment->save();
         if ($f3->get('ERROR')) {
             // if posting failed, return to comment form
             $this->getSingle($f3, $params);
         } else {
             // if posting was successful, reroute to the post view
             if (\Config::instance()->get('auto_approve_comments')) {
                 \Flash::instance()->addMessage('Your comment has been added.', 'success');
             } else {
                 \Flash::instance()->addMessage('Your comment has been added, but must be approved first before it becomes public.', 'success');
             }
             $f3->reroute('/' . $params['slug']);
         }
     } else {
         // invalid URL, no post id given
         \Flash::instance()->addMessage('No Post ID specified.', 'danger');
         $f3->reroute('/');
     }
 }