<?php
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');
require_once(MUDDER_PLUGINDIR.'announcement/function.inc.php');

$table = "{$dbpre}announcements"; // 操作表

if(!check_submit('pluginssubmit')) {

	if($job == 'new') {

		$available = 1;
		$author = $admin->adminname;
        // 加载 FCK 编辑器
        require_once(MUDDER_ROOT.'include/editor/fckeditor.php');
        $fck = new FCKeditor('content');
        $fck->ToolbarSet = 'Admin';
        $fckhtml = $fck->CreateHtml();

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

	    $data = $db->select_one('*', $table, "id='$id'");
        if(!$data) cpmsg('公告不存在或已删除.', 'stop');
        extract($data, EXTR_OVERWRITE);
        // 加载 FCK 编辑器
        require_once(MUDDER_ROOT.'include/editor/fckeditor.php');
        $fck = new FCKeditor('content');
        $fck->ToolbarSet = 'Admin';
        $fck->Value = $content;
        $fckhtml = $fck->CreateHtml();

	} else {
        $offset = 10;
        $limit = get_limit($page, $offset);
        $query = $db->query("SELECT * FROM $table ORDER BY orders,dateline DESC LIMIT $limit");
	    $HTML_list = "";
        while($row = $db->fetch_array($query)) {
            $id = $row['id'];
            $HTML_list .= "<tr>\r\n";
            $HTML_list .= "\t<td><input type=\"checkbox\" name=\"ids[]\" value=\"$id\" /></td>\r\n";
            $HTML_list .= "\t<td><input type=\"text\" class=\"txtbox2\" name=\"announcement[$id][title]\" value=\"{$row['title']}\" /></td>\r\n";
            $HTML_list .= "\t<td>{$row['pageview']}</td>\r\n";
            $HTML_list .= "\t<td>".date('Y-m-d H:i:s', $row['dateline'])."</td>\r\n";
            $selected = $row['available'] ? ' checked="checked"' : '';
            $HTML_list .= "\t<td><input type=\"checkbox\" name=\"announcement[$id][available]\" value=\"1\"{$selected}/></td>\r\n";
            $HTML_list .= "\t<td><input type=\"text\" class=\"txtbox5\" name=\"announcement[$id][orders]\" value=\"{$row['orders']}\" /></td>\r\n";
            $HTML_list .= "\t<td><a href=\"admincp.php?action=$action&job=edit&id=$id\">查看/编辑</a></td>\r\n";
            $HTML_list .= "</tr>\r\n";
        }
        !$HTML_list && $HTML_list = "<tr><td colspan=\"7\">没有公告信息。</td></tr>";
	}
	
} else {

    if(!$job) {

        if($announcement) {
            foreach($announcement as $id => $val) {
                $id = intval($id);
                if($ids && in_array($id,$ids)) {
                    $db->delete_new($table, "id='$id'");
                } else {
                
                    $list['title'] = html_special_chars_ex($val['title']);
                    $list['orders'] = intval(trim($val['orders']));
                    $list['available'] = $val['available'] = intval(trim($val['available'])) ? 1 : 0;
                    
                    !$list['title'] && cpmsg('未填写公告标题，请返回填写。');
                    $db->update_new($table, "id='$id'", $list);
                }
            }
            Announcement_Cache(true);
            cpmsg('更新完毕!');
            
        } else {

            cpmsg('无内容更新!');
            
        }

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

		$list = array();
		
		if($job == 'edit' && (!$id || !is_numeric($id))) cpmsg('无效的公告ID。');
		
		$list['title'] = $title = html_special_chars_ex($title);
        $list['orders'] = intval(trim($orders));
		$list['available'] = intval(trim($available)) ? 1 : 0;
		$list['author'] = $author = html_special_chars_ex($author);
		// 不对HTML过滤，注意HTML代码安全
		$list['content'] = $content = trim(preg_replace("/\s*(\r\n|\n\r|\n|\r)\s*/", "\r\n", $content));

		!$title && cpmsg('未填写公告标题，请返回填写。');
		!$content && cpmsg('未填写公告内容，请返回填写。');
		
		if($job == 'new') {

		    $list['dateline'] = $timestamp;
			$db->insert_new($table, $list);
            $msg = '公告添加完毕！';
			
		} elseif($job == 'edit') {
			
			$db->update_new($table, "id='$id'", $list);
            $msg = '公告更新完毕！';
			
		}

        Announcement_Cache(true);
        cpmsg($msg);
		
	} else {
	    cpmsg('无效的提交操作。', 'stop');
	}
	
}
?>
<div id="body">
<form method="post" action="admincp.php?action=<?=$action?>&">
	<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="30%">菜单名称</td>
				<td width="10%">连接地址</td>
				<td width="20%">图标</td>
                <td width="15%">显示顺序</td>
			</tr>
			<tr><td colspan="7" class="altbg1"><button type="button" class="btn2" onclick="checkbox_checked('ids[]');">全选</button></td></tr>
		</table>
	<center><input type="submit" name="pluginssubmit" value=" 提交 " class="btn" /></center>
	</div>
</form>
</div>