<?php
/*
	[Phpup.Net!] (C)2009-2011 Phpup.net.
	This is NOT a freeware, use is subject to license terms

	$Id: index.class.php 2010-08-24 10:42 $
*/

if(!defined('IN_PHPUP')) {
	exit('Access Denied');
}
class index_controller
{
	function index_controller()
	{
		include ROOT_PATH.'./models/common.php';
		include ROOT_PATH.'./models/fetion.php';
		$this->fetion=new fetion();
		$this->indexview='groups';
	}
	
	function index_action()
	{
		$groups_mod=new common('groups');
		if($GLOBALS['setting']['site_group_quanguo'])
		{
			$citycon.=' and  ( cityid="quanguo"  or  cityid="'.$GLOBALS['nowcityid'].'" ) ';
		}
		else
		{
			$citycon.=' and  cityid="'.$GLOBALS['nowcityid'].'"  ';
		}
		$totals=$groups_mod->GetCount($citycon.' AND big_catelist>0  and lasttime>='.time());
		
		if($GLOBALS['setting']['site_index_show'])
		{
			
			foreach($GLOBALS['site'] as $k=>$v)
			{
				$sites[$v['sitetype']][]=$v;
			}
			
			$limit=$GLOBALS['setting']['site_group_count']>0?$GLOBALS['setting']['site_group_count']:18;
			
			$groups=array();
			$cate_mod=new common('catelist');
			$container='and ispassed=1 and starttime<='.time().' and lasttime>='.time();
			$order='order by ';
			
			if($GLOBALS['setting']['site_group_show'] || $GLOBALS['setting']['site_group_quanguo'])
			{
				$container.=' and (cityid="'.$GLOBALS['nowcityid'].'" or cityid="quanguo") ';
				$order.=' cityid="quanguo" asc,';
			}
			elseif($GLOBALS['setting']['site_group_show'])
			{
				$container.=' and cityid="'.$GLOBALS['nowcityid'];
			}
			$order.='recommend desc,sortorder desc,temporder asc';
			$recommends=$this->_recommend($GLOBALS['nowcityid']);
			$counts=$this->get_count_num(false,true,true);
			$catecounts=$this->_categroup($_REQUEST['cateid'],$counts);
			
			foreach($GLOBALS['cate'] as $k=>$v)
			{
				$groups[$k]=$groups_mod->GetPage(array('isshow'=>0),$container.' and big_catelist='.$k,'limit 0,'.$limit,array(),$order);
				$cates[$k]=$cate_mod->GetPage(array('isshow'=>0),'and parentid='.$k);
				$count[$k]=$groups_mod->GetCount(' and big_catelist='.$k.' and cityid="'.$GLOBALS['nowcityid'].'" and lasttime>='.time());
			}

			$index_date = $this->get_sitecate();
			$indexgroup = $index_date['indexgroup'];
			$indexother = $index_date['indexother'];
			
			include template('index');
		}
		else
		{
			
			$this->indexview='groupsindex';
			$this->groups_action();
		}
	}
	function groups_action()
	{
			
		foreach($GLOBALS['site'] as $k=>$v)
		{
			$sites[$v['sitetype']][]=$v;
		}
		
		$index_date = $this->get_sitecate();
		$indexgroup = $index_date['indexgroup'];
		$indexother = $index_date['indexother'];

		$groups_mod=new common('groups');
		if($GLOBALS['setting']['site_group_quanguo'])
		{
			$citycon.=' and  ( cityid="quanguo"  or  cityid="'.$GLOBALS['nowcityid'].'" ) ';
		}
		else
		{
			$citycon.=' and  cityid="'.$GLOBALS['nowcityid'].'"  ';
		}
		$totals=$groups_mod->GetCount($citycon.' AND big_catelist>0  and lasttime>='.time());
		
		$recommends=$this->_recommend($GLOBALS['nowcityid']);
		$limit=$GLOBALS['setting']['site_group_count']>0?$GLOBALS['setting']['site_group_count']:24;
		$groups_mod=new common('groups');
		$groups=array();
		$container='and ispassed=1 and starttime<='.time().' and lasttime>='.time();
		
		if($GLOBALS['setting']['site_group_quanguo'])
		{
			$container.=' and (cityid="'.$GLOBALS['nowcityid'].'" or cityid="quanguo") ';
		}
		else
		{
			$container.=' and cityid="'.$GLOBALS['nowcityid'].'"';
		}
		$bigcateid=intval($_REQUEST['cateid']);
		$smallcateid=intval($_REQUEST['smallcatelist']);
		$bizid=intval($_REQUEST['biz']);
		$keywords=trim(strip_tags($_REQUEST['keywords']));
		$ext='';
		$fields=array('price'=>'nowprice','discount'=>'discount','time'=>'lasttime');
		$order='order by cityid="quanguo" asc,sortorder desc,temporder asc';
		
		if(!empty($_REQUEST['field']) && in_array($_REQUEST['field'],array_keys($fields)) && ($_REQUEST['desctype']=='asc' || $_REQUEST['desctype']=='desc'))
		{
			$order="order by ".$fields[$_REQUEST['field']]." ".$_REQUEST['desctype'];
			$ext['field']=$_REQUEST['field'];
			$ext['desctype']=$_REQUEST['desctype'];
		}
		
		if(!empty($keywords))
		{
			$container.=' and subject like "%'.$keywords.'%" or keyword like "%'.$keywords.'%"';
			$ext['keywords']=$keywords;
		}
		if($bizid>0)
		{
			$container.=' and bizgroup="'.$bizid.'"';
			$ext['biz']=$bizid;
		}

		if($bigcateid>0)
		{
			$container.=' and big_catelist="'.$bigcateid.'"';
			$ext['cateid']=$bigcateid;
		}

		if($smallcateid>0)
		{
			$container.=' and small_catelist="'.$smallcateid.'"';
			$ext['smallid']=$smallcateid;
		}		
		
		$counts=$this->get_count_num(true,true,true);
		$bizcounts=$this->_bizgroup($GLOBALS['nowcityid'],$counts);
		$catecounts=$this->_categroup($_REQUEST['cateid'],$counts);
		$ccon=' and lasttime>='.time();
		
		if($GLOBALS['setting']['site_group_quanguo'])
		{
			$ccon.=' and (cityid="'.$GLOBALS['nowcityid'].'" or cityid="quanguo") ';
		}
		else
		{
			$ccon.=' and cityid="'.$GLOBALS['nowcityid'].'"';
		}
		foreach($GLOBALS['cate'] as $k=>$v)
		{
			$count[$k]=$groups_mod->GetCount($ccon.' and big_catelist='.$k);
		}
		
		$showpage=array('isshow'=>1,'currentpage'=>intval($_REQUEST['page']),'pagesize'=>$limit,'url'=>url('index','groups',$ext),'example'=>2);
		$groups=$groups_mod->GetPage($showpage,$container,'',array(),$order);
	

		$pagetitle=$keywords.' '.$GLOBALS['cate'][$bigcateid]['catename'];
		include template($this->indexview);
	}
	function _bizgroup($cityid,$counts)
	{
		$bizs=array();
		if($cityid)
		{
			$biz_mod=new common('bizgroup');
			$bizs=$biz_mod->GetPage(array('isshow'=>0),'and cityid="'.$cityid.'"');
			foreach($bizs as $k=>$v)
			{
				$bizs[$k]['counts']=intval($counts['biz'][$v['id']]);
			}
		}
		return $bizs;
	}
	function _categroup($cateid,$counts)
	{
		$cate=array();
		$cate_mod=new common('catelist');
		if($cateid)
		{
			$container='and parentid='.intval($cateid);
		}
		else
		{
			$container='and recommend=1 and parentid>0';
		}
		$cate=$cate_mod->GetPage(array('isshow'=>0),$container,'',array('catename','id','css'));
		if($cate)
		{
			foreach($cate as $k=>$v)
			{
				$cate[$k]['counts']=intval($counts['cate'][$v['id']]['total']);
			}
		}
		return $cate;
	}
	function sites_action()
	{
		$site_mod=new common('site');
		$container='and cityid like "%'.$GLOBALS['nowcityid'].'%"';
		$mysite=$site_mod->GetPage(array('isshow'=>0),$container,'',array('id','sitename','sitetype','cityid','pinyin','recommend','siteurl'),'order by pinyin asc');
		
		
		ob_start();
		include template('sites');
		
		$content = ob_get_contents();//取得php页面输出的全部内容?
		$fp = fopen("test.html", "w"); //创建一个文件，并打开，准备写入?
		fwrite($fp, $content); //把php页面的内容全部写入output00001.html，然后……?
		fclose($fp);
		
	}
	
