<!--
whilvydy 新闻发布系统
-->


<%@ page contentType="text/html; charset=GB2312" language="java" import="java.sql.*" %>
<%@ include file = "func/parameter.jsp" %>
<%@ page import="java.io.*" %> 
<%@ page import="java.util.*" %>

<jsp:useBean id="NewsBean" scope="page" class="net.jspcn.db.DbConnect" />
<jsp:useBean id='oMD5' scope='page' class='net.jspcn.tool.MD5'/>
<jsp:useBean id='fileBean' scope='page' class='net.jspcn.tool.UploadBean' />
<jsp:useBean id='StrTool' scope='page' class='net.jspcn.tool.StringProcess' />

<%
int user_power = Integer.parseInt((String) session.getValue("user_power"));
if (user_power<1)
response.sendRedirect("login.jsp");

/*************************************** 本页面程序初始化 **************************************/
/************基本变量定义*************/
boolean isedit = false; 	//是否在编辑状态
int news_id,type_id,board_id;	//定义相关全局变量
String sql,temp="";		//定义sql语句
String filepath = message[7];
ResultSet rs,rs1;		
int intPageCount,intPageSize,intCurrentPage,intRowCount,i;
intPageSize = param[0];
i=0;

/******************处理从left传过来的参数********/
if(request.getParameter("type_id")!=null)
	session.putValue("type_id",request.getParameter("type_id"));
	
if(request.getParameter("board_id")!=null)
	session.putValue("board_id",request.getParameter("board_id"));
type_id = Integer.parseInt((String)session.getValue("type_id"));
board_id = Integer.parseInt((String)session.getValue("board_id"));
//out.println(type_id+","+board_id);

/*************** javabean初始化************/
NewsBean.openConnection();

	fileBean.setObjectPath(filepath);
	fileBean.setSize(200*1024);
	fileBean.setSuffix(".gif.jpg.png.doc.txt");
	fileBean.CreateObjectPath();
	fileBean.setSourceFile(request);
	


/****************************************** 处理本页面的表单 ********************************************/
if("edit".equalsIgnoreCase(request.getParameter("action")))
    isedit=true;

if(fileBean.getFieldValue("news_id")==null&&request.getParameter("news_id")==null)
	news_id = 1;
else
	{
	if(request.getParameter("news_id")==null)
	news_id = Integer.parseInt(fileBean.getFieldValue("news_id").trim());
	else
	news_id = Integer.parseInt(request.getParameter("news_id").trim());
	}
    
/******************************************新闻发布状态*****************************************/
if("pass".equals(request.getParameter("action")))
{
	String news_audit=request.getParameter("news_audit").trim();
	sql = "update "+news_table+" set news_audit="+Integer.parseInt(news_audit)+" where news_id="+news_id;
	NewsBean.executeQuery(sql);
	//log process	
	String sql_in = "insert into "+logrecord_table+" (logrecord_id,user_name,operation_time,logrecord_operation) values('','"+session.getValue("user_name")+"',now(),'change news status to "+news_audit+" type: "+type_id+", board: "+board_id+", news_id: "+news_id+" ')";
	NewsBean.executeUpdate(sql_in);	
}


