<?php
// +---------------------------------------------+
// |     Copyright 2006-2009 weenCompany |
// |     http://www.weentech.com               |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

define('IN_WEENCOMPANY', true);

$rootpath = "../";

// ########################### LOAD WEENCOMPANY CORE ############################

include($rootpath . 'includes/core.php');



// ######################### CHECK FOR INCORRECT DATA ##########################

if(!preg_match("/^[0-9]+$/", $moduleid))
{
  exit();
}else if(!preg_match("/^[0-9]+$/", $objectid)){
  exit();
}

function TranslateObjectID($moduleid, $objectid)
{
  global $DB;
  $modulepath = $DB->query_first("SELECT modulepath FROM " . TABLE_PREFIX . "modules WHERE moduleid = $moduleid");
  $modulename = substr($modulepath['modulepath'], strpos($modulepath['modulepath'], '_') + 1, strpos($modulepath['modulepath'], '/') - strpos($modulepath['modulepath'], '_') - 1);
  
  if ($modulename == "news")
  {
          $title = $DB->query_first("SELECT title FROM " . TABLE_PREFIX . "m". $moduleid."_news WHERE articleid = $objectid");
          $title = $title[0];
  }
  elseif ($modulename == "downloadmanager")
  {
          $title = $DB->query_first("SELECT title FROM " . TABLE_PREFIX . "m". $moduleid."_files WHERE fileid = $objectid");
          $title = $title[0];
  }
  elseif ($modulename == "products")
  {
          $title = $DB->query_first("SELECT title FROM " . TABLE_PREFIX . "m". $moduleid."_products WHERE imageid = $objectid");
          $title = $title[0];
  }
  elseif ($modulename == "advert")
  {
          $title = $DB->query_first("SELECT title FROM " . TABLE_PREFIX . "m". $moduleid."_advert WHERE ad_id = $objectid");
          $title = $title[0];
  }
  else{
          $title = '未知 (ID ' . $objectid . ')';
  }
  
  
  return $title;
}

// ################### SET LOCALE TIME AND HEADER INFORMATION ##################

$languageinfo = explode('|', $mainsettings['language']);

$charset = $languageinfo[2];
$m16_language = GetLanguage(16);


header("Content-type: text/html; charset=$charset");

// ############################## DISPLAY HISTORY ##############################

$start = (isset($_GET['ob_start']) AND preg_match("/^[0-9]+$/", $_GET['ob_start'])) ? $_GET['ob_start'] : 0;
$limit = 10;

$getcomments = $DB->query("SELECT date, username, comment FROM " . TABLE_PREFIX . "comments WHERE moduleid = '$moduleid' AND objectid = '$objectid' ORDER BY commentid DESC  LIMIT $start, " . ($limit+1) . "");
$rows = $DB->get_num_rows($getcomments);
$maxrows = $DB->query_first("SELECT COUNT(*) AS value FROM " . TABLE_PREFIX . "comments WHERE moduleid = '$moduleid' AND objectid = '$objectid'");

$title = TranslateObjectID($moduleid, $objectid);

echo '<html>
      <head>
	  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	  <title>'. $title .' - '. $sdlanguage['more_comments'] . '</title>
		<style type="text/css">
		<!--
		.pages_nav a {BACKGROUND: #cad9ea; PADDING-TOP: 5px; PADDING-BOTTOM: 5px; PADDING-RIGHT: 7px; PADDING-LEFT: 7px;  LINE-HEIGHT: 24px;}
		.pages_nav STRONG{font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; FONT-WEIGHT: bold; BACKGROUND: #BBBBBB; COLOR: #CC0000;PADDING-TOP: 5px; PADDING-BOTTOM: 5px; PADDING-RIGHT: 7px; PADDING-LEFT: 7px;LINE-HEIGHT: 24px;}
		a:link  { font-family: Arial, Verdana, Helvetica, sans-serif; font-size:   12px; color: #373B3F; text-decoration: underline; }
		a:active  { font-family: Arial, Verdana, Helvetica, sans-serif; font-size:   12px; color: #373B3F; text-decoration: underline; }
		a:visited { font-family: Arial, Verdana, Helvetica, sans-serif; font-size:   12px; color: #373B3F; text-decoration: underline; }
		a:hover  { font-family: Arial, Verdana, Helvetica, sans-serif; font-size:   12px; color: #395E70; text-decoration: none; }
		-->
		</style>
	  </head>
      <body bgcolor="#B4BFD3">

      <table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr>
        <td bgcolor="#FFFFFF" style="border: 1px solid #000000; padding: 10px;" valign="top">';

echo $title . ' - '. $sdlanguage['more_comments'] . '<br /><hr size=2>';

for($i = 0; $i < $rows AND $i < $limit; $i++) {
  $post = $DB->fetch_array($getcomments);

  echo '<b>' . $post['username'] . '</b> - ' . DisplayDate($post['date'], 'Y.m.d') .'<br />';


  echo nl2br($post['comment']) . '<br /><br /><hr size="1" noshade="noshade" />';
}

							 
					 
if($start > 0 OR $rows > $limit)
{
		echo '<br />
			  <table width="100%" cellpadding="0" cellspacing="0" border="0">
			  <tr>';

		if($start > 0)
		{
		  echo '<td style="padding-left: 3px; vertical-align:middle;" width="10%" nowrap="nowrap"><a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' . ($start - $limit) . '">'.$m16_language['previous_links'].'</a></td>';
		}else{
		  echo '<td style="padding-left: 3px; vertical-align:middle;" width="10%" nowrap="nowrap">&nbsp;</td>';
		}
		

		$sectionarticlesnum = $maxrows['value'];
		$page_num = ceil($sectionarticlesnum / $limit);
		$page_group = floor($start/$limit/8)*8+1;
		$now_page_num = $start/$limit+1;

		if($page_num - $now_page_num < 10){
			$start_page = $page_num - 9;
			if($start_page<1){
				$start_page = 1;
			}
			$end_page = $page_num + 1;
		}else{
			$start_page = $page_group;
			$end_page = $start_page + 8;
		}

		if($start_page > 1){
			$direct_bar = '<a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=0">1</a><a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' . ($start_page-2)*$limit . '">&lsaquo;&lsaquo;...</a>';
		}

		for ($p=$start_page; $p < $end_page; $p++ ) 
		{	
			if (($start/$limit+1) == $p){
				$direct_bar .= '<STRONG>'.$p.'</STRONG>';
			}else{
				$direct_bar .= '<a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' . ($p-1)*$limit . '">'.$p.'</a>';
			}


			if($p >= $page_num){
				break;
			}

		}
		
		if($page_num - $now_page_num >= 10){
			$direct_bar .= '<a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' . (($page_group + 7)*$limit) . '">...&rsaquo;&rsaquo;</a><a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' .($page_num-1)*$limit . '">'.$page_num.'</a>';
		}

		echo '<td width="80%" style="text-align:center; vertical-align:middle;" class="pages_nav">'.$direct_bar.'</td>';


		if($rows > $limit)
		{
		  echo '<td style="padding-right: 3px; vertical-align:middle;" width="10%" nowrap="nowrap"><div align="right"><a href="'.$weenurl.'modules/viewcomments.php?moduleid='.$moduleid.'&objectid='.$objectid.'&ob_start=' . ($start + $limit) . '">'.$m16_language['more_links'].'</a></div></td>';
		}else{
		  echo '<td style="padding-right: 3px; vertical-align:middle;" width="10%" nowrap="nowrap">&nbsp;</td>';
		}

		echo '</tr></table><br>';
}

echo '</td>
      </tr>
      </table></body>
      </html>';

unset($m16_language);
?>