<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-03-23
// $Id: view.php,v 1.5 2008/05/05 03:23:04 tad Exp $
// ------------------------------------------------------------------------- //

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



//觀看某一張照片
function view_pic($sn=""){
	global $xoopsDB,$xoopsUser,$xoopsModule,$xoopsModuleConfig,$xoopsTpl,$xoopsOption;
	$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,tag 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,$tag)=$xoopsDB->fetchRow($result);

	if(!empty($csn)){
		$ok_cat=chk_cate_power();
		if(!in_array($csn,$ok_cat)){
		 	$cate_option=get_tad_gallery_cate_option(0,0,$csn);
  		$cate=get_tad_gallery_cate($csn);
		  $select="<select onChange=\"window.location.href='index.php?csn=' + this.value\">
			$cate_option
			</select>";
			$main=div_3d(_TADGAL_NO_POWER_TITLE,sprintf(_TADGAL_NO_POWER_CONTENT,$cate['title'],$select),"corners");
			return $main;
			exit;
		}
	}


	//找出上一張或下一張
  $pnp=get_pre_next($csn,$sn);

	$back_btn=(!empty($pnp['pre']))?" id='pre_photo' onClick=\"location.href='{$_SERVER['PHP_SELF']}?sn={$pnp['pre']}#photo_top'\" ":"";
	$next_btn=(!empty($pnp['next']))?" id='next_photo' onClick=\"location.href='{$_SERVER['PHP_SELF']}?sn={$pnp['next']}#photo_top'\" ":"";
	
	
	if(!file_exists(XOOPS_ROOT_PATH."/modules/tadtools/jBreadCrumb.php")){
    redirect_header("index.php",3, _MA_NEED_TADTOOLS);
  }
  include_once XOOPS_ROOT_PATH."/modules/tadtools/jBreadCrumb.php";
  $arr=get_cate_path($csn);
  $jBreadCrumb=new jBreadCrumb($arr);
  $jBreadCrumbPath="<a name='photo_top'></a>".$jBreadCrumb->render();
	
	
	
	$title=(empty($title))?$filename:$title;
	$div_width=$xoopsModuleConfig['thumbnail_m_width']+30;
	$size_txt=sizef($size);


	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>";

		$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></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></a>";


		$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>";
		

		$option=get_tad_gallery_cate_option(0,0,$csn);
		

		$edit_form="<div id='input_form' style='clear:both;'>
		<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' name='myForm'>
	  <table class='form_tbl'>
		<tr><td class='title' nowrap>"._MA_TADGAL_CSN."</td>
		<td class='col'><select name='csn' size=1>
			$option
		</select></td></tr>
		<tr>
		<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'><input type='text' name='title' size='60' value='{$title}'></td></tr>
		<tr><td class='title' nowrap>"._MA_TADGAL_DESCRIPTION."</td>
		<td class='col'><textarea style='width: 400px; height: 44px; min-height: 44px;' name='description'>$description</textarea></td></tr>
	  <tr><td class='bar' colspan='2' align='right'>
	  <input type='hidden' name='sn' value='{$sn}'>
	  <input type='hidden' name='op' value='update_tad_gallery'>
		<input type='checkbox' name='cover' value='small/{$dir}/{$sn}_s_{$filename}'>"._MA_TADGAL_AS_COVER."
	  <input type='submit' value='"._MA_SAVE_EDIT."'></td></tr>
	  </table>
	  </form>
		</div>";

		$tag_select=tag_select($tag);
		
		$tag_form="<div id='tag_form' style='clear:both;'>
		<form action='{$_SERVER['PHP_SELF']}' method='post' id='tagForm' name='tagForm'>
	  <table class='form_tbl'>
		<tr><td class='title' nowrap>"._MD_TADGAL_TAG."</td>
		<td class='col'><input type='text' name='new_tag' size='30'>"._MD_TADGAL_TAG_TXT."</td></tr>
		<tr>
		<td class='col' colspan=2>$tag_select</td></tr>
	  <tr><td class='bar' colspan='4' align='right'>
	  <input type='hidden' name='sn' value='{$sn}'>
	  <input type='hidden' name='op' value='update_tad_gallery_tag'>
	  <input type='submit' value='"._MA_SAVE_EDIT."'></td></tr>
	  </table>
	  </form>
		</div>";
		

    $admin_tab="
    <li><a href='#fragment-3'><span>"._TADGAL_EDIT_PIC."</span></a></li>
    <li><a href='#fragment-4'><span>"._MD_TADGAL_TAG."</span></a></li>";

    $admin_tab_content="
    <div id='fragment-3'>
    $edit_form
    {$del_btn}{$good_btn}
    </div>

    <div id='fragment-4'>
    $tag_form
    </div>";
    
	}else{
	  $del_btn=$admin_tool=$del_js=$edit_form=$tag_form=$admin_tab=$admin_tab_content="";
	}
	
	
	//秀出各種尺寸圖示
	if($xoopsModuleConfig['show_copy_pic']){
  	$sel_size="
  	<a href='".get_pic_url($dir,$sn,$filename,"s")."' target='_blank' title='$description'>
    <img src='images/s.png' alt='"._TADGAL_FILE_COPY_S."' title='"._TADGAL_FILE_COPY_S."' border='0' align='left'>
    </a>

  	<a href='".get_pic_url($dir,$sn,$filename,"m")."' target='_blank' title='$description'>
    <img src='images/m.png' alt='"._TADGAL_FILE_COPY_M."' title='"._TADGAL_FILE_COPY_M."' border='0' align='left'>
    </a>

  	<a href='".get_pic_url($dir,$sn,$filename)."' target='_blank' title='$description'>
    <img src='images/l.png' alt='"._TADGAL_FILE_COPY_B."' title='"._TADGAL_FILE_COPY_B."' border='0' align='left'>
    </a>";
	}else{
    $sel_size="";
  }
	
	//推文工具
	$push=push_url();

  //計數器
	add_tad_gallery_counter($sn);

  //縮圖
	if(($width/$height)>2){
	  $mpic=getimagesize(get_pic_url($dir,$sn,$filename,"m","dir"));
	  $pic180="<marquee width='{$xoopsModuleConfig['thumbnail_m_width']}' behavior='scroll' height='{$mpic[1]}' direction=left scrolldelay=0 scrollamount=3><a href='".get_pic_url($dir,$sn,$filename)."'><img src='".get_pic_url($dir,$sn,$filename,"m")."'  alt='$title' class='instant itiltnone icolorFCFCFC' /></a></marquee>";
	}else{

    if($width > $height){
          $new_wh=round($xoopsModuleConfig['thumbnail_m_width'] * $height / $width , 0);
          $pcss="width:{$xoopsModuleConfig['thumbnail_m_width']}px;height:{$new_wh}px;";
          $pacss="width:{$new_wh}px;height:{$new_wh}px;";
      }else{
          $new_wh=round($xoopsModuleConfig['thumbnail_m_width'] * $width / $height , 0);
          $new_wh_a=$new_wh/2;
          $pcss="width:{$new_wh}px;height:{$xoopsModuleConfig['thumbnail_m_width']}px;";
          $pacss="width:{$new_wh_a}px;height:{$xoopsModuleConfig['thumbnail_m_width']}px;";
      }
      
      $photo=get_pic_url($dir,$sn,$filename,"m");
      $pic180="
      <table id='view_photo' style='{$pcss}background-image:url({$photo});background-position: top center;	background-repeat: no-repeat;	background-color: black;'>
      <tr>
      <td $back_btn style='{$pacss}background-position: top center;	background-repeat: no-repeat;border:0px;'></td>
      <td $next_btn style='{$pacss}background-position: top center;	background-repeat: no-repeat;border:0px;'></td></tr>
      </table>
    ";
	}


  //地圖部份
  $info=explode("||",$exif);
  foreach($info as $v){
	  $exif_arr=explode("=",$v);
	  $exif_arr[1]=str_replace("&#65533;","",$exif_arr[1]);
	  $bb= "\$photoexif{$exif_arr[0]}=\"{$exif_arr[1]}\";";
	  if(empty($exif_arr[0]))continue;
	  @eval($bb);
	}
	
  $latitude=$photoexif['GPS']['latitude'];
  $longitude=$photoexif['GPS']['longitude'];;
  
  $google_api_key=get_google_api_key();
  
  $gmap_script=$gmap_div=$gmap_js="";
  if(!empty($latitude)){
    $gmap_script="
    <script type='text/javascript' src='http://maps.google.com/maps/api/js?sensor=false'></script>
    <script src='class/gmap3.min.js' type='text/javascript'></script>";
    
    $gmap_div="
    <div>
    <div id='map_canvas' style='float:left;width:{$xoopsModuleConfig['thumbnail_m_width']}px;height:240px;border:1px solid gray;'></div>
    <div style='clear:both;'><a href='http://maps.google.com.tw/maps?f=q&source=s_q&output=html&q={$latitude}%2C{$longitude}' target='_blank'>{$latitude},{$longitude}</a></div>
    </div>";
    
    $gmap_js="$('#map_canvas').gmap3({
      action:'addMarker',
      latLng:[{$latitude},{$longitude}],
      map:{
        center: true,
        zoom: 15
      }
    });";

    $map_tab="
    <li><a href='#fragment-2'><span>"._TADGAL_PIC_GMAP."</span></a></li>";
    $map_tab_content="
    <div id='fragment-2'>
    $gmap_div
    </div>";
  }else{
    $map_tab=$map_tab_content="";
  }

  $jquery_path=get_jquery(true);
  $tabs="
  $jquery_path
  $gmap_script
  <script type='text/javascript'>
  $(document).ready(function() {
    $gmap_js
   $('#jquery-tabs').tabs();
  });
  </script>
  <div id='jquery-tabs'>
    <ul>
      <li><a href='#fragment-1'><span>"._MA_TADGAL_DESCRIPTION."</span></a></li>
      <li><a href='exif.php?sn=$sn'><span>"._MD_TADGAL_EXIF."</span></a></li>
      $map_tab
      $admin_tab
    </ul>
    
    <div id='fragment-1'>
      <div id='photo_title'>{$title}</div>
      <div>".nl2br($description)."</div>
      <div>$basic_exif</div>
    </div>
    $map_tab_content
    $admin_tab_content
  </div>
    ";


  
	
	$xoopsTpl->assign( "path" , $jBreadCrumbPath) ;
	$xoopsTpl->assign( "del_js" , $del_js) ;

	$fb_tag="
	<meta property=\"og:title\" content=\"{$title}\" />
  <meta property=\"og:description\" content=\"{$description}\" />
  <meta property=\"og:image\" content=\"".get_pic_url($dir,$sn,$filename,"fb")."\" />
  ";

	//$xoopsTpl->assign( "gmap_script" , $gmap_script) ;
	//$xoopsTpl->assign( "gmap_js" , $gmap_js) ;
	$xoopsTpl->assign( "div_width" , $div_width) ;
	$xoopsTpl->assign( "sel_size" , $sel_size);
	$xoopsTpl->assign( "pic180" , $pic180) ;
	$xoopsTpl->assign( "photo" , $photo) ;
	$xoopsTpl->assign( "push" , $push);
	$xoopsTpl->assign( "tabs" , $tabs);
	$xoopsTpl->assign( "xoops_module_header" , $fb_tag);

  $facebook_comments=facebook_comments();
	$xoopsTpl->assign( "facebook_comments" , $facebook_comments);


	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());
	
	//設為封面
	if(!empty($_POST['cover'])){
	 	$sql = "update ".$xoopsDB->prefix("tad_gallery_cate")." set `cover`='{$_POST['cover']}' where csn='{$_POST['csn']}'";
		$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error());
	}

}



