<?php

/**
 *     个人中心ta人动态
 *
 *     2016-4-22 wzf $
 */
    if(!defined('IN_DISCUZ')) {
            exit('Access Denied');
    }
    require_once 'includes/gotye.php';
    require_once libfile('function/misc');
    require_once libfile('function/forum');
    require_once libfile('function/attachment');
    require_once libfile('function/forumlist');
    require_once libfile('function/spacecp');
    require_once 'includes/functions/attachment.php';
    loadcache(array('forums'));
    odz_loadforum();
    $ctl_obj = new GoTye_ctl();
    if(!$_GET['uid']||!$_GET['type']) {
        showmessage('openid_not_exists');
    }
//    if($_G['forum']['password']) {
//            if(!$_GET['pw']){
//                    //need password
//                    odz_error('need_password',$code=400);
//            }
//            if($_GET['pw'] != $_G['forum']['password']) {
//                    odz_error('passwd_incorrect',$code=300);
//            }
//    }
    $space = getuserbyuid($_GET['uid']);
    $page = empty($_GET['page'])?1:intval($_GET['page']);
    if($page<1) $page=1;
    $allowviewuserthread = $_G['setting']['allowviewuserthread'];
    $perpage = 20;
    $start = ($page-1)*$perpage;
    $list = array();
    $hiddennum = $count = $pricount = 0;
    $authorid = 0;
    $replies = $closed = $displayorder = null;
    $dglue = '=';
    $isfollow_friend=0;
    $fids = $comma = '';
    if($_GET['uid']) {
        $allowview = true;
        $viewtype=$_GET['type'];
        if($viewtype == 'thread' && $allowview) {
            $authorid = $_GET['uid'];
        }
    }
    //个人资料用户信息
    if(!$space) {
        showmessage('member_nonexistence');
    }
    space_merge($space, 'count');
    space_merge($space, 'field_home');
    space_merge($space, 'field_forum');
    space_merge($space, 'profile');
    space_merge($space, 'status');
    if($space['birthyear']){
        $age=$ctl_obj->age($space[birthyear].'-'.$space[birthmonth].'-'.$space[birthday]);
    }
    if($_G['uid']){
        $isfollow_friend1='SELECT fuid,fusername FROM '.DB::table('home_follow_friend')." WHERE uid = '$_G[uid]' and fuid=".$_GET['uid'];
        $query = DB::fetch_first($isfollow_friend1);
        if($query){$isfollow_friend=1;}
    }
    $user= DB::fetch_first('SELECT *  FROM '.DB::table('common_member')." as u LEFT JOIN "
                                .DB::table('common_usergroup')." as g ON u.groupid = g.groupid   WHERE uid = '$space[uid]'");

    $profile = array(
        'uid' => $space['uid'],
        'isfollow_friend' => $isfollow_friend,
        'gender' => $space['gender']?$space['gender']:0,
        'username' => (string)$space['username'],
        'sightml' => !empty($space['sightml'])?(strip_tags($space['sightml'])?strip_tags($space['sightml']):odz_lang('no_message_left')):odz_lang('no_message_left'),
        'affectivestatus'=>$space['affectivestatus']?$space['affectivestatus']:odz_lang('secrect'),
        'age' => (string)$age,
        'avatar' => avatar($space['uid'], 'middle', true),
        'grouptitle'=>strip_tags($user['grouptitle']),//头衔
    );
    unset($space);
    $gids = $fids = $forums = array();
    $result=C::t('forum_thread')->fetch_all_by_authorid_displayorder($authorid,'>-1');
//    $result=C::t('forum_thread')->fetch_all_by_authorid_displayorder($authorid,'>-1');
//    count_by_special
    $forum_threadcount=count($result);
    $pagecount = @ceil($forum_threadcount / $perpage);
    foreach(C::t('forum_thread')->fetch_all_by_authorid_displayorder($authorid, $displayorder, $dglue, $closed, '', $start, $perpage, $replies) as $tid => $value) {
            $list[$value['tid']] = procthread($value);
    }
    if(!empty($gids)) {
            $gforumnames = C::t('forum_forum')->fetch_all_name_by_fid(array_keys($gids));
            foreach($gids as $fid => $tid) {
                    $list[$tid]['forumname'] = $gforumnames[$fid]['name'];
            }
    }
    $threads = &$list;
    $threadlist = array();
    $home_black_list= DB::fetch_first('SELECT * FROM '.DB::table('mocuz_blackfriend')." as u "
                                . "WHERE u.uid = '$_G[uid]' and bid='$authorid' ORDER BY u.dateline DESC ");
    if(!empty($home_black_list)){$home_black=1;}else{$home_black=0;};
    foreach($threads as $thread) {
        foreach(C::t('mocuz_forum_post')->fetch_all_by_tid_user_check(0, $thread['tid'], 0, 1, 0) as $post) {
            if($post['first']){
                    $post['message'] = preg_replace('/\[attach\](.*)\[\/attach\]/s', '', $post['message']);
                    $post['message'] = preg_replace('/\[.*?\]/', '', $post['message']);
                    $thread['message'] = $post['message'];
                    $thread['pid'] = $post['pid'];
                    if(!empty($_G['uid'])) {//点赞
                            $query_praid = C::t('home_praise')->fetch_by_id_idtype($thread['tid'], 'tid', $_G['uid']);
                    }
                    $total = C::t('home_praise_total')->fetch_by_id_idtype($thread['tid'],'tid');
                    $thread['praid'] = isset($query_praid['praid'])?$query_praid['praid']:'';
                    $total['num'] = $total['num']?$total['num']:0;
                    if($total['num']>999){
                        $total['num']="999+";
                    }
            }
	}
        if($thread['pid']){
            $sortlist = array();
            $_G['tid']=$thread['tid'];
            $forum_thread_list=$thread;
            odz_parseattach($thread['pid'], '', $forum_thread_list, '');
            $i=0;
            foreach($forum_thread_list[$thread['pid']]['attachments'] as  $attach) {
                $attachurl = $attach['url'].$attach['attachment'].($attach['thumb'] ? '.thumb.jpg' : '');
                if ($attach['isimage'] != 0&&$i<3) {
                        $sortlist[$i]= mocuz_thumb($attach['aid'], intval($attach['width']), intval($attach['height']));
                }
                $i++;
            }
        }
        //我的黑名单
        $threadlist[] = array(
            'tid' => $thread['tid'],
            'pics' => $sortlist,
            'author' => $thread['author'],
            'avatar' => avatar($post['authorid'], 'small', true),
            'authorid' => $thread['authorid'],
            'idtype' =>'tid' ,
            'praid' => $thread['praid'],
            'pratimes' => $total['num'],
            'message' => odz_format_subject($thread['message']),
            'subject' => odz_format_subject($thread['subject']),
            'dateline' => $thread['dateline'],
            'showtime' => dgmdate($post['dateline'], 'u', '9999', getglobal('setting/dateformat').' H:i:s'),
            'views' => $thread['views'],
            'replies' => $thread['replies'],
            'forumname' => strip_tags($thread['forumname']),
            'hasimageattachment' => ($thread['attachment'] == 2 ? 1 : 0),
            'haslock' => $thread['closed']
        );
    }
    odz_result(array('threadlist'=>$threadlist,'profile'=>$profile,'pagecount'=>$pagecount,'page'=>$page,'home_black' => $home_black,));
?>
