Exemplo n.º 1
0
 public function onComment()
 {
     // add comment reply depth to template
     $this->depth = $this->page['depth'] = $this->property('depth');
     $this->page['level'] = post('level');
     // get current user
     $user = self::getUser();
     if ($user) {
         $user_id = $user->id;
     } else {
         $user_id = 0;
         // guest
     }
     // get id of edited comment
     $id = post('id');
     if (!$id) {
         // create new comment
         // get slug
         $cid = $this->property('slug');
         $params['cid'] = $cid;
         // get parent id
         $params['pid'] = post('pid');
         // get content
         $params['content'] = post('content');
         $params['user_id'] = $user_id;
         // get ip address
         $params['hostname'] = self::getHostname();
         // parse content to html
         $params['content_html'] = Markdown::parse(strip_tags(trim($params['content'])));
         $params['published_at'] = date('Y-m-d H:i:s');
         // get name, mail, homepage if user is anonymous
         $params['name'] = post('name');
         $params['mail'] = post('mail');
         $params['homepage'] = post('homepage');
         // create comment
         $comment = Comment::createComment($params);
         // add user name and avatar to comment
         if ($user_id != 0) {
             $comment->user_avatar = $user->getAvatarThumb(64);
             $comment->user_name = $user->name;
         }
         // if success return comment
         if ($comment) {
             $this->comment = $this->page['comment'] = $comment;
         } else {
             $this->page['message'] = e(trans('xeor.comments::lang.messages.error'));
         }
     } else {
         // update comment
         // get content
         $content = post('content');
         $params = array('content' => post('content'), 'content_html' => Markdown::parse(trim($content)));
         // update content
         $comment = Comment::updateComment($id, $params, $user);
         // if success return comment
         if ($comment) {
             $this->comment = $this->page['comment'] = $comment;
         } else {
             $this->page['message'] = e(trans('xeor.comments::lang.messages.edit_error'));
         }
     }
 }