<?php
/**
 * 內容處理函數
 *
 * @version        $Id: content_batch_up.php 1 14:31 2010年7月12日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");
CheckPurview('sys_ArcBatch');
require_once(DEDEINC."/typelink.class.php");
require_once(DEDEADMIN."/inc/inc_batchup.php");
@set_time_limit(0);

//typeid,startid,endid,seltime,starttime,endtime,action,newtypeid
//批量操作
//check del move makehtml
//獲取ID條件
if(empty($startid)) $startid = 0;
if(empty($endid)) $endid = 0;
if(empty($seltime)) $seltime = 0;
if(empty($typeid)) $typeid = 0;

//生成HTML操作由其它頁面處理
if($action=="makehtml")
{
    $jumpurl  = "makehtml_archives_action.php?endid=$endid&startid=$startid";
    $jumpurl .= "&typeid=$typeid&pagesize=20&seltime=$seltime";
    $jumpurl .= "&stime=".urlencode($starttime)."&etime=".urlencode($endtime);
    header("Location: $jumpurl");
    exit();
}

$gwhere = " WHERE 1 ";
if($startid >0 ) $gwhere .= " AND id>= $startid ";
if($endid > $startid) $gwhere .= " AND id<= $endid ";
$idsql = '';

if($typeid!=0)
{
    $ids = GetSonIds($typeid);
    $gwhere .= " AND typeid IN($ids) ";
}
if($seltime==1)
{
    $t1 = GetMkTime($starttime);
    $t2 = GetMkTime($endtime);
    $gwhere .= " AND (senddate >= $t1 AND senddate <= $t2) ";
}
//特殊操作
if(!empty($heightdone)) $action=$heightdone;

//指量審核
if($action=='check')
{
    if(empty($startid) || empty($endid) || $endid < $startid)
    {
        ShowMsg('該操作必須指定起始ID！','javascript:;');
        exit();
    }
    $jumpurl  = "makehtml_archives_action.php?endid=$endid&startid=$startid";
    $jumpurl .= "&typeid=$typeid&pagesize=20&seltime=$seltime";
    $jumpurl .= "&stime=".urlencode($starttime)."&etime=".urlencode($endtime);
    $dsql->SetQuery("SELECT id,arcrank FROM `#@__arctiny` $gwhere");
    $dsql->Execute('c');
    while($row = $dsql->GetObject('c'))
    {
        if($row->arcrank==-1)
        {
            $dsql->ExecuteNoneQuery("UPDATE `#@__arctiny` SET arcrank=0 WHERE id='{$row->id}'");
            $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET arcrank=0 WHERE id='{$row->id}'");
        }
    }
    ShowMsg("完成數據庫的審核處理，準備更新HTML...",$jumpurl);
    exit();
}
//批量刪除
else if($action=='del')
{
    if(empty($startid) || empty($endid) || $endid < $startid)
    {
        ShowMsg('該操作必須指定起始ID！','javascript:;');
        exit();
    }
    $dsql->SetQuery("SELECT id FROM `#@__archives` $gwhere");
    $dsql->Execute('x');
    $tdd = 0;
    while($row = $dsql->GetObject('x'))
    {
        if(DelArc($row->id)) $tdd++;
    }
    ShowMsg("成功刪除 $tdd 條記錄！","javascript:;");
    exit();
}
//刪除空標題文檔
else if($action=='delnulltitle')
{
    $dsql->SetQuery("SELECT id FROM `#@__archives` WHERE trim(title)='' ");
    $dsql->Execute('x');
    $tdd = 0;
    while($row = $dsql->GetObject('x'))
    {
        if(DelArc($row->id)) $tdd++;
    }
    ShowMsg("成功刪除 $tdd 條記錄！","javascript:;");
    exit();
}
//刪除空內容文章
else if($action=='delnullbody')
{
    $dsql->SetQuery("SELECT aid FROM `#@__addonarticle` WHERE LENGTH(body) < 10 ");
    $dsql->Execute('x');
    $tdd = 0;
    while($row = $dsql->GetObject('x'))
    {
        if(DelArc($row->aid)) $tdd++;
    }
    ShowMsg("成功刪除 $tdd 條記錄！","javascript:;");
    exit();
}
//修正縮略圖錯誤
else if($action=='modddpic')
{
    $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET litpic='' WHERE trim(litpic)='litpic' ");
    ShowMsg("成功修正縮略圖錯誤！","javascript:;");
    exit();
}
//批量移動
else if($action=='move')
{
    if(empty($typeid))
    {
        ShowMsg('該操作必須指定欄目！','javascript:;');
        exit();
    }
    $typeold = $dsql->GetOne("SELECT * FROM #@__arctype WHERE id='$typeid'; ");
    $typenew = $dsql->GetOne("SELECT * FROM #@__arctype WHERE id='$newtypeid'; ");
    if(!is_array($typenew))
    {
        ShowMsg("無法檢測移動到的新欄目的信息，不能完成操作！", "javascript:;");
        exit();
    }
    if($typenew['ispart']!=0)
    {
        ShowMsg("你不能把數據移動到非最終列表的欄目！", "javascript:;");
        exit();
    }
    if($typenew['channeltype']!=$typeold['channeltype'])
    {
        ShowMsg("不能把數據移動到內容類型不同的欄目！","javascript:;");
        exit();
    }
    $gwhere .= " And channel='".$typenew['channeltype']."' And title like '%$keyword%'";

    $ch = $dsql->GetOne("SELECT addtable FROM `#@__channeltype` WHERE id={$typenew['channeltype']} ");
    $addtable = $ch['addtable'];

    $dsql->SetQuery("SELECT id FROM `#@__archives` $gwhere");
    $dsql->Execute('m');
    $tdd = 0;
    while($row = $dsql->GetObject('m'))
    {
        $rs = $dsql->ExecuteNoneQuery("UPDATE `#@__arctiny` SET typeid='$newtypeid' WHERE id='{$row->id}'");
        $rs = $dsql->ExecuteNoneQuery("UPDATE `#@__archives` SET typeid='$newtypeid' WHERE id='{$row->id}'");
        if($addtable!='')
        {
            $dsql->ExecuteNoneQuery("UPDATE `$addtable` SET typeid='$newtypeid' WHERE aid='{$row->id}' ");
        }
        if($rs) $tdd++;
        DelArc($row->id,true);
    }

    if($tdd>0)
    {
        $jumpurl  = "makehtml_archives_action.php?endid=$endid&startid=$startid";
        $jumpurl .= "&typeid=$newtypeid&pagesize=20&seltime=$seltime";
        $jumpurl .= "&stime=".urlencode($starttime)."&etime=".urlencode($endtime);
        ShowMsg("成功移動 $tdd 條記錄，準備重新生成HTML...",$jumpurl);
    }
    else
    {
        ShowMsg("完成操作，沒移動任何數據...","javascript:;");
    }
}
//刪除空標題內容
else if($action=='delnulltitle')
{
    $dsql->SetQuery("SELECT id FROM #@__archives WHERE trim(title)='' ");
    $dsql->Execute('x');
    $tdd = 0;
    while($row = $dsql->GetObject('x'))
    {
        if(DelArc($row->id)) $tdd++;
    }
    ShowMsg("成功刪除 $tdd 條記錄！","javascript:;");
    exit();
}
//修正縮略圖錯誤
else if($action=='modddpic')
{
    $dsql->ExecuteNoneQuery("UPDATE #@__archives SET litpic='' WHERE trim(litpic)='litpic' ");
    ShowMsg("成功修正縮略圖錯誤！","javascript:;");
    exit();
}
