<?php
/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: ajax.php 1000 2011-6-31 15:41:43 by angelrain $
 */
 /**
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
*/

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$do = empty($_GET['do'])?'':$_GET['do'];

if($do == 'comment') {
	$ac = empty($_GET['ac'])?'':$_GET['ac'];
	$cid = empty($_GET['cid'])?0:intval($_GET['cid']);

	if($cid) {
		$cidsql = "c.cid='$cid' AND";
		$ajax_edit = 1;
	} else {
		$cidsql = '';
		$ajax_edit = 0;
	}

	$list = array();
	$query = DB::query("SELECT c.*,p.viewnum,p.appid,p.replynum FROM ".DB::table('dzz_comment')." c
								  LEFT JOIN ".DB::table('dzz_apps')." p ON c.cid=p.appid AND c.idtype='appid'
								  WHERE $cidsql authorid='$_G[uid]' ORDER BY c.dateline DESC LIMIT 0,1");
	while ($value = DB::fetch($query)) {
		if($_G['cache']['usergroups'][$value['groupid']]['userstatusby'] == 1) {
				$value['authortitle'] = $_G['cache']['usergroups'][$value['groupid']]['grouptitle'];
				$value['stars'] = $_G['cache']['usergroups'][$value['groupid']]['stars'];
			}
			$value['upgradecredit'] = false;
			if($_G['cache']['usergroups'][$value['groupid']]['type'] == 'member' && $_G['cache']['usergroups'][$value['groupid']]['creditslower'] != 999999999) {
				$value['upgradecredit'] = $_G['cache']['usergroups'][$value['groupid']]['creditslower'] - $value['credits'];
			}
			$value['message']=stripslashes($value['message']);
		$list[] = $value;
	}
}elseif($do == 'getcomments') {
	$page = empty($_GET['page'])?1:intval($_GET['page']);
	if($page<1) $page=1;
	$id=intval($_GET['id']);
	$idtype=trim($_GET['idtype']);
	$perpage = 6;
	$start = ($page-1)*$perpage;
	ckstart($start, $perpage);
	
	

		$csql.=" pcid=0 and (";
		$csql.=" (id='{$id}' and idtype='{$idtype}')";
		$csql.=" )";
		$count = DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_comment')." c WHERE $csql");
		$list = array();
		if($count) {
			//$query = DB::query("SELECT * FROM ".DB::table('dzz_comment')." WHERE $csql ORDER BY dateline DESC LIMIT $start,$perpage");
			$query = DB::query("SELECT * FROM ".DB::table('dzz_comment')." WHERE $csql ORDER BY dateline DESC LIMIT $start,$perpage");
			while ($value = DB::fetch($query)) {
				$value['reply_sum']=getcount('dzz_comment',"pcid='{$value[cid]}'");
				if($_G['cache']['usergroups'][$value['groupid']]['userstatusby'] == 1) {
					$value['authortitle'] = $_G['cache']['usergroups'][$value['groupid']]['grouptitle'];
					$value['stars'] = $_G['cache']['usergroups'][$value['groupid']]['stars'];
				}
				$value['upgradecredit'] = false;
				if($_G['cache']['usergroups'][$value['groupid']]['type'] == 'member' && $_G['cache']['usergroups'][$value['groupid']]['creditslower'] != 999999999) {
					$value['upgradecredit'] = $_G['cache']['usergroups'][$value['groupid']]['creditslower'] - $value['credits'];
				}
				$value['message']=stripslashes($value['message']);
				$list[] = $value;
			}
			//updateReplyNum($id,$idtype,$count);
			//DB::update('dzz_apps', array('replynum'=>$count), array('appid'=>$id));
		}
		$multi = multi($count, $perpage, $page, DZZSCRIPT."?mod=comment&op=ajax&do=getcomments&id=$id&idtype=$idtype");

}elseif($do == 'getreply') {
	
	$pcid = empty($_GET['pcid'])?0:intval($_GET['pcid']);
	$limit=empty($_GET['limit'])?0:intval($_GET['limit']);
	if($limit) $limitsql=" LIMIT $limit";
	else $limitsql='';
	if($pcid) {
		$cidsql = "c.pcid='$pcid'";
		$ajax_edit = 1;
	} else {
		$cidsql = '';
		$ajax_edit = 0;
	}

	$list = array();
	$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_comment')." c WHERE $cidsql ");
	if($count){
		$query = DB::query("SELECT c.* FROM ".DB::table('dzz_comment')." c 
								 WHERE $cidsql  ORDER BY c.dateline DESC $limitsql");
		while ($value = DB::fetch($query)) {
			if($_G['cache']['usergroups'][$value['groupid']]['userstatusby'] == 1) {
				$value['authortitle'] = $_G['cache']['usergroups'][$value['groupid']]['grouptitle'];
				$value['stars'] = $_G['cache']['usergroups'][$value['groupid']]['stars'];
			}
			$value['upgradecredit'] = false;
			if($_G['cache']['usergroups'][$value['groupid']]['type'] == 'member' && $_G['cache']['usergroups'][$value['groupid']]['creditslower'] != 999999999) {
				$value['upgradecredit'] = $_G['cache']['usergroups'][$value['groupid']]['creditslower'] - $value['credits'];
			}
			$value['message']=stripslashes($value['message']);
			$list[] = $value;
		}
	}
}elseif($do=='commentform'){
	$id=intval($_GET['id']);
	$idtype=trim($_GET['idtype']);

}
include dzztemplate('comment/ajax');

?>