//************************************修改新闻**************************************
if("modify".equalsIgnoreCase(fileBean.getFieldValue("action")))
{
	//out.println("U have upload "+fileBean.getFieldValue("upcount")+" files");
	String news_title=fileBean.getFieldValue("news_title").trim();
	String news_author=fileBean.getFieldValue("news_author").trim();
	String news_source=fileBean.getFieldValue("news_source").trim();
	String news_audit=fileBean.getFieldValue("news_audit").trim();
	if(!"".equals(news_title)&&!"".equals(news_author)&&!"".equals(news_source))
	{
	//////////////////////////// insert into news;
	sql = "update "+news_table+" set news_title='"+news_title+"',news_author='"+news_author+"',news_source='"+news_source+"',news_audit="+Integer.parseInt(news_audit)+" where news_id="+news_id;
	NewsBean.executeQuery(sql);	
	
	////////////////////// delete news keyword
	sql = "delete from "+keyword_table+" where news_id="+news_id;
	NewsBean.executeQuery(sql);
	String s = fileBean.getFieldValue("keyword_value").trim();
		s=StrTool.StringReplace(s,"，",",");			//替换
		int k;							
	while((k=s.indexOf(","))!=-1)
	{
		sql = "insert into "+keyword_table+" (news_id,keyword_value) values ("+news_id+",'"+s.substring(0,k)+"')";
		NewsBean.executeQuery(sql);
		s = s.substring(k+1,s.length());
	}
	
	sql = "select * from "+newsdetail_table+" where news_id="+news_id;
	rs = NewsBean.executeQuery(sql);
	
	for(i=0;rs.next();i++)
	{
		File file = new File(filepath+rs.getString("newsdetail_img"));
		//out.println(filepath+rs.getString("newsdetail_img"));
		file.delete();
	}
	
	sql = "delete from "+newsdetail_table+" where news_id="+news_id;
	rs = NewsBean.executeQuery(sql);	
	
	String objectfile[] = fileBean.getObjectFileName();
	String description[] = fileBean.getDescription();
	boolean upstate[] = fileBean.getUpState();
	int icount = fileBean.getCount();	
	for(i=0;i<icount;i++)
	{
		if(upstate[i]==false&&!"ERR: File suffix is wrong.".equals(description[i]))
		{
			out.println(description[i]+"上传失败");
		}
		String newcontent = "newsdetail_content"+Integer.toString(i+1);
		String content = StrTool.HtmlEncode(fileBean.getFieldValue(newcontent));
		String img;
		if(objectfile[i]==null)
		img = "";
		else		
		img = StrTool.StringReplace(fileBean.getObjectSubPath(),"\\","\\\\")+objectfile[i];
		//out.println(img+"<br>");
		sql = "insert into "+newsdetail_table+" values('',"+news_id+",'"+content+"','"+img+"','')";
		NewsBean.executeQuery(sql);
	}
	//log process
	String sql_in = "insert into "+logrecord_table+" (logrecord_id,user_name,operation_time,logrecord_operation) values('','"+session.getValue("user_name")+"',now(),'update news where news_id is "+news_id+" ')";
	NewsBean.executeUpdate(sql_in);	

	}
	else
	out.println("请填写完整");
}


//************************************添加新闻**************************************

if("add".equalsIgnoreCase(fileBean.getFieldValue("action")))
{

	
	String news_title=fileBean.getFieldValue("news_title").trim();
	String news_author=fileBean.getFieldValue("news_author").trim();
	String news_source=fileBean.getFieldValue("news_source").trim();
	String news_audit=fileBean.getFieldValue("news_audit").trim();
	if(!"".equals(news_title)&&!"".equals(news_author)&&!"".equals(news_source))
	{
	//////////////////////////// insert into news;
	sql = "insert into "+news_table+" values('',"+type_id+","+board_id+",'"+news_title+"',0,'"+news_source+"','"+news_author+"',now(),now(),'"+Integer.parseInt(news_audit)+"',"+Integer.parseInt((String)session.getValue("user_id"))+","+Integer.parseInt((String)session.getValue("user_id"))+")";
	NewsBean.executeQuery(sql);	
	
	////////////////////// get news_id
	sql = "select news_id from "+news_table+" order by news_id desc limit 1";
	rs = NewsBean.executeQuery(sql);
	rs.next();
	int id = Integer.parseInt(rs.getString("news_id"));

	String objectfile[] = fileBean.getObjectFileName();
	String description[] = fileBean.getDescription();
	boolean upstate[] = fileBean.getUpState();
	
	String s = fileBean.getFieldValue("keyword_value").trim();
		s=StrTool.StringReplace(s,"，",",");			//替换
		s = s + ",";
		int k;							
	while((k=s.indexOf(","))!=-1)
	{
		if(!"".equals(s.substring(0,k))) {
		sql = "insert into "+keyword_table+" (news_id,keyword_value) values ("+id+",'"+s.substring(0,k)+"')";
		NewsBean.executeQuery(sql);
		}
		s = s.substring(k+1,s.length());
	}
	
	
	int icount = fileBean.getCount();
	
	for(i=0;i<icount;i++)
	{
		if(upstate[i]==false&&!"ERR: File suffix is wrong.".equals(description[i]))
		{
			out.println(description[i]+"上传失败");
		}
		
		String newcontent = "newsdetail_content"+Integer.toString(i+1);
		String content = StrTool.HtmlEncode(fileBean.getFieldValue(newcontent));
		String img;
		if(objectfile[i]==null)
		img = "";
		else		
		img = StrTool.StringReplace(fileBean.getObjectSubPath(),"\\","\\\\")+objectfile[i];
		//out.println(img);
		sql = "insert into "+newsdetail_table+" values('',"+id+",'"+content+"','"+img+"','')";
		NewsBean.executeQuery(sql);
	}
	//log process
	String sql_in = "insert into "+logrecord_table+" (logrecord_id,user_name,operation_time,logrecord_operation) values('','"+session.getValue("user_name")+"',now(),'add news type: "+type_id+",board: "+board_id+", news title: "+news_title+" ')";
	NewsBean.executeUpdate(sql_in);	
	}
	else
	out.println("请填写完整");
}



