<?php
define('IN_OUN', true);
include_once( "./includes/command.php");
include_once( ROOT_PATH."includes/item_set.php"); 
/* 产品页显示的模块 */   
$Aconf['header_title'] = $Aweb_url['vote'][0]."|".$Aconf["web_title"]; 
$Aconf['description']  = '';
$Aconf['keywords']     = '';
 
/* 调查列表 查询条件 */
$vtid = $vtid < 1?$id:$vtid; 
if(!$vtid){
   header("Location: votes.php");
   exit;
}
$show_vote = false; $strMessage = ''; 
/* 投票显示结果 */
if($_REQUEST['op'] == 'poll') {
	if($_POST['op'] && $_POST['vtid']) //投票
	{
		/* 判断是否已经投过票 24 =86400 小时限制 */
		$times = 11600;
		$add_ip = true;
        $ip = real_ip();        
        $db_table = $pre."vote_ip";
	    $sql = "SELECT add_time FROM ".$pre."vote_ip 
		       where vtid = ".$_POST['vtid']." 
			   AND ip = '$ip' 
			   AND domain_id=".$_SESSION[$un_domain_id].
			   " ORDER BY vipid desc  
			   LIMIT 1 ";
	    $Aip = $oPub->selectrow($sql);
		if($Aip) {
			$add_ip = (gmtime() - $Aip[add_time] > $times)?true:false;

			$tmp = gmtime() - $times;
			$sql = "delete from ".$pre."vote_ip where add_time < ".$tmp;
			$oPub ->query($sql);
		}

        if($add_ip) {
			$time = gmtime();
			$sql = "INSERT INTO  ".$pre."vote_ip( vtid,ip,add_time,domain_id ) 
				   VALUES ('".$_POST['vtid']."', '".$ip."', '".$time."','".$_SESSION[$un_domain_id]."' );";
			$oPub ->query($sql); 
			
			if($_POST["vote_item"]){
				foreach ($_POST["vote_item"] AS $key => $val) {	 
					if(!empty($val)){
						$sql = "INSERT INTO  ".$pre."vote_poll( vtid,viid,descs,ip,add_time,domain_id ) 
							   VALUES ('".$_POST['vtid']."','".$key."','".$val."', '".$ip."', '".$time."','".$_SESSION[$un_domain_id]."' );";
						$oPub ->query($sql);  
						$sql = "UPDATE ".$pre."vote_item SET vi_nums = vi_nums + 1 ".
							 " WHERE viid=$key AND vtid = ".$_POST['vtid']." AND domain_id='".$_SESSION[$un_domain_id]."'";
						$oPub->query($sql);
					}
				}
			}

			if($_POST["vote_vgid_radio"]){
				foreach ($_POST["vote_vgid_radio"] AS $key => $val) {
					if(!empty($val)){
						$sql = "INSERT INTO  ".$pre."vote_poll( vtid,viid,descs,ip,add_time,domain_id ) 
							   VALUES ('".$_POST['vtid']."','".$val."','".$val."', '".$ip."', '".$time."','".$_SESSION[$un_domain_id]."' );";
						$oPub ->query($sql); 
						  
						$sql = "UPDATE ".$pre."vote_item SET vi_nums = vi_nums + 1 ".
							 " WHERE viid=$val AND vtid = ".$_POST['vtid']." AND domain_id='".$_SESSION[$un_domain_id]."'";
						$oPub->query($sql);
					}
				}
			}
			/* 重新计算总票数 */  
			$sql = "SELECT sum( `vi_nums` ) AS sumnums FROM ".$pre."vote_item 
			   where vtid = ".$_POST['vtid']." 
			   AND is_show = 1 
			   AND states = 0 
			   AND domain_id=".$_SESSION[$un_domain_id];
			$Anum = $oPub->selectrow($sql);
			$sumnums = $Anum['sumnums'];

			$Afields=array('vt_nums'=>$sumnums);
			$condition = "vtid = ".$_POST['vtid']." AND domain_id=".$_SESSION[$un_domain_id];
			$oPub->update($pre."vote_title",$Afields,$condition);
			//记录可选组票数 
			$sql = "SELECT vgid,sum( `vi_nums` ) AS vg_nums FROM ".$pre."vote_item 
			   where vtid = ".$_POST['vtid']." 
			   AND is_show = 1 
			   AND states = 0 
			   AND domain_id=".$_SESSION[$un_domain_id].
			   " group by vgid ";
			$row = $oPub->select($sql); 
			while( @list( $k, $v) = @each($row) ) { 
				$Afields=array('vg_nums'=>$v['vg_nums']);
				$condition = "vgid = ".$v['vgid'];
				$oPub->update($pre."vote_group",$Afields,$condition);
			}
		   unset($Anum);
		} else {
			$strMessage = '你已经投过票了，一天之内只能投票一次';
			if($Aconf['rewrite']){ 
				$url = 'vote-'.$vtid.'-0.html';
			}else{ 
				$url = 'vote.php?id='.$vtid;
			}  
			echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!');top.location='".$url."';</script>";

		}
	}
	/* 结果 */
	$show_vote = true; 
}

if($op == 'show'){
	$show_vote = true;
}
/* 显示调查内容 */
$sql = "SELECT a.vtid,a.vt_name,a.vt_desc FROM ".$pre."vote_title AS a 
	WHERE a.states=0 AND a.is_show = 1 and a.vtid='".$vtid."' AND	 a.domain_id = ".$_SESSION[$un_domain_id]." limit 1";
$row = $oPub->getRow($sql);
$rowarticle["vote_title"] = $row;
if($row) {
	if(!$show_vote){
		$rowarticle["for_vote_group"]= vote($row["vtid"]); 
	}else{
		//显示调查结果
		$rowarticle["for_vote_group"]= vote_show($row["vtid"]);
	}
}else
{
   $strMessage = '此调查已经关闭，或者删除';
   echo "<SCRIPT language='javascript'>\nalert('".$strMessage."!!');top.location='votes.php';</script>";
   exit;
}
$Ahome["show_vote"] = $show_vote;
$Ahome["nowNave"]  = '<A HREF="./">首页</A> > <a href="votes.php">调查列表</a> > '.$row["vt_name"].' > <span style="font-weight:lighter"> 详情</span>'; 
$Aconf['header_title'] = $row["vt_name"].'|'.$Aconf["header_title"]; 
$Ahome["vote"] = $rowarticle;unset($rowarticle);unset($row);
 
if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}
/* 调用模板 */
/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */ 

$cache_id = sprintf('%X', crc32($_SESSION[$un_domain_id].$vtid.$show_vote));
if (!$smarty->is_cached($Aconf["displayFile"], $cache_id)) {
	assign_template($Aconf); 
	$smarty->assign('home', $Ahome ); 
	$smarty->assign('user_info', $_SESSION );
	unset($Ahome); 
}
$smarty->display($Aconf["displayFile"], $cache_id);

?>