	function groupsite_action()
	{
		$ext="";
		if($_REQUEST['type'])
		{
			$type=$_REQUEST['type'];		
		}else{			
			$type="now";
		}
		$ext="&type=".$type;
		
		$id=intval($_REQUEST['id']);
		$site_mod=new common('site');
		$ext="&id=".$id;
		$siteinfo = $site_mod->GetOne(" AND id=".$id);
		$citys = explode(",", $siteinfo['cityid']); 
		$citynum = count($citys);
		$cityid=$GLOBALS['session']->get('cityid');
		$cityname = $GLOBALS['city'][$cityid]['cityname'];
		$groups_mod = new common('groups');

		$container="  AND siteid = $id AND cityid = '$cityid' ";	
		$time = time();
		if($type =="now")
		{
			$container.=" AND $time  < lasttime  AND   $time > starttime ";
		}else{
			$container.=" AND $time  > lasttime ";
		}
				
		$showpage = array ('isshow' => 1, 'currentpage' => $_REQUEST ['page'], 'pagesize' => 15, 'url' => 'index.php?con=index&act=groupsite'.$ext, 'example' => 2 );
		$groups = $groups_mod->GetPage ( $showpage, $container );
		
		include template('web_wz');
	}
	

	function  othersite_action()
	{
		$tmp_othersite = $GLOBALS['othersite'];
		$top_arr = array();
		$othersite = array();
	
		foreach($tmp_othersite AS $key => $val)
		{
			
			if($val['istop'])
			{
				$top_arr[] = $val;			
			}
			
			if(count($othersite[$val['cate']]) <17 )
			{
				$othersite[$val['cate']][] =  $val;	
			}		
		}

		
		include template('wg_web');
	}
	
