<?php
if (!defined('MOD')) die("access die");
purviewCheck('announcement', 'plugin');
$action = trim( $_GET['action']) ? trim( $_GET['action']) : "manage";
$file = C_P . "/annoument.php";

switch($action)
{
	case 'add':
		checkAdminReferer();
		@unlink($file);
		if($_POST['insert'] == 1)
		{
			unset($channelid);
			$channelid  =  intval($_POST['channelid']);
			$title      =  trim($_POST['title']);
			$content    =  $_POST['content'];
			if(!trim($title) || !trim($content)){
				qbase::showmsg('您必须输入公告标题和内容，请返回修改。','plugin.php?plugin=announcement&action=add');
			}else{
				$addtime  =  $_SERVER['REQUEST_TIME'];
				$id = qdb::Query("INSERT INTO `".TABLEPRE."announcement` (`channelid`,`title`, `content`,`addtime`) VALUES ('$channelid','$title', '$content','$addtime')", true);
				qbase::showmsg('操作成功！','default','plugin.php?plugin=announcement&action=manage');
			}
		}
	break;

	case 'edit':
		checkAdminReferer();
		@unlink($file);
		if($_POST['insert']){
			$announceid = intval($_POST['announceid']);
			$channelid  = intval($_POST['channelid']);
			$title      = htmlspecialchars($_POST['title']);
			$content    = stripslashes(addslashes($_POST['content']));
			if(!trim($title) || !trim($content)){
				qbase::showmsg('您必须输入公告标题和内容，请返回修改。','default','plugin.php?plugin=announcement&action=edit&announceid=$announceid');
			}else{
				qdb::Query("UPDATE `".TABLEPRE."announcement` SET `title`='$title',`channelid`='$channelid', `content`='$content' WHERE id='$announceid'");
				qbase::showmsg('操作成功！','default','plugin.php?plugin=announcement&action=manage');
			}
		}else{
			$announceid = intval($_GET['announceid']);
			$query  = "select * from `".TABLEPRE."announcement` where `id`='$announceid' limit 1";
			$r      = qdb::QueryFirst($query);
			$r[content]  = htmlspecialchars(stripslashes($r[content]));
		}
	break;

	case 'manage':
		$announcements = '';
		unset($channelid);
		if( $admin_info['groupid'] == 3){
			if(!empty($manage_channel)) $where=" where channelid IN (".implode(',',$manage_channel).") " ;
		}else if(isset($_GET['channelid'])){
			$where=" where channelid=".intval($_GET['channelid']) ;
		}else{
			$where=" where 1=1" ;
		}
		$query = qdb::fetchAll("SELECT * FROM `".TABLEPRE."announcement` ".$where." ORDER BY `id` DESC");
		$announces = array();
		foreach($query as  $announce){
			$announce['addtime']  = qbase::formatTime('Y/md',$announce['addtime']);
			$announces[] = $announce;
		}
	break;

	case 'delete':
		checkAdminReferer();
		@unlink($file);
		$announceid = $_GET['announceid'] ? $_GET['announceid'] : $_POST['announceid'];
		if(empty($announceid)) qbase::showmsg('error request','default','plugin.php?plugin=announcement&action=manage');
		$announceids = is_array($announceid) ? implode(',',$announceid) : $announceid;
		qdb::Query("DELETE FROM `".TABLEPRE."announcement` WHERE id IN (".$announceids.")");
		qbase::showmsg('操作成功！','default','plugin.php?plugin=announcement&action=manage');
	break;
}
include qbase::template($path . 'plugin/announcement');
?>
