<?php
/**
 * This file is part of
 * pragmaMx - Web Content Management System.
 * Copyright by pragmaMx Developer Team - http://www.pragmamx.org
 *
 * pragmaMx 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 3 of the License, or
 * (at your option) any later version.
 *
 * $Revision: 1.10.2.2 $
 * $Author: tora60 $
 * $Date: 2008/01/23 12:02:06 $
 */

defined('mxMainFileLoaded') or die('access denied');

/**
 * -----------------------------------------------------------------------------
 * Einstellungen fuer den Block
 */
// die ersten Teile des Artikeltextes anzeigen,
// - 0 fuer nein, oder
// - eine beliebige Zahl, die die Menge der angezeigten Zeichen definiert
// HTML-Tags, ausser <br> werden entfernt und nicht mitgerechnet
$showintro = 100;
// Bilder im Text anzeigen, wenn $showintro angeschaltet ist
$showpics = true;
// Buttonleiste anzeigen
$showbuttons = 1;
// Veroeffentlichungsdatum anzeigen
$showdate = 1;
// Kategorie zusammen mit der Artikelueberschrift anzeigen
$showcattitle = 0;
// Anzahl der Spalten
$showcolumns = 2;
// Anzahl der angezeigten Artikel
// - eine beliebige Zahl, oder
// - die globale Variable $GLOBALS['storyhome']
$storynum = $GLOBALS['storyhome'];
// Links zu Suche etc. anzeigen
$showextendedlinks = 0;
// Blockcache erlauben
$mxblockcache = true;

/**
 * ENDE Einstellungen fuer den Block
 * -----------------------------------------------------------------------------
 */

global $prefix, $bgcolor1, $bgcolor2, $bgcolor3;

if ($GLOBALS["multilingual"] == 1) {
    if (ereg("german", $GLOBALS["currentlang"])) $thislang = "german";
    else $thislang = $GLOBALS["currentlang"];
    $querylang = "AND (alanguage LIKE '" . $thislang . "%' OR alanguage='')";
} else {
    $querylang = "";
}

$qry = "SELECT s.sid, cat.catid, aid, s.title, time, DATE_ADD(time, INTERVAL 2 DAY) AS expire, now() AS jetzt, hometext, bodytext, comments, s.counter, topic, informant, notes, acomm, cat.title AS title1
        FROM ${prefix}_stories AS s LEFT JOIN ${prefix}_stories_cat AS cat ON s.catid = cat.catid
        WHERE (ihome='0' OR s.catid='0') AND time <= now() $querylang
        ORDER BY s.time DESC, s.sid DESC
        limit 0,$storynum";

$result = sql_query($qry);
if (!$result) {
    return;
}

