<?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 (!$bo_table || !$wr_id || !$star) {
	$error = '값이 제대로 넘어오지 않았습니다.';
	amina_return($error, $success);
}

if ($amina['star'] > 1) {
	;
} else {
	$error = '이 게시판은 별점을 사용하지 않습니다.';
	amina_return($error, $success);
}

if ($amina['star'] == "2" && !$member['mb_id']) {
	$error = '회원만 가능합니다.';
	amina_return($error, $success);
}

$row = sql_fetch(" select count(*) as cnt from {$g5['board_table']} where bo_table = '$bo_table' ", FALSE);
if (!$row['cnt']) {
	$error = '존재하는 게시판이 아닙니다.';
	amina_return($error, $success);
}

if($write['wr_9'] == "lock" || $write['wr_9'] == "shingo") {
	$error = '블라인드 처리된 글은 별점을 줄 수 없습니다.';
	amina_return($error, $success);
}

if($amina['star'] == "2") {
	$star_done = "and mb_id = '{$member['mb_id']}'";
} else {
	if($member['mb_id']) {
		$star_done = "and ( mb_id = '{$member['mb_id']}' or bg_ip = '{$_SERVER['REMOTE_ADDR']}')";
	} else {
		$star_done = "and bg_ip = '{$_SERVER['REMOTE_ADDR']}'";
	}
}

$sql = " select bg_flag, bg_star from {$g5['amina_star']} where bo_table = '$bo_table' and wr_id = '$wr_id' $star_done and bg_flag = 'star' ";
$row = sql_fetch($sql);
if($row['bg_flag']) {
	$error = '이미 '.$row['bg_star'].'점의 별점을 주셨습니다.';
	amina_return($error, $success);
} else {

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

		//다운로드시
		if($amina['limit_star_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_star_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_star_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_star_poll']) {
			$wr_post = amina_array_write($write['wr_10']);
			if($wr_post['chk_poll']) {
				$row2 = sql_fetch(" select count(*) as cnt from {$g5['amina_polllog']} where bo_table = '$bo_table' and wr_id = '$wr_id' and mb_id = '{$member['mb_id']}' ");
				if(!$row2['cnt']) {
					$error = '투표를 하셔야 별점을 줄 수 있습니다.';
					amina_return($error, $success);
				}
			}
		}
	}

	//Star Skin
	$star_skin_path = AMINA_PATH.'/skin/star/'.$amina['star_skin'];
	$star_skin_url = AMINA_URL.'/skin/star/'.$amina['star_skin'];

	@include_once($star_skin_path.'/star.head.skin.php');

	//등록된 글의 관련태그 읽어오기
	$tag = amina_array_tag($write['wr_7']);

	//내역 생성
	sql_query(" insert {$g5['amina_star']} set bo_table = '$bo_table', wr_id = '$wr_id', mb_id = '{$member['mb_id']}', bg_flag = 'star', bg_datetime = '".G5_TIME_YMDHIS."', bg_star = '$star', bg_ip = '{$_SERVER['REMOTE_ADDR']}' ");

	//전체 추천 점수 계산하기
	$row = sql_fetch(" select SUM(bg_star) as score, COUNT(bg_star) as people from {$g5['amina_star']} where bo_table = '$bo_table' and wr_id = '$wr_id' and bg_flag = 'star' ");

	//별점값 정리
	$wr_7 = $row['score'].'|'.$row['people'].'|'.$tag['tag_sop'].'|'.$tag['related_tag'];

	//스코어 계산
	if(!$row['people']) $row['people'] = 0;
	$score = 0;
	if($row['people'] > 0) $score = $row['score'] / $row['people'];
	$score = round($score,1);

	//재계산된 별점 넣기
	sql_query(" update {$g5['write_prefix']}{$bo_table} set wr_7 = '$wr_7' where wr_id = '$wr_id' ");

	//포인트 주기
	if($amina['limit_date'] > 0) $chk_limit = date("Y-m-d H:i:s", G5_SERVER_TIME - ( $amina['limit_date'] * 24 * 3600 ));

	if(($member['mb_id'] == $write['mb_id'] && $amina['limit_cmt']) || ($chk_limit && $write['wr_datetime'] < $chk_limit)) { 
		;
	} else {
		$amina_point = (int)$amina['star_point'];
		$amina_repoint = (int)$amina['star_repoint'];
		$amina_msg = $board['bo_subject'].' '.$wr_id.' 글에 별점을 주었습니다.';
		$amina_remsg = $board['bo_subject'].' '.$wr_id.' 글이 별점을 받았습니다.';

		if($member['mb_id'] && $amina_point > 0) {
			insert_point($member['mb_id'], $amina_point, $amina_msg, $bo_table, $wr_id, '@star');

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

		if($member['mb_id'] && $write['mb_id'] && $amina_repoint > 0) {
			insert_point($write['mb_id'], $amina_repoint, $amina_remsg, $bo_table, $wr_id, '@star_re_'.$member['mb_id']);

			if($xp['exp']['star_re']) check_xp($write['mb_id']);
		}
	}

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

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

	$success = $star.'점의 별점을 주셨습니다.';

	@include_once($star_skin_path.'/star.tail.skin.php');

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

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

	$error = amina_star_skin($amina['star_skin'], $bo_table, $wr_id, $wr_7, true);
	amina_return($error, $success);
}

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

?>