<?php
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!check_submit('pluginssubmit')) {
    if(!$job) {
        $query = $db->query("SELECT * FROM {$dbpre}bcastr ORDER BY orders");
        while($row = $db->fetch_array($query)) {
            $contents .= "<tr>\r\n";
            $contents .= "<td><input type=\"checkbox\" name=\"bcastr_ids[]\" value=\"{$row['bcastr_id']}\" /></td>\r\n";
            $contents .= "<td><input type=\"text\" name=\"bcastr[{$row['bcastr_id']}][orders]\" value=\"{$row['orders']}\" class=\"txtbox5\" /></td>\r\n";
            $contents .= "<td><input type=\"checkbox\" name=\"bcastr[{$row['bcastr_id']}][available]\" value=\"1\"".($row['available']?' checked="checked"':'')." /></td>\r\n";
            $contents .= "<td><input type=\"text\" name=\"bcastr[{$row['bcastr_id']}][itemtitle]\" value=\"{$row['itemtitle']}\" class=\"txtbox2\" /></td>\r\n";
            $contents .= "<td><input type=\"text\" name=\"bcastr[{$row['bcastr_id']}][link]\" value=\"{$row['link']}\" class=\"txtbox2\" /></td>\r\n";
            $contents .= "<td><a href=\"?action={$action}&job=edit&bcastr_id={$row['bcastr_id']}\">查看</a></td>\r\n";
            $contents .= "</tr>\r\n";
        }
    } elseif($job == 'edit') {
        $bcastr_id = intval($bcastr_id);
        (!$bcastr_id || $bcastr_id < 0) && cpmsg('无效的ID，请返回。');
        $bcastr = $db->get_one("SELECT * FROM {$dbpre}bcastr WHERE bcastr_id='$bcastr_id'");
        !$bcastr && cpmsg('内容不存在，请返回。');
        extract($bcastr, EXTR_SKIP);
    }
} else {

    if(!$job) {
        if($bcastr) {
            foreach($bcastr as $bcastr_id => $val) {
                $bcastr_id = intval($bcastr_id);
                if($bcastr_ids && in_array($bcastr_id,$bcastr_ids)) {
                    $db->query("DELETE FROM {$dbpre}bcastr WHERE bcastr_id='$bcastr_id'");
                } else {
                    $itemtitle = html_special_chars_ex($val['itemtitle']);
                    $link = $val['link'];
                    $orders = intval($val['orders']);
                    $available = intval($val['available']);

                    $db->update("UPDATE {$dbpre}bcastr SET available='$available',itemtitle='$itemtitle',link='$link',orders='$orders' WHERE bcastr_id='$bcastr_id'");
                }
            }
            write_js();
            cpmsg('更新完毕!','admincp.php?action=bcastrmanage');
        } else {
            cpmsg('无内容更新!','admincp.php?action=bcastrmanage');
        }

    } elseif($job=='edit'||$job=='add') {

        require_once MUDDER_ROOT.'./include/upload.class.php';
        $upimg = new Upload_Image_calss($_FILES['picture']);

        $itemtitle = html_special_chars_ex($itemtitle);
        $link = $link;
        $orders = intval($orders);
        $available = intval($available);
        //$item_url = html_special_chars_ex($item_url);

        $isedit = $job=='edit';

        if(!$itemtitle) {
            cpmsg('未填写标题，请返回修改。');
        } elseif(!$link) {
            cpmsg('未填写链接地址，请返回修改。');
        }

        !$isedit && $upimg->error && cpmsg($upimg->error.'请返回修改。');
        $isedit && $_FILES['picture'] && $upimg->error && cpmsg($upimg->error.'请返回修改。');

        if($_FILES['picture'] && !$upimg->error) {
            $upimg->folder = 'bcastr';
            $upimg->max_size = $_config['picture_upload_size'];

            $upimg->upload();
            $upimg->error && cpmsg($upimg->error.'请返回修改。');

            $item_url = '/uploads/'.$upimg->folder.'/'.$upimg->filename;
        }
        
        if($isedit) {
            $set_item_url = $item_url ? ",item_url='$item_url'" : '';
            $db->update("UPDATE {$dbpre}bcastr SET available='$available',itemtitle='$itemtitle',link='$link' {$set_item_url},orders='$orders' WHERE bcastr_id='$bcastr_id'");
        } elseif(!$isedit) {
            $db->query("INSERT INTO {$dbpre}bcastr (available,itemtitle,link,item_url,orders) VALUES ('$available','$itemtitle','$link','$item_url','$orders')");
            
        }
        write_js();
        $isedit && cpmsg('内容信息完毕!', 'admincp.php?action=bcastrmanage') || cpmsg('内容增加完毕!', 'admincp.php?action=bcastrmanage');
    }
    
}

