<?php
CURSCRIPT != 'wap' && exit('FORBIDDEN');
$id = isset($id) ? intval($id) : '';
$catid = isset($catid) ? intval($catid) : '';
$page = isset($page) ? intval($page) : 1;
!ifplugin('news') && exit('管理员已禁用或未安装新闻插件...');

$keyword_sql	= $keywords ? " AND title LIKE '%".$keywords."%'" : "";
$city_limit = empty($city_limit)?"":" AND cityid = '$cityid'";

if($id){
	if(!$id) errormsg('新闻ID不能为空！');
	$news = $db ->getRow("SELECT * FROM {$db_mymps}news WHERE id = '$id'");
	if($news['iscommend']<1){return404();}
	
	$db->query("UPDATE `{$db_mymps}news` SET hit = hit +1 WHERE id = '$id';");
	$news['catname'] = $db->getOne("SELECT catname FROM `{$db_mymps}channel` WHERE catid = '$news[catid]'");
	$news['caturi']=mRewrite('news',array('catid'=>$news['catid'],'domain'=>$city['domain']));
	$news['content'] = replace_insidelink($news['content'],'news');
	$cityid = $news['cityid'];
	$city=get_city_caches($cityid,$areaid);
	if(!defined('WAP')) $city['mdomain']=$city['domain'];
	$rootchannel = get_root_channel();
	$loc				 = get_location_m('channel',$news[catid],$news[title]);
	//$page_title			 = $loc['page_title'];
	$page_title 		 = $news['title'].' - '.$city['cityname'].$mymps_global['SiteName'];
	$news['keywords']	 = $news['keywords'] ? $news['keywords'] : $news['title'];
	$news['description'] = cutstr(clear_html($news['content']),250);
	
	$shareIcon = strstr($news['imgpath'],'http')?$news['imgpath']:$mymps_global['SiteUrl'].$news['imgpath'];
	globalassign();
	include mayi_tpl('news');
}elseif($catid){
	$cat = get_cat_info($catid,'channel');
	if(!$cat) errormsg('您所指定的新闻栏目不存在或者已经删除！');
	
	$rootchannel = get_root_channel();
	$cat_children	= get_channel_children($catid);
	$perpage = $mayimobile['mobiletopicperpage'] ? $mayimobile['mobiletopicperpage'] : 10;
	$param		= setparams(array('mod','catid'));
	$rows_num = $db->getOne("SELECT COUNT(*) FROM `{$db_mymps}news` WHERE $cat_children {$keyword_sql}{$city_limit} AND iscommend > '0'{$keword_sql} {$city_limit}");
	$totalpage = ceil($rows_num/$perpage);
	$num = intval($page-1)*$perpage;
	
	$page1 = page1("SELECT * FROM {$db_mymps}news WHERE $cat_children AND iscommend > '0' {$keword_sql}{$city_limit} ORDER BY begintime DESC",$perpage);
	foreach($page1 as $kr => $r){
		$arr['begintime']   = $r['begintime'];
		$arr['begintimej']  = GetTime($r['begintime'],'m-d');
		$arr['hit']  		= $r['hit'];
		$arr['title']  	    = $r['title'];
		$arr['titlej']		= $charset=='gbk'?iconv('gb2312','utf-8',$r['title']):$r['title'];
		$arr['iscommend']  	= $r['iscommend'];
		$arr['content'] 	= clear_html($r['content']);
		$arr['contentj'] 	= cutstr($arr['content'],60);
		$arr['uri_m']	  	= $r['isjump'] ? $r['redirect_url'] : mRewrite('news',array('id'=>$r['id'],'cityid'=>$r['cityid']));
		$arr['uri_ma']	  	= $r['isjump'] ? $r['redirect_url'] : $city['mdomain'].'index.php?mod=news&id='.$r['id'];
		$arr['imgpath'] 	= $r['imgpath'];
		$news[]			  	= $arr;
	}
	
	if($mayi){
		$news = is_array($news) ? array_values($news) : NULL;
		$news = json_encode($news,JSON_UNESCAPED_SLASHES);
		echo $news;
		exit;
	}

	$parentcats = get_parent_cats('channel',$catid);
	$parentcats = is_array($parentcats) ? array_reverse($parentcats) : '';
	
	$loc		= get_location_m('channel',$catid);
	$page_title	= $cat['title']?str_replace('{city}',$city['cityname'],$cat['title']):$loc['page_title'];
	$page_title = $page_title . ($page>1?'-第'.$page.'页':'') .'-'.$city['cityname'].$mymps_global['SiteName'];
	$cat['keywords'] = str_replace('{city}',$city['cityname'],$cat['keywords']);
	$cat['description'] = str_replace('{city}',$city['cityname'],$cat['description']);

	globalassign();
	include mayi_tpl('news_list');
}else{

	$rootchannel = get_root_channel();
	$perpage = $mayimobile['mobiletopicperpage'] ? $mayimobile['mobiletopicperpage'] : 10;
	$param		= setparams(array('mod'));
	$rows_num = $db->getOne("SELECT COUNT(*) FROM `{$db_mymps}news`  WHERE 1 {$keyword_sql}{$city_limit}");
	$totalpage = ceil($rows_num/$perpage);
	$num = intval($page-1)*$perpage;
	
	$page1 = page1("SELECT * FROM {$db_mymps}news WHERE 1 {$keyword_sql}{$city_limit} AND iscommend > '0' ORDER BY begintime DESC",$perpage);
	foreach($page1 as $kr => $r){
		$arr['begintime']   = $r['begintime'];
		$arr['begintimej']  = GetTime($r['begintime'],'m-d');
		$arr['hit']  		= $r['hit'];
		$arr['title']  	    = $r['title'];
		$arr['titlej']		= $r['title'];
		$arr['content'] 	= clear_html($r['content']);
		$arr['contentj'] 	= cutstr($arr['content'],60);
		$arr['uri_m']	  	= mRewrite('news',array('id'=>$r[id],'id'=>$r['id'],'cityid'=>$r['cityid']));
		$arr['imgpath'] 	= $r['imgpath'];
		$news[]			  	= $arr;
	}
	
	if($mayi){
		$news = json_encode($news,JSON_UNESCAPED_SLASHES);
		echo $news;
		exit;
	}
	
	$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_m('news',0,'热点资讯');
	$page_title = $s['title'] ? $s['title'] : $loc['page_title'];
	$page_title = $page_title . ($page>1?'-第'.$page.'页':'') .'-'.$city['cityname'].$mymps_global['SiteName'];
	globalassign();
	include mayi_tpl('news_index');
}
?>