<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: cate.php,v 1.2 2008/05/14 01:22:58 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "../../../include/cp_header.php";
include "../function.php";
include_once XOOPS_ROOT_PATH."/Frameworks/art/functions.php";
include_once XOOPS_ROOT_PATH."/Frameworks/art/functions.admin.php";

/*-----------function區--------------*/
//tad_player_cate編輯表單
function tad_player_cate_form($pcsn=""){
	global $xoopsDB,$xoopsModuleConfig;
	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
	include_once(XOOPS_ROOT_PATH."/class/xoopseditor/xoopseditor.php");

	
	//抓取預設值
	if(!empty($pcsn)){
		$DBV=get_tad_player_cate($pcsn);
	}else{
		$DBV=array();
	}

	//預設值設定

	$pcsn=(!isset($DBV['pcsn']))?"":$DBV['pcsn'];
	$of_csn=(!isset($DBV['of_csn']))?"":$DBV['of_csn'];
	$title=(!isset($DBV['title']))?"":$DBV['title'];
	$enable_group=(!isset($DBV['enable_group']))?"":explode(",",$DBV['enable_group']);
	$sort=(!isset($DBV['sort']))?"":$DBV['sort'];
	$width=(!isset($DBV['width']))?$xoopsModuleConfig['width']:$DBV['width'];
	$height=(!isset($DBV['height']))?$xoopsModuleConfig['height']:$DBV['height'];

	$op=(empty($pcsn))?"insert_tad_player_cate":"update_tad_player_cate";

	$cate_select=get_tad_player_cate_option(0,0,$of_csn,1,false);
	
	//可見群組
	$SelectGroup_name = new XoopsFormSelectGroup("", "enable_group", false,$enable_group, 3, true);
	$SelectGroup_name->addOption("", _MA_TADPLAYER_ALL_OK, false);
	$enable_group = $SelectGroup_name->render();
	
	$main="
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl'>

	<input type='hidden' name='pcsn' value='{$pcsn}'>
	<tr><td class='title'>"._MA_TADPLAYER_OF_CSN."</td>
	<td class='col'><select name='of_csn' size=1>
	$cate_select
	</select></td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_TITLE."</td>
	<td class='col'><input type='text' name='title' size='30' value='{$title}'></td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_ENABLE_GROUP."</td>
	<td class='col'>$enable_group</td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_SORT."</td>
	<td class='col'><input type='text' name='sort' size='2' value='{$sort}'></td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_CATE_W_H."</td>
	<td class='col'><input type='text' name='width' size='2' value='{$width}'> x <input type='text' name='height' size='2' value='{$height}'>"._MA_TADPLAYER_CATE_W_H_DESC."</td></tr>
  <tr><td class='bar' colspan='2'>
  <input type='hidden' name='op' value='{$op}'>
  <input type='submit' value='"._MA_SAVE."'></td></tr>
  </table>
  </form>";

	$main=div_3d(_MA_INPUT_CATE_FORM,$main,"raised","display:inline;float:left;");

	return $main;
}

//新增資料到tad_player_cate中
function insert_tad_player_cate(){
	global $xoopsDB;
	if(empty($_POST['title']))return;
	$enable_group=implode(",",$_POST['enable_group']);
	$sql = "insert into ".$xoopsDB->prefix("tad_player_cate")." (of_csn,title,enable_group,sort,width,height) values('{$_POST['of_csn']}','{$_POST['title']}','{$enable_group}','{$_POST['sort']}','{$_POST['width']}','{$_POST['height']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	//取得最後新增資料的流水編號
	$pcsn=$xoopsDB->getInsertId();
	mk_list_xml($pcsn);
	return $pcsn;
}

