<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class ArticleAction{
	function index(){
		global $siteConf;
		$template = "";
		$id = $_GET['PATH_INFO'][1];
		load_class("Article");
		$am = new ArticleModel();
		//if($id && settype($id,"integer") && !isset($_GET['page']))
		$catId = getgpc("catId");
		if($catId && settype($catId,"integer")){
			$catName=$am->getCatName($catId);
			$catInfo = $am->getCatInfo($catId);
			$template = $catInfo['template'];//print_r($catInfo);
			assign("catId",$catId);
			assign("catName",$catName);
			assign("catInfo",$catInfo);
		}
			
		if($catInfo['title']){$catName = $catInfo['title'];}
		if($catName){$catName=$catName." - ";}
		$pageInfo['title'] = $catName.$siteConf['siteName'];
		$pageInfo['keywords'] = $catInfo['keywords']!=''?$catInfo['keywords']:$catInfo['title'];
		$pageInfo['description'] = $catInfo['description']!=''?$catInfo['description']:$catInfo['title'];

		assign("pageInfo",$pageInfo);
		display($template);
	}
	function view(){
		global $db,$siteConf;
		$templateView = "article/view";
		$userId = $_SESSION['userId'];
		$inajax = getgpc("inajax","P");
		$act = getgpc("act","P");
		$articleId = getgpc("articleId");
		if($inajax==1){
			if($act=="post" && $articleId && settype($articleId,"integer")){
				$content = getgpc("content","P");
				if($content && strlen($content)>=5 && strlen($content)<=500){
					BaseModel::checkLogin();
					$sql = "insert into qk_post set userId='$userId',content='$content',articleId='$articleId',posttime=UNIX_TIMESTAMP()";
					$sql .= "";
					if($db->query($sql)){
						exit('{"success":true,"message":"发表成功，谢谢！","type":3,"data":{"loginName":"'.$_SESSION['loginName'].'","posttime":"'.date("Y-m-d H:i:s").'"}}');
					}
				}else{
					exit('{"success":false,"message":"评论内容不符合规则","type":2}');
				}
			}
			exit();
		}
		load_class("Article");
		$am = new ArticleModel();

		$id = getgpc("id");
		if(!$id || !settype($id,"integer")){
			header("Location:/article/\n");
		}
		$articleInfo = $am->getInfo($id);
		if(!$articleInfo || $articleInfo['iStatus']==0){
			Msg::show(array('title'=>'文章不存在或已经被删除','msg'=>'文章不存在或已经被删除','httpStatusCode'=>404));
		}
		if($articleInfo['externalLink']){
			header("Location:".$articleInfo['externalLink']);
		}//print_r($articleInfo);
		$catId = $articleInfo['catId'];
		if($catId && settype($catId,"integer")){
			$catName=$am->getCatName($catId);
			$catInfo = $am->getCatInfo($catId);
			$templateView = $catInfo['templateView'];
			assign("catId",$catId);
			assign("catName",$catName);
			assign("catInfo",$catInfo);
		}
		if($catName){$catName = $catName." - ";}

		assign("articleInfo",$articleInfo);
		$pageInfo['title'] = $articleInfo['title']." - ".$catName.$siteConf['siteName'];
		$pageInfo['keywords'] = $articleInfo['keywords']!=''?$articleInfo['keywords']:$articleInfo['title'];
		$pageInfo['description'] = $articleInfo['description']!=''?$articleInfo['description']:substr(str_replace("&nbsp;"," ",strip_tags($articleInfo['content'])),0,200);

		assign("pageInfo",$pageInfo);
		if($templateView==""){$templateView="article/view";}
		$template = $articleInfo['template'];
		if($template){$templateView = $template;}
		display($templateView);
	}
}
?>