	function  catedetail_action()
	{
		$cateid = $_REQUEST['cateid'];
		$tmp_othersite = $GLOBALS['othersite'];
		$siteinfo = array();
		foreach($tmp_othersite AS $key => $val)
		{
			if($val['cate'] == $cateid)
			{
				$siteinfo[] = $val;	
			}	
		}
	
		include template('wg_weber');
	}
	
	
	
	
	
	function group_action()
	{
		$id=intval($_REQUEST['id']);
		if($id)
		{
			
			$group_mod=new common('groups');
			$group=$group_mod->GetOne('and id='.$id,array('url','groupsite','siteid','thumb','id','hits'));
			if($group)
			{
				if($GLOBALS['setting']['site_allow_remote'] && strpos(','.$group['thumb'],'http://'))
				{
					require(ROOT_PATH.'/inc/curl.class.php');
					$curl=new curl();
					$img=$curl->get($group['thumb']);
					if($img)
					{
						$dir='data/upload/'.date('Y').'/'.date('m');
						if(!is_dir($dir))
						{
							mkdir2(ROOT_PATH.$dir);
						}
						$file=$dir.'/'.md52(microtime()).'.jpg';
						file_put_contents(ROOT_PATH.'/'.$file,$img);
						$group_mod->UpdateData(array('thumb'=>$file),'and id='.$group['id']);
					}
				}
				$group_mod->UpdateData(array('hits'=>intval($group['hits']+1)),'and id='.$group['id']);
				$site_mod=new common('site');
				$sites=$site_mod->GetOne('and id='.$group['siteid'],array('breaksuff','breakurl'));
				if($sites)
				{
					$prefix=$sites['breaksuff'];
					$struff=$sites['breakurl'];
					
					if(strstr($group['url'], '?'))
					{
						$struff = str_replace("?", "&", $struff);	
					}	
					
					if(!empty($prefix))
					{
						$prefix=str_replace('http://','',$prefix);
						if(!empty($GLOBALS['setting']['lhaitun_appkey']))
						{
							$prefix=preg_replace('/([\?|&])e=[a-zA-Z0-9]*([&]?)/','\\1e='.$GLOBALS['setting']['lhaitun_appkey'].'\\2',$prefix);
						}
						$group['url']='http://'.$prefix.urlencode($group['url'].$struff);			
				
					}
					else
					{
						
						$group['url']=$group['url'].$struff;
					}					
				}
				sheader($group['url']);
			}
			else
			{
				sheader('index.php',3,'团购不存在');
			}
		}
		else
		{
			sheader('index.php');
		}
	}
	
