<?php
/**
 * 金戈企业建站系统：新闻管理页面
 *
 *
 * @name 
 * @author 烟鬼 QQ:460874 Email:460874@qq.com
 * @version 0.1  $id  rootnews.php
 * @copyright Copyright (c) 2007 - 2008 烟鬼
 */
session_start();
require('../config.php');
require('rootfun/roothtml.php');
require('rootfun/rootfun.php');

//判断是否是管理员，该函数在myfun.php 中定义
if(!check_root())
	{
		echo '<meta http-equiv="Refresh" content="0; url=../error.php?error=nono" >';
		exit;
	}

//插入页头
rootheader();
?>
<div id="rootmain">
<?php
$action = $_GET['action'];

//新闻分类管理
if ($action == 'rootnc') 
{
     $rootnc = root_all(new_class);
     if (!$rootnc) 
     {
          echo '没有分类...';
     }
     if ($rootnc) 
     {
          $rootnc = setarray($rootnc);
          foreach ($rootnc as $row)
		{
    		echo '分类名称：  '.$title = $row['n_c_name'].'    '; 
    		echo '<a href=rootnews.php?action=delnc&ncid='.$row['n_c_id'].'>删除</a>   ';
    		echo '<a href=rootnews.php?action=upnc&ncid='.$row['n_c_id'].'>编辑</a><br />';
 		}
     }
     

 	nc_form(); 
}

// 添加新闻分类
if ($action == 'innc') 
{
     $innc = my_yz($_POST['innc']);
     $conn = db_connect();
     $result = $conn->query("set names 'gb2312'");
     $result = $conn->query("INSERT INTO new_class VALUES (NULL,'$innc')");
     if (!$result) 
     {
          echo '数据库错误--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     else 
     {
     	echo '添加成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     	exit;
     }
}

//删除新闻分类
if ($action == 'delnc') 
{
     $ncid = $_GET['ncid'];
     $ncid = my_yz($ncid);
     $conn = db_connect();
     $result = $conn->query('SELECT * FROM new_news where n_n_cid='.$ncid);
     if ($result->num_rows > 0) 
     {
          echo '该分类下有新闻,请先删除新闻后在删除新闻分类';
          exit;
     }
     $result = $conn->query('DELETE FROM new_class WHERE n_c_id='.$ncid);
     if (!$result) 
     {
          echo '数据库错误';
          exit;
     }
     else 
     {
     	echo '删除成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}

//编辑新闻分类
if ($action == 'upnc') 
{
     $ncid = $_GET['ncid'];
     $upnc = upncl($ncid);
     upnc($upnc);
}

//更新新闻分类
if ($action == 'inncs') 
{
     $upncs = my_yz($_POST['upncs']);
     $upncid = trim($_POST['upncid']);
     if (upncup($upncid,$upncs)) 
     {
          echo '更新成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
     else 
     {
     	echo '更新失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}

//新闻管理
if ($action == 'rootnews') 
{
	//senews()函数在rootfun.php定义，它会将所有的新闻内容取回，并做分页等等一系列处理
	//是个比较复杂的函数。它没有返回值。
     senews();
}

//删除新闻
if ($action == 'delnews') 
{
     $del = $_POST['del_me'];
     if (count($del) > 0) 
     {
          foreach ($del as $nid)
          {
          	if (delnews($nid)) 
          	     echo '删除成功....';
          	else 
          		echo '删除失败...';
          }
     }
     else 
     {
     	echo '您还没有选择需要删除的新闻';
     }
}

//编辑新闻
if ($action == 'endnews') 
{
   $nid = $_GET['nid'];
   $upnews = root_alli(new_news,n_n_id,$nid);
   $upnews = setassoc($upnews);
   display_news_form($upnews);
}

//更新新闻
if ($action == 'upnews') 
{
	$title = my_yz($_POST['title']);
	$cop = my_yz($_POST['cop']);
	$cid = my_yz($_POST['cid']);
	$wztxt = my_yz($_POST['wztxt']);
	$nid = my_yz($_POST['nid']);
	
	if (upnews($title,$cop,$cid,$wztxt,$nid)) 
	{
	     echo '更新成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
	}
	else 
	{
		echo '更新失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
	}
}

//添加新闻表单页
if ($action == 'rootinnews') 
{
     display_news_form();
}

//添加新闻处理页
if ($action == 'innews') 
{
	$title = my_yz($_POST['title']);
	$cop = my_yz($_POST['cop']);
	$cid = my_yz($_POST['cid']);
	$wztxt = my_yz($_POST['wztxt']);
	$time = date('Y-m-d');
	$select = $_POST['select'];
	//验证表单填写是否完整
     if (!filled_out($_POST)) 
     {
          echo '请完整填写表单--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     if ($select == 'no') 
     {
          echo '请先添加新闻分类--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
          exit;
     }
     if (innews($cid,$title,$time,$cop,$wztxt)) 
     {
          echo '添加成功--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
     else 
     {
     	echo '添加失败--<a href='.$_SERVER['HTTP_REFERER'].'>返 回</a>';
     }
}
?>
</div>
<?php
//插入页脚
rootfooter();
?>