<?php
/**
* FILE_NAME : article_cate.php
*
* @copyright Copyright (c) 2009 - 2010 www.dali.edu.cn
* @author DaLi University 2009 Graduation Design
* @package 文章分类管理
* @subpackage
* @version Sun Oct 11 02:37:12 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 = $cates = '';
	
	$temcates = tree("articlecategories",0);
	
	/*--  整理数组，方便smarty输出  --*/
	$k = 0;
	for ($i = 0; $i<count($temcates); $i++ )
	{
		/*--  找出一级分类  --*/
		if ($temcates[$i]['parentid'] == 0)
		{
			$temcates[$i]['children'] = '';
			$cates[$k] = $temcates[$i];
			$m = 0;
			for ($j = 0; $j<count($temcates); $j++ )
			{
				/*--  找出二级分类  --*/
				if ($temcates[$j]['parentid'] != 0 && $temcates[$j]['parentid'] == $temcates[$i]['catid'])
				{
					$temcates[$j]['children'] = '';
					$cates[$k]['children'][$m] = $temcates[$j];
					
					for ($n = 0; $n<count($temcates);$n++)
					{
						/*--  找出三级分类  --*/
						if ($temcates[$n]['parentid'] != 0 && $temcates[$n]['parentid'] == $temcates[$j]['catid'])
						{
							$temcates[$n]['children'] = '';
							$cates[$k]['children'][$m]['children'][] = $temcates[$n];
						}
					}
					$m++;
				}
			}
			$k++;
		}
		
	}
	
	$smarty->assign("cates",$cates);
	$smarty->assign("css_root",$css_root);
	 
	$smarty->display("article_cate.html");
}

/*------------------------------------------------------ */
//-- 删除分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$id = $_REQUEST['pid'];
	
	delcate("articlecategories",$id);
	
	showmsg("分类删除成功","article_cate.php?act=show");
}

/*------------------------------------------------------ */
//-- 增加分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
	$id = $_REQUEST['pid'];
	
	$smarty->assign("pid",$id);
	$smarty->assign("css_root",$css_root);
	$smarty->assign("web_url",$web_url);
	 
	$smarty->display("article_cate_add.html");
}

/*------------------------------------------------------ */
//-- 增加分类成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok')
{
	$cate = $_REQUEST['cate'];
	
	$sql = $query = '';
	$sql = "INSERT INTO {$dbprefix}articlecategories (catid,parentid,name,note,covertemplate,template,url,displayorder) VALUE ('','".$cate['pid']."','".$cate['name']."','".$cate['note']."','".$cate['covertemplate']."','".$cate['template']."','".$cate['url']."','".$cate['displayorder']."')";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("分类添加成功","article_cate.php?act=show");
	} else {
		showmsg("分类添加失败","article_cate.php?act=show");
	}
	
}

/*------------------------------------------------------ */
//-- 编辑分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$catid = $_REQUEST['pid'];
	
	/*--  查询数据  --*/
	$sql = '';
	$sql = "SELECT * FROM {$dbprefix}articlecategories WHERE catid = $catid";
	$cate = $DB->fetch_one_array($sql);
	
	/*--  判断分类级别，查询相应的父级分类  --*/
	$ssql = "SELECT parentid FROM {$dbprefix}articlecategories WHERE catid = '".$cate['parentid']."'";
	$scate = $DB->fetch_one_array($ssql);
	if ($cate['parentid'] == 0 ) {
		$parentcate = selcate("articlecategories",1);
	} else {
		$parentcate = selcate("articlecategories",0);
	}
	
	$smarty->assign("css_root",$css_root);
	$smarty->assign("web_url",$web_url);
	
	$smarty->assign("catid",$catid);
	$smarty->assign("cate_name",$cate['name']);
	$smarty->assign("cate_parentid",$cate['parentid']);
	$smarty->assign("cate_url",$cate['url']);
	$smarty->assign("cate_covertemplate",$cate['covertemplate']);
	$smarty->assign("cate_template",$cate['template']);
	$smarty->assign("cate_note",$cate['note']);
	$smarty->assign("cate_displayorder",$cate['displayorder']);
	
	$smarty->assign("parentcate",$parentcate);
	 
	$smarty->display("article_cate_edit.html");
}

/*------------------------------------------------------ */
//-- 编辑分类成功
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'editok')
{
	$cate = $_REQUEST['cate'];
	
	$sql = '';
	$sql = "UPDATE {$dbprefix}articlecategories SET parentid = '".$cate['pid']."', name = '".$cate['name']."',url = '".$cate['url']."', covertemplate = '".$cate['covertemplate']."', template = '".$cate['template']."',note = '".$cate['note']."', displayorder = '".$cate['displayorder']."' WHERE catid = ".$cate['catid'];
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("分类修改成功","article_cate.php?act=show");
	} else {
		showmsg("分类修改失败","article_cate.php?act=show");
	}
}
?>