<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-03-23
// $Id: uploads.php,v 1.3 2008/04/10 01:19:38 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "header.php";
$xoopsOption['template_main'] = "index_tpl.html";
/*-----------function區--------------*/

//tad_gallery編輯表單
function tad_gallery_form($sn=""){
	global $xoopsDB;

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

	//預設值設定

	$sn=(!isset($DBV['sn']))?"":$DBV['sn'];
	$csn=(!isset($DBV['csn']))?$_SESSION['tad_gallery_csn']:$DBV['csn'];
	$title=(!isset($DBV['title']))?"":$DBV['title'];
	$description=(!isset($DBV['description']))?"":$DBV['description'];
	$filename=(!isset($DBV['filename']))?"":$DBV['filename'];
	$size=(!isset($DBV['size']))?"":$DBV['size'];
	$type=(!isset($DBV['type']))?"":$DBV['type'];
	$width=(!isset($DBV['width']))?"":$DBV['width'];
	$height=(!isset($DBV['height']))?"":$DBV['height'];
	$dir=(!isset($DBV['dir']))?"":$DBV['dir'];
	$uid=(!isset($DBV['uid']))?"":$DBV['uid'];
	$post_date=(!isset($DBV['post_date']))?"":$DBV['post_date'];
	$counter=(!isset($DBV['counter']))?"":$DBV['counter'];

	$op=(empty($sn))?"insert_tad_gallery":"update_tad_gallery";
	
	
	$option=get_tad_gallery_cate_option(0,0,$csn);
	//$op="replace_tad_gallery";
	$main="
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl'>

	<input type='hidden' name='sn' value='{$sn}'>
	<tr><td class='title' nowrap>"._MA_TADGAL_CSN."</td>
	<td class='col'><select name='csn' size=1>
		$option
	</select></td>
	<td class='title' nowrap>"._MA_TADGAL_NEW_CSN."</td>
	<td class='col'><input type='text' name='new_csn' size='20'></td></tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_PHOTO."</td>
	<td class='col' colspan=3><input type='file' name='image' size='50'></td></tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_TITLE."</td>
	<td class='col' colspan=3><input type='text' name='title' size='60' value='{$title}'></td></tr>
	<tr><td class='title' nowrap>"._MA_TADGAL_DESCRIPTION."</td>
	<td class='col' colspan=3><textarea style='width: 400px; height: 44px; min-height: 44px;' name='description'></textarea></td></tr>
  <tr><td class='bar' colspan='4'>
  <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_gallery中
function insert_tad_gallery(){
	global $xoopsDB,$xoopsUser;
	if(!empty($_POST['new_csn'])){
    $csn=add_tad_gallery_cate($_POST['csn'],$_POST['new_csn']);
	}else{
		$csn=$_POST['csn'];
	}

	$uid=$xoopsUser->getVar('uid');

	if(!empty($_POST['csn']))$_SESSION['tad_gallery_csn']=$_POST['csn'];
	
	
	//處理上傳的檔案
	if(!empty($_FILES['image']['name'])){

		$myts =& MyTextSanitizer::getInstance();
		
		$pic=getimagesize($_FILES['image']['tmp_name']);
		$width=$pic[0];
		$height=$pic[1];

		include_once("class/exif.php");
		$result = read_exif_data_raw($_FILES['image']['tmp_name'],0);
		$creat_date=$result['SubIFD']['DateTimeOriginal'];
		$dir=(empty($creat_date))?date("Y_m_d"):str_replace(":","_",substr($result['SubIFD']['DateTimeOriginal'],0,10));
		$exif=(empty($result))?"":implodeArray2D("||",$result);
		$exif=$myts->addSlashes ($exif);

	 	$sql = "insert into ".$xoopsDB->prefix("tad_gallery")." (`csn`,`title`,`description`,`filename`,`size`,`type`,`width`,`height`,`dir`,`uid`,`post_date`,`counter`,`exif`) values('{$csn}','{$_POST['title']}','{$_POST['description']}','{$_FILES['image']['name']}','{$_FILES['image']['size']}','{$_FILES['image']['type']}','{$width}','{$height}','{$dir}','{$uid}',now(),'0','{$exif}')";
		$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error().$sql);
		//取得最後新增資料的流水編號
		$sn=$xoopsDB->getInsertId();
		
		mk_dir(_TADGAL_UP_FILE_DIR.$dir);
		$filename=photo_name($sn,"source",1);
		if(move_uploaded_file($_FILES['image']['tmp_name'],$filename)){
		  //add_pic_mark($filename,$filename,$type,"Tad Gallery");

			$m_thumb_name=photo_name($sn,"m",1);
			$s_thumb_name=photo_name($sn,"s",1);
			if($width > 550 or $height > 550)	thumbnail($filename,$m_thumb_name,$_FILES['image']['type'],550);
			if($width > 150 or $height > 150)	thumbnail($filename,$s_thumb_name,$_FILES['image']['type'],150);
		}else{
			redirect_header($_SERVER['PHP_SELF'], 5,sprintf(_TADGAL_IMPORT_UPLOADS_ERROR,$filename));
		}
	}

	return $sn;
}

/*-----------執行動作判斷區----------*/
$_REQUEST['op']=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
switch($_REQUEST['op']){
	case "insert_tad_gallery":
	insert_tad_gallery();
	header("location: {$_SERVER['PHP_SELF']}");
	break;

	default:
	$main=tad_gallery_form();
	break;
}

/*-----------秀出結果區--------------*/
include XOOPS_ROOT_PATH."/header.php";
$xoopsTpl->assign( "css" , "<link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/tadgallery/module.css' />") ;
$xoopsTpl->assign( "toolbar" , toolbar($interface_menu)) ;
$xoopsTpl->assign( "content" , $main) ;
include_once XOOPS_ROOT_PATH.'/footer.php';

?>