<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 网站地址: http://Www.HuCart.Com；
 * 作者: HuCart.COM
 *----------------------------------------------------------------------------*
**/

class Article
{
	function Article()
	{
		//缓存
		if($GLOBALS["WebConfig"]["webtmp"]==1)
		{
			$this -> view -> caching = true;
			if($GLOBALS["WebConfig"]["webtmptime"] > 0 )
			{
				$this -> view -> cache_lifetime = $GLOBALS["WebConfig"]["webtmptime"] ;
			}else{
				$this -> view -> cache_lifetime = 3600 ;  //60 *  60 * 24
			}
			$smdirid = intval(trim($_REQUEST["id"])).intval(trim($_REQUEST["page_num"]));
			$this -> view -> cache_dir = ROOT_PATH.$GLOBALS['cache_path'].'/'.$this -> helper -> buildsmartydir( $type = 'n',$smdirid);
		}
		
 

		$getmenu = $this -> helper -> get_menu();
		$this -> view -> assign('getmenu',$getmenu);
		
		$getartsort = $this -> personal  -> get_artsort();
		$this -> view -> assign('getartsort',$getartsort);

		$getads = $this -> helper -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$this -> view -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		$this -> view ->assign('getuserlink',$getuserlink);

		$act = $GLOBALS['Reque'] -> reqval["act"];
		if($act=='')
		{
			$this -> infolist();
		}
	}
	

	
	function infolist()
	{
		$sid 			= $GLOBALS['Reque'] -> reqval["sid"];
		$page_num 		= intval(trim($_REQUEST["page_num"]));
		$newkeywordval  = $GLOBALS['Reque'] -> reqval["newkeyword"];
		
		if($newkeywordval=='' and $GLOBALS['Reque'] -> reqval["keyword"]){ $newkeywordval  = $GLOBALS['Reque'] -> reqval["keyword"]; }
		
		$addurl 		= '';
		//左则自定义内容
		$sqlQuery 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'artlist' ORDER BY `per_id`  ";	
		$infoper 	= $this -> db ->  selectOne($sqlQuery);
		$this -> personal_mod -> get_column($infoper["per_column"]);

		

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

		$addpage = '';
		
		if( $sid > 0 and  $sid !='2' )
		{
			$endArray["sid"] = $sid;
		}else{
			$endArray["sid"] = '2' ;
		}
		
		if($newkeywordval!='')
		{ 	
			$newkeywordval  	= urldecode($newkeywordval); 
			$addurl 	   .= '&newkeywordval='.urlencode($newkeywordval);
			$whereadd 	   .= " AND ( art_title  LIKE  '%".$newkeywordval."%' )";
			//关键字入库处理
			$keyid = $this -> helper -> addKeyword($newkeywordval);
 
			$endArray["keyword"] = urlencode($newkeywordval) ;
			
		}

		
		
		
		if( $sid != '' )
		{
			$seletsort = " SELECT ars_id,ars_name  FROM ".$this -> db -> prefix."artsort WHERE ars_id = ".$sid." or  ars_parentid = ".$sid ;
			$sortInfo  = $this -> db ->  select_info($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 )
					{
						$now_address = $now_address. ' > <a href="'.BASE_WEBURL.'/art-list-1-'.$sid.'.html">'.$sval["ars_name"].'</a>';
					}else{
						$now_address = $now_address. ' > <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  ".$this -> db -> prefix."article   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  $limit_num , $page_size ";
			$sqlQuerycount 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."article  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  ".$this -> db -> prefix."article   WHERE art_sysfile ='' and  art_enabled = 1 ".$whereadd."  GROUP BY  art_id ORDER BY art_uptime DESC,  `art_id` LIMIT  $limit_num , $page_size ";
			$sqlQuerycount 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."article  WHERE art_sysfile ='' and  art_enabled = 1  ".$whereadd." ORDER BY `art_id`  ";
		}
		
		$info 		= $this -> db -> select_info($sqlQuery);
		
		foreach((array)$info as $artkey => $artval)
		{
			$info[$artkey]["get_url"]= $this -> helper  -> build_url( $valid = $artval["art_id"],'art');
		}

		
		
		$infocount 	= $this -> db -> selectOne($sqlQuerycount);


		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$url = BASE_WEBURL.'/art-list';
		}else{
			$url = './?load=art&act=infolist'.$addpage.$addurl;
		}
		$page_str    = $GLOBALS['Page']   -> buildpage_str( $url , $page_num  , $page_size  , $result_count = $infocount["CNUM"] ,$endArray);

		
		$this -> view -> assign('sortval',$sortval);
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);
		
