<?php
session_start();

if( isset($_SESSION['init_alert']) )
{
	if( ( $str_temp = trim($_SESSION['init_alert']) ) != '' )
		$str_init_alert = $str_temp;

	unset($_SESSION['init_alert']);
}

if( !isset($_SESSION['search_keyword']) || !( $static_str_search_keyword = trim($_SESSION['search_keyword']) ) )
{
	$_SESSION['init_alert'] = '請輸入您要搜尋之商品關鍵字';

	header('Location:index.php');
	exit();
}
else if( strlen($static_str_search_keyword) <= 1 )
{
	unset($_SESSION['search_keyword']);

	$_SESSION['init_alert'] = '關鍵字太短，請多輸入一些關鍵字以便精確搜尋';

	header('Location:index.php');
	exit();
}

if( !isset($_REQUEST['page_no']) || ( $uint_page_no = (int)$_REQUEST['page_no'] ) <= 0 )
	$static_uint_page_no = 1;
else
	$static_uint_page_no = $uint_page_no;

$uint_row_count = 12;

require_once('includes/common.php');

require_once('includes/data.php');
$obj_class_data = new class_data();

if( !( $arr_news_cates = $obj_class_data->get_news_cates() ) )
{
	$_SESSION['init_alert'] = '指定的新聞分類不存在';

	header('Location: latest_news_list.php');
	exit();
}

$arr_page_info = $obj_class_data->page_info('`NewsId`', 'news', '`Active` = \'1\' AND `news`.`Date` <= \'' . date('Y-m-d') . '\' AND ( `Subject` LIKE \'%' . mysqli_real_escape_string($obj_class_data->sql_connect, $static_str_search_keyword) . '%\' OR `Abstract` LIKE \'%' . mysqli_real_escape_string($obj_class_data->sql_connect, $static_str_search_keyword) . '%\' )', $uint_row_count);

if( $arr_page_info['PageCount'] && $static_uint_page_no > $arr_page_info['PageCount'] )
	$uint_page_no = $arr_page_info['PageCount'];
else
	$uint_page_no = $static_uint_page_no;

$arr_news = $obj_class_data->search_news($static_str_search_keyword, ( $uint_page_no - 1 ) * $uint_row_count, $uint_row_count);

$arr_banner = $obj_class_data->get_banners(0, 2);

$arr_suggest_news = $obj_class_data->get_suggest_news(4);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <title>搜尋：<?=$static_str_search_keyword?> | 國防新聞網</title>
    <!-- Bootstrap -->
	<link href="css/bootstrap.css" rel="stylesheet">
	<link href="css/css.css" rel="stylesheet" type="text/css">
	<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />

	<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
		  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
		  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
		<![endif]-->
  </head>
  <body<?=( isset($str_init_alert) ? ' onLoad="javascript:alert(\'' . $str_init_alert . '\');"' : '' )?>>
<? include('_title.php'); ?>
  <div id="c_contents" class="container" >
    <div id="list_contents_l">
    <div class="top_itle">搜尋：<?=$static_str_search_keyword?></div>
<div class="container column_6" >
  <ul>
<?
if( $arr_news )
{
	foreach( $arr_news as $arr_row )
	{
?>
      <li><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><img src="images/news/<?=$arr_row['NewsId']?>.jpg" /></a>
      <h1><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><?=$arr_row['Subject']?></a></h1>
      <p><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><?=mb_substr($arr_row['Abstract'], 0, 25, 'UTF-8')?>...</a></p>
      </li>
<?
	}
}
?>
    </ul>
    
  </div>
     <nav class="paging">
       <!-- Add class .pagination-lg for larger blocks or .pagination-sm for smaller blocks-->
       <ul class="pagination">
         <li<?=( $static_uint_page_no <= 1 ? ' class="disabled"' : '' )?>><a href="<?=( $static_uint_page_no <= 1 ? '#' : ('search_news.php?page_no=' . ($static_uint_page_no - 1)) )?>" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
