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

/*******************************************************************************
* 태그 전용 게시판
*******************************************************************************/

unset($list);

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

//공지사항
$i = 0;

$notice_sql = '';
if(AMINA) {
    $arr_notice = explode(",", trim($board['bo_notice']));
} else {
    $arr_notice = explode("\n", trim($board['bo_notice']));
}
for ($k=0; $k<count($arr_notice); $k++) {
	if (trim($arr_notice[$k])=='') continue;
	$row = sql_fetch(" select * from $write_table where wr_id = '$arr_notice[$k]' ");
	if (!$row['wr_id']) continue;
	$list[$i] = get_list($row, $board, $board_skin_url, $board['bo_subject_len']);
    $list[$i]['bo_table'] = $bo_table;
	$list[$i]['is_notice'] = true;
	$i++;
}

//태그 정리
$sql_tag = '';
if (trim($sca)) {
	$tags = explode(',', $sca);
	while($tag = array_shift($tags)) {
		$tag = trim($tag);
		if(!$tag) continue;
		if($sop == "or") {
			$sql_tag .= " tag like '%".$tag."%' or ";
		} else {
			$sql_tag .= " tag = '".$tag."' or ";
		}
	}
}

$is_tag_cloud = true;

if($sql_tag) { // 태그 관련글수 구하기

	if($sop == "or") {
		$sql_tag = "from {$g5['amina_taglog']} where ".substr($sql_tag,0,-4)." group by bo_table, wr_id ";
		$a_total = sql_query(" select * $sql_tag ");
		$total_count = @mysql_num_rows($a_total);
	} else {
		$sql_tag = "from {$g5['amina_taglog']} where ".substr($sql_tag,0,-4);
		$a_total = sql_fetch(" select count(*) as cnt $sql_tag ");
		$total_count = $a_total['cnt'];
	}

	// 페이징 처리
	$total_page  = ceil($total_count / $board['bo_page_rows']);  // 전체 페이지 계산
	if (!$page) { $page = 1; } // 페이지가 없으면 첫 페이지 (1 페이지)
	$from_record = ($page - 1) * $board['bo_page_rows']; // 시작 열을 구함
    $sql = " select * $sql_tag order by wr_datetime desc limit $from_record, {$board['bo_page_rows']} ";
	$result = sql_query($sql);

	$k = 0;
	while ($row = sql_fetch_array($result)) {
	    $tmp_write_table = $g5['write_prefix'] . $row['bo_table']; // 게시판 테이블 전체이름

		$tmp_board = sql_fetch(" select * from {$g5['board_table']} where bo_table = '{$row['bo_table']}' ");
		if (!$tmp_board) continue;
		
		$row1 = sql_fetch(" select * from $tmp_write_table where wr_id = '{$row['wr_id']}' ");

		//글이 없으면 해당 태그로그를 삭제한다.
		if (!$row1['wr_id']) { 
			amina_del_tag($row['bo_table'], $row['wr_id']);
			continue;
		}

		//값정리
	    $row1['bo_table'] = $row['bo_table'];
		$row1['ca_name'] = $tmp_board['bo_subject'];

		$list[$i] = amina_get_list($row1, $tmp_board, $board_skin_url, $board['bo_subject_len']);
	    $list[$i]['is_notice'] = false;
	    $list[$i]['num'] = $total_count - ($page - 1) * $board['bo_page_rows'] - $k;
	    $i++;
	    $k++;
	}

	$is_tag_cloud = false;

} else { //태그구름 - 총 등록태그 구하기
	$a_total = sql_fetch(" select count(*) as cnt from {$g5['amina_tag']} where 1 ");
	$total_count = $a_total['cnt'];
}

?>
<style>
	.tag-search-tbl { width:100%; margin:15px 0; border:1px solid #ddd; }
	.tag-search { border:1px solid #ddd; backround:#fff; }
	.tag-search .select { padding:4px; }
	.tag-search .input { padding:4px; }
	.tag-search .input .sca { border:0; width:120px; }
	.tag-search .submit { border:0; display:block; }
	.tag-search img {border:0; display:block; }
</style>

<table border=0 cellpadding=10 cellspacing=0 class="tag-search-tbl">
<tr><td>
	<img src="<?php echo AMINA_URL; ?>/img/icon_tag.gif" border=0 alt=""/>
	<?php if(!$is_tag_cloud) { ?>
		<?php if($sop == "or") { ?>
			<b>"<?php echo amina_tag_link($sca, true, $sop); ?>"</b> 단어가 포함된 태그 관련글입니다.
		<?php } else { ?>
			<b>"<?php echo amina_tag_link($sca, true, $sop); ?>"</b> 태그 관련글입니다.
		<?php } ?>
	<?php } else { ?>
		<span style="color:#888;">태그 또는 태그에 포함된 단어를 입력해 주세요. *^^*</span>
	<?php } ?>
</td>
<td align=right>
	<table cellpadding=0 cellspacing=0 class="tag-search">
	<form name="ftag" method="get" id="ftag">
	<input type="hidden" name="bo_table" value="<?php echo $bo_table; ?>">
	<tr><td class="select">
		<select name="sop">
			<option value="and">일치</option>
			<option value="or"<?php if($sop == "or") echo " selected"; ?>>포함</option>
	   </select>
   </td>
   <td class="input"><input name="sca" class="sca" itemname="검색태그" required value="<?php echo stripslashes($sca); ?>"></td>
   <td><input type="image" src="<?php echo AMINA_URL; ?>/img/btn_search.png" class="submit"></td>
   </tr>
   </form>
   </table>
</td>
</tr>
</table>