	function site_action()
	{
		$id=intval($_REQUEST['id']);
		if($id)
		{
			$site_mod=new common('site');
			$site=$site_mod->GetOne('and id='.$id,array('siteurl','sitename','breaksuff','breakurl'));
			$prefix=$site['breaksuff'];
			$struff=$site['breakurl'];
			if(!empty($prefix))
			{
				$prefix=str_replace('http://','',$prefix);
				if(!empty($GLOBALS['setting']['lhaitun_appkey']))
				{
					$prefix=preg_replace('/([\?|&])e=[a-zA-Z0-9]*([&]?)/','\\1e='.$GLOBALS['setting']['lhaitun_appkey'].'\\2',$prefix);
				}
				$site['siteurl']='http://'.$prefix.urlencode($site['siteurl'].$struff);			
		
			}
			else
			{
				
				$site['siteurl']=$site['siteurl'].$struff;
			}	
			
			sheader($site['siteurl']);
		}
		else
		{
			sheader('index.php');
		}
	}
	
	function city_action()
	{
		if($GLOBALS['city']){
			foreach($GLOBALS['city'] as $k=>$v){
			$v['pinyin']=$k;
			$citys[substr($k,0,1)][]=$v;
			}
			ksort($citys);
		}
		$city_mod=new common('city');
		$hot=$city_mod->GetPage(array('isshow'=>0),'','limit 0,8',array('pinyin','cityid','cityname','sortorder'),'order by sortorder desc');
		
		include template('city');

	}
	
	
	function about_action()
	{
		include template('about');	
	}

	
	function submitsite_action()
	{
		include template('submitsite');
	}
	
	
	function contactmy_action()
	{
		include template('contactmy');	
	}
	
	
	function mylink_action()
	{
		$text = array();
		$thumb = array();
		
		foreach($GLOBALS['link'] AS $key => $val)
		{
			if($val['type']==1)
			{
				$text[] = $val;		
			}		
			if($val['type']==2)
			{
				$thumb[] =$val;
			}			
			
		}
	
		include template('mylink');	
	}
	
	
	
	function _checkcode($code)
	{
		include ROOT_PATH.'./models/imgcode.php';
		$imgcode_mode=new Helper_ImgCode();
		return $imgcode_mode->isValid($code);
	}
	
