<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &讨论组模块&
 */
 
 if (!defined('IS_RUN')) die('Forbidden');
 
	$Modname = '讨论组';
	$c=_get('c','index');

	$M_user = G_class('user');
	if($_CO['groupid']==1){
		
	}else{
	$all_bid = $M_user->B->FindAllBid($_CO['userbid']);
	$arr_all_bid=explode("/",'1/'.$all_bid);
	}
  switch ($c){
	 
	 case 'index':
		$groupid=intval(_get('groupid'));
		$cateid=intval(_get('cateid'));
		
		//分页每页显示数据量
		$listnum = '15';
		//当前页
		$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
		$lstart = $page*$listnum-$listnum;
		//链接地址
		$url = SITEURL . '?mod=group&do=group&groupid='.$groupid;
		
		$arrgroupcate=array();
		if($groupid){
			$str_group = G_class('group')->read_one($groupid);
			$str_groupcate = G_class('group_cate')->read_one($str_group['cateid']);
			$arr_group_topic = G_class('topic')->read_all($groupid,'groupid',$lstart,$listnum);
			foreach($arr_group_topic as $key=>$item){
					$arr_group_topics[] = $item;
					$arr_group_topics[$key]['user'] = G_class('user')->read($item['uid']);
					}
					
		}elseif($cateid){
			
			$str_groupcate = G_class('group_cate')->read_one($cateid);
			$arrgroupcate[0]['catename'] = $str_groupcate['catename'];
			$arrgroupcate[0]['group'] =  G_class('group')->read_all($cateid,'cateid',$arr_all_bid);
			$arrgroupcate[0]['cateid'] =$cateid;
			
		}else{
			
			$arrgroupcate = G_class('group_cate')->read_all($arr_all_bid);
			foreach($arrgroupcate as $key=>$value){
				$arrgroupcate[$key]['group'] =  G_class('group')->read_all($value['cateid'],'cateid',$arr_all_bid);
			}
		}
		
	 break;
	 
	 
	 
	 case 'addgroup':
	 
	 	$no_temp=1;
		
		$Modname = '添加讨论组 - '.$Modname;
		$cateid=intval(_get('cateid'))?intval(_get('cateid')):intval(_post('cateid'));
		if($cateid){
			$str_groupcate = G_class('group_cate')->read_one($cateid);
			if(!is_array($str_groupcate)) Note('分区不存在！','>>点击返回<<',SITEURL.'?mod=group&do=group');
		}else{
			Note('分区不存在！','>>点击返回<<',SITEURL.'?mod=group&do=group');
		}
		if($_POST){
		$post = G_class('group')->get_post($_POST);
		//if($post['groupid']==1) Note('禁止编辑系统管理员所在组！','>>点击返回<<');
		//print_r($post);
		//exit;
		
		G_class('group')-> save($post);
		
		Note('讨论组添加成功！','>>点击返回<<',SITEURL.'?mod=group&do=group');
		}
		 
		require_once Template($do.'_add');
	 
	 break;
	 
	 
	 
	 case 'addgroupcate':
	 
	 	$no_temp=1;
		
		$Modname = '添加讨论区 - '.$Modname;
		
		if($_POST){
			
		G_class('group_cate')->save($_POST);
		
		Note('讨论区添加成功！','>>点击返回<<',SITEURL.'?mod=group&do=group');
		}
		 
		require_once Template('group_cate_add');
	 
	 break;
	 
	 case 'delcate':
	 $cateid = _get('cateid');
	 if(G_class('group')-> read_all($cateid,'cateid',$arr_all_bid)){
		 Note('讨论区下尚有讨论组，删除失败！','>>点击返回<<',SITEURL.'?mod=group&do=group');
	 }else{
	 	G_class('group_cate')-> delgroupcate($cateid);
		 Note('讨论区删除成功！','>>点击返回<<',SITEURL.'?mod=group&do=group');
	 }
	  
	 break;
	 
	 case 'delgroup':
	 
	 $groupid = _get('groupid');
	 if(G_class('topic')-> read_all($groupid)){
		 Note('讨论组下尚有话题，删除失败！','>>点击返回<<',SITEURL.'?mod=group&do=group');
	 }else{
		 G_class('group')-> delgroup($groupid);
	 	 Note('讨论组删除成功！','>>点击返回<<',SITEURL.'?mod=group&do=group');
	 }

	 break;
	 
	 
	  case 'addtopic':
	  	 $Modname = '发布话题 - '.$Modname;
	  	 $no_temp=1;
		 $groupid = intval(_get('groupid'))?intval(_get('groupid')):intval(_post('groupid'));
		 
		 if($groupid){
			$str_group = G_class('group')->read_one($groupid);
			$str_groupcate = G_class('group_cate')->read_one($str_group['cateid']);
		}
		
		if($_POST){
			
		$topicid = G_class('topic')->save($_POST,$_POST['topicid']);
		
		$arr_update = array(
		$groupid=>array(
		'count_topic_today'=>$str_group['count_topic_today']+1,
		'count_topic'=>$str_group['count_topic']+1),
		);
		G_class('group')->update($arr_update);
		
		Note($_POST['topicid']?'话题编辑成功！':'话题添加成功！','>>点击返回<<',SITEURL.'?mod=group&do=group&c=topic&topicid='.$topicid);
		}
	 	 require_once Template('add_topic');
	 break;
	 
	 
	 
	 case 'topic':
	 	 $topicid = intval(_get('topicid'));
		 $content = G_class('topic')->read_one($topicid);
		 $content['user'] = G_class('user')->read($content['uid']);

		 $str_group = G_class('group')->read_one($content['groupid']);
		 $str_groupcate = G_class('group_cate')->read_one($str_group['cateid']);
	  	 $Modname = $content['title'].' - '.$Modname;
		 
		 G_class('topic')->update(array($topicid=>array('count_view'=>$content['count_view']+1)));
		 //最新讨论
		 $arr_new_topics = G_class('topic')->read_all($content['groupid'],'groupid',0,10);
		 foreach($arr_new_topics as $key=>$item){
			 	if($item['topicid']<>$topicid){
				$arr_new_topic[$key]=$item;
				$arr_new_topic[$key]['user'] = G_class('user')->read($item['uid']);
				}
			}
		 
		 //评论开始
		 //每页显示数据量
		$listnum = '9';
		//当前页
		$page = isset($_GET['p']) ? intval($_GET['p']) : 1;
		$lstart = $page*$listnum-$listnum;
		//链接地址
		$url = SITEURL . '?mod=group&do=group&c=topic&topicid='.$topicid;
		 
		 $arrComment = G_class('topics_comments')->read_all($topicid,$lstart,$listnum);
		 $arrsum = G_class('topics_comments')->read_all($topicid);
	foreach($arrComment as $key=>$item){
		$arrTopicComment[] = $item;
		$arrTopicComment[$key]['user'] = G_class('user')->read($item['uid']);
		$arrTopicComment[$key]['content'] = _HTML($item['content']);
		$arrTopicComment[$key]['recomment'] = G_class('topics_comments')->recomment($item['referid']);
		$allcomment.=$arrTopicComment[$key]['content'].$arrTopicComment[$key]['recomment'];
	}

	$pageinfo = grtpageno($url, count($arrsum), $listnum, $page);

		 if($_POST){
		  	$topicid = _post('topicid');
			$topic_str = G_class('topic')->read_one($topicid);
			$CID =  G_class('topics_comments')->save($_POST);
			$arr_update = array($topicid=>array('count_comment'=>$topic_str ['count_comment']+1));
			G_class('topic')->update($arr_update);
			
			$url = SITEURL."?mod=group&do=group&c=topic&topicid=".$topicid."#data-cmt_".$CID;
			location($url);
		 }
		 //评论结束
		 
		 
	  	 $no_temp=1;
	 	 require_once Template('group_topic');
	 break;
	 
	 //话题置顶
	 case 'topic_istop':
	 	 $topicid = intval(_get('topicid'));
		 
		 Note('话题置顶成功！','>>点击返回<<',SITEURL.'?mod=group&do=group&c=topic&topicid='.$topicid);
	 
	 break;
	 
	 //话题设精华
	 case 'topic_isposts':
	 	 $topicid = intval(_get('topicid'));
	 	 Note('话题加精成功！','>>点击返回<<',SITEURL.'?mod=group&do=group&c=topic&topicid='.$topicid);
	 break;
	 
	  case 'edit_topic':
	  
	 	 $topicid = intval(_get('topicid'));
		 $content = G_class('topic')->read_one($topicid);
		 $groupid = $content['groupid'];
		 $str_group = G_class('group')->read_one($content['groupid']);
		 $str_groupcate = G_class('group_cate')->read_one($str_group['cateid']);
	  	 $Modname = '编辑话题 - '.$Modname;
		 $no_temp=1;
	 	 require_once Template('edit_topic');
		 
	  break;
	  
	  case 'deltopic':
		 $topicid = _get('topicid');
		 $arrtopic = G_class('topic')->read_one($topicid);
		 G_class('topic')-> deltopic($topicid);
		 $str_group = G_class('group')->read_one($arrtopic['groupid']);
		 //删除评论
		 G_class('topics_comments')-> delcomment('',$topicid);
		 //删除评论
		 $arr_update = array(
			$arrtopic['groupid']=>array(
			'count_topic'=>$str_group['count_topic']-1),
			);
		 G_class('group')->update($arr_update);
	 
		 Note('话题删除成功！','>>点击返回<<',SITEURL.'?mod=group&do=group&groupid='.$arrtopic['groupid']);
	  
	 break;
		 //删除评论
	 case 'delcomment':
	 
		$commentid = _get('commentid');
		$arrcomment = G_class('topics_comments')->read_one($commentid);
		G_class('topics_comments')-> delcomment($commentid);
		$arrtopic = G_class('topic')->read_one($arrcomment['topicid']);
		$arr_update = array($arrcomment['topicid']=>array('count_comment'=>$arrtopic['count_comment']-1));
		G_class('topic')->update($arr_update);
		Note('评论删除成功！','>>点击返回<<',SITEURL.'?mod=group&do=group&c=topic&topicid='.$arrcomment['topicid']);
	  
	 break;
	 	 
	  default:
	  
         Note('参数未定义！','>>点击返回<<',SITEURL.'?mod=group&do=group');
		 
      break;
 	}
 
 if($no_temp<0) require_once Template($do);
?>