<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: index.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資料
function list_tad_player($pcsn=""){
	global $xoopsDB,$xoopsModule;
	
	
	$cate=get_tad_player_cate_all();
	
	$MDIR=$xoopsModule->getVar('dirname');
	$sql = "select * from ".$xoopsDB->prefix("tad_player")." where pcsn='{$pcsn}' order by sort";

	//PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$total=$xoopsDB->getRowsNum($result);

	$navbar = new PageBar($total, 20, 10);
	$mybar = $navbar->makeBar();
	$bar= sprintf(_BP_TOOLBAR,$mybar['total'],$mybar['current'])."{$mybar['left']}{$mybar['center']}{$mybar['right']}";
	$sql.=$mybar['sql'];

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


	$data="
	<script>
	function delete_tad_player_func(psn){
		var sure = window.confirm('"._BP_DEL_CHK."');
		if (!sure)	return;
		location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_player&psn=\" + psn;
	}
	</script>
	<form action='index.php' method='post'>
	<table id='tbl'>
	<tr>
	<th>"._MA_TADPLAYER_IMAGE."</th>
	<th>"._MA_TADPLAYER_SORT."</th>
	<th>"._MA_TADPLAYER_TITLE."</th></tr>
	<tbody>";
	while(list($psn,$pcsn,$title,$creator,$location,$image,$info,$uid,$post_date,$enable_group,$counter,$width, 	$height,$sort)=$xoopsDB->fetchRow($result)){
	
		if(substr($image,0,4)=='http'){
    	$image = basename($image);
		}
	
		if(empty($image) or !file_exists(_TAD_PLAYER_IMG_DIR."s_{$psn}.png")){
			$ext=substr($location,-3);
	    if($ext=="mp3"){
	      $pic="mp3.png";
			}else{
	      $pic="flv.png";
			}
			$pic="<img src='../images/$pic' alt='No Pic' style='width:120px;border:1px solid gray;'>";
		}else{
      $pic="<img src='"._TAD_PLAYER_IMG_URL."s_{$psn}.png' alt='{$title}' style='width:120px;border:1px solid gray;'>";
		}

    if(substr($post_date,0,2)=='20')$post_date=strtotime($post_date);
		$post_date=date("Y-m-d H:i:s",xoops_getUserTimestamp($post_date));
		
		$data.="
		<tr>
		<td rowspan=3><a href='../play.php?psn={$psn}'>{$pic}</a></td>
		<td><input type='text' name='sort[$psn]' size='3' value='{$sort}'></td>
		<td><span style='float:right;'><a href='upload.php?op=tad_player_form&psn=$psn'><img src='".XOOPS_URL."/modules/{$MDIR}/images/edit.gif' alt='"._BP_EDIT."'></a>
		<a href=\"javascript:delete_tad_player_func($psn);\"><img src='".XOOPS_URL."/modules/{$MDIR}/images/del.gif' alt='"._BP_DEL."'></a></span>
		<a href='../play.php?psn={$psn}' style='font-size:16px;'>{$title}</a></td></tr>
		<tr><td>"._MA_TADPLAYER_LOCATION."</td><td>[{$psn}] {$location} ("._MA_TADPLAYER_COUNTER." {$counter} )</td></tr>
		<tr><td>"._MA_TADPLAYER_CREATOR."</td><td>{$creator} ({$post_date})</td></tr>

		";
	}
	$data.="
	<tr><td><input type='hidden' name='op' value='save_sort'>
	<input type='hidden' name='pcsn' value='{$_GET['pcsn']}'>
	<input type='submit' value='"._MA_SAVE."'></td>
	<td colspan=8 class='bar'>{$bar}</td></tr>
	</tbody>
	</table>
	</form>";
	$data="<table style='width:auto;'><tr><td valign='top'>".cate_select($_GET['pcsn'])."</td><td valign='top'>".div_3d("",$data,"corners")."</td></tr></table>";
	return $data;
}

//刪除tad_player某筆資料資料
function delete_tad_player($psn=""){
	global $xoopsDB;
	//刪除檔案
	$file=get_tad_player($psn);
	unlink(_TAD_PLAYER_FLV_DIR."{$psn}_{$file['location']}");
	unlink(_TAD_PLAYER_IMG_DIR."s_{$psn}.png");
	unlink(_TAD_PLAYER_IMG_DIR."{$psn}_{$file['image']}");
	mk_list_xml($file['pcsn']);
	$sql = "delete from ".$xoopsDB->prefix("tad_player")." where psn='$psn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

}


//重新產生所有的XML
function mk_all_xml(){
	global $xoopsDB;
	$sql = "select pcsn,title from ".$xoopsDB->prefix("tad_player_cate");
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  $log="";
	while(list($pcsn,$title)=$xoopsDB->fetchRow($result)){
		mk_list_xml($pcsn);
		$log.=sprintf(_MA_TADPLAYER_XML_OK,$title)."<br>";
	}
	redirect_header($_SERVER['PHP_SELF'],3, $log);
	return;
}

//儲存排序
function save_sort(){
	global $xoopsDB;
	foreach($_POST['sort'] as $psn => $sort){
		$sql = "update  ".$xoopsDB->prefix("tad_player")." set sort='{$sort}' where psn='{$psn}'";
		$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	}
	return;

}


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

switch($op){

	//儲存排序
	case "save_sort";
	save_sort();
	header("location: {$_SERVER['PHP_SELF']}?pcsn=$pcsn");
	break;

	//重新產生所有的XML
	case "mk_all_xml";
	$main=mk_all_xml();
	break;

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

	//預設動作
	default:
	$main=list_tad_player($pcsn);
	break;

}

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

?>
