<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-02-28
// $Id: cate.php,v 1.4 2008/05/05 03:21:31 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "../../../include/cp_header.php";
include "../function.php";

/*-----------function區--------------*/
//tad_gallery_cate編輯表單
function tad_gallery_cate_form($csn="",$show_border='true'){
  global $xoopsDB,$xoopsModuleConfig,$cate_show_mode_array;
  include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");

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

  //預設值設定

  $csn=(!isset($DBV['csn']))?"":$DBV['csn'];
  $of_csn=(!isset($DBV['of_csn']))?"":$DBV['of_csn'];
  $title=(!isset($DBV['title']))?"":$DBV['title'];
  $enable_group=(!isset($DBV['enable_group']))?"":explode(",",$DBV['enable_group']);
  $enable_upload_group=(!isset($DBV['enable_upload_group']))?array('1'):explode(",",$DBV['enable_upload_group']);
  $sort=(!isset($DBV['sort']))?auto_get_csn_sort():$DBV['sort'];
  $passwd=(!isset($DBV['passwd']))?"":$DBV['passwd'];
  $mode=(!isset($DBV['mode']))?$xoopsModuleConfig['thumbnail_mode']:$DBV['mode'];
  $show_mode=(!isset($DBV['show_mode']))?"":$DBV['show_mode'];
  $cover=(!isset($DBV['cover']))?"":$DBV['cover'];

  $op=(empty($csn))?"insert_tad_gallery_cate":"update_tad_gallery_cate";

  $cate_select=get_tad_gallery_cate_option(0,0,$of_csn,"","",$csn,1);
  $cover_select=get_cover($csn,$cover);


  //可見群組
  $SelectGroup_name = new XoopsFormSelectGroup("", "enable_group", false,$enable_group, 3, true);
  $SelectGroup_name->addOption("", _MA_TADGAL_ALL_OK, false);
  $enable_group = $SelectGroup_name->render();

  //可上傳群組
  $SelectGroup_name = new XoopsFormSelectGroup("", "enable_upload_group", false,$enable_upload_group, 3, true);
  //$SelectGroup_name->addOption("", _MA_TADGAL_ALL_OK, false);
  $enable_upload_group = $SelectGroup_name->render();

  $cate_show_option="";
  foreach($cate_show_mode_array as $key=>$value){
    $selected=($show_mode==$key)?"selected='selected'":"";
    $cate_show_option.="<option value='$key' $selected>$value</option>";
  }


  $cover_default=(!empty($cover))?XOOPS_URL."/uploads/tadgallery/{$cover}":"../images/folder_picture.png";

  $jquery=($show_border)?get_jquery():"";

  $main="
  $jquery
  <script type='text/javascript'>
  $(document).ready(function() {
    $('#adv_form').hide();
    $('#show_adv_form').click(function() {
      if ($('#adv_form').is(':visible')) {
         $('#adv_form').fadeOut();
         $('#show_adv_form').val('"._MA_TADGAL_CATE_ADVANCE_SETUP."');
      } else{
         $('#adv_form').fadeIn();
         $('#show_adv_form').val('"._MA_TADGAL_CATE_HIDE_ADVANCE_SETUP."');
      }
    });


    $('#shl_form').hide();
    $('#show_shl_form').click(function() {
      if ($('#shl_form').is(':visible')) {
         $('#shl_form').slideUp();
         $('#show_shl_form').val('"._MA_TADGAL_CATE_SHL_SETUP."');
      } else{
         $('#shl_form').slideDown();
         $('#show_shl_form').val('"._MA_TADGAL_CATE_HIDE_SHL_SETUP."');
      }
    });
  });
  </script>
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  <table class='form_tbl' id='form_tbl' style='width:auto;'>

  <tr><td class='title' nowrap>"._MA_TADGAL_SORT."</td>
  <td class='col' colspan=2><input type='text' name='sort' size='5' value='{$sort}'>
  <input type='button' value='"._MA_TADGAL_CATE_ADVANCE_SETUP."' id='show_adv_form'>
  </td>
  <!--進階設定-->
  <td rowspan=5>
    <div id='adv_form'>
    <table class='form_tbl' id='form_tbl2'>
      <tr><td class='title' nowrap>"._MA_TADGAL_CATE_SHOW_MODE."</td>
      <td class='col' colspan=2>
      <select name='show_mode' size='1'>$cate_show_option</select>
      </td></tr>
      <tr><td class='title' nowrap>"._MA_TADGAL_PASSWD."</td>
      <td class='col' colspan=2><input type='text' name='passwd' size='10' value='{$passwd}'>"._MA_TADGAL_PASSWD_DESC."</td></tr>
      <tr>
        <td>"._MA_TADGAL_CATE_POWER_SETUP."</td>
        <td class='title'>"._MA_TADGAL_ENABLE_GROUP."</td>
        <td class='title'>"._MA_TADGAL_ENABLE_UPLOAD_GROUP."</td>
      </tr>
      <tr>
      <td></td>
      <td class='col'>$enable_group</td>
      <td class='col'>$enable_upload_group</td>
      </tr>
      </table>
    </div>
  <input type='hidden' name='csn' value='{$csn}'>
  <input type='hidden' name='op' value='{$op}'>
  <input type='submit' value='"._MA_SAVE."'>
  </td>
  <!--進階設定-->
  </tr>
  <tr><td class='title' nowrap>"._MA_TADGAL_OF_CSN."</td>
  <td class='col' colspan=2><select name='of_csn' size=1>
  $cate_select
  </select></td></tr>

  <tr><td class='title' nowrap>"._MA_TADGAL_TITLE."</td>
  <td class='col' colspan=2><input type='text' name='title' size='30' value='{$title}'></td></tr>

  <tr><td class='title' nowrap>"._MA_TADGAL_COVER."</td>
  <td class='col' colspan=2>
  <div style='float:right;width:120px;height:90px;overflow:hidden;margin-left:4px;'><img src='{$cover_default}' id='pic' vspace=4></div>
  <select name='cover' size=5 onChange='document.getElementById(\"pic\").src=\"".XOOPS_URL."/uploads/tadgallery/\" + this.value'>
  $cover_select
  </select></td></tr>
  </table>
  </form>";

  if($show_border){
    $main=div_3d(_MA_INPUT_CATE_FORM,$main,"raised","display:inline;");
  }
  return $main;
}

