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

//-------------------------------------------------------------------------------------
// 지블로그(gblog)에서 태그관련 부분 차용함
//-------------------------------------------------------------------------------------

//get Tag
function amina_get_tag($bo_table, $wr_id, $link=false) {
    global $g5, $amina;

    $tag = array();
    $sql = "select t.id, t.tag from {$g5['amina_taglog']} l left join {$g5['amina_tag']} t on l.tag_id=t.id where bo_table = '$bo_table' and wr_id = '$wr_id' order by l.tag_id ";
    $result = sql_query($sql);

    while ($row = sql_fetch_array($result)) {
        $tag[] = $row['tag'];
    }

	$sec = ', ';
	$list = '';
    foreach($tag as $val) {
		if ($link && $amina['tag_board'])
			$list .= '<a href="'.G5_BBS_URL.'/board.php?bo_table='.$amina['tag_board'].'&sca='.urlencode($val).'" rel="tag">'.$val.'</a>'.$sec;
		else
			$list .= $val.$sec;
	}

	$tag = substr($list, 0, strlen($list)-strlen($sec));

    return $tag;
}

//add Tag
function amina_add_tag($bo_table, $wr_id, $tag, $wr_datetime) {
    global $g5;

    if (trim($tag)) {
		$tags = explode(',', $tag);
		//while($tag = array_shift($tags)) {
	    foreach($tags as $tag) {
			$tag = trim($tag);

			if (!$tag) continue;

		    $row = sql_fetch("select * from {$g5['amina_tag']} where tag = '{$tag}' ");
			if (empty($row)) {
				sql_query("insert into {$g5['amina_tag']} set tag='{$tag}', cnt=1, regdate='".G5_TIME_YMDHIS."', lastdate='".G5_TIME_YMDHIS."'");
		        $tag_id = mysql_insert_id();
			} else  {
				$tag_id = $row['id'];
                sql_query("update {$g5['amina_tag']} set cnt = cnt + 1, lastdate='".G5_TIME_YMDHIS."' where id='{$tag_id}'");
	        }

			sql_query("insert into {$g5['amina_taglog']} set bo_table = '{$bo_table}', wr_id = '{$wr_id}', tag_id = '{$tag_id}', tag = '{$tag}', regdate = '".G5_TIME_YMDHIS."', wr_datetime = '$wr_datetime'");
        }
	}
}

//del tag
function amina_del_tag($bo_table, $wr_id) {
    global $g5;

    $tag = array();
    $sql = "select t.id, t.tag from {$g5['amina_taglog']} l left join {$g5['amina_tag']} t on l.tag_id=t.id where bo_table = '$bo_table' and wr_id = '$wr_id'";
    $result = sql_query($sql);

    while ($row = sql_fetch_array($result)) {
		sql_query("update {$g5['amina_tag']} set cnt = cnt - 1, lastdate='".G5_TIME_YMDHIS."' where id='{$row['id']}'");
    }

	sql_query("delete from {$g5['amina_taglog']} where bo_table='{$bo_table}' and wr_id = '{$wr_id}'");

}

//get Tag Link
function amina_tag_link($sca, $link=true, $sop='') {
    global $g5, $amina;

	$tag = array();
	$sca = trim($sca);
	$tags = explode(',', $sca);
	while($tmp = array_shift($tags)) {
		$tmp = trim($tmp);
		if(!$tmp) continue;
        $tag[] = $tmp;
	}

	$sec = ', ';
	$list = '';
    foreach($tag as $val) {
		if ($link && $amina['tag_board'])
			$list .= '<a href="'.G5_BBS_URL.'/board.php?bo_table='.$amina['tag_board'].'&sca='.urlencode($val).'&sop='.$sop.'" rel="tag">'.$val.'</a>'.$sec;
		else
			$list .= $val.$sec;
	}

	$tag = substr($list, 0, strlen($list)-strlen($sec));

    return $tag;
}

//관련태그글 출력하기
function amina_tag_skin($skin, $bo_table, $wr_id, $tag_sop, $tag_list) {
	global $g5, $board, $amina;

	$tag_skin_path = AMINA_PATH.'/skin/tag/'.$skin;
	$tag_skin_url = AMINA_URL.'/skin/tag/'.$skin;

    ob_start();
	include $tag_skin_path.'/tag.skin.php';
    $view_tag = ob_get_contents();
    ob_end_clean();

	return $view_tag;
}

?>