<?php
/**
* FILE_NAME : picture_cate.php
*
* @copyright Copyright (c) www.shuangyulin.com
* @author 双鱼林设计
* @package 图片分类管理
* @subpackage
* @version QQ:287307421或254540457
*/

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("picturecategories",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("picture_cate.html");
}

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

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

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

/*------------------------------------------------------ */
//-- 编辑分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
	$catid = $_REQUEST['pid'];
	
	/*--  查询数据  --*/
	$sql = '';
	$sql = "SELECT * FROM {$dbprefix}picturecategories WHERE catid = $catid";
	$cate = $DB->fetch_one_array($sql);
	
	/*--  判断分类级别，查询相应的父级分类  --*/
	$ssql = "SELECT parentid FROM {$dbprefix}picturecategories WHERE catid = '".$cate['parentid']."'";
	$scate = $DB->fetch_one_array($ssql);
	if ($cate['parentid'] == 0 ){
		$parentcate = selcate("picturecategories",1);
	} else {
		$parentcate = selcate("picturecategories",0);
	}
	
	$smarty->assign("css_root",$css_root);
	
	$smarty->assign("catid",$catid);
	$smarty->assign("cate_name",$cate['name']);
	$smarty->assign("cate_parentid",$cate['parentid']);
	$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("picture_cate_edit.html");
}

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