<?php
/**
 * @version $Id: cate.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', 'cate');
allowdo('setcate');
$baseurl = 'admincp.php?action=cate&type=';
require_once (PBDIGG_CP.'include/cate.func.php');

if ($type == 'add' || empty($type))
{
	if ($submitted)
	{
		$order = (int)$_POST['cate']['order'];
		$order = ($order < 0) ? 0 : $order;
		$status = (int)$_POST['cate']['status'];
		$cup = (int)$_POST['cate']['cup'];
		//cate name don't support html code
		$name = HConvert(trim($_POST['cate']['name']));
		if ($ck = checkCateName($name)) showMsg($ck, $actionurl);
		$icon = trim($_POST['cate']['icon']);
		if ($icon)
		{
			if (strpos($icon, '..') !== FALSE)
			{
				showMsg($a_message['cate_icon_illegal'], $actionurl);
			}
			elseif (!file_exists(PBDIGG_ROOT.'images/cate/'.$icon))
			{
				showMsg($a_message['cate_icon_not_exist'], $actionurl);
			}
		}
		//filter dangerous code
		$keywords = SafeConvert(trim($_POST['cate']['keywords']));
		$intro = SafeConvert(trim($_POST['cate']['intro']));
		$cateNum = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}categories WHERE cup = $cup AND name = '$name'");
		if ($cateNum['num']) showMsg($a_message['cate_exist'], $actionurl);
		$DB->db_query("INSERT INTO {$db_prefix}categories (cup, icon, name, keywords, description, status, displayorder) VALUES ($cup, '$icon', '$name', '$keywords', '$intro', $status, $order)");
		$cid = $DB->db_insertID();
		$Cache->categories();
		$Cache->singlecate($cid);
		//add tpl transfer
		addselftpl($cid);
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_option.php');
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
		redirect($a_message['add_cate_success'], $actionurl);
	}
	else
	{
		$option = '<select name="cate[cup]">'."\r\n".'<option value="0" selected="selected">'.$cp_message['top_cate'].'</option>'."\r\n";
		$cate_option = '';
		if (@include(PBDIGG_ROOT.'data/cache/cache_cate_option.php'))
		{
			$option .= $cate_option;
		}
		else
		{
			cateOption($categories, $cate_option);
			$Cache->writeCache('cate_option', 'return $cate_option = \''.addcslashes($cate_option, '\\\'').'\'');
			$option .= $cate_option;
		}
		$option .= '</select>';
		$type = 'add';
		$TPL->assign('cate_option', $option);
	}
}
elseif ($type == 'edit')
{
	if ($step == 'order')
	{
		// update cate display order
		$cate = $_POST['cate'];
		positiveInteger($cate);
		foreach ($cate as $key => $value)
		{
			$DB->db_query("UPDATE {$db_prefix}categories SET displayorder = $value WHERE cid = $key");
		}
		$Cache->categories();
		$Cache->singlecate();
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
		redirect($a_message['update_cate_success'], $actionurl);
	}
	else
	{
		$cate_table = '';
		if (!@include_once(PBDIGG_ROOT.'data/cache/cache_cate_table.php'))
		{
			cateTable($categories, $cate_table, 0, 0, array(array(), array(' class="tcell_one"', ' class="tcell_two"')));
			$Cache->writeCache('cate_table', 'return $cate_table = \''.addcslashes($cate_table, '\\\'').'\'');
		}
		$TPL->assign('cate_table', $cate_table);
	}
}
elseif ($type == 'del')
{
	//del cate
	$cid = (int)$_GET['cid'];
	if ($rt = delCate($cid)) showMsg($rt, $baseurl.'edit');
	delselftpl($cid);
	PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
	redirect($a_message['del_cate_success'], $baseurl.'edit');
}
elseif ($type == 'status')
{
	//open or close cate
	$status = (int)$_GET['status'];
	$cid = (int)$_GET['cid'];
	if ($cid <= 0) showMsg($a_message['parameter_error'], $baseurl.'edit');
	if ($status)
	{
		$db_status = 0;
		$subCate = array();
		subCate($categories, $subCate, $startID = $cid);
		$cid .= ','.implode(',', $subCate);
		if ($cid{strlen($cid)-1} == ',')
		{
			$cid = substr($cid, 0 , -1);
		}
	}
	else
	{
		$db_status = 1;
	}
	$DB->db_query("UPDATE {$db_prefix}categories SET status = $db_status WHERE cid IN ($cid)");
	$Cache->categories();
	$Cache->singlecate($cid);
	PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
	redirect($a_message['update_cate_success'], $baseurl.'edit');
}
elseif ($type == 'mod')
{
	//change cate attribute
	if ($submitted)
	{
		$cid = (int)$_GET['cid'];
		$actionurl .= '&amp;cid='.$cid;
		$order = (int)$_POST['cate']['order'];
		$order = ($order < 0) ? 0 : $order;
		$status = (int)$_POST['cate']['status'];
		$cup = (int)$_POST['cate']['cup'];
		//cate name don't support html code
		$name = HConvert(trim($_POST['cate']['name']));
		if ($ck = checkCateName($name)) showMsg($ck, $actionurl);
		$icon = trim($_POST['cate']['icon']);
		if ($icon)
		{
			if (strpos($icon, '..') !== FALSE) showMsg($a_message['cate_icon_illegal'], $actionurl);
			if (!file_exists(PBDIGG_ROOT.'images/cate/'.$icon)) showMsg($a_message['cate_icon_not_exist'], $actionurl);
		}
		//filter dangerous code
		$intro = SafeConvert(trim($_POST['cate']['intro']));
		$keywords = SafeConvert(trim($_POST['cate']['keywords']));
		$cateNum = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}categories WHERE cup = $cup AND name = '$name' AND cid <> $cid");
		if ($cateNum['num']) showMsg($a_message['same_cate_exist'], $actionurl);
		$rs = $DB->db_fetch_one_array("SELECT icon FROM {$db_prefix}categories WHERE cid = $cid");
		if ($rs['icon'] && $rs['icon'] != $icon) PDel(PBDIGG_ROOT.'images/cate/'.$rs['icon']);
		$DB->db_query("UPDATE {$db_prefix}categories SET cup = $cup, icon = '$icon', name = '$name', keywords = '$keywords', description = '$intro', status = $status, displayorder = $order WHERE cid = $cid");
		$Cache->categories();
		$Cache->singlecate($cid);
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_option.php');
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
		redirect($a_message['update_cate_success'], $baseurl.'edit');
	}
	else
	{
		$cid = (int)$_GET['cid'];
		$actionurl .= '&amp;cid='.$cid;
		if (!$cid) showMsg($a_message['parameter_error'], $baseurl.'edit');
		$cate = array();
		$cate = $DB->db_fetch_one_array("SELECT * FROM {$db_prefix}categories WHERE cid = $cid");
		if (!$cate) showMsg('·ÖÀàID´íÎó', $baseurl.'edit');
		$option = '<select name="cate[cup]">'."\r\n".'<option value="0">'.$cp_message['top_cate'].'</option>'."\r\n";
		$cate_option = '';
		cateOption($categories, $cate_option, 0, $categories[$cid]['cup']);
		$option .= $cate_option;
		$option .= '</select>';		
		radioChecked('status_', $cate['status']);
		$TPL->assign('status_0', $status_0);
		$TPL->assign('status_1', $status_1);
		$type = 'mod';
		$TPL->assign('cate_option', $option);
		$TPL->assign('cate', $cate);
	}
}
elseif ($type == 'merge')
{
	if ($submitted)
	{
		$scid = (int)$_POST['source'];
		$dcid = (int)$_POST['destination'];
		if ($scid == $dcid) showMsg($a_message['merge_same_cate'], $actionurl);
		//if source cate has sub cate
		$rs = $DB->db_fetch_one_array("SELECT COUNT(*) num FROM {$db_prefix}categories WHERE cup = $scid");
		if ($rs['num']) showMsg($a_message['sub_cate_exist'], $actionurl);
		mergeCate($scid, $dcid);
		delselftpl($scid);
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_option.php');
		PDel(PBDIGG_ROOT.'data/cache/cache_cate_table.php');
		redirect($a_message['merge_cate_success'], $actionurl);
	}
	else
	{
		$source = '<select name="source">'."\r\n";
		$destination = '<select name="destination">'."\r\n";
		$cate_option = '';
		if (!@include_once (PBDIGG_ROOT.'data/cache/cache_cate_option.php'))
		{
			cateOption($categories, $cate_option);
			$Cache->writeCache('cate_option', 'return $cate_option = \''.addcslashes($cate_option, '\\\'').'\'');
		}
		$source .= $cate_option.'</select>';
		$destination .= $cate_option.'</select>';
		$type = 'merge';
		$TPL->assign('source_option', $source);
		$TPL->assign('destination_option', $destination);
	}
}
else
{
	redirect($a_message['unknow_operate'], 'admincp.php?action=main');
}

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