		if($sid !='' or $page_num!='' or  $newkeywordval !='')
		{
			$cache_id = $sid.'p'.$page_num.'k'.$keyid;
		}
		
		$this -> view -> display('artlist_tpl.html',$cache_id);	
	}


	function detail()
	{
		$id 		= intval(trim($_REQUEST["id"]));
		$page_num 	= intval(trim($_REQUEST["page_num"]));
		
		if($page_num==''){ $page_num = 1;}
		
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/art.html">新闻资讯</a>';
		}else{
			$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=art&act=infolist">新闻资讯</a>';
		}

		//左则自定义内容
		$sqlQuery 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'art' ORDER BY `per_id`  ";	
		$infoper 	= $this -> db ->  selectOne($sqlQuery);
		
		$this -> personal_mod -> get_column( $infoper["per_column"] );

		if( $id > 0 )
		{
			$sqlQuery 		= " SELECT a.* ,s.ars_name  FROM  ".$this -> db -> prefix."article as a ,".$this -> db -> 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 	= $this -> db -> selectOne($sqlQuery);
			$reinfodetail 	= $this -> getcontent( $infodetail["art_detail"], $page = $page_num);
			$infodetail["art_detail"] = $this -> helper -> restrval( $reinfodetail["reInfo"]) ;
			
			$webtitle  		= $infodetail["art_title"];
			if($infodetail["ars_name"]!='')
			{
				if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
				{
					$now_address = $now_address. ' > <a href="'.BASE_WEBURL.'/art-list-1-'.$infodetail["ars_id"].'.html">'.$infodetail["ars_name"].'</a> > 正文';
				}else{
					$now_address = $now_address. ' > <a href="'.BASE_WEBURL.'/?load=art&act=infolist&sid='.$infodetail["ars_id"].'">'.$infodetail["ars_name"].'</a> > 正文';
				}
			}
			$this -> load_model('comemt'); 	//加载模型	

			$commentInfo = $this -> comemt_mod  -> get_comemt( $id , $Num = 10 , $type = 'art');
			$this -> view -> assign('commentInfo',$commentInfo);
			
			if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
			{
				$url = BASE_WEBURL.'/art-'.$id;
			}else{
				$url = BASE_WEBURL.'/?load=art&act=detail&id='.$id;
			}
			$page_str 	= $GLOBALS['Page'] -> buildpage_str( $url , $page_num = $page_num , $page_size = 1 , $result_count = $reinfodetail["pageCount"]);
			
			//浏览量 + 1
			$sqlup = " UPDATE ".$this -> db -> prefix."article set art_count = art_count +1 WHERE art_id = ".$id ; 
			$this -> db -> querySql($sqlup);
			
			
			//查询上一篇
			$prosql 	= " SELECT art_id, art_title  FROM ".$this -> db -> prefix."article WHERE  art_id < ".$id." GROUP BY art_id   ORDER BY art_id  DESC ";
			$Prodetail 	= $this -> db ->  selectOne($prosql);
			$Prodetail["get_url"]= $this -> helper  -> build_url( $valid = $Prodetail["art_id"],'art');
			
			//查询下一篇
			$nextsql 	= " SELECT art_id, art_title  FROM ".$this -> db -> prefix."article WHERE  art_id > ".$id." GROUP BY art_id  ORDER BY art_id  ";
			$Nextdetail = $this -> db ->  selectOne($nextsql);
			$Nextdetail["get_url"]= $this -> helper  -> build_url( $valid = $Nextdetail["art_id"],'art');
			
			$cache_id = $id.'d'.$page_num;
			
			
			$this -> view -> assign('webtitle',$webtitle);
			$this -> view -> assign('infodetail',$infodetail);
			$this -> view -> assign('page_str',$page_str);	//分页
			$this -> view -> assign('now_address',$now_address);
			$this -> view -> assign('Prodetail',$Prodetail);
			$this -> view -> assign('Nextdetail',$Nextdetail);
			$this -> view -> display('art_tpl.html',$cache_id);
			
		}else{
			$this -> error('404');
		}
	
	}

	//分页程序
	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;  
	}
	
	

	
}
 
$Article = new Article();

 
?>