<?php
define('CURSCRIPT','news');
if(!defined('IN_MYMPS')) exit('FORBIDDEN');
!ifplugin('news') && exit('管理员已禁用或未安装新闻插件...');

$id	= isset($id) ? intval($id) : '';
$catid = isset($catid) ? intval($catid) : 0;
$page = isset($page) ? intval($page) : 1;
$cityid = isset($cityid) ? intval($cityid) : 0;

$seo		= get_seoset();
$rewrite 	= $seo['seo_force_news'];

if($id) {
	$id = isset($id) ? intval($id) : '';
	$news = $db -> getRow("SELECT * FROM `{$db_mymps}news` WHERE id = '$id'");
	if($news['iscommend']<1){
		return404();
	}else{
		$news['perhit'] = $news['perhit'] ? $news['perhit'] : 1;
		$db->query("UPDATE `{$db_mymps}news` SET hit = hit + ".$news['perhit']." WHERE id = '$id'");
	}
	
	$news['content'] = replace_insidelink($news['content'],'news');
	$cityid = $news['cityid'];
	$city = get_city_caches($cityid,$areaid);
	
	$news['view_url'] = Rewrite('news',array('id'=>$news['id'],'cityid'=>$news['cityid']));
	
	if($news['redirect_url'] != '' && $news['isjump'] == 1) write_msg('请稍候，当前页面正跳转至 '.$news[redirect_url].' ',$news[redirect_url]);
	
	$loc				 = get_location('channel',$news[catid],$news[title]);
	$location	 		 = $loc['location'];
	$page_title			 = $loc['page_title'];
	$page_title 		 = $page_title .' - '.$city['cityname'].$mymps_global['SiteName'];
	
	$advertisement			= get_advertisement('other');
	$adveritems				= $city['advertisement'];
	$advertisement			= $advertisement['all'];
	
	$news['keywords']	 = $news['keywords'] ? $news['keywords'] : $news['title'];
	$news['description'] = mhtmlspecialchars(cutstr(clear_html($news['content']),400));
	
	$next=$db->getRow("SELECT id,title,cityid FROM `{$db_mymps}news` WHERE id > $id AND iscommend > '0' ORDER BY id DESC LIMIT 0,1");
	$last=$db->getRow("SELECT id,title,cityid FROM `{$db_mymps}news` WHERE id < $id AND iscommend > '0' ORDER BY id DESC LIMIT 0,1");
	$news['catname'] = $db->getOne("SELECT catname FROM `{$db_mymps}channel` WHERE catid = '$news[catid]'");
	globalassign();
	include mayi_tpl('news');
} elseif($catid) {
	
	$city = get_city_caches($cityid,$areaid);

	$catid = isset($catid) ? intval($catid) : 0;
	$channel = get_cat_info($catid,'channel');
	if(!$channel) write_msg('您所指定的新闻栏目不存在或者已经删除！');
	
	$cat_children	= get_channel_children($catid);
	$city_limit = $cityid > 0 ? " AND a.cityid = '$city[cityid]'" : " ";
	
	$param = setParam(array('catid'),$rewrite,'news/');
	$rows_num = $db->getOne("SELECT COUNT(a.id) FROM `{$db_mymps}news` AS a WHERE {$cat_children} AND (a.iscommend = 1 OR a.iscommend = 2) {$city_limit}");
	$idin = get_page_idin("id","SELECT a.id FROM `{$db_mymps}news` AS a WHERE {$cat_children} AND (a.iscommend = 1 OR a.iscommend = 2) {$city_limit} ORDER BY a.begintime DESC",25);
	
	$sql = "SELECT a.* FROM {$db_mymps}news AS a WHERE a.id IN ($idin) ORDER BY a.begintime DESC";
	$page1 = $idin ? $db -> getAll($sql) : array();
	foreach($page1 as $kr => $r){
		$arr['begintime']   = $r['begintime'];
		$arr['hit']  		= $r['hit'];
		$arr['title']  	    = $r['title'];
		$arr['iscommend']  	= $r['iscommend'];
		$arr['content'] 	= clear_html($r['content']);
		$arr['uri']	  	  	= $r['isjump'] ? $r['redirect_url'] : Rewrite('news',array('id'=>$r['id'],'cityid'=>$r['cityid']));
		$arr['imgpath'] 	= $r['imgpath']?$r['imgpath']:'/images/nophoto.gif';
		$news[]			  	= $arr;
	}
	$pageview = page2($rewrite,'/');

	$advertisement			= get_advertisement('other');
	$adveritems				= $city['advertisement'];
	$advertisement			= $advertisement['all'];
	
	$cat_list = get_categories_tree($catid,'channel');
	$pageview = page2($rewrite,'/');
	
	$loc		= get_location('channel',$catid);
	$location	= $loc['location'];
	$page_title	= $loc['page_title'];
	$page_title	= $channel['title']?str_replace('{city}',$city['cityname'],$channel['title']):$loc['page_title'];
	$page_title = $page_title . ($page>1?' - 第'.$page.'页':'') .' - '.$city['cityname'].$mymps_global['SiteName'];
	$channel['keywords']	 = str_replace('{city}',$city['cityname'],$channel['keywords']);
	$channel['description'] = str_replace('{city}',$city['cityname'],$channel['description']);
	globalassign();
	include mayi_tpl('news_list');
} else {
	
	$city = get_city_caches($cityid,$areaid);
	
	$channel = get_root_channel();
	$city_limit = $cityid > 0 ? " AND a.cityid = '$city[cityid]'" : " ";
	
	$param = setParam(array('catid'),$rewrite,'news/');
	$rows_num = $db->getOne("SELECT COUNT(*) FROM `{$db_mymps}news` AS a WHERE a.iscommend > 0 {$city_limit}");
	$page1 = page1("SELECT a.* FROM {$db_mymps}news AS a WHERE a.iscommend > 0 {$city_limit} ORDER BY a.begintime DESC",25);
	foreach($page1 as $kr => $r){
		$arr['begintime']   = $r['begintime'];
		$arr['hit']  		= $r['hit'];
		$arr['title']  	    = $r['title'];
		$arr['catname']  	= $r['catname'];
		$arr['imgpath']  	= $r['imgpath']?$r['imgpath']:'/images/nophoto.gif';
		$arr['iscommend']  	= $r['iscommend'];
		$arr['content'] 	= clear_html($r['content']);
		$arr['uri']	  	  	= $r['isjump'] ? $r['redirect_url'] : Rewrite('news',array('id'=>$r['id'],'cityid'=>$r['cityid']));
		$arr['imgpath'] 	= $r['imgpath'];
		$news[]			  	= $arr;
	}
	$pageview = page2($rewrite,'/');
	
	$s = array();
	$s['title'] = str_replace('{city}',$city['cityname'],$pluginsettings['news']['seotitle']);
	$s['keywords'] = str_replace('{city}',$city['cityname'],$pluginsettings['news']['seokeywords']);
	$s['description'] = str_replace('{city}',$city['cityname'],$pluginsettings['news']['seodescription']);
	
	$loc		= get_location('news',0,$city[cityname].'热点资讯');
	$page_title = $loc['page_title'];
	$location	= $loc['location'];
	$page_title = $s['title']?$s['title']:$page_title;

	$page_title = $page_title . ($page>1?' - 第'.$page.'页':'') .' - '.$city['cityname'].$mymps_global['SiteName'];
	
	$advertisement	= get_advertisement('other');
	$adveritems		= $city['advertisement'];
	$advertisement	= $advertisement['all'];
	globalassign();
	include mayi_tpl('news_index');
}
?>