<?php
if($_G['pa_count'] < 2) {
    odz_error('parameters_error', -112);
}
odz_loadforum();
require_once libfile('function/home');
if($isfirstpost && (($special == 1 && !$_G['group']['allowpostpoll']) || ($special == 2 && !$_G['group']['allowposttrade']) || ($special == 3 && !$_G['group']['allowpostreward']) || ($special == 4 && !$_G['group']['allowpostactivity']) || ($special == 5 && !$_G['group']['allowpostdebate']))) {
	$message=odz_lang('group_nopermission');
        if($server==1){
            showmessage('group_nopermission');
        }else{
            echo json_encode(array('status'=>3,'message'=>$message));exit;
        }
}
if(!$_G['uid'] && ($_G['setting']['need_avatar'] || $_G['setting']['need_email'] || $_G['setting']['need_friendnum'])) {
        $message=odz_lang('postperm_login_nopermission');
        if($server==1){
            showmessage('postperm_login_nopermission');
        }else{
            echo json_encode(array('status'=>3,'message'=>$message));exit;
        }
}

$idtype  = '';
$idtype = empty($_GET['idtype']) ? '' : $_GET['idtype'];
$id = empty($_GET['id']) ? 0 : intval($_GET['id']);//贴子id
$server= empty($_GET['server']) ? 0 : intval($_GET['server']);//数据段
$uid= empty($_GET['uid']) ? 0 : intval($_GET['uid']);//数据段
if($uid){ $_G['uid']= $uid;}
if(!$_G['uid']) {
    if($server==1){
        showmessage('user_not_login');
    }else{
        $message=odz_lang('user_not_login');
        echo json_encode(array('status'=>3,'message'=>$message));exit;
    }
}
checklowerlimit('post', 0, 1, $_G['forum']['fid']);
if($_GET['type'] == 'delete') {
        $thevalue = C::t('home_praise')->fetch_by_id_idtype($id,$idtype,$_G['uid']);
        if(empty($thevalue) || $thevalue['uid'] != $_G['uid']) {
            $message=odz_lang('praise_does_not_exist');
            if($server==1){
                showmessage('praise_does_not_exist');
            }else{
                echo json_encode(array('status'=>3,'message'=>$message));exit;
            }
        }
        if(C::t('home_praise')->delete_by_id_idtype($id,$idtype)){
            DB::query("UPDATE ".DB::table('home_praise_total')." SET num=(num - 1)  WHERE tid=%d", array($id));
        }
        $praisenum=DB::result_first('SELECT num FROM '.DB::table('home_praise_total')." WHERE idtype = '$idtype' and tid= '$id'");
        if($server==1){
            odz_success('praise_delete_succeed');
        }else{
            echo json_encode(array('status'=>2,'praisenum'=>$praisenum));exit;
        }
} else {
	cknewuser();
        if(!$idtype||!$id) {
            if($server==1){
                showmessage('praise_cannot_praise');
            }else{
                $message=odz_lang('praise_cannot_praise');
                echo json_encode(array('status'=>3,'message'=>$message));exit;
            }
        }
	$fav = C::t('home_praise')->fetch_by_id_idtype($id, $idtype, $_G['uid']);
	if($fav) {
            if($server==1){
                showmessage('praise_repeat');
            }else{
                $message=odz_lang('praise_repeat');
                echo json_encode(array('status'=>3,'message'=>$message));exit;
            }
	}
	$description = '';
	$description_show = nl2br($description);
	$fav_count = C::t('home_praise')->count_by_id_idtype($id, $idtype);
        $arr = array(
                'uid' => intval($_G['uid']),
                'idtype' => $idtype,
                'id' => $id,
                'spaceuid' => $spaceuid,
                'title' => getstr($title, 255),
                'dateline' => TIMESTAMP
        );
        $praid = C::t('home_praise')->insert($arr, true);
        if($praid){
            $praise_total_id=DB::query("UPDATE ".DB::table('home_praise_total')." SET num=(num + 1)  WHERE tid=%d", array($id));
            if(!$praise_total_id){
                $arr1 = array(
                    'idtype' => $idtype,
                    'tid' => $id,
                    'num' => 1,
                );
                C::t('home_praise_total')->insert($arr1, true);
            }
        }
        //回复推送
        require 'includes/functions/function_push.php';
        push_reply($praid);
        if($_G['setting']['cloud_status']) {
            $praiseService = Cloud::loadClass('Service_Client_Favorite');
            $praiseService->add($arr['uid'], $praid, $arr['id'], $arr['idtype'], $arr['title'], $arr['description'], TIMESTAMP);
        }
        C::t('forum_thread')->increase($id, array('pratimes'=>1));
        require_once libfile('function/forum');
        update_threadpartake($id);
        $praisenum=DB::result_first('SELECT num FROM '.DB::table('home_praise_total')." WHERE idtype = '$idtype' and tid= '$id'");
        if($server==1){
             $result = array('praid'=>$praid);  
             odz_result($result, 'praise_do_success');
        }else{
             echo json_encode(array('status'=>1,'praisenum'=>$praisenum));exit;
        }
}
?>