<?php
/**
 * @version        $Id: story_do.php 1 9:02 2010年9月25日Z 藍色隨想 $
 * @package        DedeCMS.Module.Book
 * @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('story_Del');
require_once(DEDEINC. "/oxwindow.class.php");
if(empty($action))
{
    ShowMsg("你沒指定任何參數！","-1");
    exit();
}

/*--------------------
function DelBook()
刪除整本圖書
-------------------*/
if($action=='delbook')
{
    $bids = explode(',', $bid);
    foreach($bids as $i => $bid)
    {
        if(intval($bid)<=0)
        {
            continue;
        }
        $row = $dsql->GetOne("SELECT booktype FROM #@__story_books WHERE bid='$bid' ");
        $dsql->ExecuteNoneQuery("DELETE FROM #@__story_books WHERE bid='$bid' ");
        $dsql->ExecuteNoneQuery("DELETE FROM #@__story_chapter  WHERE bookid='$bid' ");

        //刪除圖片
        if(empty($row['booktype']))
        {
            $row['booktype'] = '';
        }
        if($row['booktype']==1)
        {
            $dsql->SetQuery("SELECT bigpic FROM #@__story_content WHERE bookid='$bid' ");
            $dsql->Execute();
            while($row = $dsql->GetArray())
            {
                $bigpic = $row['bigpic'];
                if( $bigpic!="" && !eregi('^http://',$bigpic) )
                {
                    @unlink($cfg_basedir.$bigpic);
                }
            }
        }
        $dsql->ExecuteNoneQuery("DELETE FROM #@__story_content WHERE bookid='$bid' ");
    }
    $i = $i+1;
    if(empty($ENV_GOBACK_URL))
    {
        $ENV_GOBACK_URL = 'story_books.php';
    }
    ShowMsg("成功刪除 {$i} 本圖書！",$ENV_GOBACK_URL);
    exit();
}

/*--------------------
function DelStoryContent()
刪除圖書內容
-------------------*/
else if($action=='delcontent')
{

    $row = $dsql->GetOne("SELECT bigpic,chapterid,bookid FROM #@__story_content WHERE id='$cid' ");
    $chapterid = $row['chapterid'];
    $bookid = $row['bookid'];

    //如果圖片不為空，先刪除圖片
    if( $row['bigpic']!="" && !eregi('^http://',$row['bigpic']) )
    {
        @unlink($cfg_basedir.$row['bigpic']);
    }
    $dsql->ExecuteNoneQuery(" DELETE FROM #@__story_content WHERE id='$cid' ");

    //更新圖書記錄
    $row = $dsql->GetOne("SELECT count(id) AS dd FROM #@__story_content WHERE bookid='$bookid' ");
    $dsql->ExecuteNoneQuery("Update #@__story_books SET postnum='{$row['dd']}' WHERE bid='$bookid' ");

    //更新章節記錄
    $row = $dsql->GetOne("SELECT count(id) AS dd FROM #@__story_content WHERE chapterid='$chapterid' ");
    $dsql->ExecuteNoneQuery("Update #@__story_chapter SET postnum='{$row['dd']}' WHERE id='$chapterid' ");
    ShowMsg("成功刪除指定內容！",$ENV_GOBACK_URL);
    exit();
}

/*--------------------
function EditChapter()
保存章節信息
-------------------*/
else if($action=='editChapter')
{

    require_once(DEDEINC."/charSET.func.php");
    //$chaptername = gb2utf8($chaptername);
    $dsql->ExecuteNoneQuery("Update #@__story_chapter SET chaptername='$chaptername',chapnum='$chapnum' WHERE id='$cid' ");
    AjaxHead();
    echo "<font color='red'>成功更新章節：{$chaptername} ！ [<a href=\"javascript:CloseLayer('editchapter')\">關閉提示</a>]</font> <br /><br /> 提示：修改章節名稱或章節序號直接在左邊修改，然後點擊右邊的 [更新] 會保存。 ";
    exit();
}

/*--------------------
function DelChapter()
刪除章節信息
-------------------*/
else if($action=='delChapter')
{
    $row = $dsql->GetOne("SELECT c.bookid,b.booktype FROM #@__story_chapter c LEFT JOIN  #@__story_books b ON b.bid=c.bookid WHERE c.id='$cid' ");
    $bookid = $row['bookid'];
    $booktype = $row['booktype'];
    $dsql->ExecuteNoneQuery("DELETE FROM #@__story_chapter WHERE id='$cid' ");

    //刪除圖片
    if($booktype==1)
    {
        $dsql->SetQuery("SELECT bigpic FROM #@__story_content WHERE bookid='$bookid' ");
        $dsql->Execute();
        while($row = $dsql->GetArray())
        {
            $bigpic = $row['bigpic'];
            if( $bigpic!="" && !eregi('^http://',$bigpic) )
            {
                @unlink($cfg_basedir.$bigpic);
            }
        }
    }
    $dsql->ExecuteNoneQuery("DELETE FROM #@__story_content WHERE chapterid='$cid' ");

    //更新圖書記錄
    $row = $dsql->GetOne("SELECT count(id) AS dd FROM #@__story_content WHERE bookid='$bookid' ");
    $dsql->ExecuteNoneQuery("UPDATE #@__story_books SET postnum='{$row['dd']}' WHERE bid='$bookid' ");
    ShowMsg("成功刪除指定章節！",$ENV_GOBACK_URL);
    exit();
}

/*---------------
function EditChapterAll()
批量修改章節
-------------------*/
else if($action=='upChapterSort')
{
    if(isSET($ids) && is_array($ids))
    {
        foreach($ids as $cid)
        {
            $chaptername = ${'chaptername_'.$cid};
            $chapnum= ${'chapnum_'.$cid};
            $dsql->ExecuteNoneQuery("UPDATE #@__story_chapter SET chaptername='$chaptername',chapnum='$chapnum' WHERE id='$cid' ");
        }
    }
    ShowMsg("成功更新指定章節信息！", $ENV_GOBACK_URL);
    exit();
}
