<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2005-2010 GV32.COM，并保留所有权利。
 * 网站地址: http://www.gv32.com；
 * 作者: GV32.COM
 *----------------------------------------------------------------------------*
**/
class Pic
{
	function Pic()
	{ 
		//缓存
		if($GLOBALS["WebConfig"]["webtmp"]==1)
		{
			$GLOBALS['Templ'] -> caching = true;
			if($GLOBALS["WebConfig"]["webtmptime"] > 0 )
			{
				$GLOBALS['Templ'] -> cache_lifetime = $GLOBALS["WebConfig"]["webtmptime"] ;
			}else{
				$GLOBALS['Templ'] -> cache_lifetime = 3600 ;  //60 *  60 * 24
			}
			$smdirid 	= trim($_REQUEST["id"]).trim($_REQUEST["pageNum"]);
			$GLOBALS['Templ'] -> cache_dir = ROOT_PATH.$GLOBALS['cache_path'].'/'.$GLOBALS['Helpe'] -> buildsmartydir( $type = 'i', $smdirid );
		}
		
		$getconfig = $GLOBALS['Helpe'] -> get_config();
		$GLOBALS['Templ'] -> assign('getconfig',$getconfig);

		$getmenu = $GLOBALS['Helpe'] -> get_menu();
		$GLOBALS['Templ'] -> assign('getmenu',$getmenu);
		
		$getpicsort = $GLOBALS['Helpe'] -> get_picsort();
		$GLOBALS['Templ'] -> assign('getpicsort',$getpicsort);
		
		$getlink = $GLOBALS['Helpe'] -> get_link();
		$GLOBALS['Templ'] -> assign('getlink',$getlink);
			
		$getads = $GLOBALS['Helpe'] -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$GLOBALS['Templ'] -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		$GLOBALS['Templ']->assign('getuserlink',$getuserlink);
		
		$act = trim($_REQUEST["act"]);
		if($act=='')
		{
			$this -> infolist();
		}
		
		
	}
	