	function _recommend($cityid='quanguo')
	{
		$container=" and recommend=1 and cityid='".$cityid."'";
		$groups_mod=new common('groups');
		$filed=array('id','subject','thumb','url','groupsite','oldprice','nowprice','discount');
		$groups=$groups_mod->GetPage(array('isshow'=>0),$container,'limit 0,5',$filed,'order by sortorder desc');
		if(empty($groups))
		{
			$container=" and recommend=1 and cityid='quanguo'";
			$groups=$groups_mod->GetPage(array('isshow'=>0),$container,'limit 0,5',$filed,'order by sortorder desc');
			if(empty($groups))
			{
				$groups=$groups_mod->GetPage(array('isshow'=>0),"and cityid='".$cityid."'",'limit 0,5',$filed,'order by sortorder desc');
			}
		}
		return $groups;
	}
	function get_count_num($biztype=true,$catetype=true,$caterecommend=false)
	{
		$cityid = $GLOBALS['nowcityid'];
		$big_catelist   = $_REQUEST['cateid'];
		$small_catelist = $_REQUEST['small_catelist'];
		$biz            = $_REQUEST['biz']; 
		
		$cate_count=$bizgroup_count=array();
		if($biztype)
		{
			$sql_bizgroup_count ="SELECT  b.id AS bizgroup, b.bizname AS bizname, COUNT(g.id) AS total 
								  FROM ".tname('bizgroup')." AS b LEFT  JOIN  ".tname('groups')." AS g  ON b.id=g.bizgroup
								  WHERE g.cityid = '".$cityid."'
								  AND g.ispassed=1 and g.starttime<=".time()." and g.lasttime>=".time();
			if($GLOBALS['setting']['site_group_quanguo'])
			{
				$sql_bizgroup_count.=' or g.cityid="quanguo" ';
			}
			if($big_catelist)
			{
				$sql_bizgroup_count.="  AND   g.big_catelist = ".$big_catelist;	
			}
				
			if($small_catelist)
			{
				$sql_bizgroup_count.="  AND   g.small_catelist = ".$small_catelist;	
			}
			
			$sql_bizgroup_count.="  GROUP BY b.id ";
			
			$query=$GLOBALS['db']->query($sql_bizgroup_count);
			while($rows=$GLOBALS['db']->fetch_array($query))
			{
				if($rows['bizname'])
				{
					$bizgroup_count[$rows['bizgroup']]=array('bizname'=>$rows['bizname'],'total'=>$rows['total']);
				}
			}
		}
		if($catetype)
		{
			$sql_cate_count ="SELECT  c.id  AS  small_catelist, c.catename AS small_catelist_catename ,c.parentid, COUNT(g.id) AS total 
							  FROM ".tname('catelist')." AS c LEFT JOIN  ".tname('groups')." AS g ON  c.id =g.small_catelist 
							  WHERE g.cityid = '".$cityid."'
							  AND g.ispassed=1 and  g.starttime<=".time()." and g.lasttime>=".time();
			if($GLOBALS['setting']['site_group_quanguo'])
			{
				$sql_bizgroup_count.=' or g.cityid="quanguo" ';
			}
			if($biz)
			{
				$sql_cate_count.="  AND g.bizgroup = ".$biz;	
			}
				
			if($big_catelist)
			{
				$sql_cate_count.="  AND  g.big_catelist = ".$big_catelist;	
			}

			$sql_cate_count.="  GROUP BY c.id ";
				
			$query=$GLOBALS['db']->query($sql_cate_count);
			while($rows=$GLOBALS['db']->fetch_array($query))
			{
				$cate_count[$rows['small_catelist']]=array('parentid'=>$rows['parentid'],'small_catelist_catename'=>$rows['small_catelist_catename'],'total'=>$rows['total']);	
			}
		}
		
		return array("biz"=>$bizgroup_count,"cate"=>$cate_count);
		
	}
	
	
	function get_sitecate()
	{
		
		$site_mod=new common('site');
		$indexgroup = $site_mod->GetPage(array(),' AND  recommend = 1'," limit 20 ",array(),'ORDER BY sort');
		$othersite_mod = new common('othersite');
		$indexother = $othersite_mod->GetPage(array(),' AND recommend = 1'," limit 20 ",array());
		
		$showsite = array();
		$showcate = array();
		
		foreach($GLOBALS['sitecate'] AS $key => $val)
		{
			if($val['isshow'])
			{
				$showsite[$key] = $val;	
				$showcate[] = $key;	
			}
		}
		
		return array("indexgroup"=>$indexgroup,"indexother" => $indexother);
	
	}
	
	
	function goothersite_action()
	{
		$id = $_REQUEST['id'];
		$othersite = $GLOBALS['othersite'][$id];
		sheader($othersite['url']);

	}


}
