<?php
define('THIS_SCRIPT', 'forum_help');
include_once 'global.php';
$lang = load_lang($lang,'forum_help');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'4,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$action,$info_list,$page,$total,$page_size,$offset,$page_count,$id,$back_url,$keyword,$linkaction;
	$back_url="javascript:void(window.history.back())";
	switch ($action){
		case "search":
			search();
		break;
		case "add":
			add();
		break;
		case "edit":
			edit();
		break;
		case "view":
			view();
		break;
		case "del":
			del();
		break;
		case "editok":
			editok();
		break;
		default:
			help_info();
		break;
	}
include printout('forum_help');
}
function help_info(){
	global $dv,$db,$lang,$info_list,$page,$total,$page_size,$offset,$page_count;
	$info_list=array();
	$stmt="select h_id,h_parentid,h_title,h_content,h_type,h_stype,h_bgimg,h_addtime from {$dv}help order by h_id desc";
	$total=intval($db->scalar("select count(1) from {$dv}help"));
	page_info(10);
	if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
			
				$info_list[]=$arr;
			}
	}
$db->free_result($result);
}
function search(){
	global $dv,$db,$lang,$action,$keyword,$info_list,$page,$total,$page_size,$offset,$page_count,$back_url;
	$info_list=array();
	$keyword=$_POST['keyword'];
	if($keyword==''){
		$keyword=$_GET['keyword'];
	}
	$keyword=str_replace(" ","",$keyword);
	if($keyword!=""){
		$stmt="SELECT h_id,h_parentid,h_title,h_content,h_type,h_stype,h_bgimg,h_addtime,h_fileurl from {$dv}help where (h_title like '%$keyword%' or h_content like '%$keyword%') and h_id!=1 order by h_id desc";
		$total=intval($db->scalar("select count(1) from {$dv}help where (h_title like '%$keyword%' or h_content like '%$keyword%') and h_id!=1"));//得到总记录;
	}
	else{
		$stmt="SELECT h_id,h_parentid,h_title,h_content,h_type,h_stype,h_bgimg,h_addtime,h_fileurl from {$dv}help where h_id!=1 order by h_id desc";
		$total=intval($db->scalar("select count(1) from {$dv}help where h_id!=1"));//得到总记录;
	}
	if($total<=0){
		show_msg($lang['sys.info'],$lang['error.info'],0,'','',$back_url,$lang['cmdback']);
		exit;	
	}
	if($total==1){
		$result_set=$db->query($stmt);
		$listarr=$db->fetch_array($result_set);
		if($listarr['h_fileurl']!=''){
			header("Location:".$listarr['h_fileurl']);
			exit;			
		}
		else{
			page_info(10);
			if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
				while ($arr=$db->fetch_array($result)){
					$info_list[]=$arr;
				}
			}			
		}
	}
	else{
		page_info(10);
		if($result=$db->query($stmt,array('absolutePage'=>$offset,'pageSize'=>$page_size))){
			while ($arr=$db->fetch_array($result)){
				$info_list[]=$arr;
			}
		}
	}
$db->free_result($result);
}
function add(){
	global $db,$lang,$dv,$title,$body,$type,$sort,$back_url;
	if($title==""){
		show_msg($lang['sys.info'],$lang['add.info3'],0,'','',$back_url,$lang['cmdback']);
		exit;		
	}
	if($type==1){
		$type=1;
	}
	else{
		$type=0;
	}
	$now=strtotime("now");
	$stmt="insert into {$dv}help set h_title='$title',h_parentid='$sort',h_type='0',h_stype='$type',h_content='$body',h_addtime='$now'";
	$result=$db->query($stmt);
	if($result){
		show_msg($lang['sys.info'],$lang['add.info1'],1,'','',$back_url,$lang['cmdback']);
	exit;	
	}
	else{
		show_msg($lang['sys.info'],$lang['add.info2'],0,'','',$back_url,$lang['cmdback']);
	exit;	
	}
}
function edit(){
	global $dv,$db,$lang,$id,$info_list,$id;
	$info_list=array();
	$result=$db->query("SELECT h_id,h_parentid,h_title,h_content,h_type,h_stype,h_bgimg,h_addtime from {$dv}help where h_id='".intval($id)."'");
	while($arr=$db->fetch_array($result)){
		$info_list[]=$arr;
	}
$db->free_result($result);
}
function view(){
	global $dv,$db,$lang,$id,$info_list,$id;
	$info_list=array();
	$result=$db->query("SELECT h_id,h_parentid,h_title,h_content,h_type,h_stype,h_bgimg,h_addtime from {$dv}help where h_id='".intval($id)."'");
	while($arr=$db->fetch_array($result)){
		$info_list[]=$arr;
	}
$db->free_result($result);
}
function del(){
	global $dv,$db,$lang,$id,$back_url;
	if($result=$db->query("delete from {$dv}help where h_id='".intval($id)."'")){
		show_msg($lang['sys.info'],$lang['del.info1'],1,'','','forum_help.php',$lang['cmdback']);
	exit;	
	}
	else{
		show_msg($lang['sys.info'],$lang['del.info2'],0,'','',$back_url,$lang['cmdback']);
	exit;	
	}
	
}

function editok(){
	global $dv,$db,$lang,$action,$id,$title,$body,$type,$sort,$title_type,$bgurl,$back_url;
	if($title==""){
		show_msg($lang['sys.info'],$lang['add.info3'],0,'','',$back_url,$lang['cmdback']);
	exit;		
	}
	if($type==1){
		$type=1;
	}
	else{
		$type=0;
	}
	$now=strtotime('now');
	$stmt="update {$dv}help set h_title='$title',h_stype='$type',h_type='$title_type',h_parentid='$sort',h_content='$body',h_bgimg='$bgurl',h_addtime='$now' where h_id='".intval($id)."'";
	$result=$db->query($stmt);
	if($result){
		show_msg($lang['sys.info'],$lang['edit.info6'],1,'','','forum_help.php',$lang['cmdback']);
	exit;	
	}
	else{
		show_msg($lang['sys.info'],$lang['edit.info7'],0,'','','forum_help.php',$lang['cmdback']);
	exit;	
	}	
}
?>