<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

if(!defined('TAG_BASIC')) {
	define('TAG_BASIC', true);
	echo '<link rel="stylesheet" href="'.$tag_skin_url.'/'.$amina['tag_css'].'.css">'."\n";
}

//관련글 추출하기
if(!function_exists('amina_tag_list')) {

function amina_tag_list($bo_table, $wr_id, $tag_sop, $tag_list, $bo_list, $bo_subj="60", $bo_rows="5") {
	global $g5, $tag_skin_url;

	$rlist = array();

	//검색 게시판 정리
	$sql_bo = '';
	if($bo_list) {
		$bo = explode(",", trim($bo_list));
		$bo_num = count($bo);
		if($bo_num > 1) {
			for ($k=0; $k < count($bo_num); $k++) {
				if (trim($bo[$k])=='') continue;
				$sql_bo .= " bo_table = '$bo[$k]' or ";
			}
			if($sql_bo) $sql_bo = " and (".substr($sql_bo,0,-3).")";
		} else {
			$sql_bo = " and bo_table = '$bo[0]' ";
		}
	}

	$sql_tag = '';
	if (trim($tag_list)) {
		$tags = explode(',', $tag_list);
		if(count($tags) > 1) {
			while($tag = array_shift($tags)) {
				$tag = trim($tag);
				if(!$tag) continue;
			
				if($tag_sop == "or") {
					$sql_tag .= " tag like '%$tag%' or ";
				} else {
					$sql_tag .= " tag = '$tag' or ";
				}
			}

			if($sql_tag) $sql_tag = " and (".substr($sql_tag,0,-3).")";
		} else {
			if($tag_sop == "or") {
				$sql_tag .= " and tag like '%$tags[0]%' ";
			} else {
				$sql_tag .= " and tag = '$tags[0]' ";
			}
		}
	}

	if($tag_sop == "or") {
		$result = sql_query(" select bo_table, wr_id from {$g5['amina_taglog']} where 1 $sql_bo $sql_tag group by bo_table, wr_id order by wr_datetime desc limit 0, {$bo_rows} ");
	} else {
		$result = sql_query(" select bo_table, wr_id from {$g5['amina_taglog']} where 1 $sql_bo $sql_tag order by wr_datetime desc limit 0, {$bo_rows} ");
	}

	for ($i=0; $row=sql_fetch_array($result); $i++) {
	    $tmp_write_table = $g5['write_prefix'] . $row['bo_table']; // 게시판 테이블 전체이름
		$board1 = sql_fetch(" select * from {$g5['board_table']} where bo_table = '{$row['bo_table']}' ");

		if (!$board1) continue;

		$row1 = sql_fetch(" select * from {$tmp_write_table} where wr_id = '{$row['wr_id']}' "); //글 정보 불러오기..
		$row1['bo_table'] = $board1['bo_table'];
		if($bo_num > 1) $row1['ca_name'] = $board1['bo_subject']; //게시판 제목을 카테고리로 사용..
		if(strstr($row1['wr_option'], "secret")) $row1['wr_content'] = '비밀글입니다.';
		$rlist[$i] = amina_get_list($row1, $board1, $tag_skin_url, $bo_subj);
	}

	return $rlist;
} }

include_once($tag_skin_path.'/set/set.load.php');

$set = amina_set_tag($amina['tag_set']);

//제목, 더보기 주소
$tag_href = $amina['tag_board'] ? G5_BBS_URL.'/board.php?bo_table='.$amina['tag_board'].'&sca='.urlencode($tag_list).'&sop='.$tag_sop : '';

$rlist = array();

$rlist = amina_tag_list($bo_table, $wr_id, $tag_sop, $tag_list, $set['tag_bo_list'], $set['tag_bo_subj'], $set['tag_bo_rows']);

?>

<div class="tag_head">
	<?php if($tag_href) { ?>
		<b>"<a href="<?php echo $tag_href; ?>" target="_top"><?php echo $tag_list; ?></a>"</b> 관련글
		<a href="<?php echo $tag_href; ?>" target="_top"><span>+ 더보기</span></a>
	<? } else { ?>
		<b>"<?php echo $tag_list; ?>"</b> 관련글
	<? } ?>
	<div class="clear"></div>
</div>
<div class="tag_list">
	<table border=0>
	<tbody>
	<?php for ($i=0; $i < count($rlist); $i++) { 
		//xp 및 모바일 아이콘
		$wr_post = amina_array_write($rlist[$i]['wr_10']);
		$wr_icon = amina_array_icon($rlist[$i]['wr_5']);
	?>
		<tr>
		<td class="tag_subject">
		<?php
			echo '<a href="'.$rlist[$i]['href'].'" target="_top">';
			if($set['tag_bo_cate'] && $rlist[$i]['ca_name']) echo '['.$rlist[$i]['ca_name'].'] ';
			echo $rlist[$i]['subject'];
			echo '</a>';
			if ($rlist[$i]['comment_cnt']) echo ' <span class="tag_comment">'.$rlist[$i]['comment_cnt'].'</span>';
			if($wr_post['video'] == "video") echo ' <img src="'.$tag_skin_url.'/img/icon_video.gif" alt="" />';
			if($wr_post['photo'] == "photo") echo ' <img src="'.$tag_skin_url.'/img/icon_image.gif" alt="" />';
			if($wr_post['file'] == "file") echo ' <img src="'.$tag_skin_url.'/img/icon_file.gif" alt="" />';
			if($rlist[$i]['icon_new']) echo ' <img src="'.$tag_skin_url.'/img/icon_new.gif" alt="" />';
			if($rlist[$i]['icon_hot']) echo ' <img src="'.$tag_skin_url.'/img/icon_hot.gif" alt="" />';

			echo '</td>';

			if($set['tag_bo_name']) echo '<td class="tag_name">'.xp_icon($rlist[$i]['mb_id'], $wr_icon['level']).' '.$rlist[$i]['name'].'</td>'."\n";
			if($set['tag_bo_date']) echo '<td class="tag_date">'.date("m.d", strtotime($rlist[$i]['wr_datetime'])).'</td>'."\n";
			if($set['tag_bo_hit']) echo '<td class="tag_hit">조회 '.$rlist[$i]['wr_hit'].'</td>'."\n";
			if($set['tag_bo_good']) echo '<td class="tag_good">'.$rlist[$i]['wr_good'].'</td>'."\n";
		?>
		</tr>
	<?php } ?>
	</tbody>
	</table>
</div>