<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
// Based on:								     //
// myPHPNUKE Web Portal System - http://myphpnuke.com/	  		     //
// PHP-NUKE Web Portal System - http://phpnuke.org/	  		     //
// Thatware - http://thatware.org/					     //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //

include("header.php");
if ( $xoopsConfig['startpage'] == $xoopsModule->dirname() ) {

	$xoopsOption['show_rblock'] =1;
	include(XOOPS_ROOT_PATH."/header.php");
	if ( empty($HTTP_GET_VARS['start']) ) {
		make_cblock();
		echo "<br />";
	}
} else {
	$xoopsOption['show_rblock'] =0;
	include(XOOPS_ROOT_PATH."/header.php");
}
include(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/cache/config.php");
if ( file_exists(XOOPS_ROOT_PATH."/themes/".$xoopsTheme['thename']."/themenews.php") ) {
	include(XOOPS_ROOT_PATH."/themes/".$xoopsTheme['thename']."/themenews.php");
} else {
	die("No Theme files!");
}
include_once("class/class.newsstory.php");

if ( isset($HTTP_GET_VARS['storytopic']) ) {
	if ( ereg("^[0-9]{1,}$",$HTTP_GET_VARS['storytopic']) ) {
		$xoopsOption['storytopic'] = intval($HTTP_GET_VARS['storytopic']);
	} else {
		$xoopsOption['storytopic'] = 0;
	}
} else {
	$xoopsOption['storytopic'] = 0;
}
if ( isset($HTTP_GET_VARS['storynum']) ) {
	if (ereg("^[0-3][0-5]$",$HTTP_GET_VARS['storynum'])) {
		$xoopsOption['storynum'] = intval($HTTP_GET_VARS['storynum']);
	} else {
		$xoopsOption['storynum'] =$xoops_newsConfig['storyhome'];
	}
} else {
	$xoopsOption['storynum'] =$xoops_newsConfig['storyhome'];
}
if ( isset($HTTP_GET_VARS['start']) ) {
	$start = intval($HTTP_GET_VARS['start']);
} else {
	$start = 0;
}
if ( $xoops_newsConfig['displaynav'] == 1 ){
	echo "<br />";
	echo "<form name='form1' action='".$PHP_SELF."' method='get'><div>\n";
	$xt = new XoopsTopic($xoopsDB->prefix("topics"));
	$xt->makeTopicSelBox(1, $xoopsOption['storytopic'],"storytopic");
	echo "&nbsp;<select name='storynum'>\n";
	
	for ( $i = 5; $i <= 30; $i = $i + 5 ) {
		$sel = "";
		if ( $i == $xoopsOption['storynum'] ) {
			$sel = " selected='selected'";
		}
		echo "<option value='$i'$sel>$i</option>\n";
	}
	echo "</select>\n";
	echo "<input type='submit' value='"._NW_GO."' />\n";
	echo "</div></form>\n";
}

$sarray = NewsStory::getAllPublished($xoopsOption['storynum'], $start, $xoopsOption['storytopic']);

$storycount = NewsStory::countByTopic($xoopsOption['storytopic']);
foreach ( $sarray as $story ) {
	if ( $story->uname() != false ) {
		$poster = "<a href='".XOOPS_URL."/userinfo.php?uid=".$story->uid()."'>".$story->uname()."</a>";
	} else {
		$poster = $xoopsConfig['anonymous'];
	}
	$created = formatTimestamp($story->created());
	$hometext = $story->hometext();
	$introcount = strlen($hometext);
	$fullcount = strlen($story->bodytext());
	$totalcount = $introcount + $fullcount;
	$morelink = "";
	if ( $fullcount > 1 ) {
            	$morelink .= "<a href='".XOOPS_URL."/modules/".$xoopsModule->dirname()."/article.php?storyid=".$story->storyid()."";
 
            	$morelink .= "'><b>"._NW_READMORE."</b></a> | ";
		$morelink .= sprintf(_NW_BYTESMORE,$totalcount);
		$morelink .= " | "; 
	}
        $count = $story->getCommentsCount();
        $morelink .= "<a href='".XOOPS_URL."/modules/".$xoopsModule->dirname()."/article.php?storyid=".$story->storyid()."";

        $morelink2 = "<a href='".XOOPS_URL."/modules/".$xoopsModule->dirname()."/article.php?storyid=".$story->storyid()."";

        if ( $count == 0 ) {
                $morelink .= "'>"._NW_COMMENTS."</a>";
        } else {
            	if ( $fullcount < 1 ) {
                	if ( $count == 1 ) {
                    		$morelink .= "'><b>"._NW_READMORE."</b></a> | $morelink2'>"._NW_ONECOMMENT."</a>";
                    	} else {
                    		$morelink .= "'><b>"._NW_READMORE."</b></a> | $morelink2'>";
				$morelink .= sprintf(_NW_NUMCOMMENTS,$count);
				$morelink .= "</a>";
                	}
            	} else {
                	if ( $count == 1 ) {
                    		$morelink .= "'>"._NW_ONECOMMENT."</a>";
                	} else {
                    		$morelink .= "'>";
				$morelink .= sprintf(_NW_NUMCOMMENTS,$count);
				$morelink .= "</a>";
                	}
            	}
        }
	$thetext = $hometext."\n";
	$storytopic = $story->topic();
	$adminlink = "&nbsp;";
	if ( $xoopsUser ) {
		if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {					$adminlink = $story->adminlink();
		}
	}
	$imglink = "";
	if ( $story->topicdisplay() ) {
		$imglink = $story->imglink();
	}
	$title = $story->textlink()."&nbsp;:&nbsp;".$story->title();
// formatting story ends here
// now, display the story
        themenews($poster, $created, $title, $story->counter(), $thetext, $imglink, $adminlink, $morelink);
}
echo "<div>\n";
echo "<table border='0' width='90%'>\n";
echo "<tr><td align='left'>\n";
// check whether to display previous page link
if ( $start != 0 ) {
	if  ($start - $xoopsOption['storynum'] >0 ) {
		$prevstart = $start - $xoopsOption['storynum'];
	} else {
		$prevstart = 0;
	}
	echo "<b><a href='index.php?start=".$prevstart."&amp;storytopic=".$xoopsOption['storytopic']."&amp;storynum=".$xoopsOption['storynum']."'>"._NW_PREVPAGE."</a></b>\n";
}
echo "</td><td align='right'>\n";

if ( $start + $xoopsOption['storynum'] < $storycount ) {
	$nextstart = $start + $xoopsOption['storynum'];
	
	// check whether to display next page or return to top link
	echo "<b><a href='index.php?start=".$nextstart."&amp;storytopic=".$xoopsOption['storytopic']."&amp;storynum=".$xoopsOption['storynum']."'>"._NW_NEXTPAGE."</a></b>\n";
} elseif ( $start != 0 ) {
	echo "<b><a href='index.php?start=0&amp;storytopic=".$xoopsOption['storytopic']."&amp;storynum=".$xoopsOption['storynum']."'>"._NW_RETURNTOP."</a></b>\n";
}
echo "</td></tr></table>\n";
echo "</div>\n";

include_once(XOOPS_ROOT_PATH."/footer.php");

?>