<?
for( $i=-2; $i<=2; $i++ )
{
	$uint_page_no_this = $static_uint_page_no + $i;

	if( $uint_page_no_this < 1 || $uint_page_no_this > $arr_page_info['PageCount'] )
		continue;
	
	echo('         <li' . ( !$i ? ' class="active"' : '' ) . '><a href="search_news.php?page_no=' . $uint_page_no_this . '">' . $uint_page_no_this . '</a></li>' . "\n");
}
?>
         <li<?=( $static_uint_page_no >= $arr_page_info['PageCount'] ? ' class="disabled"' : '' )?>><a href="<?=( $static_uint_page_no >= $arr_page_info['PageCount'] ? '#' : ('search_news.php?page_no=' . ($static_uint_page_no + 1)) )?>" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
       </ul>
     </nav>
    </div>
  
  <div id="c_contents_r">
<?
if( isset($arr_banner[0]) && $arr_banner[0] )
{
?>
     <div class="contents_r_ad"><a href="<?=$arr_banner[0]['Url']?>"<?=( $arr_banner[0]['NewTarget'] ? ' target="_blank"' : '' )?>><img alt="<?=$arr_banner[0]['Slogan']?>" title="<?=$arr_banner[0]['Slogan']?>" src="images/banners/<?=$arr_banner[0]['BannerId']?>.jpg" /></a></div>
<?
}
?>
     <div class="container top_title_a">推薦要聞</div>
    <div class="container column_5" >
    <ul>
<?
if( $arr_suggest_news )
{
	foreach( $arr_suggest_news as $arr_row )
	{
?>
      <li>
      <a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><img src="images/news/<?=$arr_row['NewsId']?>.jpg" /></a>
      <h1><a href="news.php?news_id=<?=$arr_row['NewsId']?>&cate_id=<?=$arr_row['NewsCateId']?>"><?=mb_substr($arr_row['Subject'], 0, 25, 'UTF-8')?>...</a></h1>
      <h2><?=$arr_row['Date']?>｜<span class="category_subhead"><a href="news_list.php?cate_id=<?=$arr_row['NewsCateId']?>"><?=$arr_row['NewsCateName']?></a></span></h2>
      </li>
<?
	}
}
?>
    </ul>
    
    </div> 
<?
if( isset($arr_banner[1]) && $arr_banner[1] )
{
?>
     <div class="contents_r_ad"><a href="<?=$arr_banner[1]['Url']?>"<?=( $arr_banner[1]['NewTarget'] ? ' target="_blank"' : '' )?>><img alt="<?=$arr_banner[1]['Slogan']?>" title="<?=$arr_banner[1]['Slogan']?>" src="images/banners/<?=$arr_banner[1]['BannerId']?>.jpg" /></a></div>
<?
}
?>
  </div>


 
  </div>
    
   
    
  <div class="container-fluid" id="footer">
    <div class="container footer_nav">
      <ul>
      <li>使用條款</li>
      <li>關於國防新聞網</li>
      <li>隱私政策</li>
      <li>瀏覽協助</li>
      <li>聯絡我們</li>
      </ul>
      
    </div>
    <div class="container">
      <div class="copyright">Copyright © 2015 國防新聞網 All Rights Reserved.</div>
      <div class="go_top">
      <ul>
      <li><a id="footer_menu_2" title="返回頂端" href="#">go top</a></li>
      <li><a id="footer_menu_1" href="https://www.facebook.com/%E9%98%B2%E5%8B%99%E5%BF%AB%E9%96%80-243554065699495/" title="防務快門粉絲團" target="_blank" >防務快門粉絲團</a></li>
      </ul>
      </div>
    </div>
  </div>
  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
	<script src="js/jquery-1.11.2.min.js"></script>

	<!-- Include all compiled plugins (below), or include individual files as needed --> 
	<script src="js/bootstrap.js"></script>
    <?php include_once("analyticstracking.php") ?>
  </body>
</html>
