<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-03-23
// $Id: batch_tool.php,v 1.1 2008/05/05 03:21:15 tad Exp $
// ------------------------------------------------------------------------- //

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

/*-----------function區--------------*/
//列出所有tad_gallery資料
function list_tad_gallery($csn="",$show_function=1){
  global $xoopsDB,$xoopsModule,$xoopsModuleConfig;
  
  
  $MDIR=$xoopsModule->getVar('dirname');

  if($_SESSION['gallery_list_mode']=="good"){
    $mode_select="<a href='index.php?op=chg_mode&mode=normal#gallery_top' class='mode_change'>"._MA_TADGAL_LIST_NORMAL."</a>";
    $mode_where="good='1'";
    $cate_select="";
    $link_to_cate="";
  }else{
    $mode_select="<a href='index.php?op=chg_mode&mode=good#gallery_top' class='mode_change'>"._MA_TADGAL_LIST_GOOD."</a>";
    $mode_where="csn='{$csn}'";
    $cate_option=get_tad_gallery_cate_option(0,0,$csn);
    $cate_select="<select size=1 name='csn' onChange='location.href=\"index.php?csn=\"+this.value+\"#gallery_top\"'>
    $cate_option
    </select>";
    $cate=get_tad_gallery_cate($csn);
    $link_to_cate=(!empty($csn))?"<a href='../index.php?csn={$csn}' class='mode_change'>".sprintf(_MA_TADGAL_LINK_TO_CATE,$cate['title'])."</a>":"";
  }

  $sql = "select sn,csn,title,description,filename,size,width,height,dir,uid,post_date,counter,good from ".$xoopsDB->prefix("tad_gallery")." where $mode_where order by photo_sort , post_date";


  $thumbnail_number=$xoopsModuleConfig['thumbnail_number']*2;
  //getPageBar($原sql語法, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
  $PageBar=getPageBar($sql,$thumbnail_number, 10);
  $bar=$PageBar['bar'];
  $sql=$PageBar['sql'];

  $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

  if(!file_exists(XOOPS_ROOT_PATH."/modules/tadtools/jeditable.php")){
    redirect_header("index.php",3, _MA_NEED_TADTOOLS);
  }
  include_once XOOPS_ROOT_PATH."/modules/tadtools/jeditable.php";
  $jeditable = new jeditable();

  $all_photo="<ul id='all_photos'>";

  $save_file=XOOPS_URL."/modules/{$MDIR}/admin/save.php";
  
  $i=4;
  while(list($sn,$photo_csn,$title,$description,$filename,$size,$width,$height,$dir,$uid,$post_date,$counter,$good)=$xoopsDB->fetchRow($result)){
  
    $jeditable->setTextCol("#pt{$sn}",$save_file,'112px','12px',"{'sn':$sn,'col_name':'title'}",_MA_TADGAL_CLICK_EDIT_TITLE);
    $jeditable->setTextAreaCol("#pd{$sn}",$save_file,'112px','26px',"{'sn':$sn,'col_name':'description'}",_MA_TADGAL_CLICK_EDIT_DESC);

    $good_pic=($good=='1')?"<img src='".XOOPS_URL."/modules/{$MDIR}/images/icon_favourites.gif' alt='good' title='Good' border='0'>":"";
    

    $all_photo.="
    <li id='recordsArray_{$sn}' class='pic_li_border' style='background-image:url(".get_pic_url($dir,$sn,$filename,"s").");' title='{$filename} (".sizef($size,false).") {$width} x {$height}'>
    
      <div class='pic_bg' onClick='check_one(\"p{$sn}\",true);'>

        <div><input type='checkbox' id='p{$sn}' name='pic[]' value='{$sn}' class='p{$csn}' onClick='check_one(\"p{$sn}\",true);'></div>
        
        <div style='float:left'>
        <a href='../view.php?sn=$sn'><img src='".XOOPS_URL."/modules/{$MDIR}/images/action_go.gif' alt='action_go.png, 3.9kB' title='action_go' border='0'></a>
        <br>
        $good_pic
        </div>
        
      </div>
      <div class='pic_title' id='pt{$sn}'>$title</div>
      <div class='pic_desc' id='pd{$sn}'>$description</div>
    </li>
    ";
    $i++;
  }
  $all_photo.="</ul>";

  $jeditable_set=$jeditable->render();
  
  $option=get_tad_gallery_cate_option(0,0,$csn);

  $tag_select=tag_select("","add_tag");
  

  $data="
  <script type='text/javascript' src='".XOOPS_URL."/modules/tadtools/jquery/ui/jquery-ui.js'></script>
  $jeditable_set
  <script language=\"JavaScript\">
  jQuery().ready(function(){
  	jQuery(function() {
  		jQuery(\"ul#all_photos\").sortable({ opacity: 0.6, cursor: 'move', update: function() {
  			var order = $(this).sortable(\"serialize\") + '&action=updateRecordsListings';
  			jQuery.post(\"save_sort.php\", order, function(theResponse){
				  jQuery(\"#save_msg\").html(theResponse);
  			});
  		}
  		});
    });
  });

  function check_one(id_name,change){
    if(document.getElementById(id_name).checked && change){
      document.getElementById(id_name).checked = false;
    }else{
      document.getElementById(id_name).checked = true;
    }
  }
  </script>
  <div id='save_msg'></div>
  <form action='index.php' method='post' name='form1'>
  $cate_select
  <input type='checkbox' id='u{$csn}' onClick=\"$('#thumb input:checkbox.p{$csn}').attr('checked',$('#u{$csn}').attr('checked'));\">"._MA_TADGAL_SELECT_ALL."
  $mode_select
  $link_to_cate
  <div id='thumb'>
    $all_photo
  </div>

  <div style='clear:both;'></div>

  <p style='text-align:center;margin:10px auto;'>{$bar}</p>

  <fieldset>
  <legend>"._MA_TADGAL_THE_ACT_IS."</legend>
    <table>
    <tr>
    <td valign='top'>
    <label for='del'><input type='radio' name='op' value='del' id='del'>"._BP_DEL."</label><br>
    
    <label for='move'><input type='radio' name='op' value='move' id='move'>"._MA_TADGAL_MOVE_TO."</label><select name='new_csn' onChange=\"check_one('move',false)\">$option</select><br>
    
    <label for='add_title'><input type='radio' name='op' value='add_title' id='add_title'>"._MA_TADGAL_ADD_TITLE."</label><input type='text' name='add_title' size='30' onClick=\"check_one('add_title',false)\"><br>
    
    <label for='add_description'><input type='radio' name='op' value='add_description' id='add_description'>"._MA_TADGAL_ADD_DESCRIPTION."</label><br /><textarea name='add_description' style='width:250px;height:40px;' onClick=\"check_one('add_description',false)\"></textarea><br>
    </td>
    
    <td valign='top'>
      <label for='remove_tag'><input type='radio' name='op' value='remove_tag' id='remove_tag'>"._MA_TADGAL_REMOVE_TAG."</label><br />
      
      <label for='add_tag'><input type='radio' name='op' value='add_tag' id='add_tag'>"._MA_TADGAL_TAG."</label><input type='text' name='new_tag' size='20' onClick=\"check_one('add_tag',false)\">"._MA_TADGAL_TAG_TXT."
      <p>$tag_select</p>
    </td>

    <td valign='top'>
      <label for='add_good'><input type='radio' name='op' value='add_good' id='add_good'>"._MA_TADGAL_ADD_GOOD."</label><br>
      <label for='del_good'><input type='radio' name='op' value='del_good' id='del_good'>"._MA_TADGAL_DEL_GOOD."</label><br>
    </td>
    </tr>
    </table>

  </fieldset>
  <input type='submit' value='"._MA_TADGAL_GO."'>
  </form>
  ";
  $data=div_3d(_MA_TADGAL_LIST_ALL,$data,"corners","display:inline;");
