<?php
class MainAction extends Action {
	function _initialize(){
		if(!$_SESSION['NR_USER_ID']){
			$this->error('请先登录!','/',3);
		}
    }
    public function index(){
		//退出登录
		if($_GET['login'] == 'out'){
			$login = new UserModel();
			$login->Logout();
		}
		
		//分页显示话题列表
		if($_POST['submit']){
			$User = M("forum"); 
			import("ORG.Util.Page"); // 导入分页类
			$count = $User->where("ishot != 2")->count(); 
			$Page  = new Page($count,25);
			$show  = $Page->show(); // 分页显示输出
			$list  = $User->where("ishot != 2 && title LIKE '%".$_POST['key']."%'")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$User = M("forum"); 
			import("ORG.Util.Page"); // 导入分页类
			$count = $User->where("ishot != 2")->count(); 
			$Page  = new Page($count,25);
			$show  = $Page->show(); // 分页显示输出
			$list  = $User->where('ishot != 2')->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		}
		
		if($_GET['id']){
			$User = M("forum"); 
			import("ORG.Util.Page"); // 导入分页类
			$count = $User->where("ishot != 2 && memu = ".$_GET['id'])->count(); 
			$Page  = new Page($count,25);
			$show  = $Page->show(); // 分页显示输出
			$list  = $User->where("ishot != 2 && memu = ".$_GET['id'])->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			
			$t = M("memu")->select($_GET['id']);
			$litle = $t[0]['title'];
		}else{
			$litle = "最新话题";
		}
		
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		
		//关注我的人
		$fme = M("follower")->where("uid = ".$_SESSION['NR_USER_ID'])->limit(0,16)->select();
		$this->assign("fme",$fme);
		$this->assign("litle",$litle);
		$this->assign("memu",$memu);
		$this->assign("forum",$list);
		$this->assign("page",$show);
        $this->display();
    }
	
	function post(){
		$u = M("user")->select($_SESSION['NR_USER_ID']);
		if($u[0]['isok']){
			$this->error('您已被锁定，不能发帖!','',3);
		}
		if($_POST['submit']){
			$addforum = new UserModel();
			$addforum->add();
			$this->success('发表成功!','/Main.html',3);
		}
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign("memu",$memu);
		$this->display();
	}
	function Mylist(){
		$User = M("forum"); 
		import("ORG.Util.Page"); // 导入分页类
		$count = $User->where("ishot != 2 && pid = ".$_SESSION['NR_USER_ID'])->count(); 
		$Page  = new Page($count,25);
		$show  = $Page->show(); // 分页显示输出
		$list  = $User->where("ishot != 2 && pid = ".$_SESSION['NR_USER_ID'])->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		//关注我的人
		$fme = M("follower")->where("uid = ".$_SESSION['NR_USER_ID'])->limit(0,16)->select();
		$this->assign("fme",$fme);
		$this->assign("memu",$memu);
		$this->assign("forum",$list);
		$this->assign("page",$show);
        $this->display();
	}
	function U_data(){
		$u  = M("user")->select($_SESSION['NR_USER_ID']);
		$ud = M("user_date")->where("uid = ".$_SESSION['NR_USER_ID'])->select();
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		if($_POST['submit']){
			$edu = new UserModel();
			$edu->edu();
			$this->success('修改成功!','/U_data.html',3);
		}
		$this->assign("memu",$memu);
		$this->assign("u",$u);
		$this->assign("ud",$ud);
		$this->display();
	}
	function U_img(){
		header("Expires: 0");
		header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
		header("Pragma: no-cache");
		header("Cache-Control:no-cache");
		
		include_once("./Uploads/avatar.php");
		$au = new AvatarUploader();
		if ( $au->processRequest() ) {
			exit();
		}
		
		// 显示编辑页面，页面中包含 camera.swf
		$uid = $_SESSION['NR_USER_ID'];
		$urlAvatarBig    = $au->getAvatarUrl( $uid, 'big' );
		$urlAvatarMiddle = $au->getAvatarUrl( $uid, 'middle' );
		$urlAvatarSmall  = $au->getAvatarUrl( $uid, 'small' );
		$urlCameraFlash = $au->renderHtml( $uid );
		
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		$this->assign("memu",$memu);
		$this->assign("uid",$uid);
		$this->assign("urlAvatarBig",$urlAvatarBig);
		$this->assign("urlAvatarMiddle",$urlAvatarMiddle);
		$this->assign("urlAvatarSmall",$urlAvatarSmall);
		$this->assign("urlCameraFlash",$urlCameraFlash);
		$this->display();
	}
	function joinfoll(){
		if(!$_GET['id']){
			exit();
		}
		if($_GET['id'] == $_SESSION['NR_USER_ID']){
			$this->error('不能关注你自己!','',3);
			exit();
		}
		$uid = $_GET['id'];
		$n = M("follower")->where("uid = ".$uid." and fid = ".$_SESSION['NR_USER_ID'])->count();
		if($n){
			$this->error('您已经关注过该用户!','',3);
			exit();
		}
		$date['uid'] = $uid;
		$date['fid'] = $_SESSION['NR_USER_ID'];
		M("follower")->add($date);
		$this->success('关注成功!','',3);
	}
	function unfoll(){
		if(!$_GET['id']){
			exit();
		}
		M("follower")->where("uid = ".$_GET['id']." and fid = ".$_SESSION['NR_USER_ID'])->delete();
		$this->success('取消关注成功!','',3);
	}
	function edforum(){
		if(!$_GET['id']){
			exit();
		}
		$f = M("forum")->select($_GET['id']);
		if($f[0]['pid'] != $_SESSION['NR_USER_ID'] && $_SESSION['NR_USER_ID'] != 1){
			$this->error('没有权限!','',3);
		}
		if($_POST['submit']){
			$date['title'] 		= htmlspecialchars($_POST['title']);
			$date['memu']		= $_POST['memu'];
			$date['content'] 	= htmlspecialchars($_POST['content']);
			M("forum")->where("id = ".$_GET['id'])->save($date);
			$this->success('修改成功!','',3);
		}
		$cate=M('memu');
		$memu=$cate->field("id,fid,title,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		foreach($memu as $key=>$value){
			$memu[$key]['count']=count(explode('-',$value['bpath']));
		}
		$forum = M("forum")->select($_GET['id']);
		$this->assign("forum",$forum);
		$this->assign("memu",$memu);
		$this->display();
	}
	function adminlogin(){
		if($_SESSION['NR_USER_ID'] != 1){
			$this->error('没有权限!','',3);
		}
		if($_SESSION['NR_ADMIN_ID']){
			$this->success('登录成功!','/Admin_main.html',1);
		}
		if($_POST['submit']){
			$login = new UserModel();
			$login->adminlogin();
			$this->success('登录成功!','/Admin_main.html',3);
		}
		echo $_SESSION['NR_ADMIN_KEY'];
		$this->display();
	}
}
?>