<?php
/**
 * @version $Id: tag.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

if(!defined('IN_ADMIN') || !isset($_PBDEV['PHP_SELF']) || !preg_match('/[\/\\\\]admincp\.php$/i', $_PBDEV['PHP_SELF']))
{
	exit('Access Denied');
}
define('PB_CSCRIPT', 'tag');
allowdo('settag');
$baseurl = 'admincp.php?action=tag&type=';
require_once (PBDIGG_CP.'include/tag.func.php');

if ($type == 'add' || !$type)
{
	if ($submitted)
	{
		$tagname = trim($_POST['tag']['name']);
		$status = (int)$_POST['tag']['status'];
		if ($ck = checkTag($tagname))
		{
			showMsg($ck, $actionurl);
		}
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}tags WHERE tagname = '$tagname'");
		if ($rs['num'])
		{
			showMsg($a_message['same_tag_exist'], $actionurl);
		}
		else
		{
			$DB->db_query("INSERT INTO {$db_prefix}tags (tagname, usenum, ifopen, ifsys) VALUES('$tagname', 0, $status, 1)");
		}
		$Cache->tags();
		$Cache->systags();
		redirect($a_message['add_tag_success'], $actionurl);
	}
	else
	{
		$type = 'add';
	}
}
elseif ($type == 'manage')
{
	
	if ($submitted)
	{
		$sql = ' WHERE 1';
		if ($_POST['ifsys']) $sql .= ' AND ifsys = 1';
		if ($tagname)
		{
			$tagname = explode(',', trim($_POST['tagname']));
			$sql .= ' AND';
			foreach ($tagname as $v)
			{
				$v = str_replace('*', '%', preg_replace('/\*{2,}/i', '*', $v));
				$sql .= " tagname LIKE '".$v."' OR";
			}
			$sql = substr($sql, 0, -3);
		}
	}
	$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}tags".$sql);
	$totalRecords = (int)$rs['num'];
	$pageSize = 30;
	$page = !isPage($page) ? 1 : $page;
	$start_limit = ($page -1) * $pageSize;
	$mult = cpmultLink($page, $totalRecords, 'admincp.php?action=tag&amp;type=manage&amp;tagname='.$_POST['tagname'].'&ifsys='.$_POST['ifsys'].'&', $pageSize);
	$DB->db_query("SELECT * FROM {$db_prefix}tags ".$sql." ORDER BY ifopen ASC LIMIT $start_limit, $pageSize");
	while ($rs = $DB->db_fetch_array())
	{
		if ($rs['ifopen'])
		{
			$rs['tagstatus'] = $a_message['open'];
			$rs['opptagstatus'] = $a_message['close'];
		}
		else
		{
			$rs['tagstatus'] = '<span style="color:#f00">'.$a_message['close'].'</span>';
			$rs['opptagstatus'] = $a_message['open'];
		}
		$rs['ifsys'] && ($rs['tagname'] = '<strong>'.$rs['tagname'].'</strong>');
		$tag[] = $rs;
	}
	$TPL->assign('total_records', $totalRecords);
	$TPL->assign('mult', $mult);
	$TPL->assign('tag', $tag);
}
elseif ($type == 'status')
{
	//change tag status
	if (empty($_GET['tagid']))
	{
		showMsg($a_message['parameter_error'], $baseurl.'manage');
	}
	$status = (int)$_GET['status'];
	$tagid = (int)$_GET['tagid'];
	if ($status)
	{
		$db_status = 0;
	}
	else
	{
		$db_status = 1;
	}
	$DB->db_query("UPDATE {$db_prefix}tags SET ifopen = $db_status WHERE tagid = $tagid");
	$Cache->tags();
	redirect($a_message['update_tag_success'], $baseurl.'manage');
}
elseif ($type == 'del')
{
	positiveInteger($tagid);
	$rt = 'del_tag_failed';
	if (delTag($tagid))
	{
		$Cache->tags();
		$rt = 'del_tag_success';
	}
	redirect($a_message[$rt], $baseurl.'manage');
}
elseif ($type == 'mod')
{
	if ($submitted)
	{
		$tagid = (int)$tagid;
		$tagname = trim($_POST['tag']['name']);
		$ifopen = (int)$tag['ifopen'];
		$ifsys = (int)$tag['ifsys'];
		if (isset($tag['usenum']))
		{
			$sql['usenum'] = (int)$tag['usenum'];
		}
		if ($ck = checkTag($tagname))
		{
			showMsg($ck, $actionurl.'&tagid='.$tagid);
		}
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}tags WHERE tagname = '$tagname' AND tagid <> $tagid");
		if ($rs['num'])
		{
			showMsg($a_message['same_tag_exist'], $actionurl.'&tagid='.$tagid);
		}
		$DB->db_query("UPDATE {$db_prefix}tags SET tagname = '$tagname', ifopen = $ifopen, ifsys = $ifsys WHERE tagid = $tagid");
		$Cache->tags();
		$Cache->systags();
		redirect($a_message['mod_tag_success'], $baseurl.'manage');
	}
	else
	{
		$tagid = (int)$_GET['tagid'];
		$tag = $DB->db_fetch_one_array("SELECT * FROM {$db_prefix}tags WHERE tagid = $tagid");
		radioChecked('ifopen_', $tag['ifopen']);
		$TPL->assign('ifopen_0', $ifopen_0);
		$TPL->assign('ifopen_1', $ifopen_1);
		radioChecked('ifsys_', $tag['ifsys']);
		$TPL->assign('ifsys_0', $ifsys_0);
		$TPL->assign('ifsys_1', $ifsys_1);
		$actionurl .= '&amp;tagid='.$tagid;
		$TPL->assign('tag', $tag);
	}
}
elseif ($type == 'tidy')
{
	if ($step)
	{
		if (!$count) $count = 100;
		$count = (int)$count;
		$start = ($step - 1) * $count;
		$next = $start + $count;
		$step++;
		$jumpurl = 'admincp.php?action=tag&type=tidy&step='.$step.'&count='.$count;
		if (tidyTag($start, $count))
		{
			redirect(sprintf($a_message['tidy_tag_process'], $start, $next), $jumpurl);
		}
		else
		{
			$Cache->tags();
			$Cache->systags();
			redirect($a_message['tidy_tag_success'], $baseurl.'manage');
		}
	}
}
else
{
	redirect($a_message['unknow_operate'], 'admincp.php?action=main');
}

$TPL->assign('menu', $m_tag);
?>
