<?php
define('THIS_SCRIPT', 'plus_tools_magicface');
include_once 'global.php';
$lang = load_lang($lang, 'plus_tools_magicface' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'46,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$brow_set,$action,$doaction,$id,$page_size,$page,$total,$page_count,$offset,$p,$stype;
	switch($doaction){
		case "del":
			batch_del();
		break;
		case "edit":
			batch_edit();
		break;
		case "add":
			brow_add();
		break;
		default:
			brow_list();
		break;
	}
include printout('plus_tools_magicface');
}
function brow_list(){
	global $lang,$dv,$db,$stype,$brow_set,$page_size,$page,$total,$page_count,$offset,$p;
	$brow_set=array();
	$stype=intval($_GET['stype']);
	if(!is_numeric($stype)){
		show_msg($lang['sys.info'],$lang['brow.info30'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$stype=intval($_GET['stype']);
	}
	if($stype==5){
		$total=$db->scalar("SELECT count(1) FROM {$dv}plus_tools_magicface");
		page_info(20);
		$sql=$db->query("SELECT id,title,magicface_s,magicface_s As magicface_l,magictype,imoney,iticket,magicsetting,tmoney,tticket FROM {$dv}plus_tools_magicface order by id desc",array('absolutePage'=>$offset,'pageSize'=>$page_size));
	}
	else{
		$total=$db->scalar("SELECT count(1) FROM {$dv}plus_tools_magicface WHERE magictype=".$stype."");
		page_info(20);
		$sql=$db->query("SELECT id,title,magicface_s,magicface_s As magicface_l,magictype,imoney,iticket,magicsetting,tmoney,tticket FROM {$dv}plus_tools_magicface WHERE magictype=".$stype." order by id desc",array('absolutePage'=>$offset,'pageSize'=>$page_size));
	}
	while ($arr=$db->fetch_array($sql)){
		$brow_set[]=$arr;
	}
}
function batch_edit(){
	global $lang,$dv,$db,$id,$explain,$f_type,$pic,$money1,$ticket1,$money2,$ticket2,$forum,$money,$experience,$charm,$cachet;
	$id=$_POST['id'];
	if(count($id)==0){
		show_msg($lang['sys.info'],$lang['brow.info34'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	foreach( $id as $val){
		$explain=str_replace(" ","",$_POST["explain_".$val]);
		$f_type=intval($_POST["f_type_".$val]);
		$pic=intval($_POST["pic_".$val]);
		$money1=intval($_POST["money1_".$val]);
		$ticket1=intval($_POST["ticket1_".$val]);
		$money2=intval($_POST["money2_".$val]);
		$ticket2=intval($_POST["ticket2_".$val]);		$magic_setting=intval($_POST["forum_".$val])."|".intval($_POST["money_".$val])."|".intval($_POST["experience_".$val])."|".intval($_POST["charm_".$val])."|".intval($_POST["cachet_".$val]);
		$sql=$db->query("UPDATE {$dv}plus_tools_magicface SET title='".$explain."',magicface_s=".$pic.",magictype=".$f_type.",imoney=".$money1.",iticket=".$ticket1.",magicsetting='".$magic_setting."',tmoney=".$money2.",tticket=".$ticket2." WHERE id=".$val."");
		$re++;
	}
	if($re>0){
		show_msg($lang['sys.info'],$lang['brow.info35'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['brow.info36'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function batch_del(){
	global $lang,$dv,$db,$id;
	$id=$_POST['id'];
	if(count($id)==0){
		show_msg($lang['sys.info'],$lang['brow.info34'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$num=count($id);
	$j=0;
	for($i=0;$i<$num;$i++){
		$sql=$db->query("DELETE FROM {$dv}plus_tools_magicface WHERE id=".intval($id[$i])."");
		$j++;
	}
	if($j>0){
		show_msg($lang['sys.info'],$lang['brow.info37'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['brow.info38'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function brow_add(){
	global $lang,$dv,$db,$explain,$type,$pic,$money1,$ticket1,$money2,$ticket2,$forum,$money,$experience,$charm,$cachet;
	$explain=$_POST['explain'];
	$type=$_POST['type'];
	$pic=$_POST['pic'];
	$money1=$_POST['money1'];
	$ticket1=$_POST['ticket1'];
	$money2=$_POST['money2'];
	$ticket2=$_POST['ticket2'];
	$forum=$_POST['forum'];
	$money=$_POST['money'];
	$experience=$_POST['experience'];
	$charm=$_POST['charm'];
	$cachet=$_POST['cachet'];
	if($explain=="" or !is_numeric($type) or $type=="" or !is_numeric($pic) or $pic=="" or !is_numeric($money1) or $money1=="" or !is_numeric($ticket1) or $ticket1=="" or !is_numeric($money2) or $money2=="" or !is_numeric($ticket2) or $ticket2=="" or !is_numeric($forum) or $forum=="" or !is_numeric($money) or $money=="" or !is_numeric($experience) or $experience=="" or !is_numeric($charm) or $charm=="" or !is_numeric($cachet) or $cachet==""){
		show_msg($lang['sys.info'],$lang['brow.info39'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$explain=str_replace(" ","",$_POST['explain']);
		$type=intval($_POST['type']);
		$pic=intval($_POST['pic']);
		$money1=intval($_POST['money1']);
		$ticket1=intval($_POST['ticket1']);
		$money2=intval($_POST['money2']);
		$ticket2=intval($_POST['ticket2']);
		$forum=intval($_POST['forum']);
		$money=intval($_POST['money']);
		$experience=intval($_POST['experience']);
		$charm=intval($_POST['charm']);
		$cachet=intval($_POST['cachet']);
	}
	$magic_setting=$forum."|".$money."|".$experience."|".$charm."|".$cachet;
	$sql=$db->query("INSERT INTO {$dv}plus_tools_magicface (title,magicface_s,magictype,imoney,iticket,magicsetting,tmoney,tticket) values ('".$explain."',".$pic.",".$type.",".$money1.",".$ticket1.",'".$magic_setting."',".$money2.",".$ticket2.")");
	if($sql){
		show_msg($lang['sys.info'],$lang['brow.info40'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['brow.info41'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
?>