<?php
class articleAction extends baseAction {
    public function view(){
    	$data = D('content_content');
        $id = I('id');
        $info = $data->join('left join '.C('DB_PREFIX').'content_yesno ON '.C('DB_PREFIX').'content_content.id = '.C('DB_PREFIX').'content_yesno.cid left join '.C('DB_PREFIX').'users on '.C('DB_PREFIX').'content_content.authorid='.C('DB_PREFIX').'users.id')->where("".C('DB_PREFIX')."content_content.id='$id'")->field(''.C('DB_PREFIX').'content_content.*,'.C('DB_PREFIX').'content_yesno.value,'.C('DB_PREFIX').'users.authpic')->find();
        $info['yi'] = unserialize($info['value']);
        // echo $data->getLastSql();
        $this->assign('info',$info);
        $this->assign('user',$this->user_info());

        //获取下一条
        $next = $data->where("id > ".$id."")->find();
        $this->assign('next',$next);
        //调取列表页笑话大全
        $dq_list = M('content_content')->where("status=1 and thumb = ''")->order('rand()')->limit(20)->select();
        //调取爆笑精选，带图片
        $bx_list = M('content_content')->where("status=1 and thumb != ''")->order('rand()')->limit(10)->select();
        $this->assign('dq_list',$dq_list);
        $this->assign('bx_list',$bx_list);
        //赞过的人
        $result = M('content_yesno_data')->join("
            fm_users on ".C('DB_PREFIX')."content_yesno_data.uid=".C('DB_PREFIX')."users.id"
            )->where("
            ".C('DB_PREFIX')."content_yesno_data.cid='$id'
            ")->limit(15)->order("
            ".C('DB_PREFIX')."content_yesno_data.id asc
            ")->field("
            ".C('DB_PREFIX')."users.id,".C('DB_PREFIX')."users.authpic,".C('DB_PREFIX')."users.username
            ")->select();
            // print_r($result);
        $this->assign('yes_user',$result);
        //调取评论
        $result = M('content_comment')->join("
            fm_users on ".C('DB_PREFIX')."content_comment.uid=".C('DB_PREFIX')."users.id"
            )->where("
            ".C('DB_PREFIX')."content_comment.cid='$id'
            ")->limit(200)->order("
            ".C('DB_PREFIX')."content_comment.id asc
            ")->field("
            ".C('DB_PREFIX')."users.id,".C('DB_PREFIX')."users.authpic,".C('DB_PREFIX')."users.username,".C('DB_PREFIX')."content_comment.*
            ")->select();
        $this->assign('comment_list',$result);
        //可能喜欢的图片笑话
        $like_list = M('content_content')->where("status=1 and thumb != ''")->order('rand()')->limit(12)->select();
        $this->assign('like_list',$like_list);
        $this->display(C('default_tpl').'/Index/view');
    }
    public function post_comment(){
        if(IS_POST){
            $user = $this->user_info();
            if($user['id'] > 0) {
                if(I('content') != ''){
                $insert['content'] = I('content');
                $insert['cid'] = I('cid');
                $insert['uid'] = $user['id'];
                $insert['addtime'] = time();
                if($cmid = M('content_comment')->add($insert)){
                    // echo '评论成功';
                    M('content_content')->query("update ". C('DB_PREFIX') ."content_content set comment=comment+1 where id='".I('cid')."'");
                    $html = '<div id="comment-'.$cmid.'" class="comment-block clearfix floor-1">';
                    $html .= '<div class="avatars">';
                    if($user['authpic'] == '') {
                    $html .= '<a href="'. gurl(''.$user['id'].'','user','index') .'" target="_blank" rel="nofollow"><img src="'. __ROOT__ .'/Public/Home/images/anony.png" alt="'. $user['username'] .'"></a>';
                    }else{
                    $html .= '<a href="'. gurl(''.$user['id'].'','user','index') .'" target="_blank" rel="nofollow"><img src="'. __ROOT__ .'/'. $user['authpic'] .'" alt="'. $user['username'] .'"></a>';
                    }
                    $html .= '</div>';
                    $html .= '<div class="replay">';
                    $html .= '<a href="'. gurl(''.$user['id'].'','user','index') .'" class="userlogin" target="_blank" title="'. $user['username'] .'">'. $user['username'] .'</a>';
                    $html .= '<span class="body">'. I('content') .'</span>';
                    $html .= '</div>';
                    $html .= '<div class="report"></div>';
                    $html .= '</div>';
                    $this->ajaxReturn("",$html,"1");
                }else{
                    // echo '评论失败';
                    $this->ajaxReturn("","评论失败","-1");
                }
                }else{
                    $this->ajaxReturn("","内容不能为空","-3");
                }
            }else{
                // echo '未登录状态';
                $this->ajaxReturn("","您还没登录哦","-2");
            }
        }
    }
}