<?php
/**
 * pragmaMx  Content Management System
 * Copyright (c) 2007 pragmaMx Dev Team - http://pragmaMx.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.
 * 
 * $Source: /home/www/dps3311/home/cvsroot/pragmamx/pragmamx_01/modules/News/index.php,v $
 * $Revision: 1.11 $
 * $Author: tora60 $
 * $Date: 2007/03/24 12:38:47 $
 */

if (!defined("mxMainFileLoaded")) die ("You can't access this file directly...");
// rechte Blöcke an oder aus
$index = 1;

$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);
include_once("includes/mxNewsFunctions.php");
// ###################################
function theindex($new_topic = 0)
{
    global $prefix, $pagetitle;
    if (isset($_GET['pagenum'])) {
        $pagenum = intval($_GET['pagenum']);
    } 
    if (empty($pagenum)) {
        $pagenum = 1;
    } 
    $new_topic = intval($new_topic);
    $pagecols = (empty($GLOBALS["storyhome_cols"])) ? 1 : intval($GLOBALS["storyhome_cols"]);
    if ($GLOBALS["multilingual"] == 1) {
        if (ereg("german", $GLOBALS["currentlang"])) $thislang = "german";
        else $thislang = $GLOBALS["currentlang"];
        $where[] = "(s.alanguage LIKE '" . $thislang . "%' OR s.alanguage='')";
    } 
    $where[] = (empty($new_topic)) ? "(s.ihome=0 OR s.catid=0)" : "topic='" . $new_topic . "'";
    $where[] = "(s.time <= now())";
    $where = implode(' AND ', $where);
    $qry = "SELECT Count(sid) FROM ${prefix}_stories AS s WHERE " . $where;
    $result = sql_query($qry);
    list($allstories) = sql_fetch_row($result);
    if (MX_IS_USER) {
        $userinfo = mxGetUserData();
        $storynum = (empty($userinfo['storynum'])) ? $GLOBALS["storyhome"] : $userinfo['storynum'];
    } else {
        $storynum = $GLOBALS["storyhome"];
    } 
    $storynum = (empty($storynum) || $storynum > 50 || $storynum < 1) ? 10 : intval($storynum);
    $storynum = ceil($storynum / $pagecols) * $pagecols;
    $colwidth = ceil(100 / $pagecols);
    $numpages = ceil($allstories / $storynum);
    $pagenum = intval($pagenum);
    $pagenum = ($pagenum < 1) ? 1 : $pagenum;
    $pagenum = ($pagenum > $numpages) ? $numpages : $pagenum;
    $offset = ($pagenum-1) * $storynum ;
    $offset = ($offset < 0) ? 0 : $offset;

    $qry = "SELECT s.sid, s.informant, s.title, s.hometext, s.bodytext, s.comments, s.counter, s.notes, s.time, s.alanguage, s.acomm, s.aid, s.score, s.ratings, s.topic, t.topicname, t.topicimage, t.topictext, c.catid, c.title AS cattitle, a.url, a.email
			FROM ((${prefix}_stories AS s
			LEFT JOIN ${prefix}_stories_cat AS c ON s.catid = c.catid) 
			LEFT JOIN ${prefix}_topics AS t ON s.topic = t.topicid) 
			LEFT JOIN ${prefix}_authors AS a ON s.aid = a.aid
			WHERE " . $where . "
			ORDER BY s.time DESC, s.sid DESC 
			LIMIT " . $offset . "," . $storynum . ";";
    $result = sql_query($qry);

    $storiecount = 0;
    $topictext = '';
    while ($stories[$storiecount] = sql_fetch_assoc($result)) {
        if (!empty($new_topic) && $storiecount == 0) {
            $topictext = $stories[$storiecount]["topictext"];
        } 
        $storiecount++;
    } 

    if (empty($storiecount)) {
        $msg = "<br><br><b>" . _NOINFO4TOPIC . "</b><br><br>[&nbsp;<a href=\"modules.php?name=News\">" . _GOTONEWSINDEX . "</a> | <a href=\"modules.php?name=Topics\">" . _SELECTNEWTOPIC . "</a>&nbsp;]";
        mxMessageScreen($msg);
        die();
    } 

    $pagetitle = _GOTONEWSINDEX;
    if (!empty($new_topic) && $topictext) {
        $pagetitle .= ', ' . $topictext;
    } 
    if ($pagenum > 1) {
        $pagetitle .= ', ' . _NEWS_PAGE . ' ' . $pagenum;
    } 

    include("header.php");
    if (!empty($new_topic)) {
        OpenTable();
        echo "<center><font class=\"title\">" . $GLOBALS["sitename"] . ": " . $topictext . "</font><br><br>"
         . "<form action=\"modules.php?name=Search\" method=\"post\">"
         . _SEARCHONTOPIC . ": <input type=\"text\" name=\"query\" size=\"30\">&nbsp;&nbsp;"
         . "<input type=\"submit\" value=\"" . _SEARCH . "\">"
         . "<input type=\"hidden\" name=\"type\" value=\"stories\">"
         . "<input type=\"hidden\" name=\"topic\" value=\"$new_topic\">"
         . "</form><br>"
         . "[&nbsp;<a href=\"index.php\">" . _GOTOHOME . "</a> | <a href=\"modules.php?name=Topics\">" . _SELECTNEWTOPIC . "</a>&nbsp;]</center>";
        CloseTable();
        echo "<br>";
    } // End New Topic
    if (!empty($storiecount)) {
        $linkadd = (empty($new_topic)) ? "" : "&amp;new_topic=" . $new_topic;
        if (empty($GLOBALS["home"])) {
            vkpShowPageLinks($numpages, $pagenum, $storynum, $allstories, $linkadd);
        } 
        $i = 0;
        if ($pagecols > 1) {
            echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr valign=\"top\">";
        } 
        foreach($stories as $index => $story) {
            if (!is_array($story)) continue;
            $story = vkpGetStoryDetails($story);
            $i++;
            if ($pagecols > 1) {
                if ($i == 1) {
                    echo "<tr valign=\"top\">";
                } 
                echo "<td width=\"" . $colwidth . "%\">";
                $story['column'] = $i;
            } else {
                $story['column'] = 1;
            } 
            themeindex($story["aid"], $story["informant"], $story["datetime"], $story["title_formated"], $story["counter"], $story["topic"], $story["hometext"], $story["notes"], $story["morelink"], $story["topicname"], $story["topicimage"], $story["topictext"], $story);
            if ($pagecols > 1) {
                echo "</td>";
                if ($i == $pagecols) {
                    echo "</tr>";
                    $i = 0;
                } 
            } 
        } 
        if ($pagecols > 1) {
            if ($i) {
                $colspan = $pagecols - $i;
                echo "<td colspan=\"" . $colspan . "\">&nbsp;</td></tr>";
            } 
            echo "</table>";
        } 
        echo "<br>";
        vkpShowPageLinks($numpages, $pagenum, $storynum, $allstories, $linkadd);
    } 
    include("footer.php");
} 
// ###################################
function rate_article($sid, $score)
{
    global $prefix, $sitename;
    list($sid, $score) = mxForceInteger($sid, $score);
    if ($score) {
        if ($score > 5) {
            $score = 5;
        } 
        if ($score < 1) {
            $score = 1;
        } 
        if (isset($_COOKIE['ratecookie'])) {
            $rcookie = base64_decode($_COOKIE['ratecookie']);
            $r_cookie = explode(":", $rcookie);
        } 
        for ($i = 0; $i < sizeof($r_cookie); $i++) {
            if ($r_cookie[$i] == $sid) {
                $a = 1;
            } 
        } 
        if ($a == 1) {
            header("Location: modules.php?name=News&op=rate_complete&sid=$sid&rated=1");
        } else {
            $result = sql_query("update ${prefix}_stories set score=score+$score, ratings=ratings+1 where sid=" . intval($sid));
            $info = base64_encode("$rcookie$sid:");
            mxSetCookie("ratecookie", $info, time() + 3600);
            header("Location: modules.php?name=News&op=rate_complete&sid=$sid");
        } 
    } else {
        include("header.php");
        title("" . $GLOBALS["sitename"] . ": " . _ARTICLERATING);
        OpenTable();
        echo "<center>" . _DIDNTRATE . "<br><br>" . _GOBACK . "</center>";
        CloseTable();
        include("footer.php");
    } 
} 

if (empty($new_topic)) $new_topic = 0;
if (empty($op)) $op = "";

switch ($op) {
    case "rate_article":
        if (empty($score)) $score = 0;
        rate_article((int)$sid, (int)$score);
        break;

    case "rate_complete":
        if (!isset($sid)) $sid = 0;
        if (empty($rated)) {
            mxRedirect("modules.php?name=News&amp;file=article&amp;sid=" . $sid, _THANKSVOTEARTICLE, 1);
        } elseif ($rated == 1) {
            mxRedirect("modules.php?name=News&amp;file=article&amp;sid=" . $sid, _ALREADYVOTEDARTICLE, 3);
        } 
        break;

    default:
        theindex($new_topic);
        break;
} 

?>
