<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2005-2010 GV32.COM，并保留所有权利。
 * 网站地址: http://www.gv32.com；
 * 作者: GV32.COM
 *----------------------------------------------------------------------------*
**/
class Art
{
	function Art()
	{
		//缓存
		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 = 'n',$smdirid);
		}
		
		$getconfig = $GLOBALS['Helpe'] -> get_config();
		$GLOBALS['Templ'] -> assign('getconfig',$getconfig);

		$getmenu = $GLOBALS['Helpe'] -> get_menu();
		$GLOBALS['Templ'] -> assign('getmenu',$getmenu);
		
		$getartsort = $GLOBALS['Helpe'] -> get_artsort();
		$GLOBALS['Templ'] -> assign('getartsort',$getartsort);
		
		$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"]);
		$newkeyword = trim($_REQUEST["newkeyword"]);
		$addurl 	= '';
		
		if($newkeyword!='')
		{ 	
			$newkeyword  	= urldecode($newkeyword); 
			$addurl 	   .= '&newkeyword='.urlencode($newkeyword);
			$whereadd 	   .= " AND ( art_title  LIKE  '%".$newkeyword."%' )";
			//关键字入库处理
			$keyid = $GLOBALS['Helpe'] -> addKeyword($newkeyword);
		}

		$webtitle = '新闻动态';
		//处理你现在的位置
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/art.html">新闻资讯</a>';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=art&act=infolist">新闻资讯</a>';
		}
		
		$pageSize = 20 ;
		
		if( $pageNum  > 1 )
		{
			$LIMITnum = ceil( ( $pageNum -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		$addpage = '';
		
		if( $sid != '' )
		{
			$seletsort = " SELECT ars_id,ars_name  FROM ".SQL_PREFIX."artsort WHERE ars_id = ".$sid." or  ars_parentid = ".$sid ;
			$sortInfo  = $GLOBALS['MySql']  ->  selectInfo($seletsort);
			$sinid 	   = '';
			$addpage   = '&sid='.$sid;

			foreach( (array)$sortInfo as $skey => $sval )
			{
				if( $sval["ars_id"]== $sid )
				{
					$webtitle 	= $sortval = $sval["ars_name"];
					if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
					{
						$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/art-list-1-'.$sid.'.html">'.$sval["ars_name"].'</a>';
					}else{
						$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/?load=art&act=infolist'.$addpage.'">'.$sval["ars_name"].'</a>';
					}
				}
				if($skey==0)
				{
					$sinid = $sval["ars_id"];
				}else{
					$sinid .= ','.$sval["ars_id"];
				}
			}

			
			$sqlQuery 		= " SELECT art_id, art_title , art_uptime   FROM  ".SQL_PREFIX."artlice   WHERE art_sysfile ='' and  art_enabled = 1 and  ars_id in ( ".$sinid." ) ".$whereadd."  GROUP BY  art_id ORDER BY art_uptime DESC, `art_id` LIMIT  $LIMITnum , $pageSize ";
			$sqlQuerycount 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."artlice  WHERE art_sysfile ='' and  art_enabled = 1  and   ars_id in ( ".$sinid." ) ".$whereadd." ORDER BY `art_id`  ";
		
		}else{
			$sqlQuery 		= " SELECT art_id, art_title , art_uptime   FROM  ".SQL_PREFIX."artlice   WHERE art_sysfile ='' and  art_enabled = 1 ".$whereadd."  GROUP BY  art_id ORDER BY art_uptime DESC,  `art_id` LIMIT  $LIMITnum , $pageSize ";
			$sqlQuerycount 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."artlice  WHERE art_sysfile ='' and  art_enabled = 1  ".$whereadd." ORDER BY `art_id`  ";
		}
		
		$info 		= $GLOBALS['MySql']  -> selectInfo($sqlQuery);
		
		foreach((array)$info as $artkey => $artval)
		{
			$info[$artkey]["art_url"]= $GLOBALS['Helpe']  -> buildUrl( $valid = $artval["art_id"],'art');
		}

		
		
		$infocount 	= $GLOBALS['MySql']  -> selectOne($sqlQuerycount);


		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$url = BASE_WEBURL.'/art-list';
			$endArray["sid"] = $sid;
		}else{
			$url = './?load=art&act=infolist'.$addpage.$addurl;
		}
		$PageStr    = $GLOBALS['Page']   -> buildPageStr( $url , $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] ,$endArray);

		
		$GLOBALS['Templ'] -> assign('sortval',$sortval);
		$GLOBALS['Templ'] -> assign('PageStr',$PageStr);
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
		$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
		
		if($sid !='' or $pageNum!='' or  $newkeyword !='')
		{
			$cache_id = $sid.'p'.$pageNum.'k'.$keyid;
		}
		
		$GLOBALS['Templ'] -> display('artlist_tpl.html',$cache_id);	
	}


	function detail()
	{
		$id 		= trim($_REQUEST["id"]);
		$pageNum 	= trim($_REQUEST["pageNum"]);
		if($pageNum==''){ $pageNum = 1;}
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/art.html">新闻资讯</a>';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=art&act=infolist">新闻资讯</a>';
		}
		if($id > 0 )
		{
			$sqlQuery 		= " SELECT a.* ,s.ars_name  FROM  ".SQL_PREFIX."artlice as a ,".SQL_PREFIX."artsort  as s   WHERE a.ars_id = s.ars_id and a.art_enabled = 1  and a.art_id = ".$id." ORDER BY a.`art_id`  ";
			$infodetail 	= $GLOBALS['MySql']  ->  selectOne($sqlQuery);
			$reinfodetail 	= $this -> getcontent( $infodetail["art_detail"], $page=$pageNum);
			$infodetail["art_detail"] = $reinfodetail["reInfo"];
			
			$webtitle  = $infodetail["art_title"];
			
			if($infodetail["ars_name"]!='')
			{
				if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
				{
					$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/art-list-1-'.$infodetail["ars_id"].'.html">'.$infodetail["ars_name"].'</a>';
				}else{
					$nowaddress = $nowaddress. ' > <a href="'.BASE_WEBURL.'/art.php?act=infolist'.$infodetail["ars_id"].'">'.$infodetail["ars_name"].'</a>';
				}
			}
			
			
			
			if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
			{
				$url = BASE_WEBURL.'/art-'.$id;
			}else{
				$url = BASE_WEBURL.'/?load=art&act=detail&id='.$id;
			}
			$PageStr 	= $GLOBALS['Page'] -> buildPageStr( $url , $pageNum = $pageNum , $pageSize = 1 , $resultCount = $reinfodetail["pageCount"]);
			
			//浏览量 + 1
			$sqlup = " UPDATE ".SQL_PREFIX."artlice set art_count = art_count +1 WHERE art_id = ".$id ; 
			$GLOBALS['MySql'] -> querySql($sqlup);
			
			
			//查询上一篇
			$prosql = " SELECT art_id, art_title  FROM ".SQL_PREFIX."artlice WHERE  art_id < ".$id." GROUP BY art_id   ORDER BY art_id  DESC ";
			$Prodetail 	= $GLOBALS['MySql']  ->  selectOne($prosql);
			$Prodetail["art_url"]= $GLOBALS['Helpe']  -> buildUrl( $valid = $Prodetail["art_id"],'art');
			//查询下一篇
			$nextsql = " SELECT art_id, art_title  FROM ".SQL_PREFIX."artlice WHERE  art_id > ".$id." GROUP BY art_id  ORDER BY art_id  ";
			$Nextdetail 	= $GLOBALS['MySql']  ->  selectOne($nextsql);
			$Nextdetail["art_url"]= $GLOBALS['Helpe']  -> buildUrl( $valid = $Nextdetail["art_id"],'art');
			
			$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
			$GLOBALS['Templ'] -> assign('infodetail',$infodetail);
			$GLOBALS['Templ'] -> assign('PageStr',$PageStr);	//分页
			$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
			
			$GLOBALS['Templ'] -> assign('Prodetail',$Prodetail);
			$GLOBALS['Templ'] -> assign('Nextdetail',$Nextdetail);
			$cache_id = $id.'d'.$pageNum;
			$GLOBALS['Templ'] -> display('art_tpl.html',$cache_id);
			
		}else{
			$GLOBALS['Helpe'] -> error404();
		}
	
	}

	//分页程序
	function getcontent($content, $page=1)
	{  
		$page = $page ? intval($page) : 1;
		
		$reArticle = array(  'reInfo' => array(), 'pageCount' => 1 );
		
		if(!empty($content))
		{  
			$pattern = "/<div style=\"page-break-after: always;?\">\s*<span style=\"display: none;?\">&nbsp;<\/span>\s*<\/div>/";
			$contents = preg_split($pattern, $content);  
			$reArticle['pageCount'] = count($contents); 
			($page > $reArticle['pageCount']) && $page = $reArticle['pageCount'];  
			$reArticle['reInfo'] = $contents[$page - 1];
			
		}else{
			return false;
		}
		
		return $reArticle;  
	}
	
}
 
$Art = new Art();

 
?>