$count = 0;
$out = '';
$width = ceil(100 / $showcolumns);
while ($story = sql_fetch_assoc($result)) {
    // Textlaengen
    // $introcount = strlen(strip_tags($story['hometext']));
    // $fullcount = strlen(strip_tags($story['bodytext']));
    $totalcount = mxValueToString(strlen(strip_tags($story['hometext'])) + strlen(strip_tags($story['bodytext'])), 0);

    $introtext = '';
    if ($showintro) {
        if ($showpics) {
            // Alle Tags ausser <br> & <img> entfernen
            $introtext = trim(strip_tags($story['hometext'], '<br><img>'));
            // <br> am Textbeginn entfernen
            $introtext = preg_replace('#^(?:<br\s*/*>\s*)*#is', '', $introtext);
            // array zuruecksetzen
            $replaces = array();
            $textlentemp = $showintro;
            // alle Imagetags suchen und zwischenspeichern
            if (preg_match_all('#<img[^>]*>#si', $introtext, $images)) {
                foreach($images[0] as $i => $img) {
                    // einen alternativen String zum Ersetzen erstellen
                    $alternate = md5($img);
                    // die gewuenschte Textlaenge um die Textlaenge des alternativen String erweitern
                    $textlentemp = $showintro + strlen($alternate);
                    // den gefundenen Imagetag in das array stellen
                    $replaces[$alternate] = $img;
                    // den gefundenen Imagetag aus dem Text entfernen und dafuer den alternativen String einsetzen
                    $introtext = trim(str_replace($img, $alternate, $introtext));
                }
            }
            // Text auf die gewuenschte Laenge kuerzen
            $introtext = mxCutString($introtext, $textlentemp, "&nbsp;...", " ");
            // wenn imagetags gefunden wurden, die alternativen Textteile wieder
            // durch die im Array zwischengespeicherten Imagetags ersetzen
            if (count($replaces)) {
                $introtext = str_replace(array_keys($replaces), array_values($replaces), $introtext);
            }
        } else {
            // Alle Tags ausser <br> entfernen
            $introtext = trim(strip_tags($story['hometext'], '<br>'));
            // <br> am Textbeginn entfernen
            $introtext = preg_replace('#^(?:<br\s*/*>\s*)*#is', '', $introtext);
            // Text auf die gewuenschte Laenge kuerzen
            $introtext = trim(mxCutString($introtext, $showintro, "&nbsp;...", " "));
        }
        if ($introtext) {
            $introtext = '<div class="content">' . $introtext . '</div>';
        }
    }
    // das formatierte Datum
    $datetime = '';
    if ($showdate) {
        $datetime = '<td width="96%">' . mx_strftime(_SHORTDATESTRING, mxSqlDate2UnixTime($story['time'])) . '</td>';
    }
    // die Buttons
    $buttons = array();
    if ($showbuttons) {
        $buttons[] = '<a href="modules.php?name=News&amp;file=print&amp;sid=' . $story['sid'] . '" rel="nofollow"><img src="images/menu/print.gif" width="12" height="13" border="0" alt="' . _BMXPRINTER . '"  title="' . _BMXPRINTER . '"  /></a>';
        if (mxModuleAllowed('Recommend_Us')) {
            $buttons[] = '<a href="modules.php?name=News&amp;file=friend&amp;sid=' . $story['sid'] . '" rel="nofollow"><img src="images/menu/friend.gif" width="12" height="13" border="0" alt="' . _BMXFRIEND . '" title="' . _BMXFRIEND . '"  /></a>';
        }
        $buttons[] = '<a href="modules.php?name=News&amp;file=article&amp;sid=' . $story['sid'] . '"><img src="images/menu/bytesmore.gif" border="0" width="12" height="13" alt="' . _BMXBYTESMORE . ' ' . $totalcount . '" title="' . _BMXBYTESMORE . ' ' . $totalcount . '"  /></a>';
        if ($GLOBALS['articlecomm']) {
            $buttons[] = '<a href="modules.php?name=News&amp;file=article&amp;sid=' . $story['sid'] . '#comments"><img src="images/menu/comments2.gif" width="12" height="13" border="0"  alt="' . _COMMENTS . ' ' . mxValueToString($story['comments'], 0) . '"  title="' . _COMMENTS . ' ' . mxValueToString($story['comments'], 0) . '" ></a>';
        }
        if (mxModuleAllowed('Userinfo')) {
            $buttons[] = '<a href="modules.php?name=Userinfo&amp;uname=' . urlencode($story['informant']) . '"><img src="images/menu/informant.gif" width="12" height="13" border="0" alt="' . $story['informant'] . '" title="' . _UNICKNAME . ': ' . $story['informant'] . '" ></a>';
        }
        $buttons[] = '<img src="images/menu/counter.gif" width="12" height="13" border="0" alt="' . mxValueToString($story['counter'], 0) . ' ' . _READS . '" title="' . mxValueToString($story['counter'], 0) . ' ' . _READS . '" >';
    }
    $colspan = count($buttons) + 1;
    // $buttons = '<table border="0" cellspacing="1" cellpadding="1" align="right"><tr><td>'.implode('</td><td>', $button).'</td></tr></table>';
    $buttons = '<td>' . implode('</td><td>', $buttons) . '</td>';
    // der Titel des Artikels
    $story['title'] = '<a href="modules.php?name=News&amp;file=article&amp;sid=' . $story['sid'] . '">' . $story['title'] . '</a>';
    // den Kategoriename mit anzeigen
    if ($showcattitle && $story['catid']) {
        $story['title'] = $story['title'] . '&nbsp; [<a href="modules.php?name=News&amp;file=categories&amp;op=newindex&catid=' . $story['catid'] . '">' . $story['title1'] . '</a>]';
    }
    // $image = ($story['jetzt'] > $story['expire']) ? 'sign' : 'new';
    // $out .= '<td colspan="'.$colspan.'"><img src="images/menu/' . $image . '.gif" align="middle" alt="" />&nbsp;'.$story['title'].'</td>';
    if ($count == 0) {
        $out .= '<tr valign="top">';
    }
    $out .= '
    <td width="' . $width . '%" valign="top" style="border: 1px solid ' . $bgcolor2 . ';">
        <table border="0" cellspacing="0" cellpadding="2" width="100%">';
    if ($datetime || $buttons) {
        $out .= '
            <tr valign="top" style="height: 10px;">
                ' . $datetime . $buttons . '
            </tr>
            ';
    }
    $image = '';
    if ($story['jetzt'] < $story['expire']) {
        $datetime = mx_strftime(_SHORTDATESTRING, mxSqlDate2UnixTime($story['time']));
        $image = mxCreateImage("images/menu/new.gif", 'neu: ' . $datetime) . "&nbsp;";
    }
    if ($story['jetzt'] > $story['expire']) {
        $datetime = mx_strftime(_SHORTDATESTRING, mxSqlDate2UnixTime($story['time']));
        $image = mxCreateImage("images/menu/deinbild.gif", ' ' . $datetime) . "&nbsp;";
    }
    $out .= '
            <tr valign="top" style="height: 35px;">
                <td colspan="' . $colspan . '">' . $image . '<b>' . $story['title'] . '</b><br /><br />' . $introtext . '</td>
            </tr>
        </table>
    </td>
    ';

    $count++;
    if ($count * $width == 100) {
        $out .= '</tr>';
        $count = 0;
    }
}
// falls nicht alle Tabellenspalten am Ende gefuellt sind, eine zus. anfuegen
if ($count && $out) {
    $out .= '<td colspan="' . ($showcolumns - $count) . '">&nbsp;</td>';
}
// die Links zu anderen Modulen
if ($showextendedlinks) {
    if (mxModuleAllowed('Stories_Archive')) {
        $links[] = '<a href="modules.php?name=Stories_Archive">' . _STORIEARCHIVE . '</a>';
    }
    if (mxModuleAllowed('Submit_News')) {
        $links[] = '<a href="modules.php?name=Submit_News">' . _SUBMITNEWS . '</a>';
    }
    if (mxModuleAllowed('Search')) {
        $links[] = '<a href="modules.php?name=Search">' . _SEARCH . '</a>';
    }
    if (isset($links)) {
        $out .= '<tr><td align="center" colspan="' . $showcolumns . '" style="border: 1px solid ' . $bgcolor2 . ';">';
        $out .= '[&nbsp;' . implode('&nbsp;|&nbsp;', $links) . '&nbsp;]';
        $out .= '</td></tr>';
    }
}
// content-tabelle, nur wenn ueberhaupt was zum anzeigen da ist...
if ($out) {
    $content = '<table width="100%" border="0" cellspacing="2" cellpadding="5">' . $out . '</table>';
}

?>
