<?php
include_once('./_common.php');

include_once(AMINA_PATH.'/lib/head.lib.php');

if($_POST['js'] != "on") $move_href = G5_BBS_URL.'/board.php?bo_table='.$bo_table.'&wr_id='.$wr_id.'&page='.$page.$qstr;

function amina_return($error, $success) {
	global $move_href;

	if($move_href) {
		if($success) $error = $success;
		alert($error, $move_href);
	} else {
		if($success) {
			echo $error;
			exit;
		} else {
			echo 'error'.$error;
			exit;
		}
	}
}

if (!$poll) {
	$error = '항목을 선택해 주세요.';
	amina_return($error, $success);
}

if (!($bo_table && $wr_id)) {
	$error = '값이 제대로 넘어오지 않았습니다.';
	amina_return($error, $success);
}

if($write['wr_9'] == "lock" || $write['wr_9'] == "shingo") {
	$error = '블라인드 처리된 설문입니다.';
	amina_return($error, $success);
}

$row = sql_fetch(" select * from {$g5['amina_poll']} where bo_table = '$bo_table' and wr_id = '$wr_id' limit 1 ");

if (!$row['poll_id'] || !$write['wr_id']) {
	$error = '등록된 설문이 아닙니다.';
	amina_return($error, $success);
}

if(G5_SERVER_TIME > strtotime($row['poll_enddate'])) {
	$error = '이미 종료되었습니다.';
	amina_return($error, $success);
}

if($row['poll_level']) {
	if(!$member['mb_id']) {
		$error = '회원만 가능합니다.';
		amina_return($error, $success);
	}
	$poll_done = "and mb_id = '{$member['mb_id']}'";
} else {
	if($member['mb_id']) {
		$poll_done = "and ( mb_id = '{$member['mb_id']}' or poll_ip = '{$_SERVER['REMOTE_ADDR']}')";
	} else {
		$poll_done = "and poll_ip = '{$_SERVER['REMOTE_ADDR']}'";
	}
}

$row1 = sql_fetch(" select poll_id from {$g5['amina_polllog']} where bo_table = '$bo_table' and wr_id = '$wr_id' $poll_done limit 1 ");
if($row1['poll_id']) {
	$error = '이미 참여하셨습니다.';
	amina_return($error, $success);
}

//투표 제한 - 회원만 가능,  관리자와 글쓴이는 제외
if(!$is_admin && $member['mb_id'] && $write['mb_id'] != $member['mb_id']) {

	//다운로드시
	if($amina['limit_poll_down'] && $board['bo_download_point']) {
		$attach = get_file($bo_table, $wr_id);
		if(amina_attach_file($attach)) {
			$row2 = sql_fetch(" select count(*) as cnt from {$g5['point_table']} where mb_id = '{$member['mb_id']}' and po_rel_table = '$bo_table' and po_rel_id = '$wr_id' and po_rel_action = '다운로드' ");
			if(!$row2['cnt']) {
				$error = '다운로드후 투표할 수 있습니다.';
				amina_return($error, $success);
			}
		}
	}

	//댓글작성시
	if($amina['limit_poll_cmt']) {
		$row2 = sql_fetch(" select count(*) as cnt from $write_table where wr_is_comment=1 and wr_parent = '$wr_id' and mb_id = '{$member['mb_id']}' ");
		if(!$row2['cnt']) {
			$error = '댓글 작성시 투표할 수 있습니다.';
			amina_return($error, $success);
		}
	}

	//추천 또는 비추천시
	if($amina['limit_poll_good'] && ($board['bo_use_good'] || $board['bo_use_nogood'])) {
		$row2 = sql_fetch(" select count(*) as cnt from {$g5['board_good_table']} where bo_table = '$bo_table' and wr_id = '$wr_id' and mb_id = '{$member['mb_id']}' ");
		if(!$row2['cnt']) {
			$error = '추천 또는 비추천시 투표할 수 있습니다.';
			amina_return($error, $success);
		}
	}

	//별점시
	if($amina['limit_poll_star'] && $amina['star'] > 1) {
		$row2 = sql_fetch(" select count(*) as cnt from {$g5['amina_star']} where bo_table = '$bo_table' and wr_id = '$wr_id' and mb_id = '{$member['mb_id']}' ");
		if(!$row2['cnt']) {
			$error = '별점을 주셔야 투표할 수 있습니다.';
			amina_return($error, $success);
		}
	}

}

//Poll Skin
$poll_skin_path = AMINA_PATH.'/skin/poll/'.$amina['poll_skin'];
$poll_skin_url = AMINA_URL.'/skin/poll/'.$amina['poll_skin'];

@include_once($poll_skin_path.'/poll.head.skin.php');

// 투표 카운트 증가
sql_query(" update {$g5['amina_poll']} set poll_cnt = poll_cnt + 1, poll_cnt{$poll} = poll_cnt{$poll} + 1 where bo_table = '$bo_table' and wr_id = '$wr_id' ");

// 투표내역 생성
sql_query(" insert {$g5['amina_polllog']} set bo_table = '$bo_table', wr_id = '$wr_id', mb_id = '{$member['mb_id']}', poll = '$poll', poll_datetime = '".G5_TIME_YMDHIS."', poll_ip = '{$_SERVER['REMOTE_ADDR']}' ");

//참여포인트 적립
if ($member['mb_id'] && $write['mb_id'] != $member['mb_id'] && $row['poll_point'] > 0) {
	insert_point($member['mb_id'], (int)$row['poll_point'], "{$board['bo_subject']} {$wr_id} 투표 참여 MP", $bo_table, $wr_id, '@poll');

	if($xp['exp']['poll']) check_xp($member['mb_id']);
}

//글반응에 추가
if($write['mb_id'] && $write['mb_id'] != $member['mb_id'] && $aminaRe['poll']) amina_response($bo_table, $wr_id, $write['wr_subject'], $write['mb_id'], $member['mb_id'], $member['mb_nick'], 'poll');

$success = '투표해 주셔서 감사합니다.';

@include_once($poll_skin_path.'/poll.tail.skin.php');

//js미사용시 먼저 실행
if($move_href) amina_return($error, $success);

//AMINA Ajax
$g5['amina_ajax'] = true;

include_once(AMINA_PATH.'/lib/view.lib.php');
$error = amina_poll_skin($amina['poll_skin'], $bo_table, $wr_id, $member['mb_id'], true);
amina_return($error, $success);

?>