<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: batch_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_batch_upload_form(){
	global $xoopsDB,$xoopsModuleConfig;

	$cate_select=get_tad_player_cate_option(0,0,$pcsn,1,false);
	$i=0;

  if ($dh = opendir(_TAD_PLAYER_BATCH_UPLOAD_DIR)) {
    while (($file = readdir($dh)) !== false) {
      if(strlen($file)<=2)continue;
			$f=explode(".",$file);
			$filename=$f[0];
      $ext=strtolower($f[1]);
      
      if($ext=="flv" or $ext=="mp4" or $ext=="m4v" or $ext=="mp3" or $ext=="swf"){
        $flv_arr['flv'][$filename]=$file;
			}elseif($ext=="jpg" or $ext=="jpeg" or $ext=="gif" or $ext=="png"){
        $flv_arr['img'][$filename]=$file;
			}
    }
    closedir($dh);
    
    foreach($flv_arr['flv'] as $filename=>$file ){
      if(!empty($flv_arr['img'][$filename])){
				$image=$flv_arr['img'][$filename];
				$image_form="<input type='hidden' name='img[$filename]' value='{$image}'>";
			}else{
        $image=$image_form="";
			}
    
			$tr.="<tr>
			<td class='title'><input type='checkbox' name='flv[$filename]' value='{$file}' checked>{$title}</td>
			<td class='col'>$file</td>
			<td class='col'>{$image}{$image_form}</td>
			</tr>\n";
		}
  }



	$main="
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl'>
  <tr><td class='title'>"._MA_TADPLAYER_OF_CSN."</td>
	<td class='col' colspan=2><select name='pcsn' size=1>
	$cate_select
	</select>"._MA_TADPLAYER_NEW_PCSN."<input type='text' name='new_pcsn' size='10'></td></tr>
	$tr
  <input type='hidden' name='op' value='import'>
  <tr><td colspan=3 class='bar'><input type='submit' value='"._MA_SAVE."'></td></tr>
  </table>
  </form>";

	$main=div_3d(_MA_INPUT_FORM,$main);

	return $main;
}


//批次匯入
function tad_player_batch_import(){
	global $xoopsDB,$xoopsUser,$xoopsModuleConfig;
	
	if(!empty($_POST['new_pcsn'])){
    $pcsn=add_tad_player_cate();
	}else{
		$pcsn=$_POST['pcsn'];
	}
	
	$uid=$xoopsUser->getVar('uid');
  $uid_name=XoopsUser::getUnameFromId($uid,1);
  //$now=xoops_getUserTimestamp(time());
  
	foreach($_POST['flv'] as $filename => $flv){
	  if(empty($flv))continue;
		$sql = "insert into ".$xoopsDB->prefix("tad_player")." (pcsn,title,creator,location,image,info,uid,post_date,enable_group,counter,width,height) values('{$pcsn}','{$flv}','{$uid_name}','{$flv}','','{$flv}','{$uid}',now(),'','0','{$xoopsModuleConfig['width']}','{$xoopsModuleConfig['height']}')";
		$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
		//取得最後新增資料的流水編號
		$psn=$xoopsDB->getInsertId();
		
		$sql = "update ".$xoopsDB->prefix("tad_player")." set image='{$psn}.png' where psn='$psn'";
		$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

		set_time_limit(0);
		ini_set('memory_limit', '50M');
		if(rename(_TAD_PLAYER_BATCH_UPLOAD_DIR.$flv,_TAD_PLAYER_FLV_DIR.$psn."_".$flv)){
		  if(!empty($_POST['img'][$filename])){
				$pic_file=_TAD_PLAYER_BATCH_UPLOAD_DIR.$_POST['img'][$filename];
				$pic_b_file=_TAD_PLAYER_IMG_DIR.$psn.".png";
				$pic_s_file=_TAD_PLAYER_IMG_DIR."s_".$psn.".png";
				
				$sub=strtolower(substr($_POST['img'][$filename],-3));
				if($sub=="gif"){
          $type="image/gif";
				}elseif($sub=="png"){
          $type="image/png";
				}elseif($sub=="jpg" or $sub=="peg" ){
          $type="image/jpeg";
				}
        thumbnail($pic_file,$pic_b_file,$type,$xoopsModuleConfig['width']);
        thumbnail($pic_file,$pic_s_file,$type,"120");

		  	unlink($pic_file);
		  }
		}
	}
	return $pcsn;
}




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

switch($op){
	case "import":
	$pcsn=tad_player_batch_import();
	mk_list_xml($pcsn);
	header("location:index.php?pcsn=$pcsn");
	break;
	
	default:
	$main=tad_player_batch_upload_form();
	break;

}

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

?>
