<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: cate.php,v 1.1 2008/04/03 01:35:03 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "../../../include/cp_header.php";
include "../function.php";

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

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

	//預設值設定

	$csn=(!isset($DBV['csn']))?"":$DBV['csn'];
	$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']);
	$enable_upload_group=(!isset($DBV['enable_upload_group']))?array('1'):explode(",",$DBV['enable_upload_group']);
	$sort=(!isset($DBV['sort']))?"":$DBV['sort'];
	$passwd=(!isset($DBV['passwd']))?"":$DBV['passwd'];

	$op=(empty($csn))?"insert_tad_gallery_cate":"update_tad_gallery_cate";

	$cate_select=get_tad_gallery_cate_option(0,0,$of_csn);
	
	//可見群組
	$SelectGroup_name = new XoopsFormSelectGroup("", "enable_group", false,$enable_group, 3, true);
	$SelectGroup_name->addOption("", _MA_TADGAL_ALL_OK, false);
	$enable_group = $SelectGroup_name->render();

	//可上傳群組
	$SelectGroup_name = new XoopsFormSelectGroup("", "enable_upload_group", false,$enable_upload_group, 3, true);
	$SelectGroup_name->addOption("", _MA_TADGAL_ALL_OK, false);
	$enable_upload_group = $SelectGroup_name->render();
	
	$main="
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl'>

	<tr><td class='title' nowrap>"._MA_TADGAL_SORT."</td>
	<td class='col' colspan=2><input type='text' name='sort' size='2' value='{$sort}'></td></tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_OF_CSN."</td>
	<td class='col' colspan=2><select name='of_csn' size=1>
	$cate_select
	</select></td></tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_TITLE."</td>
	<td class='col' colspan=2><input type='text' name='title' size='30' value='{$title}'></td></tr>
	<tr><td></td><td class='title'>"._MA_TADGAL_ENABLE_GROUP."</td><td class='title'>"._MA_TADGAL_ENABLE_UPLOAD_GROUP."</td>
	</tr>
	<tr><td></td>
	<td class='col'>$enable_group</td><td class='col'>$enable_upload_group</td>
	</tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_PASSWD."</td>
	<td class='col' colspan=2><input type='text' name='passwd' size='10' value='{$passwd}'>"._MA_TADGAL_PASSWD_DESC."</td></tr>
  <tr><td class='bar' colspan='2'>
	<input type='hidden' name='csn' value='{$csn}'>
  <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_gallery_cate中
function insert_tad_gallery_cate(){
	global $xoopsDB;
	if(empty($_POST['title']))return;
	$enable_group=implode(",",$_POST['enable_group']);
	$enable_upload_group=implode(",",$_POST['enable_upload_group']);
	$sql = "insert into ".$xoopsDB->prefix("tad_gallery_cate")." (of_csn,title,passwd,enable_group,enable_upload_group,sort) values('{$_POST['of_csn']}','{$_POST['title']}','{$_POST['passwd']}','{$enable_group}','{$enable_upload_group}','{$_POST['sort']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	//取得最後新增資料的流水編號
	$csn=$xoopsDB->getInsertId();
	return $csn;
}

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

 $MDIR=$xoopsModule->getVar('dirname');
	
	$sql = "select csn,of_csn,title,passwd,enable_group,enable_upload_group,sort from ".$xoopsDB->prefix("tad_gallery_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_gallery_cate_func(csn){
			var sure = window.confirm('"._BP_DEL_CHK."');
			if (!sure)	return;
			location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_gallery_cate&csn=\" + csn;
		}
		</script>
		<table id='tbl'>
		<tr>
		<th>"._MA_TADGAL_SORT."</th>
		<th>"._MA_TADGAL_TITLE."</th>
		<th>"._MA_TADGAL_PASSWD."</th>
		<th>"._MA_TADGAL_ENABLE_GROUP."</th>
		<th>"._MA_TADGAL_ENABLE_UPLOAD_GROUP."</th>
		<th>"._BP_FUNCTION."</th></tr>
		<tbody>";
	}else{
		$data="";
	}
	
	while(list($csn,$of_csn,$title,$passwd,$enable_group,$enable_upload_group,$sort)=$xoopsDB->fetchRow($result)){
		$g_txt=txt_to_group_name($enable_group,_MA_TADGAL_ALL_OK);
		$gu_txt=txt_to_group_name($enable_upload_group,_MA_TADGAL_ALL_OK);
		
		$data.="<tr>
		<td class='col'>{$sort}</td>
		<td style='padding-left: {$left}px;' class='col'>{$title}</td>
		<td class='col'>{$passwd}</td>
		<td class='col'>{$g_txt}</td>
		<td class='col'>{$gu_txt}</td>
		<td>
		<a href='{$_SERVER['PHP_SELF']}?op=tad_gallery_cate_form&csn=$csn'><img src='".XOOPS_URL."/modules/{$MDIR}/images/edit.gif' alt='"._BP_EDIT."'></a>
		<a href=\"javascript:delete_tad_gallery_cate_func($csn);\"><img src='".XOOPS_URL."/modules/{$MDIR}/images/del.gif' alt='"._BP_DEL."'></a></td></tr>";
		$data.=list_tad_gallery_cate($csn,$level);
	}
	
	if($old_level==0){
		$data.="
		</tbody>
		</table>";
	}
	
	
	
	return $data;
}





//更新tad_gallery_cate某一筆資料
function update_tad_gallery_cate($csn=""){
	global $xoopsDB;
	$enable_group=implode(",",$_POST['enable_group']);
	$enable_upload_group=implode(",",$_POST['enable_upload_group']);
	$sql = "update ".$xoopsDB->prefix("tad_gallery_cate")." set of_csn = '{$_POST['of_csn']}', title = '{$_POST['title']}', passwd = '{$_POST['passwd']}', enable_group = '{$enable_group}', enable_upload_group = '{$enable_upload_group}', sort = '{$_POST['sort']}' where csn='$csn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	return $csn;
}

//刪除tad_gallery_cate某筆資料資料
function delete_tad_gallery_cate($csn=""){
	global $xoopsDB;
	$sql = "delete from ".$xoopsDB->prefix("tad_gallery_cate")." where csn='$csn'";
	$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 "replace_tad_gallery_cate":
	replace_tad_gallery_cate();
	header("location: {$_SERVER['PHP_SELF']}");
	break;

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

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

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

	//更新資料
	case "update_tad_gallery_cate";
	update_tad_gallery_cate($_POST['csn']);
	header("location: {$_SERVER['PHP_SELF']}");
	break;
	
	//預設動作
	default:
	$main=tad_gallery_cate_form($_GET['csn']);
	$data=list_tad_gallery_cate(0);
	$main.=div_3d(_MA_TADGAL_LIST_CATE,$data,"corners","display:inline;");
	mk_dir(_TADGAL_UP_FILE_DIR);
	mk_dir(_TADGAL_UP_IMPORT_DIR);
	break;


}

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

?>