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

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

//取得路徑
function get_csn_path($csn=""){
	global $xoopsDB,$xoopsModule,$xoopsModuleConfig;
	$sql = "select of_csn,title from ".$xoopsDB->prefix("tad_gallery_cate")." where csn='{$csn}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	list($of_csn,$title)=$xoopsDB->fetchRow($result);
	if(!empty($of_csn))$path=get_csn_path($of_csn);
	if(empty($csn))$title=_MA_TADGAL_CATE_SELECT;
	$path.="/ <a href='index.php?csn={$csn}'>{$title}</a>";
	return $path;
}

//觀看某一張照片
function view_pic($sn=""){
	global $xoopsDB,$xoopsUser,$xoopsModule,$xoopsModuleConfig;
	$MDIR=$xoopsModule->getVar('dirname');
	//判斷是否對該模組有管理權限，  若空白
  if ($xoopsUser) {
    $nowuid=$xoopsUser->getVar('uid');
    $module_id = $xoopsModule->getVar('mid');
    $isAdmin=$xoopsUser->isAdmin($module_id);
  }else{
    $isAdmin=false;
    $nowuid="";
	}
	$cate_all=get_tad_gallery_cate_all();
	
	$sql = "select csn,title,description,filename,size,type,width,height,dir,uid,post_date,counter,exif,good from ".$xoopsDB->prefix("tad_gallery")." where sn='{$sn}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	list($csn,$title,$description,$filename,$size,$type,$width,$height,$dir,$uid,$post_date,$counter,$exif,$good)=$xoopsDB->fetchRow($result);
	
	$path=get_csn_path($csn);
	//找出上一張或下一張
  $pnp=get_pre_next($csn,$sn);

	$back_btn=(!empty($pnp['pre']))?"<a href='{$_SERVER['PHP_SELF']}?sn={$pnp['pre']}'><img src='images/back.png' align='absmiddle' hspace=6 style='display:inline;width:22px;float:left;' onmouseover=\"showToolTip(event,'"._BP_BACK_PAGE."');return false\" onmouseout=\"hideToolTip()\"></a>":"";
	$next_btn=(!empty($pnp['next']))?"<a href='{$_SERVER['PHP_SELF']}?sn={$pnp['next']}'><img src='images/forward.png' align='absmiddle' hspace=6 style='display:inline;width:22px;float:right;' onmouseover=\"showToolTip(event,'"._BP_NEXT_PAGE."');return false\" onmouseout=\"hideToolTip()\"></a>":"";
	$title=(empty($title))?$filename:$title;
	$div_width=$xoopsModuleConfig['thumbnail_m_width']+30;
	$size_txt=sizef($size);
	
	$copy_pic_url=get_copy_pic_url($dir,$sn,$filename);
	
	$exif_btn=(empty($exif))?"":"<a href='exif.php?sn=$sn'><img src='images/view_exif.png' alt='view exif' title='view exif' border='0' height='22' width='22' align='absmiddle' onmouseover=\"showToolTip(event,'".sprintf(_MD_TADGAL_EXIF,$filename)."');return false\" onmouseout=\"hideToolTip()\"></a>";
	
	
	if($uid==$nowuid or $isAdmin){
	  $del_btn="<img src='images/view_del.png' alt='"._TADGAL_DEL_PIC."' title='"._TADGAL_DEL_PIC."' border='0' height='22' width='22' onClick='delete_tad_gallery_func($sn)' style='cursor:pointer' align='absmiddle' hspace=4 onmouseover=\"showToolTip(event,'"._TADGAL_DEL_PIC."');return false\" onmouseout=\"hideToolTip()\">";

		$good_btn=(empty($good))?"<a href='view.php?op=good&sn={$sn}'><img src='images/good_add.png' alt='"._TADGAL_GOOD_PIC."' title='"._TADGAL_GOOD_PIC."' border='0' height='22' width='22' align='absmiddle' hspace=2 onmouseover=\"showToolTip(event,'"._TADGAL_GOOD_PIC."');return false\" onmouseout=\"hideToolTip()\"></a>":"<a href='view.php?op=good_del&sn={$sn}'><img src='images/good_del.png' alt='"._TADGAL_REMOVE_GOOD_PIC."' title='"._TADGAL_REMOVE_GOOD_PIC."' border='0' height='22' width='22' align='absmiddle' hspace=2 onmouseover=\"showToolTip(event,'"._TADGAL_REMOVE_GOOD_PIC."');return false\" onmouseout=\"hideToolTip()\"></a>";
		
		$admin_tool="
		<img src='images/view_edit.png' alt='"._TADGAL_EDIT_PIC."' title='"._TADGAL_EDIT_PIC."' border='0' height='22' width='22' align='absmiddle' hspace=2 id='show_input_form' onmouseover=\"showToolTip(event,'"._TADGAL_EDIT_PIC."');return false\" onmouseout=\"hideToolTip()\">{$good_btn}
		";
		

		$del_js="
		<script>
		function delete_tad_gallery_func(sn){
			var sure = window.confirm('"._BP_DEL_CHK."');
			if (!sure)	return;
			location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_gallery&sn=\" + sn;
		}
		</script>";
	}else{
	  $del_btn="";
		$admin_tool="";
		$del_js="";
	}
	$option=get_tad_gallery_cate_option(0,0,$csn);
	
	add_tad_gallery_counter($sn);
	
	$data="
	$del_js
	<script type='text/javascript' src='".XOOPS_URL."/modules/{$MDIR}/class/bubble-tooltip.js'></script>
	<script language='javascript' type='text/javascript' src='".XOOPS_URL."/modules/tadgallery/class/copy.js'></script>
	<script type='text/javascript' src='".XOOPS_URL."/modules/tadgallery/class/rounded-corners.js'></script>
	<script type='text/javascript' src='".XOOPS_URL."/modules/tadgallery/class/form-field-tooltip.js'></script>
	<script type='text/javascript'>
	$(document).ready(function() {
		$('#input_form').hide();
		$('#show_input_form').click(function() {
			if ($('#input_form').is(':visible')) {
	       $('#input_form').slideUp();
			} else{
	       $('#input_form').slideDown();
			}
		});
	});
	</script>
	
	<div id='bubble_tooltip'>
	<div class='bubble_top'><span></span></div>
	<div class='bubble_middle'><span id='bubble_tooltip_content'>Content is comming here as you probably can see.Content is comming here as you probably can see.</span></div>
	<div class='bubble_bottom'></div>
	</div>
	<div style='width:{$div_width}px;margin-left:auto;margin-right:auto;'>
		<dl class='bot_rgt'>
		<dt>{$next_btn}{$back_btn}{$path}/{$title}
		{$copy_pic_url}{$exif_btn}</dt>
		<dd><p align='center'><a href='".get_pic_url($dir,$sn,$filename)."'><img src='".get_pic_url($dir,$sn,$filename,"m")."' class='instant itiltnone icolorFCFCFC' /></a></p>
		<p align='center'>$description</p>
		<p style='background-color:#AAAAAA;height:20px'>
		<span style='border-top:1px solid #303030;border-left:1px solid #303030;width:auto;float:right;font-size:10px;background-color:#F0F0F0;color:gray;padding:2px 5px 2px 5px;'>$del_btn<b>{$filename}</b> ({$size_txt}) {$width} x {$height}</span>
		{$admin_tool}
		</p>
		</dd>
		</dl>
		<dl><div id='input_form' style='clear:both;'>
	<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl'>
	<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_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='sn' value='{$sn}'>
  <input type='hidden' name='op' value='update_tad_gallery'>
  <input type='submit' value='"._MA_SAVE."'></td></tr>
  </table>
  </form>
	</div></dl>
	</div>
	
	
	";
	
	return $data;
}



