<?php
if (!defined('puyuetian')) {
    exit('403');
}

$table = $_G['GET']['TABLE'];
$r     = '操作成功';
switch ($table) {
    case 'set':
        //set表删除
        $_G['TABLE']['SET']->delData(Cnum($_G['GET']['ID'], 0, true, 0));
        break;
    case 'plug':
    case 'readsort':
    case 'upload':
    case 'download':
    case 'user':
        if ('upload' == $table || 'download' == $table) {
            $fileinfo = $_G['TABLE'][strtoupper($table)]->getData($id);
            if ($fileinfo) {
                $uid      = $fileinfo['uid'];
                $filename = $fileinfo['filename'];
                @unlink("{$_G['SYSTEM']['PATH']}/uploadfiles/{$uid}/{$filename}");
            }
        }
        /*
         * 删除版块
         */
        if ('readsort' == $table) {
            //判断该版块下是否还有自版块
            $childbk = $_G['TABLE']['READSORT']->getData(array('pid' => $_G['GET']['ID']));
            if (!$childbk) {
                $readdatas = $_G['TABLE']['READ']->getDatas(array('sortid' => $_G['GET']['ID']));
                if ($readdatas) {
                    //删除文章下的回复
                    foreach ($readdatas as $value) {
                        //进回收站
                        mysql_query("update `{$_G['MYSQL']['PREFIX']}reply` set `del`=1 where `rid`={$value['id']}");
                        //直接删除
                        //$_G['TABLE']['REPLY'] -> delData(array('rid' => $value['id']));
                    }
                }
                //删除文章
                //进回收站
                mysql_query("update `{$_G['MYSQL']['PREFIX']}read` set `del`=1 where `sortid`={$_G['GET']['ID']}");
                //直接删除
                //$_G['TABLE']['READ'] -> delData(array('sortid' => $_G['GET']['ID']));

                $_G['TABLE'][strtoupper($table)]->delData($_G['GET']['ID']);
            } else {
                $r = '请先删除该板块下的子版块';
            }
        }
        //删除用户
        if ('user' == $table) {
            if (!$_G['GET']['ID'] || 1 == $_G['GET']['ID']) {
                $r = '错误的用户ID或无法删除创始人';
            } else {
                $_G['TABLE']['USER']->delData($_G['GET']['ID']);
            }
        }
        break;
    case 'read':
    case 'reply':
    case 'reply_reply':
        $ids = (array) $_POST['ids'];
        /*delcmd 0:正常 1:移动至回收站 2:移动至审核区 del:删除 empty:清空回收站*/
        if ('empty' == $_POST['delcmd']) {
            if ('read' == $table) {
                $datas = $_G['TABLE']['READ']->getDatas(0, 0, array('del' => 1));
                foreach ($datas as $data) {
                    $_G['TABLE']['REPLY']->delData(array('rid' => $data['id']));
                }
            }
            $_G['TABLE'][strtoupper($table)]->delData(array('del' => 1));
        } else {
            foreach ($ids as $id) {
                $id = Cnum($id);
                if ($id) {
                    if ('del' == $_POST['delcmd']) {
                        $_G['TABLE'][strtoupper($table)]->delData($id);
                        if ('read' == $table) {
                            $_G['TABLE']['REPLY']->delData(array('rid' => $id));
                        }
                    } else {
                        $array        = array();
                        $array['id']  = $id;
                        $array['del'] = Cnum($_POST['delcmd']);
                        $rrdata = $_G['TABLE'][strtoupper($table)]->getData($id);
                        $_G['TABLE'][strtoupper($table)]->newData($array);
                        if ('read' == $table) {
                            $readsortdata = $_G['TABLE']['READSORT']->getData($rrdata['sortid']);
                        } else {
                            if ('reply_reply' == $table) {
                                $replydata = $_G['TABLE']['REPLY']->getData($rrdata['rid']);
                                $readdata  = $_G['TABLE']['READ']->getData($replydata['rid']);
                            } else {
                                $readdata = $_G['TABLE']['READ']->getData($rrdata['rid']);
                            }
                            $readsortdata = $_G['TABLE']['READSORT']->getData($readdata['sortid']);
                        }
                        $_table = $table;
                        if ('reply_reply' == $table) {
                            $_table = 'reply';
                        }
                        $jf                         = array();
                        $jf["post{$_table}jifen"]   = Cnum($readsortdata["post{$_table}jifen"]);
                        $jf["post{$_table}tiandou"] = Cnum($readsortdata["post{$_table}tiandou"]);
                        if (!$jf["post{$_table}jifen"]) {
                            $jf["post{$_table}jifen"] = Cnum($_G['SET']['POST' . strtoupper($_table) . 'JIFEN']);
                        }
                        if (!$jf["post{$_table}tiandou"]) {
                            $jf["post{$_table}tiandou"] = Cnum($_G['SET']['POST' . strtoupper($_table) . 'TIANDOU']);
                        }
                        //还原或审核通过加分给指定用户
                        // 这里jifen和天豆必须都大于0，因为小于0的提前加减了
                        if (!$array['del'] && ($jf["post{$_table}jifen"] >= 0 && $jf["post{$_table}tiandou"] >= 0)) {
                            if ($rrdata['uid']) {
                                UserDataChange(array(
                                    'jifen'   => $jf["post{$_table}jifen"],
                                    'tiandou' => $jf["post{$_table}tiandou"],
                                ), $rrdata['uid'], '+', ('read' == $_table ? '文章' : '回复') . '审核通过');
                            }
                        }
                    }
                }
            }
        }
        break;
    case 'usergroup':
        $_G['TABLE']['USERGROUP']->delData(Cnum($_G['GET']['ID']));
        break;
    default:
        break;
}
if ($_G['GET']['JSON']) {
    ExitJson($r, '操作成功' == $r ? true : false);
}
ExitGourl("index.php?c=app&a=superadmin:index&s={$_G['GET']['OS']}&t={$_G['GET']['OT']}&table={$_G['GET']['TABLE']}&sortid={$_G['GET']['SORTID']}&uid={$_G['GET']['UID']}&date1={$_G['GET']['DATE1']}&date2={$_G['GET']['DATE2']}&pkalert=show&alert=" . urlencode($r) . "&rnd={$_G['RND']}");
