<?php
defined('IN_TS') or die('Access Denied.');

switch($ts){
	//分类列表 
	case "list":
		
		$arrCate = $new['article']->findAll('article_cate',array(
		    'referid'=>0,
        ),'orderid asc');

		foreach($arrCate as $key=>$item){
		    $arrCate[$key]['twocate'] = $new['article']->findAll('article_cate',array(
		        'referid'=>$item['cateid'],
            ),'orderid asc');
        }

		include template("admin/cate_list");
		
		break;
	
	//分类添加
	case "add":

	    $referid = tsIntval($_GET['referid']);

		include template("admin/cate_add");
		
		break;
		
	case "add_do":


		
		$new['article']->create('article_cate',array(
		    'referid'=>tsIntval($_POST['referid']),
			'catename'=>tsTrim($_POST['catename']),
			'cateinfo'=>tsTrim($_POST['cateinfo']),
			'orderid'=>tsIntval($_POST['orderid']),
		
		));
		
		
		header("Location: ".SITE_URL."index.php?app=article&ac=admin&mg=cate&ts=list");
		
		break;
	
	//分类删除
	case "del":
		
		$cateid = tsIntval($_GET['cateid']);


		$strCate = $new['article']->find('article_cate',array(
		    'cateid'=>$cateid,
        ));

		if($strCate['referid']==0){
		    $arrCate = $new['article']->findAll('article_cate',array(
		        'referid'=>$strCate['cateid'],
            ));

            foreach($arrCate as $key=>$item){
                $new['article']->update('article',array(
                    'cateid'=>$item['cateid']
                ),array(
                    'cateid'=>0,
                ));
            }

            $new['article']->delete('article_cate',array(
                'referid'=>$strCate['cateid'],
            ));

        }


        $new['article']->update('article',array(
            'cateid'=>$cateid
        ),array(
            'cateid'=>0,
        ));

		
		$new['article']->delete('article_cate',array(
			'cateid'=>$cateid,
		));
		
		
		
		
		qiMsg("分类删除成功！");
		
		break;
	
	//分类修改
	case "edit":
	
		$cateid = tsIntval($_GET['cateid']);
		
		$strCate = $new['article']->find('article_cate',array(
			'cateid'=>$cateid,
		));


		include template("admin/cate_edit");
		
		break;
	
	//分类修改执行 
	case "edit_do":
		$cateid = tsIntval($_POST['cateid']);
		$catename = tsTrim($_POST['catename']);
		$cateinfo = tsTrim($_POST['cateinfo']);
		
		$new['article']->update('article_cate',array(
			'cateid'=>$cateid,
		),array(
			'catename'=>$catename,
			'cateinfo'=>$cateinfo,
			'orderid'=>tsIntval($_POST['orderid']),
		));
		
		header("Location: ".SITE_URL."index.php?app=article&ac=admin&mg=cate&ts=list");
		
		break;
}