<?php

// ACP Header - START
if (!empty($setmodules))
{
	$module['General']['Rules'] = basename(__FILE__);
	return;
}

require('./pagestart.php');
require(INC_DIR .'bbcode.php');
require(INC_DIR .'functions_post.php');

// ACP Header - END

$mode = htmlspecialchars(request_var('mode', ''));
$mode_id = htmlspecialchars(request_var('id', ''));

switch($mode)
{
	case 'add':

		$s_hidden_fields = '<input type="hidden" name="mode" value="save" />';
    	$rules_is_active = "checked=\"checked\"";


    	$template->assign_vars(array(
			'RULES_NOT_ACTIVE' => '',
			'RULES_ACTIVE' => $rules_is_active,

			'RULES_TEXT' => @$rules_info['text'],

			'U_RULES' => (@$rules_info['rules_type']==4) ? htmlentities(@$rules_info['text'], ENT_QUOTES) : ( @ereg("http",strtolower(@$rules_info['text']))) ? @$rules_info['text'] : '../'.@$rules_info['text'],
			'RULES_TITLE' => @$rules_info['title'],
			'S_RULES_ACTION' => 'admin_rules.php',
			'S_HIDDEN_FIELDS' => $s_hidden_fields,
		));

		print_page('rules_edit_body.tpl', 'admin');

	break;

	case 'edit':

		$rules_id = (isset($mode_id)) ? $mode_id : 0;
		$s_hidden_fields = "";

		if( empty($rules_id) )
		{
			bb_die($lang['MISSING_RULES_ID']);
		}

		$sql = "SELECT * FROM ". BB_RULES ."
			WHERE id = '$rules_id'";
		if(!$result = DB()->sql_query($sql))
		{
			bb_die("Couldn't obtain rules data");
		}

		$rules_info = DB()->sql_fetchrow($result);
		$s_hidden_fields .= '<input type="hidden" name="id" value="' . $rules_id . '" />';

		$s_hidden_fields .= '<input type="hidden" name="mode" value="save" />';
		$rules_is_active = ( $rules_info['active'] ) ? "checked=\"checked\"" : "";
		$rules_is_not_active = ( !$rules_info['active'] ) ? "checked=\"checked\"" : "";


		$template->assign_vars(array(
			'RULES_NOT_ACTIVE' => $rules_is_not_active,
			'RULES_ACTIVE' => $rules_is_active,

			'RULES_TEXT' => @$rules_info['text'],

			'U_RULES' => (@$rules_info['rules_type']==4) ? htmlentities(@$rules_info['text'], ENT_QUOTES) : ( @ereg("http",strtolower(@$rules_info['text']))) ? @$rules_info['text'] : '../'.@$rules_info['text'],
			'RULES_TITLE' => @$rules_info['title'],
  		    'S_RULES_ACTION' => 'admin_rules.php',
      		'S_HIDDEN_FIELDS' => $s_hidden_fields,
		));

		print_page('rules_edit_body.tpl', 'admin');

	break;

	case 'save':

		$rules_id = (isset($mode_id)) ? $mode_id : 0;

		$active = ( $_POST['active'] == 1 ) ? TRUE : 0;
    	$text = ( isset($_POST['text']) ) ? trim($_POST['text']) : "";
    	$title = ( isset($_POST['title']) ) ? trim($_POST['title']) : "";

    	if( !empty($rules_id) )
    	{
			$sql = "UPDATE " . BB_RULES . "
				SET   title = '" . DB()->escape(prepare_message($title)) . "',
				text = '" . DB()->escape($text) . "', active = $active WHERE id = '$rules_id'";
			$message = $lang['RULES_UPDATED'];
		}else{
			$sql = "INSERT INTO " . BB_RULES . " (title, text, active)
				VALUES ('" . DB()->escape(prepare_message($title)) . "', '" . DB()->escape($text) . "', '$active')";
			$message = $lang['RULES_ADDED'];
		}

		if( !$result = DB()->query($sql) )
		{
			bb_die("Не удалось обновить или добавить новость");
		}
		CACHE('bb_cache')->rm('rules');
		$message .= "<br /><br />" . sprintf($lang['CLICK_RETURN_RULESADMIN'], "<a href=\"admin_rules.php\">", "</a>") . "<br /><br />" . sprintf($lang['CLICK_RETURN_ADMIN_INDEX'], "<a href=\"index.php?pane=right\">", "</a>");
		bb_die($message);

		print_page('rules_edit_body.tpl', 'admin');

	break;

	case 'delete':

		if($mode_id)
    	{
			$sql = "DELETE FROM " . BB_RULES . "
				WHERE id = '$mode_id'";

			if( !$result = DB()->query($sql) )
			{
				bb_die("не не то?");
			}
			CACHE('bb_cache')->rm('rules');
			$message = $lang['RULES_REMOVED'] . "<br /><br />" . sprintf($lang['CLICK_RETURN_RULESADMIN'], "<a href=\"admin_rules.php\">", "</a>") . "<br /><br />" . sprintf($lang['CLICK_RETURN_ADMIN_INDEX'], "<a href=\"index.php?pane=right\">", "</a>");
			bb_die($message);
		}else{
			bb_die($lang['MISSING_RULES_ID']);
		}

	break;

	default:

		$sql = "SELECT * FROM " . BB_RULES ." ORDER BY id";

		if( !$result = DB()->sql_query($sql) )
		{
			bb_die("Пусто");
		}

		$rules_count = DB()->num_rows($result);
		$rules_rows = DB()->sql_fetchrowset($result);

		$template->assign_vars(array(
			'U_RULES_ADD'=> 'admin_rules.php?mode=add',
			'S_RULES_ACTION' => 'admin_rules.php',
		));

		for($i = 0; $i < $rules_count; $i++)
		{
			$rules_text = $rules_rows[$i]['text'];
			$rules_id = $rules_rows[$i]['id'];
			$row_class = ( !($i % 2) ) ? 'row1' : 'row2';
			$rules_is_active = ( $rules_rows[$i]['active'] ) ? $lang['YES'] : $lang['NO'];
			$orig_word = $replacement_word = array();
		    obtain_word_list($orig_word, $replacement_word);

		    $title = str_replace('\"', '"', substr(@preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "@preg_replace(\$orig_word, \$replacement_word, '\\0')", '>' . $rules_rows[$i]['title'] . '<'), 1, -1));

		    $template->assign_block_vars("rules", array(
		      'ROW_CLASS' => $row_class,
	 	     'RULES_TITLE' => $title,
		      'RULES_ACTIVE' => $rules_is_active,
		      'RULES_TEXT' => $rules_text,
		      'U_RULES_EDIT' => 'admin_rules.php?mode=edit&amp;id='.$rules_id,
		      'U_RULES_DELETE' => 'admin_rules.php?mode=delete&amp;id='.$rules_id,
			));
	  	}
	  	print_page('rules_list_body.tpl', 'admin');
}