//更新標籤資料到tad_gallery中
function update_tad_gallery_tag($sn=""){
	global $xoopsDB;
	
	$all=implode(",",$_POST['tag']);
	
	if(!empty($_POST['new_tag'])){
   $new_tags=explode(",",$_POST['new_tag']);
	}
	
	foreach($new_tags as $tag){
	  if(!empty($tag)){
		  $tag=trim($tag);
	    $all.=",{$tag}";
    }
	}
	
	
 	$sql = "update ".$xoopsDB->prefix("tad_gallery")." set `tag`='{$all}' 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());

}

/*-----------執行動作判斷區----------*/
$_REQUEST['op']=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
$sn=(isset($_REQUEST['sn']))?intval($_REQUEST['sn']) : 0;
$csn=(isset($_REQUEST['csn']))?intval($_REQUEST['csn']) : 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);
	if($_POST['go_cate']=='1'){
    header("location: index.php?csn={$csn}");
	}else{
		header("location: view.php?sn={$sn}");
	}
	break;


	case "update_tad_gallery_tag":
	update_tad_gallery_tag($sn);
	header("location: view.php?sn={$sn}");
	break;

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

	default:
	$main=view_pic($sn);
	break;
}

/*-----------秀出結果區--------------*/
$xoopsTpl->assign( "css" , "<link rel='alternate' href='"._TADGAL_UP_FILE_URL."photos.rss' type='application/rss+xml' title='' id='gallery' />
<script type='text/javascript' src='".XOOPS_URL."/modules/tadgallery/class/piclens.js'></script>
<link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/tadgallery/module.css' />") ;
$xoopsTpl->assign( "toolbar" , toolbar($interface_menu)) ;

if(!empty($sn)){
	$photo=get_tad_gallery($sn);
	$xoopsTpl->assign("xoops_pagetitle",$photo['title']);
	if (is_object($xoTheme)) {
		$xoTheme->addMeta( 'meta', 'keywords', $photo['title']);
		$xoTheme->addMeta( 'meta', 'description', $photo['title']) ;
	} else {
		$xoopsTpl->assign('xoops_meta_keywords','keywords',$photo['title']);
		$xoopsTpl->assign('xoops_meta_description', $photo['title']);
	}
}
include_once XOOPS_ROOT_PATH.'/include/comment_view.php';
include_once XOOPS_ROOT_PATH.'/footer.php';

?>