//************************************删除新闻**************************************
if("delete".equalsIgnoreCase(request.getParameter("action")))
{

	if (user_power>=9999) {

	sql = "delete from "+keyword_table+" where news_id ="+news_id;
	NewsBean.executeQuery(sql);
	
	sql = "select * from "+newsdetail_table+" where news_id="+news_id;
	rs = NewsBean.executeQuery(sql);
	
	for(i=0;rs.next();i++)
	{
		File file = new File(filepath+rs.getString("newsdetail_img"));
		//out.println(filepath+rs.getString("newsdetail_img"));
		file.delete();
	}
	
	sql = "delete from "+newsdetail_table+" where news_id ="+news_id;
	NewsBean.executeQuery(sql);
	sql = "delete from "+news_table+" where news_id = "+news_id;
	NewsBean.executeQuery(sql);

	//log process
	String sql_in = "insert into "+logrecord_table+" (logrecord_id,user_name,operation_time,logrecord_operation) values('','"+session.getValue("user_name")+"',now(),'delete news type: "+type_id+",board: "+board_id+", news_id: "+news_id+" ')";
	NewsBean.executeUpdate(sql_in);		
	}
}

%>


<%@ include file = "func/header.jsp" %>

<body>


<script language="javascript">
	function setid()
	{
	str='<div align="left">';
	if(!window.form1.upcount.value)
		window.form1.upcount.value=1;
		//j=1
		//str1 = window.form1.newsdetail_content+j+.value;
		
	for(i=1;i<=window.form1.upcount.value;i++)
		str+='新闻内容'+i+'<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea name="newsdetail_content'+i+'" cols="49" rows="9" class="textare"></textarea><br>相关图片'+i+'&nbsp;&nbsp;<input type="file" name="newsdetail_img'+i+'" size="41" class="input"><br><hr size=0 width=100%>';
		window.upid.innerHTML=str+'</div>';
	}
</script>



<div align=center>
新闻管理
<br>
<table width="90%"  border="1" cellspacing="0" cellpadding="0"  bordercolor="#00CC66"  borderColorDark="#FFFFFF" borderColorLight="#00CC66">
      <tr> 
    <td width="10%"> 
      <div align="center">新闻编号</div>
    </td>
    <td width="35%"> 
      <div align="center">新闻标题</div>
    </td>
    <td width="15%"> 
      <div align="center">新闻作者</div>
    </td>
    <td width="17%"> 
      <div align="center">新闻来源</div>
    </td>
    <td width="23%"> 
      <div align="center">操    作</div>
    </td>
   </tr>
