<?php
/**
* @package phpBB-WAP
* @copyright (c) phpBB Group
* @Оптимизация под WAP: Гутник Игорь ( чел ).
* @简体中文：中文phpBB-WAP团队
* @license http://opensource.org/licenses/gpl-license.php
**/

/**
* 这是一款自由软件, 您可以在 Free Software Foundation 发布的
* GNU General Public License 的条款下重新发布或修改; 您可以
* 选择目前 version 2 这个版本（亦可以选择任何更新的版本，由
* 你喜欢）作为新的牌照.
**/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
}

if( $is_rules_auth )
{
	$subj = ( isset($_POST['subject']) ) ? htmlspecialchars(trim($_POST['subject'])) : '';

	if($rule_cat_id)
	{
		$sql = "SELECT * FROM " . RULES_CAT_TABLE . " WHERE cat_r_id = $rule_cat_id";
		if( !($result = $db->sql_query($sql)) )
		{
			trigger_error('Could not obtain rules information.', E_USER_WARNING);
		}
		$crid = $db->sql_fetchrow($result);
		$cridn = $crid['cat_r_name'];
		$crid = $crid['cat_r_id'];
	}
	else
	{
		$crid = '';
	}

	if( $crid != '' && $subj != '' )
	{
		$sql = "UPDATE " . RULES_CAT_TABLE . " SET cat_r_name = '" . str_replace("\'", "''", $subj) . "'
					WHERE cat_r_id = $crid";
		if(!($result=$db->sql_query($sql)))
		{
			trigger_error('Error rules cat table!', E_USER_WARNING);
		}
		redirect(append_sid("rules.php", 1));
	}
	elseif( $crid == '' && $subj != '' )
	{
		$sql = "SELECT MAX(cat_r_id) AS max_id FROM " . RULES_CAT_TABLE;
		if(!($result=$db->sql_query($sql)))
		{
			trigger_error('Error rules cat table!', E_USER_WARNING);
		}
		$result = $db->sql_fetchrow($result);
		$new_id = $result['max_id'] + 1;
		$sql = "INSERT INTO " . RULES_CAT_TABLE . " (cat_r_id, cat_r_name) VALUES ($new_id, '" . str_replace("\'", "''", $subj) . "')";
		if(!($result=$db->sql_query($sql)))
		{
			trigger_error('Error rules cat table!', E_USER_WARNING);
		}

		redirect(append_sid("rules.php", 1));
	}
	else
	{
		if ( $mode == 'editcat' )
		{
			$page_title = '编辑规则分类';
			$s_rules_action = append_sid("rules.php?mode=editcat&amp;crid=$crid");
			$cat_name = $cridn;
			$title = '编辑';
			$template->assign_block_vars('r_edit', array());
		}
		else
		{
			$page_title = '新增规则分类';
			$s_rules_action = append_sid("rules.php?mode=addcat");
			$cat_name = '';
			$title = '新增';
			$template->assign_block_vars('r_add', array());
		}
		
		page_header($page_title);

		$template->set_filenames(array(
			'body' => 'rules/rule_cat.tpl')
		);

		$template->assign_vars(array(
			'TITLE'				=> $title,
			'CAT_NAME' 			=> $cat_name,
			'S_RULES_ACTION' 	=> $s_rules_action)
		);

		$template->pparse('body');

		page_footer();
	}
}
else
{
	redirect(append_sid('rules.php', true));
}
?>