//新增資料到tad_gallery_cate中
function insert_tad_gallery_cate(){
  global $xoopsDB;
  if(empty($_POST['title']))return;
   if(empty($_POST['enable_group']) or in_array("",$_POST['enable_group'])){
    $enable_group="";
  }else{
    $enable_group=implode(",",$_POST['enable_group']);
  }

  if(empty($_POST['enable_upload_group'])){
    $enable_upload_group="1";
  }else{
    $enable_upload_group=implode(",",$_POST['enable_upload_group']);
  }

  $sql = "insert into ".$xoopsDB->prefix("tad_gallery_cate")." (of_csn,title,passwd,enable_group,enable_upload_group,sort,mode,show_mode) values('{$_POST['of_csn']}','{$_POST['title']}','{$_POST['passwd']}','{$enable_group}','{$enable_upload_group}','{$_POST['sort']}','{$_POST['mode']}','{$_POST['show_mode']}')";
  $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  //取得最後新增資料的流水編號
  $csn=$xoopsDB->getInsertId();
  return $csn;
}

//列出所有tad_gallery_cate資料
function list_tad_gallery_cate($of_csn=1,$level=0,$modify_csn=""){
  global $xoopsDB,$xoopsModule;


  $old_level=$level;
  $left=$level*20;
  $level++;

 $MDIR=$xoopsModule->getVar('dirname');

  $sql = "select csn,of_csn,title,passwd,enable_group,enable_upload_group,sort,mode,cover from ".$xoopsDB->prefix("tad_gallery_cate")." where of_csn='{$of_csn}' order by sort";
  $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());


  if($old_level==0){

    $form=tad_gallery_cate_form($modify_csn,false);

    //加入圖片提示
    if(!file_exists(XOOPS_ROOT_PATH."/modules/tadtools/bubblepopup.php")){
     redirect_header("index.php",3, _MA_NEED_TADTOOLS);
    }
    include_once XOOPS_ROOT_PATH."/modules/tadtools/bubblepopup.php";
    $bubblepopup = new bubblepopup(false);
    $sql_cover="select csn,cover from ".$xoopsDB->prefix("tad_gallery_cate")." order by sort";
    $result_cover = $xoopsDB->query($sql_cover) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
    while(list($csn,$cover)=$xoopsDB->fetchRow($result_cover)){
      if(!empty($cover)){
        $bubblepopup->add_tip("#cover_{$csn}","<img src=\'".XOOPS_URL."/uploads/tadgallery/{$cover}\'>",'right');
      }
    }
    $bubblepopup_code=$bubblepopup->render();
  
    //加入表格樹
    if(!file_exists(XOOPS_ROOT_PATH."/modules/tadtools/treetable.php")){
       redirect_header("index.php",3, _MA_NEED_TADTOOLS);
      }
    include_once XOOPS_ROOT_PATH."/modules/tadtools/treetable.php";

    
    $treetable=new treetable(true , "csn" , "of_csn" , "#tbl" , "save_drag.php" , ".folder" , "#save_msg" , false , ".sort", "save_cate_sort.php" , "#save_msg2");
    $treetable_code=$treetable->render();

    $data="
    $treetable_code
    $bubblepopup_code
    <script>
    function delete_tad_gallery_cate_func(csn){
      var sure = window.confirm('"._BP_DEL_CHK."');
      if (!sure)  return;
      location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_gallery_cate&csn=\" + csn;
    }
    </script>
    <div id='save_msg' style='float:right;'></div>
    <div id='save_msg2' style='float:right;'></div>

    <table id='tbl' style='border:1px solid gray;padding:8px;margin:10px;'>
    <tr><td colspan=5>$form</td></tr>
    <tr>
    <th>"._MA_TADGAL_TITLE."</th>
    <th>"._MA_TADGAL_ENABLE_GROUP."</th>
    <th>"._MA_TADGAL_ENABLE_UPLOAD_GROUP."</th>
    <th>"._MA_TADGAL_RE_CREATE_THUMBNAILS."</th>
    <th>"._BP_FUNCTION."</th>
    </tr>
    <tbody class='sort'>";
  }else{
    $data="";
  }

  while(list($csn,$of_csn,$title,$passwd,$enable_group,$enable_upload_group,$sort,$mode,$cover)=$xoopsDB->fetchRow($result)){
    $g_txt=txt_to_group_name($enable_group,_MA_TADGAL_ALL_OK,",");
    $gu_txt=txt_to_group_name($enable_upload_group,_MA_TADGAL_ALL_OK,",");
    $passwd_txt=(empty($passwd))?"":""._MA_TADGAL_PASSWD.":{$passwd}";
    $lock=(empty($passwd))?"":" (<span style='color:red;margin:3px 0px;'>$passwd_txt</span>)";
    $pic=(empty($cover))?"":"<img src='../images/image.png' id='cover_{$csn}'>";

    $class=(empty($of_csn))?"":"class='child-of-node-_{$of_csn}'";

    
    $data.="
    <tr id='node-_{$csn}' $class style='letter-spacing: 0em;'>

    <td nowrap>
    <img src='".XOOPS_URL."/modules/tadtools/treeTable/images/move_s.png' class='folder'>
    
    <a href='../index.php?csn=$csn'>{$title}</a>
    {$pic}{$lock}
    </td>
    <td class='col'>{$g_txt}</td>
    <td class='col'>{$gu_txt}</td>
    <td class='col' style='line-height:150%;'><a href='cate.php?op=re_thumb&csn=$csn'>"._MA_TADGAL_RE_CREATE_THUMBNAILS_ALL."</a> |
    <a href='cate.php?op=re_thumb&kind=m&csn=$csn'>"._MA_TADGAL_RE_CREATE_THUMBNAILS_M."</a> |
    <a href='cate.php?op=re_thumb&kind=s&csn=$csn'>"._MA_TADGAL_RE_CREATE_THUMBNAILS_S."</a> |
    <a href='cate.php?op=re_thumb&kind=fb&csn=$csn'>"._MA_TADGAL_RE_CREATE_THUMBNAILS_FB."</a></td>
    <td style='line-height:150%;'>
    <a href='{$_SERVER['PHP_SELF']}?op=tad_gallery_cate_form&csn=$csn'>"._BP_EDIT."</a> | <a href=\"javascript:delete_tad_gallery_cate_func($csn);\">"._BP_DEL."</a></td></tr>";
    $data.=list_tad_gallery_cate($csn,$level);
  }

  if($old_level==0){

    $data.="
    </tbody>
    </table>
    ";
  }

  

  return $data;
}