<%
	//out.println(fileBean.getFieldValue("page"));
	if(request.getParameter("page")==""||request.getParameter("page")==null)
		intCurrentPage = 1;
	else
	{
		intCurrentPage = Integer.parseInt(request.getParameter("page"));
		if(intCurrentPage<1) intCurrentPage = 1;
	}									//得到当前页数
	
	sql = "select * from "+news_table+" where type_id="+type_id+" and board_id="+board_id+" order by news_id desc";
	rs = NewsBean.executeQuery(sql);
	rs.last();
	intRowCount = rs.getRow();
	intPageCount = (intRowCount+intPageSize-1)/intPageSize;
	if(intCurrentPage>intPageCount) intCurrentPage=intPageCount;		//对当前页数的控制
		
	if(intPageCount>0)
	{	
	rs.absolute((intCurrentPage-1)*intPageSize+1);	
	//for(color=0,i=0;i<intPageSize&&!rs.isAfterLast();color++,i++)		
	i=0;
	
	do {
%>
  <tr> 
    <td width="10%"> 
      <div align="center"><%=rs.getString("news_id")%></div>
    </td>
    <td width="35%"> 
      <div align="center"><a href="..\newsdetail.jsp?news_id=<%=rs.getString("news_id")%>" target=_blank><%=rs.getString("news_title")%></a></div>
    </td>
    <td width="15%"> 
      <div align="center"><%=rs.getString("news_author")%></div>
    </td>
    <td width="17%"> 
      <div align="center"><%=rs.getString("news_source")%>&nbsp;</div>
    </td>
    <td width="23%"> 
      <div align="center">
      <a href="news_man.jsp?action=pass&news_id=<%=rs.getString("news_id")%>&news_audit=<% if(Integer.parseInt(rs.getString("news_audit"))==1) out.print("0"); else out.print("1"); %>&page=<%=intCurrentPage%>"><% if(Integer.parseInt(rs.getString("news_audit"))==1) {%> 取消审核 <%} else {%> 通过审核 <%} %></a>&nbsp;
      <a href="news_man.jsp?action=edit&news_id=<%=rs.getString("news_id")%>&page=<%=intCurrentPage%>">编辑</a>&nbsp;<% if (user_power>=9999) { %>
      <a href="news_man.jsp?action=delete&news_id=<%=rs.getString("news_id")%>&page=<%=intCurrentPage%>">删除</a><% } %></div>
    </td>
   </tr>
<%	
	i++;
	}
	while(rs.next()&&i<intPageSize);
	}
%>
</table>
<br>
<%
	if(intCurrentPage == 1) { 
		%> 首页 前页 <% }
	else
		out.println("<a href='news_man.jsp?page=1'>首页</a> <a href='news_man.jsp?page="+(intCurrentPage-1)+"'>前页</a> ");
	
	if(intCurrentPage==intPageCount) {
		%>后页 末页<% }
	else
		out.println("<a href='news_man.jsp?page="+(intCurrentPage+1)+"'>后页</a> <a href='news_man.jsp?page="+intPageCount+"'>末页</a> ");

%>
<hr size=0 width=100%>

<%
if(isedit==true)
	{
	       sql = "select * from "+news_table+" where news_id="+news_id;
	       rs = NewsBean.executeQuery(sql);
	       rs.next();
	}
%>

<form action="news_man.jsp" method="post" name="form1" enctype="MULTIPART/FORM-DATA" onsubmit="return formCheck()">
    <input type="Hidden" name="action" value="<%if(isedit==true) out.print("modify"); else out.print("add"); %>">
<%if(isedit==true)
     { 
%>
    <input type="Hidden" name="news_id" value="<%=news_id%>">
<%
	}