	function infolist()
	{
	
		$sid 		= trim($_REQUEST["sid"]);
		$pageNum 	= trim($_REQUEST["pageNum"]);
		
		$pageSize = 16 ;

		if( $pageNum > 1 )
		{
			$pageNum  = $pageNum;
			$LIMITnum = ceil( ( $pageNum -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		

		$addsql = '';

		$webtitle = '图片中心';
		//处理你现在的位置
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/pic.html">图片中心</a>';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=pic&act=infolist">图片中心</a>';
		}

		if( $sid > 0 )
		{
			$addsql = " and pis_id = ".$sid;
			$endArray["sid"] = $sid;
		}
		if( $sid != '' )
		{
			$seletsort = " SELECT pis_id,pis_name  FROM ".SQL_PREFIX."picsort WHERE pis_id = ".$sid." or  pis_parentid = ".$sid ;
			$sortInfo  = $GLOBALS['MySql']  ->  selectInfo($seletsort);
			$sinid 	   = '';
			$addpage   = '&sid='.$sid;
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if( $sval["pis_id"]== $sid )
					{
						$webtitle = $sortval = $sval["pis_name"];
						if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
						{
							$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/pic-list-1-'.$sid.'.html">'.$sval["pis_name"].'</a>';
						}else{
							$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/?load=pic&act=infolist'.$addpage.'">'.$sval["pis_name"].'</a>';
						}
					}
					if($skey==0)
					{
						$sinid = $sval["pis_id"];
					}else{
						$sinid .= ','.$sval["pis_id"];
					}
				}
			}
			$addsql = " and pis_id in ( ".$sinid." ) ";
		}
		
		
		if($GLOBALS['Reque'] -> reqval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$addsql .= " AND ( pic_name LIKE  '%".$keywordval."%' )";  
		}


		$sqlQuery 	= " SELECT pic_id, pic_name ,pic_url, pic_uptime   FROM  ".SQL_PREFIX."picture   WHERE pic_enabled= 1 $addsql  GROUP BY  pic_id ORDER BY  `pic_id` LIMIT  $LIMITnum , $pageSize ";
		$info 		= $GLOBALS['MySql']  ->  selectInfo( $sqlQuery );
		
		

		foreach((array)$info as $pkey => $pval )
		{
			$info[$pkey]["pic_url"] = $GLOBALS['Helpe'] -> buildImg( $pval["pic_url"] ) ;  
			$info[$pkey]["get_url"] = $GLOBALS['Helpe']  -> buildUrl( $valid = $pval["pic_id"],'pic');	
		}
		

		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."picture  WHERE pic_enabled = 1 $addsql ORDER BY `pic_id`  ";
		$infocount 	= $GLOBALS['MySql']  ->  selectOne( $sqlQuery );
		
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$url = BASE_WEBURL.'/pic-list';
			$endArray["sid"] = $sid;
		}else{
			$url = './?load=pic&act=infolist'.$addpage.$addurl;
		}
		$PageStr 	= $GLOBALS['Page'] -> buildPageStr( $url , $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] ,$endArray );
		
		$GLOBALS['Templ'] -> assign('keyword',$keywordval);
		
		$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
		$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
		$GLOBALS['Templ'] -> assign('PageStr',$PageStr);
		$GLOBALS['Templ'] -> assign('info',$info);
		
		if($sid !='' or $pageNum!='' or  $newkeyword !='')
		{
			$cache_id = $sid.'p'.$pageNum.'k'.$keyid;
		}
		$GLOBALS['Templ'] -> display('piclist_tpl.html',$cache_id);
	}
	
	function detail()
	{
		$id 		= trim($_REQUEST["id"]);
		$pageNum 	= trim($_REQUEST["pageNum"]);
		
		if($pageNum !='' and $iid =='' ){ $iid = $pageNum ;  }
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/pic.html">图片中心</a>';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=pic&act=infolist">图片中心</a>';
		}
		if($id > 0 )
		{
			$sqlQuery 	= " SELECT p.* ,s.pis_name  FROM  ".SQL_PREFIX."picture as p , ".SQL_PREFIX."picsort  as s   WHERE p.pis_id = s.pis_id and p.pic_enabled = 1  and p.pic_id = ".$id." ORDER BY p.`pic_id`  ";
			
			$infodetail 	= $GLOBALS['MySql']  ->  selectOne($sqlQuery);
			
			$infoItemval = array();
					
			//查询其他图片信息
			$sqlQueryitem 	= " SELECT  pii_id, pic_id , pii_content, pii_url    FROM  ".SQL_PREFIX."picitem    WHERE  pii_enabled = 1  and  pic_id = ".$id."  ORDER BY  `pii_id`  LIMIT 0,200";
			$infoItem 	= $GLOBALS['MySql']  ->  selectInfo( $sqlQueryitem );
			
			
 
			$fkey = 0 ;
			$picnum = count($infoItem);
			
			if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
			{
				foreach((array)$infoItem as $itemkey => $itemval )
				{
						if($fkey % 4 == 0  ){   $infoItemval[$itemval["pii_id"]]["fkeystart"]  = $fkey / 4 +1  ;}else{ $infoItemval[$itemval["pii_id"]]["fkeystart"]  = 0 ; }
						
						if(($fkey+1) % 4 == 0  or  ($fkey+1 == $picnum) ){  $infoItemval[$itemval["pii_id"]]["fkeyend"]  =  1   ; } else{ $infoItemval[$itemval["pii_id"]]["fkeyend"]  =  0 ;} 
						
						$infoItemval[$itemval["pii_id"]]["pii_url"] = $GLOBALS['Helpe'] -> buildImg( $itemval["pii_url"] ) ;
						$infoItemval[$itemval["pii_id"]]["pii_picurl"] = BASE_WEBURL."/pic-".$itemval["pic_id"]."-".$itemval["pii_id"].".html" ;
						$infoItemval[$itemval["pii_id"]]["pii_id"]  = $itemval["pii_id"] ;
						$infoItemval[$itemval["pii_id"]]["pic_id"]  = $itemval["pic_id"] ;  
						$infoItemval[$itemval["pii_id"]]["pii_urlbig"] = $itemval["pii_url"] ;
						$fkey++ ;
				}
			}else{
				foreach((array)$infoItem as $itemkey => $itemval )
				{
						if($fkey % 4 == 0  ){   $infoItemval[$itemval["pii_id"]]["fkeystart"]  = $fkey / 4 +1  ;}else{ $infoItemval[$itemval["pii_id"]]["fkeystart"]  = 0 ; }
						
						if(($fkey+1) % 4 == 0  or  ($fkey+1 == $picnum) ){  $infoItemval[$itemval["pii_id"]]["fkeyend"]  =  1   ; } else{ $infoItemval[$itemval["pii_id"]]["fkeyend"]  =  0 ;} 
						
						$infoItemval[$itemval["pii_id"]]["pii_url"] = $GLOBALS['Helpe'] -> buildImg( $itemval["pii_url"] ) ;
						$infoItemval[$itemval["pii_id"]]["pii_picurl"] = BASE_WEBURL."/?load=pic&act=detail&id=".$itemval["pic_id"]."&pageNum=".$itemval["pii_id"] ;
						$infoItemval[$itemval["pii_id"]]["pii_id"]  = $itemval["pii_id"] ;
						$infoItemval[$itemval["pii_id"]]["pic_id"]  = $itemval["pic_id"] ;  
						$infoItemval[$itemval["pii_id"]]["pii_urlbig"] = $itemval["pii_url"] ;
						$fkey++ ;
				}
			}
			
			
 
			if($iid > 0)
			{

				$infodetail["pic_url"] = $infoItemval[$iid]["pii_urlbig"];
				$infodetail["pii_id"] = $infoItemval[$iid]["pii_id"];
				$infodetail["pic_content"] = $infoItemval[$iid]["pii_content"];
				
				$sqlupitem = " UPDATE ".SQL_PREFIX."picitem set pii_count = pii_count +1 WHERE pii_id = ".$iid ; 
			
				$GLOBALS['MySql'] -> querySql($sqlupitem);
				
				$sqlup = " UPDATE ".SQL_PREFIX."picture set pic_count = pic_count +1 WHERE pic_id = ".$id ; 
			
				$GLOBALS['MySql'] -> querySql($sqlup);
			}else{
				$sqlup = " UPDATE ".SQL_PREFIX."picture set pic_count = pic_count +1 WHERE pic_id = ".$id ; 
			
				$GLOBALS['MySql'] -> querySql($sqlup);
			}
			
			if($infodetail["pic_id"])
			{
				//查询上一组
				$prosql = " SELECT pic_id, pic_name  FROM ".SQL_PREFIX."picture WHERE  pic_id < ".$infodetail["pic_id"]." GROUP BY pic_id   ORDER BY pic_id  DESC ";
				$Prodetail 	= $GLOBALS['MySql']  ->  selectOne($prosql);
				$Prodetail["pic_url"]= $GLOBALS['Helpe']  -> buildUrl( $valid = $Prodetail["pic_id"],'pic');
				
				//查询下一组
				$nextsql = " SELECT pic_id, pic_name  FROM ".SQL_PREFIX."picture WHERE  pic_id > ".$infodetail["pic_id"]." GROUP BY pic_id  ORDER BY pic_id  ";
				$Nextdetail 	= $GLOBALS['MySql']  ->  selectOne($nextsql);
				$Nextdetail["pic_url"]= $GLOBALS['Helpe']  -> buildUrl( $valid = $Nextdetail["pic_id"],'pic');
			}
			$GLOBALS['Templ'] -> assign('Prodetail',$Prodetail);
			$GLOBALS['Templ'] -> assign('Nextdetail',$Nextdetail);

			$nowaddress = $nowaddress. " > 详细内容";
			$webtitle = $infodetail["pic_name"];
			

			
			$GLOBALS['Templ'] -> assign('infoItemval',$infoItemval);
			$GLOBALS['Templ'] -> assign('picnum',$picnum);
			$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
			$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
			$GLOBALS['Templ'] -> assign('infodetail',$infodetail);
			$cache_id = $id.'d'.$pageNum;
			$GLOBALS['Templ'] -> display('pic_tpl.html',$cache_id );
			
		}else{
			$GLOBALS['Helpe'] -> error404();
		}
	
	}

}

$Pic = new Pic();

?>