//更新tad_gallery_cate某一筆資料
function update_tad_gallery_cate($csn=""){
  global $xoopsDB;
  if(empty($_POST['enable_group']) or in_array("",$_POST['enable_group'])){
    $enable_group="";
  }else{
    $enable_group=implode(",",$_POST['enable_group']);
  }

  if(empty($_POST['enable_upload_group'])){
    $enable_upload_group="1";
  }else{
    $enable_upload_group=implode(",",$_POST['enable_upload_group']);
  }
  $sql = "update ".$xoopsDB->prefix("tad_gallery_cate")." set of_csn = '{$_POST['of_csn']}', title = '{$_POST['title']}', passwd = '{$_POST['passwd']}', enable_group = '{$enable_group}', enable_upload_group = '{$enable_upload_group}', sort = '{$_POST['sort']}', mode = '{$_POST['mode']}', show_mode = '{$_POST['show_mode']}', cover = '{$_POST['cover']}' where csn='$csn'";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $csn;
}

//刪除tad_gallery_cate某筆資料資料
function delete_tad_gallery_cate($csn=""){
  global $xoopsDB;

  //先找出底下所有相片
  $sql="select sn from ".$xoopsDB->prefix("tad_gallery")." where csn='$csn'";
  $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  while(list($sn)=$xoopsDB->fetchRow($result)){
    delete_tad_gallery($sn);
  }

  //找出底下分類，並將分類的所屬分類清空
  $sql="update ".$xoopsDB->prefix("tad_gallery_cate")." set  of_csn='' where of_csn='$csn'";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  //刪除之
  $sql = "delete from ".$xoopsDB->prefix("tad_gallery_cate")." where csn='$csn'";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  
  //刪掉RSS
  $rss_filename =_TADGAL_UP_FILE_DIR."photos{$csn}.rss";
	unlink($rss_filename);

}

