<?php
class NewslistAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	public function index(){
		$this->checkUser('3_2_1');
		if($_GET['pid']){
			$pid = $_GET['pid'];
			$where = "pid = $pid";
		}elseif($_GET['uid']){
			$uid = $_GET['uid'];
			$where = "uid = $uid";
		}else{
			$where = '1=1';
		}
		$data = M('News');
		import("@.ORG.Page");
		$count = $data->where($where)->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->where($where)->order('state,taxis,time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		$this->assign('list',$list);
		$p->setConfig('header','条记录');
		$p->setConfig('prev','<<上一页');
		$p->setConfig('next','下一页>>');
		$p->setConfig('first','<<第一页'); 
        $p->setConfig('last','最后一页>>'); 
		$page = $p->show();
		$this->assign("page", $page);
		$nav = M('Nav')->where('type=1')->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($nav as $key=>$value){
			$nav[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign('nav',$nav);
		$this->display();
	}
	public function del(){
		$this->checkUser('3_2_2');
		$id = (int)$_GET['id'];
		$data = M('News');
		$pic = $data->where("id = $id")->find();
		@unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/s_'.$pic['pic']);
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!');
		M('Reply')->where("pid = $id")->delete();
		$this->success('删除成功!');
	}
	public function stop(){
		$this->checkUser('3_2_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		if($list['state'] == '1'){
			$id = $list['id'];
			$news = M('News')->field('id,author,email')->where("id = $id")->find();
			$passemail = C('passemail');
			if(!empty($news['email'])  && $passemail){
				import("@.ORG.Smtp");
				$smtpserver = C('smtpserver'); 
				$port = C('smtpport');
				$smtpuser = C('smtpuser');
				$smtppwd = C('smtppwd');
				$mailtype = "TXT";
				$sender = C('smtpuser');
				$smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
				$to = $news['email']; 
				$subject = iconv('UTF-8','gb2312',C('smtpsubject'));
				$smtpbody = str_replace('{username}',$news['author'],C('smtpbody'));
				$smtpbody = str_replace('{url}',C('URL').U('Home/Index/news?id='.$news['id']),$smtpbody);
				$smtpbody = str_replace('{time}',switchtime($_SERVER['REQUEST_TIME']),$smtpbody);
				$body = iconv('UTF-8','gb2312',$smtpbody);
				$send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype);
			}
		}
		$data = M('News');
		if(!$data->save($list)) $this->error('操作失败!请重试');
		$this->success('操作成功!');
	}
	public function edit(){
		$this->checkUser('3_2_2');
		$id = (int)$_GET['id'];
		$data = M('News');
		$news = $data->where("id = $id")->find();
		$this->assign('news',$news);
		if(!empty($news['video'])){
			$type['showVideo']='style="display:block;"';
			$type['focusVideo']= 'background:#CCC;';
		}else{
			$type['showVideo']='style="display:none;"';
		}
		if(!empty($news['pic'])){
			$type['showPic']='style="display:block;"';
			$type['focusPic']= 'background:#CCC;';
		}else{
			$type['showPic']='style="display:none;"';
		}
		$this->assign('type',$type);
		$this->display();
	}
	public function amend(){
		if(empty($_POST['title'])) $this->error('标题不能为空吧!!');	
		$this->checkUser('3_2_2');
		unset($_POST['__hash__']);
		$_POST['time'] = $_SERVER['REQUEST_TIME'];
		if($_FILES['pic']['error']=='4'){
			if(empty($_POST['video'])) $_POST['pid'] = 2 ;
			else $_POST['pid'] = 4;
			$this->_amend();
		}else{
			$id = (int)$_POST['id'];
			$data = M('News');
			$pic = $data->field('pic')->where("id = $id")->find();
			unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/m_'.$pic['pic']);
			unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/s_'.$pic['pic']);
			import("@.ORG.UploadFile");
			$upload = new UploadFile();
			$upload->maxSize =10971520;
			$upload->allowExts = explode(',','jpg,gif,png,jpeg');
			$upload->savePath ='Public/news/';
			if($_FILES['pic']['type']!='image/gif'){
				$upload->thumb=true;
				$upload->imageClassPath = '@.ORG.Image';
				$upload->thumbPrefix = 'm_,s_';
				$upload->thumbMaxWidth  = C('newsmwidth').','.C('newsmwidth');
				$upload->thumbMaxHeight = C('newsmheight').','.C('newsmheight');
				$upload->thumbRemoveOrigin = true;
			}
			$upload->saveRule = uniqid;
			if(!$upload->upload()){
				$this->error($upload->getErrorMsg());
			}else{
				$uploadList = $upload->getUploadFileInfo();
				$_POST['pic'] = $uploadList[0]['savename'];
				if($_FILES['pic']['type']!='image/gif'){
					import("@.ORG.Image");
					Image::watermark($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename']);
					Image::watermark($uploadList[0]['savepath'].'s_'.$uploadList[0]['savename']);
				}else{
					copy($uploadList[0]['savepath'].$uploadList[0]['savename'],$uploadList[0]['savepath'].'m_'.$uploadList[0]['savename']);
					copy($uploadList[0]['savepath'].$uploadList[0]['savename'],$uploadList[0]['savepath'].'s_'.$uploadList[0]['savename']);
					unlink($uploadList[0]['savepath'].$uploadList[0]['savename']);
				}
			}
			$_POST['pid'] = 3;
			$this->_amend();
		}
	}
	public function _amend(){
		$this->checkUser('3_2_2');
		$data = M('News');
		if(!$data->save($_POST)) $this->error('编辑失败!!');
		$this->assign('jumpUrl','__URL__/');
		$this->success('编辑成功!');
	}
	public function add(){
		$this->checkUser('3_2_2');
		$this->display();
	}
	public function check(){
		if(empty($_POST['content'])) $this->error('不是我强迫你，内容必须有！对吧？');	
		$this->checkUser('3_2_2');
		$_POST['content'] = stripslashes($_POST['content']);
		$_POST['uid'] = '1';
		$_POST['state']  = '1';
		$_POST['author'] = '匿名';
		unset($_POST['__hash__']);
		$_POST['tag'] =  htmlspecialchars(trim($_POST['tag']));
		$_POST['showname'] =0;
		$_POST['time'] = $_SERVER['REQUEST_TIME'];
		if(!empty($_POST['type'])) $_POST['type'] = implode(',',$_POST['type']);
		$_POST['content'] = stripslashes($_POST['content']);
		if($_FILES['pic']['error']=='4'){
			$_POST['pid'] = 1;
			$this->_check();
		}else{
			$_POST['pid'] = 2;
			$this->_upload();
			$this->_check();
		}
	}
	public function _check(){
		$this->checkUser('3_2_2');
		$data = M('News');
		if(!$data->add($_POST)) $this->error('添加失败！');
		$this->assign('jumpUrl','__URL__/');
		$this->success('添加成功!');
	}
	public function _upload(){
		$this->checkUser('3_2_2');
		import("@.ORG.UploadFile");
		$upload = new UploadFile();
		$upload->maxSize =10485760;
		$upload->allowExts = explode(',','jpg,gif,png,jpeg');
		$upload->savePath ='Public/news/'; 
		$upload->thumb=true;
		$upload->imageClassPath = '@.ORG.Image';
		$upload->thumbPrefix = 'm_,s_';
		$upload->thumbMaxWidth  = C('newsmwidth').','.C('newsmwidth');
		$upload->thumbMaxHeight = C('newsmheight').','.C('newsmheight');
		$upload->saveRule = uniqid;
		$upload->thumbRemoveOrigin = true;
		if(!$upload->upload()){
			$this->error($upload->getErrorMsg());
		}else{
			$uploadList = $upload->getUploadFileInfo();
			$_POST['pic'] = $uploadList[0]['savename'];
			import("@.ORG.Image");
			Image::watermark($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename']);
			Image::watermark($uploadList[0]['savepath'].'s_'.$uploadList[0]['savename']);
		}
	}
	public function delAll(){
		$this->checkUser('3_2_2');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
		$id = implode(',',$_POST['xuan']);
		M('News')->where("id in ($id)")->delete();
		$this->success('删除成功! ^_^');
	}
	public function shiftAll(){
		$this->checkUser('3_2_2');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
		$id = implode(',',$_POST['xuan']);
		$data['pid'] = $_POST['pid'];
		M('News')->where("id in ($id)")->data($data)->save();
		$this->success('转移成功! ^_^');
	}
	public function tag(){
		$this->checkUser('3_3_1');
		$data = M('Tag');
		import("@.ORG.Page");
		$count = $data->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->limit($p->firstRow.','.$p->listRows)->order('taxis ASC,id DESC')->select();
		$this->assign('list',$list);
		$page = $p->show();
		$this->assign("page",$page);
		$this->display();
	}
	public function addTag(){
		$this->checkUser('3_3_2');
		$this->display();
	}
	public function checkTag(){
		$this->checkUser('3_3_2');
		$list['name'] = trim($_POST['name']);
		if(empty($list['name'])) $this->error('请添加标签名字吧!');
		$list['url'] = trim($_POST['url']);
		$list['taxis'] = (int)$_POST['taxis'];
		M('Tag')->add($list);
		$this->success('添加成功!');
	}
	public function delTag(){
		$this->checkUser('3_3_2');
		$id = (int)$_GET['id'];
		if(M('Tag')->where("id = $id")->delete()) $this->success('删除成功!');
		else $this->error('删除失败!');
	}
	public function editTag(){
		$this->checkUser('3_3_1');
		$id = (int)$_GET['id'];
		$list = M('Tag')->where("id = $id")->find();
		$this->assign('list',$list);
		$this->display();
	}
	public function amendTag(){
		$this->checkUser('3_3_2');
		$list['name'] = trim($_POST['name']);
		if(empty($list['name'])) $this->error('请添加标签名字吧!');
		$list['url'] = trim($_POST['url']);
		$list['taxis'] = (int)$_POST['taxis'];
		$list['id'] = (int)$_POST['id'];
		M('Tag')->save($list);
		$this->success('修改成功!');
	}
}
?>