//列出所有tad_player_cate資料
function list_tad_player_cate($of_csn=1,$level=0){
	global $xoopsDB,$xoopsModule;
	$old_level=$level;
	$left=$level*12+4;
	$level++;

 $MDIR=$xoopsModule->getVar('dirname');
	
	$sql = "select * from ".$xoopsDB->prefix("tad_player_cate")." where of_csn='{$of_csn}' order by sort";

	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());


	if($old_level==0){
		$data="
		<script>
		function delete_tad_player_cate_func(pcsn){
			var sure = window.confirm('"._BP_DEL_CHK."');
			if (!sure)	return;
			location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_player_cate&pcsn=\" + pcsn;
		}
		</script>
		<table id='tbl'>
		<tr>
		<th>"._MA_TADPLAYER_SORT."</th>
		<th>"._MA_TADPLAYER_TITLE."</th>
		<th>"._MA_TADPLAYER_CATE_W_H."</th>
		<th>"._MA_TADPLAYER_ENABLE_GROUP."</th>
		<th>"._BP_FUNCTION."</th></tr>
		<tbody>";
	}else{
		$data="";
	}
	
	while(list($pcsn,$of_csn,$title,$enable_group,$sort,$width,$height)=$xoopsDB->fetchRow($result)){
		$g_txt=(empty($enable_group))?_MA_TADPLAYER_ALL_OK:$enable_group;
		
		$data.="<tr>
		<td>{$sort}</td>
		<td style='padding-left: {$left}px;'>{$title}</td>
		<td>{$width} x {$height}</td>
		<td>{$g_txt}</td>
		<td>
		<a href='{$_SERVER['PHP_SELF']}?op=tad_player_cate_form&pcsn=$pcsn'><img src='".XOOPS_URL."/modules/{$MDIR}/images/edit.gif' alt='"._BP_EDIT."'></a>
		<a href=\"javascript:delete_tad_player_cate_func($pcsn);\"><img src='".XOOPS_URL."/modules/{$MDIR}/images/del.gif' alt='"._BP_DEL."'></a></td></tr>";
		$data.=list_tad_player_cate($pcsn,$level);
	}
	
	if($old_level==0){
		$data.="
		</tbody>
		</table>";
	}
	
	
	
	return $data;
}




//更新tad_player_cate某一筆資料
function update_tad_player_cate($pcsn=""){
	global $xoopsDB;
	$enable_group=implode(",",$_POST['enable_group']);
	$sql = "update ".$xoopsDB->prefix("tad_player_cate")." set  of_csn = '{$_POST['of_csn']}', title = '{$_POST['title']}', enable_group = '{$enable_group}', sort = '{$_POST['sort']}', width = '{$_POST['width']}', height = '{$_POST['height']}' where pcsn='$pcsn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	mk_list_xml($pcsn);
	$log="update $pcsn OK!";
	return $log;
}

//刪除tad_player_cate某筆資料資料
function delete_tad_player_cate($pcsn=""){
	global $xoopsDB;
	$sql = "delete from ".$xoopsDB->prefix("tad_player_cate")." where pcsn='$pcsn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	unlink(_TAD_PLAYER_UPLOAD_DIR."{$psn}_list.xml");
}


/*-----------執行動作判斷區----------*/
$op = (!isset($_REQUEST['op']))? "main":$_REQUEST['op'];

switch($op){

	//新增資料
	case "insert_tad_player_cate":
	insert_tad_player_cate();
	header("location: {$_SERVER['PHP_SELF']}");
	break;

	//輸入表格
	case "tad_player_cate_form";
	$main=tad_player_cate_form($_GET['pcsn']);
	break;

	//刪除資料
	case "delete_tad_player_cate";
	delete_tad_player_cate($_GET['pcsn']);
	header("location: {$_SERVER['PHP_SELF']}");
	break;
	

	//更新資料
	case "update_tad_player_cate";
	$log=update_tad_player_cate($_POST['pcsn']);
	redirect_header($_SERVER['PHP_SELF'],3, $log);
	break;

	//預設動作
	default:
	$main=tad_player_cate_form($_GET['pcsn']);
	$data=list_tad_player_cate(0);
	$main.=div_3d(_MA_TADPLAYER_LIST_CATE,$data,"corners","display:inline;");
	break;


}

/*-----------秀出結果區--------------*/
xoops_cp_header();
echo "<link rel='stylesheet' type='text/css' media='screen' href='../module.css' />";
loadModuleAdminMenu(2);
echo $main;
xoops_cp_footer();

?>