return $data;
}



//批次搬移
function batch_move($new_csn=""){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set `csn` = '{$new_csn}' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error()."<br>$sql");
  return $sn;
}

//批次新增精華
function batch_add_good(){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `good` = '1' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $sn;
}

//批次新增標題
function batch_add_title(){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `title` = '{$_POST['add_title']}' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $sn;
}
//批次新增說明
function batch_add_description(){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `description` = '{$_POST['add_description']}' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $sn;
}


//批次加上標籤
function batch_add_tag(){
  global $xoopsDB;

  $sel_tags_arr=$_POST['tag'];

  if(!empty($_POST['new_tag'])){
    $new_tags=explode(",",$_POST['new_tag']);
    foreach($new_tags as $t){
      $t=trim($t);
      $sel_tags_arr[$t]=$t;
    }
  }



  foreach($_POST['pic'] as $sn){
    $old_tad_arr="";
    $sql = "select tag from ".$xoopsDB->prefix("tad_gallery")." where sn ='$sn'";
    $result=$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
    list($old_tad)=$xoopsDB->fetchRow($result);
    $old_tad_arr=explode(",",$old_tad);
    foreach($old_tad_arr as $t){
      $t=trim($t);
      $sel_tags_arr[$t]=$t;
    }
    $all_tags=implode(",",$sel_tags_arr);
    
    $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `tag` = '{$all_tags}' where sn ='$sn'";
    $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  }
  
  
  return $sn;
}

//批次取消精選
function batch_del_good(){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `good` = '0' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $sn;
}

//批次清空標籤
function batch_remove_tag(){
  global $xoopsDB;
  $pics=implode(",",$_POST['pic']);
  $sql = "update ".$xoopsDB->prefix("tad_gallery")." set  `tag` = '' where sn in($pics)";
  $xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  return $sn;
}


//批次刪除
function batch_del(){
  global $xoopsDB;
  foreach($_POST['pic'] as $sn){
    delete_tad_gallery($sn);
  }
}


//產生所有分類之rss
function mk_csn_rss_xml(){
  global $xoopsDB,$xoopsModule;
  $sql = "select csn from ".$xoopsDB->prefix("tad_gallery_cate")."";
  $result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  while(list($csn)=$xoopsDB->fetchRow($result)){
    mk_rss_xml($csn);
  }
}

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

switch($op){
  case "del":
  batch_del();
  mk_rss_xml();
  mk_rss_xml($csn);
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;

  case "move":
  batch_move($new_csn);
  mk_rss_xml();
  mk_rss_xml($csn);
  header("location: {$_SERVER['PHP_SELF']}?csn={$new_csn}");
  break;

  case "add_good":
  batch_add_good();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;


  case "del_good":
  batch_del_good();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;

  case "add_tag":
  batch_add_tag();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;

  case "remove_tag":
  batch_remove_tag();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;


  case "add_title":
  batch_add_title();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;

  case "add_description":
  batch_add_description();
  header("location: {$_SERVER['PHP_SELF']}?csn={$csn}");
  break;
  

  //產生Media RSS
  case "mk_rss_xml":
  mk_rss_xml();
  mk_csn_rss_xml();
  header("location: {$_SERVER['PHP_SELF']}");
  break;
  
  //
  case "chg_mode":
  $_SESSION['gallery_list_mode']=$_GET['mode'];
  header("location: {$_SERVER['PHP_SELF']}");
  break;

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

}

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

?>
