<?php
class showControl extends skymvc{
	public function __construct(){
		parent::__construct();
		$this->loadModel(array("product","product_attr","category","comment","product_data","user","feeds"));
		$this->loadConfig("c_category_children");
			 
	}
	
	public function onDefault(){
			$category_children=$this->config_item('category_children');	
			$this->loadconfig("taobao_config");
			$id=get('id','i');			
			$data=$rs=$this->product->selectRow(array("where"=>"id='$id'  "));
			if(!$rs){
				$this->gomsg("产品不存在或者被禁止");
			}
			$tpl="product/show.html";
			//获取分类
			if($catid=$rs['catid']){
				$cat=$this->category->selectRow(array("where"=>array("id"=>$catid)));
				if($cat['level']==1){
					$cat_top=$cat;
				}
				if($cat['pid']){
					$parent=$this->category->selectRow(array("where"=>array("id"=>$cat['pid'])));
					if($parent['pid']){
						$cat_2nd=$parent;
						$cat_top=$this->category->selectRow(array("where"=>array("id"=>$parent['pid'])));
						$cat_3nd=$cat;
						if($cat_top['showtpl']) $tpl=$cat_top['showtpl'];	
					}else{
						$cat_top=$parent;
						$cat_2nd=$cat;
					}
					if($parent['showtpl']) $tpl=$parent['showtpl'];					 
				}
				if($cat['showtpl']) $tpl=$cat['showtpl'];
			}
			
			$this->loadModel(array("mote"));
			//获取模特信息
			if($rs['mote_id']){
				$mote=$this->mote->selectRow(array("where"=>"id={$rs['mote_id']}"));
				$rs['mote_name']=$mote['nickname'];
				$rs['mote_logo']=$mote['imgurl'];
			}
			//获取标签
			$this->loadClass("keywords",false,false);
			$keyword=new keyword('config/dict/');
			$keyword->set('my');
			$tags=$keyword->get($rs['title'],6);
			//获取标签结束
			$rs['content']=$this->product_data->selectOne(array("where"=>" id='$id' ","fields"=>"content"));
			/*******获取产品属性*****************/
			$this->loadModel("attribute");
			$data_attr=$attr=array();
			$r=$this->product_attr->selectRow(array("where"=>" id=$id "));
			if($r['attr_cat_id']){
				$attr=$this->attribute->getAttr($r['attr_cat_id']); 
				$data_attr=json_decode($r['attr_content'],true);
				$this->smarty->assign("data_attr",$data_attr);
			}
			$rs['attr']=$attr;
			
			/*******获取产品属性结束****************************/
			$cids=$category_children[$rs['catid']];
			/*******获取上下篇*******/
			$cids && $last_rs=$this->product->selectRow(array("where"=>" catid in("._implode($cids).")  AND id<".$rs['id']." AND (status<10 AND status >0 )  ", "order"=>"id DESC") );
			$cids && $next_rs=$this->product->selectRow(array("where"=>" catid in("._implode($cids).")  AND id>".$rs['id']." AND (status<10 AND status >0 )  ", "order"=>"id ASC") );
			/*******End获取上下篇*******/
			/***************获取相关产品推荐***********************/
			
			$mem_key="show_recommend_$id";
			if(!$recommendlist=$this->cache->get($mem_key)){
				
			$cids &&	$recommend=$this->product->select(array("where"=>" catid in("._implode($cids).")  AND (status<10 AND status >0 ) AND grade>='{$rs['grade']}' ", "order"=>"grade ASC","start"=>0,"limit"=>20) );
				if(!$recommend or count($recommend)==1){
					$cids &&	$recommend=$this->product->select(array("where"=>" catid in("._implode($cids).")  AND (status<10 AND status >0 ) AND grade<='{$rs['grade']}' ","order"=>"grade desc","start"=>0,"limit"=>20) );
				}
				
				$reclist=array();
				if($recommend){
					foreach($recommend as $k=>$v){
						if($v['id']!=$rs['id']){
							$reclist[]=$v;
						}
					}
					
					if(count($reclist)>10){
						shuffle($reclist);
						$recommendlist=array_slice($reclist,0,10);
					}else{
						$recommendlist=$reclist;
					}
					$this->cache->set($mem_key,$recommendlist,120);
				}
			}
			/***************获取相关产品推荐**********************/
			//获取评论
			$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=".PRODUCT_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=".PRODUCT_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[$k]=$v;
						
					}
				}
			}
			$pagelist=$this->pagelist($rscount,$limit,APPINDEX."?m=show&id=$id");
			$seo=array(
				"title"=>htmlspecialchars($rs['title']),
				"keywords"=>htmlspecialchars($rs['keywords']?$rs['keywords']:$rs['title']),
				"description"=>htmlspecialchars($rs['description']?$rs['description']:$rs['title']),
			);
			//评论发布处理
			$this->smarty->assign(array(
				"comment_object_id"=>$id,
				"comment_type_id"=>PRODUCT_TYPE_ID,
				"comment_object_userid"=>$data['userid'],
				"comment_referer"=>APPINDEX."?m=show&id=$id",
				"comment_post_url"=>APPINDEX."?m=comment&a=product&ajax=1"
			
			));
			
			$this->smarty->assign(
				array(
					"data"=>$rs,
					"rs"=>$rs,
					"seo"=>$seo,
					"comment_list"=>$comment,
					"tags"=>$tags,
					"recommendlist"=>$recommendlist,
					"cat"=>$cat,
					"cat_top"=>$cat_top,
					"cat_2nd"=>$cat_2nd,
					"cat_3nd"=>$cat_3nd,
					"pagelist"=>$pagelist,
					"last_rs"=>$last_rs,
					"next_rs"=>$next_rs
				)
			);
			$this->smarty->display($tpl);
	}
	
}
?>