<?php
class indexControl extends skymvc{

	public function __construct(){
		parent::__construct();
		$this->loadModel(array("product","category","comment","product_data","user","feeds","site_city"));
		$this->loadConfig("c_category_children");
	}
		//页面显示
	public function  onDefault(){
			/*
			if(!isset($_COOKIE['SC_ID'])){
				header("Location: /index.php?m=index&a=site_city");
				exit;
			}
			*/
			$category_children=$this->config_item('category_children');	 			 
			$w=" status<99 AND isproduct=1 ";
			$url="/index.php?m=index&a=index";
			$catid=get_post('catid','i');
			if($catid){
				$w.=" AND catid=$catid ";
				$url.="&catid=".$catid;
			}
			$this->smarty->assign("baseurl",$url);
			
			if(is_robot()){
				$w.=" AND status<10 ";
				$ord=" id DESC";	
			}else{
				$w.=" AND status<10 AND status>0 ";
				if(get('order')=='hot'){
					$ord=" grade DESC";
					$url.="&order=hot";
				}else{
					$ord=" id DESC";
					$url.="&order=new";
				}
			}
			
			 	 			 
			$pagesize=ISWAP?24:48;
			$start=get('per_page','i');
			
			$rscount=true;
			$list=$this->product->select(array(
				"where"=>$w,
				"start"=>$start,
				"limit"=>$pagesize,
				"order"=>$ord
				
			),$rscount);
			 
			$pagelist=$this->pagelist($rscount,$pagesize,$url);
			 
			$c=$this->category->selectRow(array("where"=>array("id"=>$catid)));
			$seo=array(
				"title"=>strip_tags($c['title']),
				"keywords"=>$c['keywords'],
				"description"=>$c['description']
			);
			if(get('ajax')){
				exit(json_encode($list));
			}
			$this->smarty->assign(
				array(
				"baseurl"=>$url,
				"seo"=>$seo,
				"list"=>$list,
				"rscount"=>$rscount,
				"pagesize"=>$pagesize,
				"pagelist"=>$pagelist,
				)
			);

			$this->smarty->display("index.html");
		 
	}
	
 
 	public function onSetSiteCity(){
		$sc_id=get_post('sc_id','i');
		setcookie("SC_ID",$sc_id,time()+360000000,"/",DOMAIN);
		header("Location: /index.php");
	}
	
	public function onsite_city(){
		$this->smarty->display("site_city.html");
	}

		
	public function onFeed(){
			if(!$_SESSION['ssuser']['userid']){
				$this->gourl("/index.php?m=login");
			}
			$userid=intval($_SESSION['ssuser']['userid']);
			$u=$this->user->selectRow(array("where"=>"userid=$userid"));
			if($u['lastfeed']<time()-3600){//一个小时订阅一次
				$data=$this->user->getCols("SELECT id FROM ".table('product')." WHERE dateline > '{$u['lastfeed']}' AND (status<10 AND status >0 ) order by  dateline LIMIT 96 ");
				if($data){
					foreach($data as $object_id){
						$this->feeds->query("INSERT INTO ".table('feeds')." SET userid='$userid',object_id='$object_id',dateline=".time()." ");
					}
				}
				$lastfeed=($u['lastfeed']+2600*4)>time()?time():($u['lastfeed']+3600*4);
				$this->user->query("UPDATE ".table('user')." SET lastfeed=".$lastfeed." where userid='$userid' " );
			}
			$object_ids=$this->feeds->getCols("SELECT object_id FROM ".table('feeds')." WHERE userid='$userid'  ");
			$object_ids && $list=$this->product->getAll("SELECT * FROM ".table('product')." WHERE id in("._implode($object_ids).") AND status <10 limit 24 ");
			$seotitle=$_SESSION['ssuser']['nickname']."订阅的商品"; 
			$seo=array(
				"title"=>$seotitle,
				"keywords"=>$seotitle,
				"description"=>$seotitle
			);
			$this->smarty->assign("baseurl","/index.php?m=index&a=myfeed");
			$this->smarty->assign("list",$list);
			$this->smarty->display("feed.html");
			
	}
	
 
		public function onCategory(){
			$this->smarty->display("category.html");
		}
		 
}



?>