%>

    <table width="90%"  border="1" cellspacing="0" cellpadding="2" bordercolor="#00CC66"  borderColorDark="#FFFFFF" borderColorLight="#00CC66">
      <tr> 
        <td><div align="right">新闻标题</div></td>
        <td><div align="left"> 
            <input name="news_title" type="text"  class=input size="16" maxlength=100  value="<% if(isedit==true) out.print(rs.getString("news_title")); %>">
          </div></td>
        <td><div align="right">新闻作者</div></td>
        <td><div align="left"> 
            <input name="news_author" type="text"  class=input size="16" maxlength=100  value="<% if(isedit==true) out.print(rs.getString("news_author")); %>">
          </div></td>
      </tr>
      <tr> 
        <td><div align="right">关键字</div></td>
        <td><div align="left"> 
            <input name="keyword_value" type="text"  class=input size="16" maxlength=100  value="<% if(isedit==true){ sql="select * from "+keyword_table+" where news_id="+news_id+" order by keyword_id"; rs1=NewsBean.executeQuery(sql); while(rs1.next()) temp=temp+rs1.getString("keyword_value")+","; out.println(temp); } %>">
          </div></td>
        <td><div align="right">如右格式：</div></td>
        <td>学习，jsp，配置，</td>
      </tr>
      <tr> 
        <td><div align="right">新闻来源</div></td>
        <td><input name="news_source" type="text"  class=input size="16" maxlength=100 value="<% if(isedit==true) out.print(rs.getString("news_source")); %>"></td>
        <td><div align="right"><input type="button" name="Button" onClick="setid();" value="设定" class="button"></div></td>
        <td><input type="text" value="1" name="upcount" class=input size="16" maxlength=2></td>
      </tr>
      <tr> 
        <td colspan="4" id="upid">
<% if(isedit==true) {
	sql="select * from "+newsdetail_table+" where news_id="+news_id+" order by newsdetail_id"; 
	rs1=NewsBean.executeQuery(sql); 
	for(i=1;rs1.next();i++) {
%>
	<div align="left">新闻内容<%=i%><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea name="newsdetail_content<%=i%>" cols="49" rows="9" class="textare"><%=StrTool.UnHtmlEncode(rs1.getString("newsdetail_content"))%></textarea><br>
	相关图片<%=i%>&nbsp;&nbsp;<input type="file" name="newsdetail_img<%=i%>" size="41" class="input"><br><hr size=0 width=100%><br>
<%
	}
	}
	else
	{
%>
        <div align="left">
        新闻内容1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea name="newsdetail_content1" cols="49" rows="9" class="textare"></textarea><br>
        相关图片1&nbsp;&nbsp;<input type="file" name="newsdetail_img1" size="41" class="input"><br><hr size=0 width=100%>        
        </div>       
<%
	}
%> 
        </td>
      </tr>
      <tr> 
        <td colspan="1">
        <div align="right">状态</div>        
        </td>
        <td colspan="3">
        <div align="left">
        	<input type="radio" name="news_audit" value=1 checked>已审核&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        	<input type="radio" name="news_audit" value=0>未审核    
        </div>
        </td>
      </tr>
    </table>
  <br>
  <input type=submit value="确 定" class=button>&nbsp;&nbsp;&nbsp;&nbsp;<input type=reset value="重 置" class=button>
</form>
 </div>
 
<SCRIPT language=JavaScript>
         function formCheck()
	{
		if(document.form1.news_title.value=="")
            	{
            		alert("请输入新闻标题!");
			document.form1.news_title.focus();
			return false;
		}     
		
		if(document.form1.news_author.value=="")
		{
			alert("请输入新闻作者!");
			document.form1.news_author.focus();
			return false;
		}
		
		if(document.form1.news_source.value=="")
		{
			alert("请输入新闻来源!");
			document.form1.news_source.focus();
			return false;
		}
		
		if(document.form1.newsdetail_content1.value=="")
		{
			alert("请输入新闻内容");
			document.form1.newsdetail_content1.focus();
			return false;
		}

         }
</SCRIPT> 

<% rs.close();
NewsBean.close();
%>
<%@ include file = "func/footer.jsp" %>