<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/* NSN News                                             */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright  2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: ../../index.php");

if (is_file("modules/News/includes/functions.php")) {include_once("modules/News/includes/functions.php");}
$module_name = "News";
get_lang($module_name);
$neconfig = ne_get_configs();
$index = 1;
$categories = 1;
automated_news();
if ($catid == 0 OR $catid == "") { redirect_url("modules.php?name=$module_name"); }

switch ($op) {

    default:
    case "newindex":
	$storyhome = $db->XMFnum( $storyhome );
	$cookie[3] = $db->XMFnum( $cookie[3] );

        if($neconfig['homenumber'] == 0) {
            if (isset($cookie[3])) { $storynum = $cookie[3]; } else { $storynum = $storyhome; }
        } else {
            $storynum = $neconfig['homenumber'];
        }

	$multilingual = $db->XMFbin( $multilingual );
	$currentlang  = $db->XMFlang( $currentlang );
        if (!isset($min)) { $min = 0; }
        if (!isset($max)) { $max = $min + $storynum; }

        if ($multilingual == 1) { $querylang = "AND (alanguage='$currentlang' OR alanguage='')"; } else { $querylang = ""; }
        incoh();
        if($neconfig['readmore'] == 1) {
         include_once ("kernel/popup.php");
        }
        $db->sql_query("UPDATE `".TABLE_STORIES_CAT."` SET `counter`=counter+1 WHERE `catid`='$catid'");
        $result = $db->sql_query("SELECT * FROM `".TABLE_STORIES."` WHERE `catid`='$catid' $querylang");
        $totalarticles = $db->sql_numrows($result);
        $result = $db->sql_query("SELECT * FROM `".TABLE_STORIES."` WHERE `catid`='$catid' $querylang ORDER BY `sid` DESC LIMIT $min, $storynum");
        if($neconfig['columns'] == 1) { // DUAL
            echo "<table border='0' cellpadding='0' cellspacing='0' width='100%'>\n";
        }
        $a = 0;
        while ($artinfo = $db->sql_fetchrow($result)) {
            formatTimestamp($artinfo['time']);
            $subject              = stripslashes(check_html($subject, "nohtml"));
            $artinfo['hometext']  = stripslashes($artinfo['hometext']);
            $artinfo['notes']     = stripslashes($artinfo['notes']);
            $artinfo['sid']       = $db->XMFnum($artinfo['sid']);
            $artinfo['aid']       = stripslashes($artinfo['aid']);
            $artinfo['title']     = stripslashes(check_html($artinfo['title'], "nohtml"));
            $artinfo['comments']  = $db->XMFnum($artinfo['comments']);
            $artinfo['counter']   = $db->XMFnum($artinfo['counter']);
            $artinfo['topic']     = $db->XMFnum($artinfo['topic']);
            $artinfo['informant'] = stripslashes($artinfo['informant']);
            $artinfo['notes']     = stripslashes($artinfo['notes']);
            $artinfo['acomm']     = $db->XMFnum($artinfo['acomm']);
            $artinfo['score']     = $db->XMFnum($artinfo['score']);
            $artinfo['ratings']   = $db->XMFnum($artinfo['ratings']);
            getTopics($artinfo['sid']);

            if($neconfig['texttype'] == 0) {
                $introcount = strlen($hometext);
                $fullcount  = strlen($bodytext);
            } else {
                $introcount = strlen(strip_tags($artinfo['hometext'], "<br />"));
                $fullcount  = strlen($artinfo['bodytext']);
            }

            $totalcount = $introcount + $fullcount;
            $r_options  = "";
            if (isset($cookie[4])) { $r_options .= "&amp;mode=$cookie[4]"; } else { $r_options .= "&amp;mode=thread"; }
            if (isset($cookie[5])) { $r_options .= "&amp;order=$cookie[5]"; } else { $r_options .= "&amp;order=0"; }
            if (isset($cookie[6])) { $r_options .= "&amp;thold=$cookie[6]"; } else { $r_options .= "&amp;thold=0"; }
            $the_icons = "";
            if (defined('USER_ALLOW')) {
                $the_icons .= " | <a href='modules.php?name=$module_name&amp;file=print&amp;sid=".$artinfo['sid']."'><img src='images/print.gif' border='0' alt='"._PRINTER."' title='"._PRINTER."' width='11' height='11' /></a>&nbsp;<a href='modules.php?name=$module_name&amp;file=friend&amp;op=FriendSend&amp;sid=".$artinfo['sid']."'><img src='images/friend.gif' border='0' alt='"._FRIEND."' title='"._FRIEND."' width='11' height='11' /></a>\n";
            }
            if (defined('ADMIN_ALLOW')) {
                $the_icons .= " | <a href=\"".$admin_file.".php?op=EditStory&amp;sid=".$artinfo['sid']."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\" title=\""._EDIT."\" width=\"11\" height=\"11\" /></a>&nbsp;<a href=\"".$admin_file.".php?op=RemoveStory&amp;sid=".$artinfo['sid']."\"><img src=\"images/delete.gif\" border=\"0\" alt=\""._DELETE."\" title=\""._DELETE."\" width=\"11\" height=\"11\" /></a>\n";
            }
            $read_link = "<a href='modules.php?name=$module_name&amp;file=read_article&amp;sid=".$artinfo['sid']."$r_options' onclick=\"NewsReadWindow(this.href,'ReadArticle','600','400','yes');return false;\">";
            $story_link = "<a href='modules.php?name=$module_name&amp;file=article&amp;sid=".$artinfo['sid']."$r_options'>";
            $morelink = "(";

            if($neconfig['texttype'] == 0) {
                if ($fullcount > 0 OR $artinfo['comments'] > 0 OR $articlecomm == 0 OR $artinfo['acomm'] == 1) {
                    if($neconfig['readmore'] == 1) {
                        $morelink .= "$read_link<b>"._READMORE."</b></a> | ";
                    } else {
                        $morelink .= "$story_link<b>"._READMORE."</b></a> | ";
                    }
                } else { $morelink .= ""; }
            } else {
                if ($introcount > 255 OR $fullcount > 0 OR $artinfo['comments'] > 0 OR $articlecomm == 0 OR $artinfo['acomm'] == 1) {
                    if($neconfig['readmore'] == 1) {
                        $morelink .= "$read_link<b>"._READMORE."</b></a> | ";
                    } else {
                        $morelink .= "$story_link<b>"._READMORE."</b></a> | ";
                    }
                } else { $morelink .= ""; }
                if ($introcount > 255) {
                    $artinfo['hometext'] = strip_tags($artinfo['hometext'], "<br />");
                    $artinfo['hometext'] = substr($artinfo['hometext'], 0, 255);
                }
            }

            if ($fullcount > 0) { $morelink .= "$totalcount "._BYTESMORE." | "; }
            if ($articlecomm == 1 AND $artinfo['acomm'] == 0) {
                if ($artinfo['comments'] == 0) {
                    $morelink .= "$story_link"._COMMENTSQ."</a>";
                } elseif ($artinfo['comments'] == 1) {
                    $morelink .= "$story_link".$artinfo['comments']." "._COMMENT."</a>";
                } elseif ($artinfo['comments'] > 1) {
                    $morelink .= "$story_link".$artinfo['comments']." "._COMMENTS."</a>";
                }
            }
            $morelink .= "$the_icons";
            $sid = $artinfo['sid'];
            if ($artinfo['catid'] != 0) {
                $result3 = $db->sql_query("SELECT title FROM ".TABLE_STORIES_CAT." WHERE catid='".$artinfo['catid']."'");
                $catinfo = $db->sql_fetchrow($result3);
                $morelink .= " | <a href='modules.php?name=$module_name&amp;file=categories&amp;op=newindex&amp;catid=".$artinfo['catid']."'>".$catinfo['title']."</a>";
            }
            if ($artinfo['score'] != 0) {
                $rated = substr($artinfo['score'] / $artinfo['ratings'], 0, 4);
            } else { $rated = 0; }
            $morelink .= " | "._SCORE." $rated";
            $morelink .= ")";
            $morelink = str_replace(" |  | ", " | ", $morelink);
            if($neconfig['columns'] == 1) { // DUAL
                if ($a == 0) { echo "<tr>"; }
                echo "<td valign='top' width='50%'>";
                themeindex($artinfo['aid'], $artinfo['informant'], $datetime, $artinfo['title'], $artinfo['counter'], $artinfo['topic'], $artinfo['hometext'], $artinfo['notes'], $morelink, $topicname, $topicimage, $topictext);
                echo "</td>\n";
                $a++;
                if ($a == 2) { echo "</tr>"; $a = 0; } else { echo "<td>&nbsp;</td>"; }
            } else { // SINGLE
                themeindex($artinfo['aid'], $artinfo['informant'], $datetime, $artinfo['title'], $artinfo['counter'], $artinfo['topic'], $artinfo['hometext'], $artinfo['notes'], $morelink, $topicname, $topicimage, $topictext);
            }
        }
        if($neconfig['columns'] == 1) { // DUAL
            if ($a ==1) { echo "<td width='50%'>&nbsp;</td></tr>\n"; } else { echo "</tr>\n"; }
            echo "</table>\n";
        }

	// Paging
        $articlepagesint = ($totalarticles / $storynum);
        $articlepageremain = ($totalarticles % $storynum);
        if ($articlepageremain != 0) {
            $articlepages = ceil($articlepagesint);
            if ($totalarticles < $storynum) { $articlepageremain = 0; }
        } else {
            $articlepages = $articlepagesint;
        }
        if ($articlepages!=1 && $articlepages!=0) {
            br('1');
            Max_OpenTable();
            $counter = 1;
            $currentpage = ($max / $storynum);
            echo "<form action='modules.php?name=$module_name' method='post'>\n";
            echo "<table align='center' border='0' cellpadding='2' cellspacing='2'>\n";
            echo "<tr>\n<td><b>"._NE_SELECT." </b><select name='min' onChange='top.location.href=this.options[this.selectedIndex].value'>\n";
            while ($counter <= $articlepages ) {
                $cpage = $counter;
                $mintemp = ($storynum * $counter) - $storynum;
                if ($counter == $currentpage) {
                    echo "<option selected>$counter</option>\n";
                } else {
                    echo "<option value='modules.php?name=$module_name&amp;min=$mintemp&amp;file=categories&amp;catid=$catid'>$counter</option>\n";
                }
                $counter++;
            }
            echo "</select><b> "._NE_OF." $articlepages "._NE_PAGES.".</b></td>\n</tr>\n";
            echo "</table>\n";
            echo "</form>\n";
            Max_CloseTable();
        }
	// Close page
        incof();
    break;

}