<?php

/*************************************
 * 我的足迹接口
 * author:ZuoLei
 * date:2016-4-26 9:27:04
 *************************************
*/
if(!defined('IN_DISCUZ')){
    exit('Access Denied');
}
if(!$_G['uid']){
    echo odz_error('not_loggedin');exit;
}
require_once libfile('function/post');
require_once 'includes/class/thread_service.php';
$page = $_GET['page'] ? intval($_GET['page']) : 1;
$perpage = 20;
$start = ($page-1)*$perpage;
//ckstart($start, $perpage);
$ordersql = 't.dateline DESC';
$info = $threadlist = array();
$listcount = '';

$space = getuserbyuid($_G['uid']);
//$regdays = ceil((strtotime(date("Y-m-d")) - strtotime(date("Y-m-d,",$space['regdate'])))/86400);
$regdays = (strtotime(date("Y-m-d")) - strtotime(date("Y-m-d,",$space['regdate'])))/86400 +1 ;
//我的关注
$friends = DB::fetch_all("select * from ".DB::table("home_follow_friend")." where uid = ".$_G['uid']);
$my_follow = count($friends);
//被关注
$my_fans = DB::fetch_all("select * from ".DB::table("home_follow_friend")." where fuid = ".$_G['uid']);
$my_fans = count($my_fans);
//回帖数
$space_posts = getuserprofile('posts') - getuserprofile('threads');
//主题数
$space_threads = getuserprofile('threads');
$login_days = DB::fetch_first("select * from ".DB::table("mocuz_logindays")." where uid = ".$_G['uid']);
$info = array(
    'regdays' => $regdays,
    'my_follow' => $my_follow,
    'my_fans' => $my_fans,
    'space_posts' => $space_posts,
    'space_threads' => $space_threads,
    'login_days' => $login_days['login_days'],
);


//我的帖子
if($_GET['type'] == "thread"){
    loadcache('forums');
    $listcount = DB::result_first("SELECT count(*) as num FROM ".DB::table('forum_thread')."  WHERE authorid =".$_G['uid']." ORDER BY dateline DESC ");
    $posts = DB::fetch_all("SELECT * FROM ".DB::table('forum_thread')."  WHERE authorid =".$_G['uid']." ORDER BY dateline DESC ".DB::limit($start, $perpage));
    $thread_service = new Thread_Service();
    foreach($posts as $thread) {
        $thread_service->input_thread($thread);
    }
    $threadlist = $thread_service->output_threads('full');
//我的回帖
}elseif($_GET['type'] == "reply"){
    $listcount = DB::result_first('SELECT count(*) as num FROM '.DB::table('forum_post'). " WHERE authorid = ".$_G[uid]." AND first =0 ORDER BY dateline DESC ");
    $reply_list = DB::fetch_all('SELECT fp.tid,fp.message,fp.authorid,fp.dateline,fr.subject,fp.pid,fp.position FROM '.DB::table('forum_post')." as fp "
                                . "LEFT JOIN ".DB::table('forum_thread')." as fr ON fp.tid = fr.tid   "
                                . "WHERE fp.authorid = ".$_G['uid']." AND fp.first =0 AND fr.displayorder != -1 ORDER BY fp.dateline DESC ".DB::limit($start, $perpage));
    $userinfo = getuserbyuid($_G['uid']);
    $url = '';
    foreach($reply_list as $thread) {
        $threads = DB::fetch_first("SELECT * FROM ".DB::table('forum_post')." WHERE `tid` =".$thread['tid']." AND `first` =1");

        if(!empty($threads) && $threads['tid'] == $thread['tid']){
            $attach = current(C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$thread['tid'], 'pid', $threads['pid'], '', array(1,-1)));
            if($attach){
                $url = mocuz_thumb($attach['aid'], 120, 120);
            }else{
                $url = '';
            }
        }
        $post_message = preg_replace('/\[quote\](.*?)\[\/quote\]/s', ' ',   $thread['message']);
        if(preg_match('/\[quote\](.*?)\[\/quote\]/s', $thread['message'], $match)) {
            $quotemsg = preg_replace('/\[img\].*?\[\/img\]/', '', $match[1]);
            $quotemsg = preg_replace('/\[.*?\]/', '', $quotemsg);
            $quotemsg = odz_lang('post_quote') . ':' . $quotemsg;
            $thread['message'] = str_replace($match[0], $quotemsg, $thread['message']);
            $thread['message'] = str_replace(odz_lang('post_requte').':', ' ', $thread['message']);
            preg_match('/([\S\s]+) '.odz_lang('post_requte_match').' \d+\-\d+-\d+/', $thread['message'], $matchs);
            $post['requtename']=$matchs[1];
            $post_message = str_replace("\n", '', $post_message);
            $thread['message']=$post_message;
        }
        $thread['message'] = preg_replace('/\[attach\](.*)\[\/attach\]/s', odz_lang('image'), $thread['message']);
        $thread['message'] = str_replace("\r\n", '', $thread['message']);
        $threadlist[] = array(
            'tid' => $thread['tid'],
            'pid' => $thread['pid'],
            'pidpage' => ceil($thread['position']/$perpage),
            'author' => $userinfo['username'],
            'avatar' => avatar($_G['uid'], 'middle',true),
            'title' => odz_format_subject($thread['subject']),
            'content' => replace_smile($thread['message']) ? replace_smile($thread['message']) : "...",
            'thumb' => $url,
            'showtime' => gmdate('Y-n-j', $thread['dateline'])
        );
    }
}elseif($_GET['type'] == 'favorite'){
    $listcount = C::t('home_favorite')->count_by_uid_idtype($_G['uid'], 'tid', 0);
    if($listcount){
        $favorite = DB::fetch_all('SELECT * FROM '.DB::table('home_favorite')." as hf "
                                . "INNER JOIN ".DB::table('forum_thread')." as fr ON hf.id = fr.tid   "
                                . " WHERE hf.uid = ".$_G['uid']." AND hf.idtype = 'tid'  ORDER BY hf.dateline DESC ".DB::limit($start, $perpage));
        $thread_service = new Thread_Service;
        foreach($favorite as $thread){
            $thread_service->input_thread($thread);
        }
        $threadlist = $thread_service->output_threads('full');

    }
}else{
    odz_error('parameters_error');
}
odz_result(array("info"=>$info,"threadlist" => $threadlist,'total'=>ceil($listcount/$perpage)));
