<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: upload.php,v 1.1 2008/05/05 03:23:49 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 tad_player_form($psn=""){
	global $xoopsDB,$xoopsModuleConfig;
	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
	include_once(XOOPS_ROOT_PATH."/class/xoopseditor/xoopseditor.php");

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

	//預設值設定

	$psn=(!isset($DBV['psn']))?"":$DBV['psn'];
	$pcsn=(!isset($DBV['pcsn']))?"":$DBV['pcsn'];
	$title=(!isset($DBV['title']))?"":$DBV['title'];
	$creator=(!isset($DBV['creator']))?"":$DBV['creator'];
	$location=(!isset($DBV['location']))?"":$DBV['location'];
	$image=(!isset($DBV['image']))?"":$DBV['image'];
	$info=(!isset($DBV['info']))?"":$DBV['info'];
	$uid=(!isset($DBV['uid']))?"":$DBV['uid'];
	$post_date=(!isset($DBV['post_date']))?"":$DBV['post_date'];
	$enable_group=(!isset($DBV['enable_group']))?"":explode(",",$DBV['enable_group']);
	$counter=(!isset($DBV['counter']))?"":$DBV['counter'];
	$width=(!isset($DBV['width']))?$xoopsModuleConfig['width']:$DBV['width'];
	$height=(!isset($DBV['height']))?$xoopsModuleConfig['height']:$DBV['height'];
	$content=(!isset($DBV['content']))?"":$DBV['content'];
	$youtube=(!isset($DBV['youtube']))?"":$DBV['youtube'];
	
	include(XOOPS_ROOT_PATH."/modules/tad_player/class/fckeditor/fckeditor.php") ;
	$oFCKeditor = new FCKeditor('content') ;
	$oFCKeditor->BasePath	= XOOPS_URL."/modules/tad_player/class/fckeditor/" ;
	$oFCKeditor->Config['AutoDetectLanguage']=false;
	$oFCKeditor->Config['DefaultLanguage']		= 'zh' ;
	$oFCKeditor->ToolbarSet ='my';
	$oFCKeditor->Width = '100%' ;
	$oFCKeditor->Height = '200' ;
	$oFCKeditor->Value =$content;
	$editor=$oFCKeditor->CreateHtml() ;

  $cate_select=get_tad_player_cate_option(0,0,$pcsn,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();

	$op=(empty($psn))?"insert_tad_player":"update_tad_player";
	//$op="replace_tad_player";
	
  if(empty($location) and !empty($youtube)){
    $hide="$('#flv_local').hide();
		$('#flv_link').hide();";
		$selected_youtube="selected";
	}elseif(substr($location,0,4)=='http'){
    $hide="$('#flv_youtube').hide();
		$('#flv_local').hide();";
		$selected_link="selected";
	}else{
    $hide="$('#flv_youtube').hide();
		$('#flv_link').hide();";
		$selected_local="selected";
	}

  if(substr($image,0,4)=='http'){
    $hide_img="$('#img_local').hide();";
		$selected_img_link="selected";
	}else{
    $hide_img="$('#img_link').hide();";
		$selected_img_local="selected";
	}
	
	
	$main="
	<script type='text/javascript' src='".XOOPS_URL."/modules/tad_player/class/jquery.js'></script>
	<script type=\"text/javascript\">


	$(document).ready(function() {

		$hide
		$hide_img
		
		$('#flv_where').change(function() {
			if ($('#flv_where').val()=='youtube') {
	       $('#flv_youtube').slideDown();
	       $('#flv_local').slideUp();
	       $('#flv_link').slideUp();
			} else if ($('#flv_where').val()=='link'){
	       $('#flv_link').slideDown();
	       $('#flv_local').slideUp();
	       $('#flv_youtube').slideUp();
			}else{
	       $('#flv_local').fadeIn();
	       $('#flv_link').slideUp();
	       $('#flv_youtube').slideUp();
			}
		});
		

		$('#img_where').change(function() {
			if ($('#img_where').val()=='link'){
	       $('#img_link').slideDown();
	       $('#img_local').hide();
			}else{
	       $('#img_local').slideDown();
	       $('#img_link').hide();
			}
		});

	});
	</script>
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl' style='width:640px;'>
	<input type='hidden' name='psn' value='{$psn}'>
	<tr><td class='title'>"._MA_TADPLAYER_OF_CSN."</td>
	<td class='col'><select name='pcsn' size=1>
	$cate_select
	</select>
	"._MA_TADPLAYER_NEW_PCSN."<input type='text' name='new_pcsn' size='10'></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'>
	<select id='flv_where'>
	<option value='local' $selected_local>"._MA_TADPLAYER_UPLOAD_FLV."</option>
	<option value='link' $selected_link>"._MA_TADPLAYER_LINK_FLV."</option>
	<option value='youtube' $selected_youtube>YouTube</option>
	</select>
	"._MA_TADPLAYER_LOCATION."</td>
	<td class='col'>
		<div id='flv_local'><input type='file' name='location' size='30' value='{$location}'></div>
		<div id='flv_link'><input type='text' name='location' size='50' value='{$location}'> "._MA_TADPLAYER_FLV_LINK."</div>
		<div id='flv_youtube'><input type='text' name='youtube' size='50' value='{$youtube}'> "._MA_TADPLAYER_YOUTUBE_LINK."</div>
	</td></tr>
	<tr><td class='title'>
	<select id='img_where'>
	<option value='local' $selected_img_local>"._MA_TADPLAYER_UPLOAD_FLV."</option>
	<option value='link' $selected_img_link>"._MA_TADPLAYER_LINK_FLV."</option>
	</select>
	"._MA_TADPLAYER_IMAGE."</td>
	<td class='col'>
	<div id='img_local'><input type='file' name='image' size='30' value='{$image}'></div>
	<div id='img_link'><input type='text' name='image' size='50' value='{$image}'> "._MA_TADPLAYER_IMG_LINK."</div>
	</td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_CREATOR."</td>
	<td class='col'><input type='text' name='creator' size='30' value='{$creator}'></td></tr>
	<tr><td class='col' colspan='2'>$editor</td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_ENABLE_GROUP."</td>
	<td class='col'>$enable_group</td></tr>
	<tr><td class='title'>"._MA_TADPLAYER_FLV_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_FLV_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_FORM,$main);

	return $main;
}

//新增資料到tad_player中
function insert_tad_player(){
	global $xoopsDB,$xoopsUser;
	if(!empty($_POST['new_pcsn'])){
    $pcsn=add_tad_player_cate();
	}else{
		$pcsn=$_POST['pcsn'];
	}

	$uid=$xoopsUser->getVar('uid');
	$enable_group=implode(",",$_POST['enable_group']);
	
	//$now=xoops_getUserTimestamp(time());

	if(empty($_FILES['location']['name']) and !empty($_POST['location'])){
    $location=$_POST['location'];
	}else{
		$location=$_FILES['location']['name'];
	}
	

	if(!empty($_POST['image'])){
    $image=$_POST['image'];
	}else{
		$image=$_FILES['image']['name'];
	}

	if(!empty($_POST['title'])){
    $title=$_POST['title'];
	}else{
		$title=basename($location);
	}

	$sql = "insert into ".$xoopsDB->prefix("tad_player")." (pcsn,title,creator,location,image,info,uid,post_date,enable_group,counter,width,height,content,youtube) values('{$pcsn}','{$title}','{$_POST['creator']}','{$location}','{$image}','{$_FILES['location']['name']}','{$uid}',now(),'{$enable_group}','0','{$_POST['width']}','{$_POST['height']}','{$_POST['content']}','{$_POST['youtube']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	//取得最後新增資料的流水編號
	$psn=$xoopsDB->getInsertId();

	//上傳影片
	if(!empty($_FILES['location']['name'])){
		upload_flv($psn);
	}
	
	//上傳圖檔
  if(!empty($_FILES['image']['name'])){
  	upload_pic($psn,$_POST['width']);
 	}elseif(!empty($_POST['image'])){
 		$filename = basename($_POST['image']);
 		$type=getimagesize ($_POST['image']);
 		$pic_s_file=_TAD_PLAYER_IMG_DIR."s_".$psn.".png";
    thumbnail($_POST['image'],$pic_s_file,$type['mime'],"120");
	}
 	
  mk_list_xml($pcsn);

	return $psn;
}

//列出所有tad_player資料
function list_tad_player($show_function=1){
	global $xoopsDB,$xoopsModule;
	$MDIR=$xoopsModule->getVar('dirname');
	$sql = "select * from ".$xoopsDB->prefix("tad_player")."";

	//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());

	$function_title=($show_function)?"<th>"._BP_FUNCTION."</th>":"";

	$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>
	<table id='tbl'>
	<tr><th>"._MA_TADPLAYER_LSN."</th>
	<th>"._MA_TADPLAYER_SN."</th>
	<th>"._MA_TADPLAYER_PSN."</th>
	<th>"._MA_TADPLAYER_CSN."</th>
	<th>"._MA_TADPLAYER_OF_LSN."</th>
	<th>"._MA_TADPLAYER_PCSN."</th>
	<th>"._MA_TADPLAYER_TITLE."</th>
	<th>"._MA_TADPLAYER_CREATOR."</th>
	<th>"._MA_TADPLAYER_LOCATION."</th>
	<th>"._MA_TADPLAYER_IMAGE."</th>
	<th>"._MA_TADPLAYER_INFO."</th>
	<th>"._MA_TADPLAYER_UID."</th>
	<th>"._MA_TADPLAYER_POST_DATE."</th>
	<th>"._MA_TADPLAYER_ENABLE_GROUP."</th>
	<th>"._MA_TADPLAYER_COUNTER."</th>
	$function_title</tr>
	<tbody>";
	while(list($psn,$pcsn,$title,$creator,$location,$image,$info,$uid,$post_date,$enable_group,$counter)=$xoopsDB->fetchRow($result)){
		$fun=($show_function)?"<td>
		<a href='{$_SERVER['PHP_SELF']}?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></td>":"";

    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 bgcolor='white'>
		<td>{$psn}</td>
		<td>{$pcsn}</td>
		<td>{$title}</td>
		<td>{$creator}</td>
		<td>{$location}</td>
		<td>{$image}</td>
		<td>{$info}</td>
		<td>{$uid}</td>
		<td>{$post_date}</td>
		<td>{$enable_group}</td>
		<td>{$counter}</td>
		$fun</tr>";
	}
	$data.="
	<tr>
	<td colspan=16 class='bar'>
	<a href='{$_SERVER['PHP_SELF']}?op=tad_player_form'><img src='".XOOPS_URL."/modules/{$MDIR}/images/add.gif' alt='"._BP_ADD."' align='right'></a>
	{$bar}</td></tr>
	</tbody>
	</table>";
	return $data;
}




//更新tad_player某一筆資料
function update_tad_player($psn=""){
	global $xoopsDB;

	//上傳影片
	if(!empty($_FILES['location']['name'])){
		upload_flv($psn,true);
	}

  //上傳圖檔
  if(!empty($_FILES['image']['name'])){
  	upload_pic($psn,$_POST['width'],true);
  	$image_sql="";
 	}elseif(!empty($_POST['image'])){
 		$filename = basename($_POST['image']);
 		$type=getimagesize ($_POST['image']);
 		$pic_s_file=_TAD_PLAYER_IMG_DIR."s_".$psn.".png";
    thumbnail($_POST['image'],$pic_s_file,$type['mime'],"120");
    $image_sql=", image = '{$_POST['image']}'";
	}
	if(!empty($_POST['location'])){
    $location_sql=", location = '{$_POST['location']}', youtube=''";
	}elseif(!empty($_POST['youtube'])){
		$location_sql=", location = '', youtube='{$_POST['youtube']}'";
	}else{
    $location_sql="";
	}

	if(!empty($_POST['title'])){
    $title=$_POST['title'];
	}else{
		$title=basename($_POST['location']);
	}
	
 	
	$enable_group=implode(",",$_POST['enable_group']);
	
	//$now=xoops_getUserTimestamp(time());
	
	$sql = "update ".$xoopsDB->prefix("tad_player")." set  pcsn = '{$_POST['pcsn']}', title = '{$title}', creator = '{$_POST['creator']}' {$location_sql} {$image_sql}, post_date = now(), enable_group = '{$enable_group}', width = '{$_POST['width']}', height = '{$_POST['height']}' , content = '{$_POST['content']}' where psn='$psn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	
	mk_list_xml($_POST['pcsn']);
	return $psn;
}


//上傳影片
function upload_flv($psn="",$update_sql=false){
	global $xoopsDB;
  include_once "../class/upload/class.upload.php";
	set_time_limit(0);
  ini_set('memory_limit', '50M');
	$flv_handle = new upload($_FILES['location'],"zh_TW");
	if ($flv_handle->uploaded) {
	    $name=substr($_FILES['location']['name'],0,-4);
      $flv_handle->file_safe_name = false;
      $flv_handle->auto_create_dir = true;
      $flv_handle->file_new_name_body   = "{$psn}_{$name}";
      $flv_handle->process(_TAD_PLAYER_FLV_DIR);
      if ($flv_handle->processed) {
          $flv_handle->clean();
          if($update_sql){
	          $sql = "update ".$xoopsDB->prefix("tad_player")." set image='{$_FILES['location']['name']}' where psn='$psn'";
						$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
          }
					return true;
      } else {
          $sql = "delete from ".$xoopsDB->prefix("tad_player")." where psn='{$psn}'";
					$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
					redirect_header($_SERVER['PHP_SELF'],3, "Error:".$flv_handle->error);
	    }
   }
}


//上傳圖檔
function upload_pic($psn="",$width=480,$update_sql=false){
	global $xoopsDB;
  include_once "../class/upload/class.upload.php";
  set_time_limit(0);
  ini_set('memory_limit', '50M');
  
  if(file_exists(_TAD_PLAYER_IMG_DIR."/{$psn}.png")){
		unlink(_TAD_PLAYER_IMG_DIR."/{$psn}.png");
	}

	if(file_exists(_TAD_PLAYER_IMG_DIR."/{$psn}_s.png")){
		unlink(_TAD_PLAYER_IMG_DIR."/{$psn}_s.png");
	}
  
  $img_handle = new upload($_FILES['image'],"zh_TW");
  if ($img_handle->uploaded) {
	    //$name=substr($_FILES['image']['name'],0,-4);
      $img_handle->file_safe_name = false;
      $img_handle->file_new_name_body   = "{$psn}";
      $img_handle->image_convert = 'png';
      $img_handle->image_resize         = true;
      $img_handle->image_x              = $width;
      $img_handle->image_ratio_y        = true;
      $img_handle->process(_TAD_PLAYER_IMG_DIR);

      //製作縮圖
      $img_handle->file_safe_name = false;
      $img_handle->file_new_name_body   = "s_{$psn}";
      $img_handle->image_convert = 'png';
      $img_handle->image_resize         = true;
      $img_handle->image_x              = 120;
      $img_handle->image_ratio_y        = true;
      $img_handle->process(_TAD_PLAYER_IMG_DIR);
      $img_handle->auto_create_dir = true;
      if ($img_handle->processed) {
          if($update_sql){
	          $sql = "update ".$xoopsDB->prefix("tad_player")." set `image`='{$psn}.png' where `psn`='$psn'";
	          $result=$xoopsDB->queryF($sql) or die(mysql_error());

          }

          $img_handle->clean();
					return true;
      } else {
					redirect_header($_SERVER['PHP_SELF'],3, "Error:".$img_handle->error);
      }
  }
}
/*-----------執行動作判斷區----------*/
$op = (!isset($_REQUEST['op']))? "main":$_REQUEST['op'];
$psn = (!isset($_REQUEST['psn']))? "":$_REQUEST['psn'];
$pcsn = (!isset($_REQUEST['pcsn']))? "":$_REQUEST['pcsn'];

switch($op){

	//新增資料
	case "insert_tad_player":
	insert_tad_player();
	header("location: index.php?pcsn=$pcsn");
	break;

	//輸入表格
	case "tad_player_form";
	$main=tad_player_form($psn);
	break;


	//更新資料
	case "update_tad_player";
	update_tad_player($psn);
	header("location: index.php?pcsn=$pcsn");
	break;
	

	default:
	$main=tad_player_form($psn);
	break;

}

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

?>