//重新產生縮圖，沒有 $kind 就是全部縮圖
function re_thumb($csn="",$kind=""){
  global $xoopsDB,$xoopsModuleConfig,$type_to_mime;
  if(empty($csn))return 0;
  
  //找出分類下所有相片
  $sql = "select sn,title,filename,type,width,height,dir,post_date from ".$xoopsDB->prefix("tad_gallery")." where csn='{$csn}' order by photo_sort , post_date";
$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  $n=0;
  while(list($sn,$title,$filename,$type,$width,$height,$dir,$post_date)=$xoopsDB->fetchRow($result)){

  	$b_thumb_name=photo_name($sn,"b",1,$filename,$dir);
  	if(substr($type,0,5)!=="image"){
      $file_ending= substr(strtolower($filename), -3); //file extension
      $type=$type_to_mime[$file_ending];
    }
    
    if($kind=="m" or empty($kind)){
  	 $m_thumb_name=photo_name($sn,"m",1,$filename,$dir);
    	if($width > $xoopsModuleConfig['thumbnail_m_width'] or $height > $xoopsModuleConfig['thumbnail_m_width']){
    		thumbnail($b_thumb_name,$m_thumb_name,$type,$xoopsModuleConfig['thumbnail_m_width']);
    	}
  	}

    if($kind=="s" or empty($kind)){
  	 $m_thumb_name=photo_name($sn,"m",1,$filename,$dir);
  	 $s_thumb_name=photo_name($sn,"s",1,$filename,$dir);
    	if($width > $xoopsModuleConfig['thumbnail_s_width'] or $height > $xoopsModuleConfig['thumbnail_s_width']){
    		thumbnail($m_thumb_name,$s_thumb_name,$type,$xoopsModuleConfig['thumbnail_s_width']);
    	}
    }

    if($kind=="fb" or empty($kind)){
  	  $s_thumb_name=photo_name($sn,"s",1,$filename,$dir);
  	  $fb_thumb_name=photo_name($sn,"fb",1,$filename,$dir);
  	  //echo "thumbnail(<font color='red'>{$s_thumb_name}</font> , <font color='blue'>{$fb_thumb_name}</font> , <font color='yellow'>{$type}</font> , <font color='green'>100</font>);<br/>";
      thumbnail($s_thumb_name,$fb_thumb_name,$type,100);
    }
    $n++;
  }
  //exit;
  return $n;
}


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

switch($op){
  //替換資料
  case "replace_tad_gallery_cate":
  replace_tad_gallery_cate();
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //新增資料
  case "insert_tad_gallery_cate":
  insert_tad_gallery_cate();
  header("location: {$_SERVER['PHP_SELF']}");
  break;


  //刪除資料
  case "delete_tad_gallery_cate";
  delete_tad_gallery_cate($csn);
  mk_rss_xml();
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //更新資料
  case "update_tad_gallery_cate";
  update_tad_gallery_cate($csn);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
  
  //重新產生縮圖
  case "re_thumb":
  $n=re_thumb($csn,$_REQUEST['kind']);
  redirect_header("{$_SERVER['PHP_SELF']}?csn={$_REQUEST['csn']}",3, "All ($n) OK!");
  break;

  //預設動作
  default:
  $main=list_tad_gallery_cate(0,0,$csn);
  break;


}

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

?>
