<?php
/**
* FILE_NAME : article.php
*
* @copyright Copyright (c) 2009 - 2010 www.dali.edu.cn
* @author DaLi University 2009 Graduation Design
* @package 文章内容管理
* @subpackage
* @version Sat Oct 17 00:52:13 GMT 2009
*/

define('IN_WEB', TRUE);
	
require_once( str_replace('\\', '/', dirname(__FILE__)). '/include/global.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])) {
    $_REQUEST['act'] = 'show';
} else {
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 显示文章列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
	$sql = $query = $artlist = '';
	
	/*--  处理分页数据  --*/
	$sql = "SELECT * FROM {$dbprefix}articles";
	$query = $DB->query($sql);
	
	$allnum = $DB->num_rows($query);
	$perpage = 20;
	$curpage = isset($_GET['page']) ? intval($_GET['page']) : 1;
	$php_self = "articles.php?act=show";
	
	/*--  创建分页  --*/
	$pages = multi($allnum, $perpage, $curpage, $php_self, 10);
	
	if (($curpage-1) >= 0) {
		$step = $curpage-1;
	} else {
		$step = 0;
	}
	$startpage = $perpage * $step;
	$sql = "SELECT a.aid,a.catid,a.title,a.username,a.dateline,a.viewnum,c.catid,c.name FROM {$dbprefix}articles a LEFT JOIN {$dbprefix}articlecategories c ON a.catid = c.catid LIMIT $startpage,$perpage";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
		$row['dateline'] = formatdate($_CFG['format_type'],$row['dateline']);
		$artlist[] = $row;
	}
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("pages",$pages);
	$smarty->assign("artlist", $artlist);
	
	$smarty->display("article.html");
}

/*------------------------------------------------------ */
//-- 增加文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	$sql = $query = $artcatelist = $username = '';
	
	/*--  查询分类信息  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}articlecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query))
	{
		$artcatelist[] = $row;
	}
	
	$username = $_SESSION['admin_name'];
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("username",$username);
	$smarty->assign("artcatelist",$artcatelist);
	
	$smarty->display("article_add.html");
}

/*------------------------------------------------------ */
//-- 添加文章成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	$art = $_REQUEST['art'];
	$sql = $query = '';
	
	/*--  插入信息  --*/
	$sql = "INSERT INTO {$dbprefix}articles (aid,catid,title,username,dateline,viewnum) VALUE ('','".$art['catid']."','".$art['title']."','".$art['username']."','".gmtime()."','0')";
	$query = $DB->query($sql);
	$aid = $DB->insert_id();
	
	/*--  插入主要内容  --*/
	$sql = "INSERT INTO {$dbprefix}articlecontent (id,aid,title,content) VALUE ('','".$aid."','".$art['title']."','".$art['content']."')";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文章添加成功","article.php?act=show");
	} else {
		showmsg("文章添加失败","article.php?act=add");
	}
}

/*------------------------------------------------------ */
//-- 编辑文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$aid = $_REQUEST['aid'];
	
	$sql = $query = $artcatelist = '';
	
	/*--  查询分类信息  --*/
	$sql = "SELECT catid,name FROM {$dbprefix}articlecategories";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		$artcatelist[] = $row;
	}
	
	$sql = "SELECT a.aid,a.catid,a.title,a.username,a.dateline,a.viewnum,c.aid,c.content FROM {$dbprefix}articles a LEFT JOIN {$dbprefix}articlecontent c ON a.aid = c.aid WHERE a.aid=$aid";
	$query = $DB->query($sql);
	$list = $DB->fetch_array($query);
	
	$smarty->assign("css_root", $css_root);
	$smarty->assign("web_url", $web_url);
	
	$smarty->assign("art_aid",$list['aid']);
	$smarty->assign("art_catid",$list['catid']);
	$smarty->assign("art_title",$list['title']);
	$smarty->assign("art_user",$list['username']);
	$smarty->assign("art_cont",$list['content']);
	
	$smarty->assign("artcatelist",$artcatelist);
	
	$smarty->display("article_edit.html");
}

/*------------------------------------------------------ */
//-- 提交编辑文章
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'editok')
{
	$art = $_REQUEST['art'];
	$sql = $query = $artcatelist = '';
	
	/*--  修改信息  --*/
	$sql = "UPDATE {$dbprefix}articles SET catid = '".$art['catid']."',title = '".$art['title']."',username = '".$art['user']."' WHERE aid = '".$art['aid']."'";
	$query = $DB->query($sql);
	/*--  修改内容  --*/
	$sql = "UPDATE {$dbprefix}articlecontent SET title = '".$art['title']."',content = '".$art['content']."' WHERE aid = '".$art['aid']."'";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文章修改成功","article.php?act=show");
	} else {
		showmsg("文章修改失败","article.php?act=show");
	}
}

/*------------------------------------------------------ */
//-- 删除单页
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$aid = $_REQUEST['aid'];
	$sql = $query = '';
	
	/*--  删除信息  --*/
	$sql = "DELETE FROM {$dbprefix}articles WHERE aid = $aid";
	$query = $DB->query($sql);
	/*--  删除内容  --*/
	$sql = "DELETE FROM {$dbprefix}articlecontent WHERE aid = $aid";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("文章删除成功","article.php?act=show");
	} else {
		showmsg("文章删除失败","article.php?act=show");
	}
}
?>