<?php

class activityControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("login","user","activity","activity_data","activity_topic","activity_user"));
		$this->userid=$this->login->userid;
		if(!in_array(strtolower(get('a')),array('','default','list','show'))){
			$this->login->checkLogin();
		}
		
		$this->smarty->assign("type_id_list",$this->activity->type_id_list());
	}
	


	public function onDefault(){
			$start=get('per_page','i');
			$d=get('d','i');
			$url="/index.php?m=activity&d=".$d;
			$baseurl="/index.php?m=activity";
			$w=" status=2 ";
			if($d==1){
				$w.=" AND  starttime>".time()." ";
			}elseif($d==2){
				$w.="  AND endtime<".time()." ";
			}else{
				$w.=" AND endtime>".time()." AND starttime<".time()." ";
			}
			
			$type_id=get('type_id','i');
			if($type_id){
				$w.=" AND type_id=".$type_id." ";
				$url.="&type_id=".$type_id;
			}
			$option=array(
				"where"=>$w,
				"start"=>$start,
				"limit"=>20,
				"order"=>"id DESC"
			);
			$rscount=true;
			$data=$this->activity->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,20,$url); 
			$this->smarty->assign(array(
				"list"=>$data,
				"rscount"=>$rscount,
				"baseurl"=>$baseurl,
				"pagelist"=>$pagelist
			));
			$this->smarty->display("activity/index.html");
	}
	
	public function onList(){
		$start=get('per_page','i');
		$url="index.php?m=activity&a=list";
		$d=intval(get('d'));
		 
		if($d==1){
			$w="   starttime>".time()." ";
		}elseif($d==2){
			$w="   endtime<".time()." ";
		}else{
			$w="  endtime>".time()." AND starttime<".time()." ";
		}
		$w="";
		$option=array(
			"where"=>$w,
			"start"=>$start,
			"limit"=>20,
			"order"=>"id DESC"
		);
		$rscount=true;
		$data=$this->activity->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,20,$url); 
		$this->smarty->assign(array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		));
		$this->smarty->display("activity/list.html");
	}
	
	public function onShow(){
			$id=intval(get('id'));
	 
			$data=$this->activity->selectRow(array("where"=>" id='$id' AND status=2  "));
			if(!$data){
				 $this->gomsg("该活动不存在或者为审核通过","index.php?m=activity");	
			}
			$data['content']=$this->activity_data->selectOne(array("where"=>"id='$id' ","fields"=>"content"));
			$this->smarty->assign(array(
				"data"=>$data,
				"activity"=>$data,				
			));
			/*******************评论处理**************************/
			$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_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_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_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/show.html");
			
	}
	
	public function onApply(){
			$id=intval(get('id'));
			if($id){
				$data=$this->activity->selectRow(array("where"=>"id='$id' "));
				if($data['userid']!=$this->userid) $this->gomsg("die_access",R("/index.php?m=activity"));
				$data['content']=$this->activity_data->selectOne(array("where"=>"id='$id' ","fields"=>"content"));
				$this->smarty->assign(array(
					"activity"=>$data,
					"data"=>$data
				));
			}
			$this->smarty->display("activity/apply.html");
	}
	
	public function onSave(){
			
			$data['title']=post('title','h');
			$data['keywords']=post('keywords','h');
			$data['description']=post('description','h');
			$data['info']=post('info','h');
			
			$data['starttime']=strtotime(post('starttime'));
			$data['endtime']=strtotime(post('endtime'));
			$data['address']=post('address','h');
			$id=intval(get_post('id'));
			$sdata['content']=post('content','x');
			if($id){
				$row=$this->activity->selectRow(array("where"=>"id=$id"));
				if($row['userid']!=$this->userid) $this->gomsg("die_access",R("/index.php?m=activity"));
				$this->activity->update($data,"   id='$id' ");
				$this->activity_data->update($sdata,"   id='$id' ");		
				$this->gomsg("活动修改成功");
			}else{
				$data['siteid']=SITEID;
				$sdata['dateline']=$data['dateline']=time();
				$data['userid']=$this->userid;
				$data['gid']=post('gid','i');
				$data['type_id']=max(1,post('type_id','i'));
				$sdata['id']=$this->activity->insert($data);
				$this->activity_data->insert($sdata);
				//添加创始人
				$this->activity_user->insert(array(
					"siteid"=>SITEID,
					"act_id"=>$sdata['id'],
					"userid"=>$this->userid,
					"status"=>2,
					"dateline"=>time(),
					"truename"=>"创始人",
					"user_group"=>2,
				));
				$this->gomsg("活动申请成功");
			}
  
	}
	
 
	
	public function onmycreate(){
		$start=get('per_page','i');
		$url="index.php?m=activity&a=mycreate";
		$where=" userid=".$this->userid;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>20,
			"order"=>"id DESC"
		);
		$rscount=true;
		$data=$this->activity->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,20,$url); 
		$this->smarty->assign(array(
			"list"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist
		));
				
		$this->smarty->display("activity/mycreate.html");
	}
	
	public function onmyjoin(){
		$ids=$this->activity_user->selectCols(array("fields"=>"act_id","where"=>" userid=".$this->userid." "));  
		
		if($ids){
			$start=get('per_page','i');
			$url="index.php?m=activity&a=myjoin";
			$where=" id in("._implode($ids).") ";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>20,
				"order"=>"id DESC"
			);
			$rscount=true;
			$data=$this->activity->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,20,$url); 
			$this->smarty->assign(array(
				"list"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist
			));
		}
		$this->smarty->display("activity/myjoin.html");
	}
			


}
?>