//更新資料到tad_gallery中
function update_tad_gallery($sn=""){
	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'];

	//$myts =& MyTextSanitizer::getInstance();

 	$sql = "update ".$xoopsDB->prefix("tad_gallery")." set `csn`='{$csn}',`title`='{$_POST['title']}',`description`='{$_POST['description']}' where sn='{$sn}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error());

}


//更新人氣資料到tad_gallery中
function add_tad_gallery_counter($sn=""){
	global $xoopsDB;
 	$sql = "update ".$xoopsDB->prefix("tad_gallery")." set `counter`=`counter`+1 where sn='{$sn}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error());

}


//加入精選相片
function update_tad_gallery_good($sn="",$v='0'){
	global $xoopsDB;
 	$sql = "update ".$xoopsDB->prefix("tad_gallery")." set `good`='{$v}' where sn='{$sn}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error());

}



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

switch($_REQUEST['op']){
	case "good":
	update_tad_gallery_good($sn,'1');
	header("location: view.php?sn={$sn}");
	break;

	case "good_del":
	update_tad_gallery_good($sn,'0');
	header("location: view.php?sn={$sn}");
	break;
	
	case "update_tad_gallery":
	update_tad_gallery($sn);
	header("location: view.php?sn={$sn}");
	break;

	case "delete_tad_gallery":
	$csn=delete_tad_gallery($sn);
	header("location: index.php?csn=$csn");
	break;

	default:
	$main=view_pic($sn);
	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';

?>