<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class activity_topicControl extends skymvc{
		public $userid;
		public function __construct(){
			parent::__construct();
			$this->loadmodel(array("activity","activity_topic","activity_topic_data","activity_user","login","user"));
			$this->loadControl("activityapi");
			$this->userid=$this->login->userid;
			if(!in_array(strtolower(get('a')),array('','default','list','show'))){
				$this->login->checkLogin();
			}
		}
		
		public function onDefault(){
			$act_id=get('act_id','i');
			$activity=$this->activity->selectRow(array("where"=>" id=".$act_id));
			if(empty($activity)){
				$this->lang['activity_not_exists'];
			}
			$where=" act_id=".$act_id."  ";
			if(ACTIVITY_TOPIC_STATUS_CHECK){
				$where.=" AND status=2 ";
			}
			$url="/index.php?m=activity_topic&a=default";
			$limit=20;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=$this->activity_topic->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$uids[]=$v['userid'];
				}
				$us=$this->user->getUserByIds($uids);
				foreach($data as $k=>$v){
					$u=$us[$v['userid']];
					$v['nickname']=$u['nickname'];
					$v['user_head']=$u['user_head'];
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"activity"=>$activity
				)
			);
			$this->smarty->display("activity_topic/index.html");
		}
		
		public function onShow(){
			$id=get_post("id","i");
			$data=$this->activity_topic->selectRow(array("where"=>"id={$id}"));
			if(empty($data)) $this->gomsg($this->lang['activity_topic_not_exists']);
			$u=$this->user->selectRow(array("where"=>" userid=".$data['userid'],"fields"=>"userid,nickname,user_head"));
			$data['nickname']=$u['nickname'];
			$data['user_head']=$u['user_head'];
			$data['content']=$this->activity_topic_data->selectOne(array("where"=>"id=".$id." ","fields"=>"content"));
			$act_id=$data['act_id'];
			$activity=$this->activity->selectRow(array("where"=>" id=".$act_id));
			$this->smarty->assign(array(
				"data"=>$data,
				"activity"=>$activity
			));
			/*******************评论处理**************************/
			$this->loadModel("comment");
			$limit=20;
			$start=get('per_page','i');
			if(COMMENT_STATUS_CHECK){
				$status_where=" AND status=1";
			}else{
				$status_where=" AND status<10";
			}
			$option=array(
				"where"=>" object_id='$id'  AND type_id=".ACTIVITY_TOPIC_TYPE_ID." AND pid=0 $status_where ",
				"order"=>" id ASC",
				"start"=>$start,
				"limit"=>$limit
			);
			$rscount=true;
			$comment=$this->comment->setTable("comment",$id)->select($option,$rscount);
			$temp_uids=array();
			if($comment){
				foreach($comment as $k=>$v){
					$temp_uids[]=$v['userid'];
					$temp_uids[]=$v['object_user_id'];
					$child=$this->comment->setTable("comment",$id)->select(array("where"=>"  object_id='$id'  AND type_id=".ACTIVITY_TOPIC_TYPE_ID." AND pid=".$v['id']."  $status_where "));
					if(!empty($child)){
						foreach($child as $kk=>$vv){
							$temp_uids[]=$vv['userid'];
							$temp_uids[]=$vv['object_user_id'];
						}
						$v['child']=$child;
					}
					
					$comment[$k]=$v;
				}
				$temp_uids=array_unique($temp_uids);
				if(!empty($temp_uids)){
					$temp_users=$this->user->select(array("where"=>" userid in (".implode(",",$temp_uids).")","fields"=>"nickname,userid,user_head"));
					$temp_uids=array();
					if(!empty($temp_users)){
						
						foreach($temp_users as $k=>$v){
							$temp_uids[$v['userid']]=$v;
						}
					}
				}
				if(!empty($temp_uids)){
					foreach($comment as $k=>$v){
						$v['nickname']=$temp_uids[$v['userid']]['nickname'];
						$v['user_head']=$temp_uids[$v['userid']]['user_head'];
						if(!empty($v['child'])){
							foreach($v['child'] as $kk=>$vv){
								$vv['nickname']=$temp_uids[$vv['userid']]['nickname'];
								$vv['user_head']=$temp_uids[$vv['userid']]['user_head'];
								$v['child'][$kk]=$vv;
							}
						}
						$comment_list[$k]=$v;
						
					}
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,APPINDEX."?m=group_title&a=show&id=$id");
			//评论发布处理
			$this->smarty->assign(array(
				"comment_object_id"=>$id,
				"comment_type_id"=>ACTIVITY_TOPIC_TYPE_ID,
				"comment_object_userid"=>$data['userid'],
				"comment_referer"=>APPINDEX."?m=group_title&a=show&id=$id",
				"comment_post_url"=>APPINDEX."?m=comment&a=product&ajax=1",
				"comment_list"=>$comment_list,
				"rscount"=>$rscount
			
			));
			/***********END 评论处理*********************/
			$this->smarty->display("activity_topic/show.html");
		}
		public function onAdd(){
			$id=get_post("id","i");
			$act_id=get_post('act_id');
			if($id){
				$data=$this->activity_topic->selectRow(array("where"=>"id={$id}"));
				$data['content']=$this->activity_topic_data->selectOne(array("where"=>"id={$id}","fields"=>"content"));
				$act_id=$data['act_id'];
				if($data['userid']!=$this->userid){
					$this->activityapiControl->checkRole($act_id,2);
				}else{
					$this->activityapiControl->checkRole($act_id);
				}
			}
			$activity=$this->activity->selectRow(array("where"=>" id=".$act_id));
			
			$this->smarty->assign(array(
				"data"=>$data,
				"activity"=>$activity
			));
			$this->smarty->display("activity_topic/add.html");
		}
		
		public function onSave(){
			
			$id=get_post("id","i");
			$data["title"]=post("title","h");
			$data["siteid"]=SITEID;
			$data["act_id"]=post("act_id","i");
			$data["userid"]=$this->userid;	 
			$sdata["content"]=post("content","x");
			$data['imgurl']=post('imgurl','h');
			if($id){
				$row=$this->activity_topic->selectRow(array("where"=>"id={$id}"));
				if($row['userid']!=$this->userid){
					$this->activityapiControl->checkRole($act_id,2);
				}else{
					$this->activityapiControl->checkRole($act_id);
				}
				$this->activity_topic->update($data,"id='$id'");
				$this->activity_topic_data->update($sdata,"id='$id'");
			}else{
				$data["dateline"]=time();
				$sdata['id']=$this->activity_topic->insert($data);
				$sdata['dateline']=time();
				$this->activity_topic_data->insert($sdata);
			}
			$this->gomsg("保存成功");
		}
		
		public function onMy(){
			$where=" userid=".$this->userid." AND status<99 ";
			$url="/index.php?m=activity_topic&a=my";
			$limit=20;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=$this->activity_topic->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$a=$this->activity->selectRow(array("where"=>"id=".$v['act_id'],"fields"=>"title,logo"));
					$v['act_title']=$a['title'];
					$v['act_logo']=$a['logo'];
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"activity"=>$activity
				)
			);
			$this->smarty->display("activity_topic/my.html");
		}
		
		public function onActivity(){
			$act_id=get('act_id','i');
			$this->activityapiControl->checkRole($act_id);
			$activity=$this->activity->selectRow(array("where"=>" id=".$act_id));
			if(empty($activity)){
				$this->lang['activity_not_exists'];
			}
			$where=" act_id=".$act_id."  ";
			$where.=" AND status<99 ";
			$url="/index.php?m=activity_topic&a=default";
			$limit=20;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=$this->activity_topic->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					$uids[]=$v['userid'];
				}
				$us=$this->user->getUserByIds($uids);
				foreach($data as $k=>$v){
					$u=$us[$v['userid']];
					$v['nickname']=$u['nickname'];
					$v['user_head']=$u['user_head'];
					$data[$k]=$v;
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"activity"=>$activity
				)
			);
			$this->smarty->display("activity_topic/activity.html");
		}
		 
		public function onStatus(){
			$id=get_post('id',"i");
			$status=get_post("status","i");
			$_GET['ajax']=true;
			$data=$this->activity_topic->selectRow(array("where"=>" id=$id AND status<99 "));
			if(empty($data)) exit(json_encode(array("error"=>1,"message"=>"主题不存在")));
			$this->activityapiControl->checkRole($data['act_id'],2);
			if($this->activity_topic->update(array("status"=>$status),"id=$id")){
				$num=0;
				if($status==2){
					$num=1;
				}elseif( $data['status']==2){
					$num=-1;
				}
				$this->activity->changenum("topic_num",$num,"id=".$data['act_id']);
			}
			exit(json_encode(array("error"=>0,"message"=>"状态修改成功")));
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			$data=$this->activity_topic->selectRow(array("where"=>"id=$id"));
			if($data['userid']!=$this->userid ){
				exit(json_encode(array("error"=>1,"message"=>"你无删除权限")));
			}
			if($this->activity_topic->delete("id={$id}")){
				$this->activity_topic_data->delete("id={$id}");
				if($data['status']==2){
					$this->activity->changenum("topic_num",-1,"id=".$data['act_id']);
				}			
			}
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		 
		
		
	}

?>