<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("临时数据管理"."-" .$menuList[1][0]);
$errorInfo = "信息填写不完整，请检查";
$action = getForm("action", "get");
switch ($action) {
	case "import" : importTempData();break;
	case "del" : delTempData();break;
	case "delall" : delAll();break;
	default : main();break;
} 
viewFoot();

function main(){
	global $menuList,$id;
	$page = getForm("page","get");$id = getForm("id");
    $number = 30;
    $sqlStr = "SELECT m_id,m_name,m_type,m_actor,m_addtime FROM ".DB::table('temp')." ORDER BY m_addtime DESC";
	$allRecordset = db::getCount($sqlStr);
	$tpagecount = ceil($allRecordset / $number);
	if ($page > $tpagecount) $page = $tpagecount;
	$page = max(intval($page), 1);
	if ($allRecordset > 0) {
?>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?php echo $menuList[1][0];?>&nbsp;&raquo;&nbsp;临时表管理';</script>
<form action="?action=delall" method="post" name="tempdataform">

<table class="tb nobdt">
    <tr class="thead"><td colspan="15">临时表管理&nbsp;&nbsp;<a href="?action=delall&do=all" onclick="if(confirm('确定要删除所有吗')){return true;}else{return false;}"><u>清空临时表</u></a></td></tr>
    <TR>
      <TD  align="center">ID</TD>
      <TD align="center">名称</TD>
       <TD align="center">演员</TD>
       <TD  align="center">添加时间</TD>
      <TD   align="center">操作</TD>
    </TR>
<?php
$offset = $number*($page-1);
$query = DB::query("$sqlStr LIMIT $offset, $number");
while($row = DB::fetch($query)) {
    $m_id=$row['m_id'];
?>
    <tr id="adtr<?php echo $m_id;?>" name="adtr" <?php if($id==$m_id) echo " class=\"editlast\"";?> >
      <td  align="center"><input type="checkbox" value="<?php echo $m_id;?>" name="m_id[]" class="checkbox"/><?php echo $m_id;?></TD>
      <td align="center"><?php echo $row['m_name'];?></TD>
      <td  align="center" title="<?php echo $row['m_actor'];?>" ><?php echo msubstr($row['m_actor'],20);?></TD>
      <td  align="center"><?php echo isCurrentDay($row['m_addtime']);?></TD>
      <td   align="center"><a href="?action=del&id=<?php echo $m_id;?>" onclick="if(confirm('确定要删除吗')){return true;}else{return false;}">删除</a></TD>
    </tr>
<?php
}
?>
    <tr>
    <td colspan="7">全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','m_id[]')" />反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','m_id[]')" />
    <select name="type" id="type"  ><option value="">请选择数据分类</option>
    <?php makeTypeOption(0,"&nbsp;|&nbsp;&nbsp;");?>
	</select>
    
    <input type="submit" value="导入到数据表" onclick="tempdataform.action='?action=import';" class="btn"  />
	<input type="submit" value="批量删除" onclick="if(confirm('确定要批量删除吗')){return true}else{return false}" class="btn"  />
    &nbsp;&nbsp; <?=fPageCount($allRecordset,$number,$page)?></td></tr>
</TABLE>
</form> 
</div>
<?php
   } else {
     echo "<tr><td colspan='6'>对不起，无记录</td></tr>";
  }
}

function delAll() {
	if (getForm("do","get") == "all") {
		DB::delete('temp');
	} else {
		$id = getForm("m_id","post");
		DB::delete('temp', 'm_id IN ('.dimplode($id).')');
	}
	alertMsg("", "admin_tempvideo.php");
}

function delTempData() {
	$id = getForm("id","get");
	DB::delete('temp', array('m_id' => $id));
	alertMsg("", getRefer());
}

function importTempData() {
	$ids = getForm("m_id","post") ; $vtype=getForm("type","post");
	if (empty($ids)) die ("请选择数据");
	if (empty($vtype)) die ("请选择导入的分类");
	echo "<div style='font-size:13px'><font color=red>从临时数据库导出数据开始：</font><br>";
	$query = DB::query("SELECT m_id,m_name,m_type,m_state,m_pic,m_actor,m_des,m_playdata,m_publishyear,m_publisharea FROM ".DB::table('temp')." WHERE m_id IN (".dimplode($ids).")");
	while ($row = DB::fetch($query)) {
		$m_where = "";$sql = "";$title = $row['m_name'];$titleArray = explode("/",$title);
		foreach ($titleArray as $m_title) {
			if (!empty($m_title)) $m_where .= " OR CONCAT('/',m_name,'/') LIKE '%/".$m_title."/%' ";
		}
		$m_where = ltrim($m_where," OR");
		$rs = DB::fetch_first("SELECT m_id,m_playdata FROM ".DB::table('data')." WHERE ".$m_where);
		if (!is_array($rs)) {
			$m_enname=MoviePinYin($title);
			$feedarr = array(
			'm_name' => $row['m_name'],
			'm_type' => $vtype,
			'm_state' => $row['m_state'],
			'm_pic' => $row['m_pic'],
			'm_actor' => $row['m_actor'],
			'm_des' => $row['m_des'],
			'm_playdata' => $row['m_playdata'],
			'm_isunion' => 1,
			'm_publishyear' => $row['m_publishyear'],
			'm_publisharea' => $row['m_publisharea'],
			'm_letter' => msubstr($m_enname,1),
			'm_enname' => $m_enname,
			'm_datetime' => time(),
			'm_addtime' => time(),
			);
			DB::insert('data', $feedarr);
	    } else {
			switch (gatherSet) {
				case 0 :case 1 :case 2 :case 3 :
					$m_playdata = gatherIntoLibTransfer($rs['m_playdata'],$row['m_playdata'],gatherSet);$m_enname=MoviePinYin($title);
					$feedarr = array(
					'm_name' => $row['m_name'],
					'm_type' => $tid,
					'm_state' => $row['m_state'],
					'm_playdata' => $m_playdata,
					'm_isunion' => 1,
					'm_addtime' => time(),
					'm_letter' => msubstr($m_enname,1),
					'm_enname' => $m_enname,
					);
			        DB::update('data', $feedarr, array('m_id' => $rs['m_id']));
				break;
				case 4 :
					$m_enname=MoviePinYin($title);
					$feedarr = array(
					'm_name' => $row['m_name'],
					'm_type' => $vtype,
					'm_state' => $row['m_state'],
					'm_pic' => $row['m_pic'],
					'm_actor' => $row['m_actor'],
					'm_des' => $row['m_des'],
					'm_playdata' => $row['m_playdata'],
					'm_isunion' => 1,
					'm_publishyear' => $row['m_publishyear'],
					'm_publisharea' => $row['m_publisharea'],
					'm_letter' => msubstr($m_enname,1),
					'm_enname' => $m_enname,
					'm_addtime' => time(),
					);
					DB::insert('data', $feedarr);
				break;
				case 5 :
				    $feedarr = array(
					'm_pic' => $row['m_pic'],
					'm_actor' => $row['m_actor'],
					'm_des' => $row['m_des'],
					'm_publishyear' => $row['m_publishyear'],
					'm_publisharea' => $row['m_publisharea'],
					'm_addtime' => time(),
					);
			        DB::update('data', $feedarr, array('m_id' => $rs['m_id']));
				break;
		    }
		}
		DB::delete('temp', array('m_id' => $row['m_id']));
		echo "数据".$row['m_name']."导入成功<br>";
	}
	alertMsg("", "admin_tempvideo.php");
}
?>