function write_js() {
    global $db,$dbpre,$charset;
    $bcastrs = $db->get_all("SELECT * FROM {$dbpre}bcastr WHERE available=1 ORDER BY orders");
    $files = $links = $texts = array();
    if($bcastrs) foreach($bcastrs as $bcastr) {
        $files[] = '.'.$bcastr['item_url'];
        $links[]= $bcastr['link'];
        $texts[]= $bcastr['itemtitle'];
    }
    $jsfile = MUDDER_PLUGINDIR.'bcastr/bcastr.js';
    $fp = @fopen($jsfile, 'r');
    $filecontent = @fread($fp, @filesize($jsfile));
    @fclose($fp);
    $filecontent = preg_replace("/(var files = )\".*?\";/is","\\1\"".implode("|",$files)."\";",$filecontent);
    $filecontent = preg_replace("/(var links = )\".*?\";/is","\\1\"".implode("|",$links)."\";",$filecontent);
    $filecontent = preg_replace("/(var texts = )\".*?\";/is","\\1\"".implode("|",$texts)."\";",$filecontent);
    if($fp = @fopen($jsfile, 'w')) {
        @fwrite($fp, trim($filecontent));
        @fclose($fp);
    } else {
        echo 'Can not write to bcastr.js cache files, please check directory ./plugins/bcastr/ .';
        exit;
    }
}
?>
<div id="body">
<?if(!$job){?>
<form method="post" action="admincp.php?action=bcastrmanage&">
<div class="space">
    <div class="subtitle">图片管理</div>
        <table class="maintable" border="0" cellspacing="0" cellpadding="0">
            <tr class="altbg1">
                <td width="5%">删?</td>
                <td width="10%">排序</td>
                <td width="6%">可用</td>
                <td width="35%">标题</td>
                <td width="34%">链接地址</td>
                <td width="10%">编辑</td>
            </tr>
            <?=$contents?>
            <tr class="altbg1"><td colspan="6"><input type="button" value="全选" class="btn2" onclick="checkbox_checked('bcastr_ids[]');" />&nbsp;<input type="button" value="增加" class="btn2" onclick="location.href='admincp.php?action=bcastrmanage&job=add'"/></td></tr>
    </table>
    <center><input type="submit" name="pluginssubmit" value=" 提交 " class="btn" /></center>
</div>
</form>
<?}elseif($job=='add'||$job=='edit'){?>
<form method="post" action="admincp.php?action=bcastrmanage&job=<?=$job?>&" enctype="multipart/form-data">
<?if($job=='edit'){?>
<input type="hidden" name="bcastr_id" value="<?=$bcastr_id?>" />
<?}?>
<div class="space">
    <div class="subtitle"><?=$job=='add'?'增加内容':'编辑内容'?></div>
    <table class="maintable" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td class="altbg1" width="20%">标题</td>
            <td width="80%"><input type="text" name="itemtitle" class="txtbox" value="<?=$itemtitle?>" /></td>
        </tr>
        <tr>
            <td class="altbg1">可用</td>
            <td><input type="checkbox" name="available" value="1"<?=$available?' checked="checked"':''?> /></td>
        </tr>
        <tr>
            <td class="altbg1">顺序</td>
            <td><input type="text" name="link" class="txtbox5" value="<?=$job=='add'?'0':$orders?>" /></td>
        </tr>
        <tr>
            <td class="altbg1">网址</td>
            <td><input type="text" name="link" class="txtbox" value="<?=$link?>" /></td>
        </tr>
        <tr>
            <td class="altbg1">图片</td>
            <td>
                <?if($job=='edit'){?><span id="reload"><a href="<?=$_config['siteurl'].$item_url?>" target="_blank"><?=$item_url?></a></span>&nbsp;&nbsp;[<a href="javascript:reload();" id="switch">重新上传</a>]<?}else{?><input type="file" name="picture" size="50" class="upfile"><?}?>
            </td>
        </tr>
    </table>
    <center><input type="submit" name="pluginssubmit" value=" 提交 " class="btn" />&nbsp;<input type="button" value=" 返回 " class="btn" onclick="history.go(-1);" /></center>
</div>
</form>
<script type="text/javascript">
var g;
function reload() {
    var obj = document.getElementById('reload');
    var btn = document.getElementById('switch');
    if(obj.innerHTML.match(/^<.+href=.+>/)) {
        g = obj.innerHTML;
        obj.innerHTML = '<input type="file" name="picture" size="50" class="upfile">';
        btn.innerHTML = '取消上传';
    } else {
        obj.innerHTML = g;
        btn.innerHTML = '重新上传';
    }
}
</script>
<?}?>
</div>