<?php 
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//某一个attach
	case "":
		$albumid = isset($_GET['id']) ? intval($_GET['id']) : '0';
		
		$strAlbum = $new['attach']->find('attach_album',array(
			'albumid'=>$albumid,
		));
		//404
			if($albumid!=0)
			{
				
				if($strAlbum==''){
				header("HTTP/1.1 404 Not Found");
				header("Status: 404 Not Found");
				$title = '404';
				include pubTemplate("404");
				exit;
				if($strAlbum['isaudit']==1){
				tsNotice('内容审核中...');
				}
			}
		}

		

		
		$strAlbum['albumname'] = tsTitle($strAlbum['albumname']);
		$strAlbum['albumdesc'] = tsTitle($strAlbum['albumdesc']);
		
		
		$page = isset($_GET['page']) ? intval($_GET['page']) : '1';
		
		$url = tsUrl('attach','album',array('id'=>$albumid,'page'=>''));
		
		$lstart = $page*24-24;
		
		$strUser = aac('user')->getSimpleUser($strAlbum['userid']);
		if($albumid!=0)
		{
			$arrattach = $new['attach']->findAll('attach',array(
				'albumid'=>$albumid,
			),'attachid desc',null,$lstart.',24');
			
			$attachNum = $new['attach']->findCount('attach',array(
				'albumid'=>$albumid,
			));			
			
		}else
		{
			$userid = aac('user')->isLogin();
			$arrattach = $new['attach']->findAll('attach',array(
				'userid'=>$userid,
				'albumid'=>0,
			),'attachid desc',null,$lstart.',24');
			
			$attachNum = $new['attach']->findCount('attach',array(
				'userid'=>$userid,
			));	
			$strAlbum['albumname']="个人附件库";
			$strAlbum['userid']=$userid;
			
		}


		foreach($arrattach as $key=>$item){
			$arrattach[$key]['attachdesc'] = tsTitle($item['attachdesc']);
		}
		
		$pageUrl = pagination($attachNum, 24, $page, $url);
		

		$title = $strAlbum['albumname'];

		
		include template("album");
		
		$new['attach']->update('attach_album',array(
			'albumid'=>$strAlbum['albumid'],
		),array(
			'count_view'=>$strAlbum['count_view']+1,
		));
		
		
		break;

		
	//修改attach
	case "edit":
	
		//用户是否登录
		$userid = aac('user')->isLogin();
		
		$albumid = intval($_GET['albumid']);
		
		$strAlbum = $new['attach']->find('attach_album',array(
			'albumid'=>$albumid,
		));
		
		if($strAlbum['userid'] == $userid || $TS_USER['isadmin']==1) {

            $strAlbum['albumname'] = tsTitle($strAlbum['albumname']);
            $strAlbum['albumdesc'] = tsTitle($strAlbum['albumdesc']);
		
			$title = '修改attach属性-'.$strAlbum['albumname'];
			include template("album_edit");
		
		}else{
		
			tsNotice('非法操作！');
		
		}
		
		break;
	
	case "editdo":
	
		//用户是否登录
		$userid = aac('user')->isLogin();
	
		$albumid = intval($_POST['albumid']);
		
		$strAlbum = $new['attach']->find('attach_album',array(
			'albumid'=>$albumid,
		));
		
		if($strAlbum['userid']==$userid || $TS_USER['isadmin']==1){
		
			$albumname = trim($_POST['albumname']);
			if($albumname == '') qiMsg("attach名称不能为空！");
			
			$albumdesc = trim($_POST['albumdesc']);
			
			
			if($TS_USER['isadmin']==0){
				//过滤内容开始
				aac('system')->antiWord($albumname);
				aac('system')->antiWord($albumdesc);
				//过滤内容结束
			}
			
			$new['attach']->update('attach_album',array(
				'userid'=>$strAlbum['userid'],
				'albumid'=>$strAlbum['albumid'],
			),array(
				'albumname'=>$albumname,
				'albumdesc'=>$albumdesc,
			));

			header("Location: ".tsUrl('attach','album',array('id'=>$albumid)));
		}else{
			tsNotice('非法操作！');
		}
		break;
		

		

	
	//删除attach
	case "del":

		//用户是否登录
		$userid = aac('user')->isLogin();
	
		$albumid = intval($_GET['albumid']);
		
		$strAlbum = $new['attach']->find('attach_album',array(
			'albumid'=>$albumid,
		));
		
		if($strAlbum['userid'] == $userid || $TS_USER['isadmin'] == 1) {
		
			$new['attach']->delete('attach_album',array(
				'albumid'=>$albumid,
			));
			
			$new['attach']->delete('attach',array(
				'albumid'=>$albumid,
			));
			
		
		}

		
		header("Location: ".tsUrl('attach'));
		
		break;
}