<?php
require_once('core/TpConfig.class.php');
require_once('core/TpDb.class.php');
require_once('core/TpHelper.class.php');
require_once('core/TpTemplate.class.php');

require_once('classes/TpNewsItem.class.php');

error_reporting(E_ALL);
if (! isset($_SESSION)) {
    session_start();
}
$config = TpConfig::GetInstance();
error_reporting($config->Get()->global->error_reporting);
date_default_timezone_set($config->Get()->global->timezone);
$db = TpDb::GetInstance();
$template = new TpTemplate('News.rss');
$template->Set('TITLE', 'Breitband-Neuigkeiten',
        TpTemplate::CONVERSATION_TYPE_XML);
$template->Set('LINK', $config->Get()->global->application_url,
        TpTemplate::CONVERSATION_TYPE_XML);
$template->Set('DESCRIPTION', 'News zum Thema Breitband-Internet: ' .
        'DSL-Anbieter, VDSL-Anbieter, Kabelnetzbetreiber und LTE-Anbieter. ' .
        'Tarife, Vergleich und Sonderaktionen.',
        TpTemplate::CONVERSATION_TYPE_XML);
$sql = 'SELECT' .
        ' gnid,' .
        ' ueberschrift,' .
        ' teaser,' .
        ' DATE_FORMAT(datum, "%a, %d %b %Y %T") AS pub_date' .
        ' FROM' .
        ' news' .
        ' WHERE' .
        ' datum <= NOW()' .
        ' AND' .
        ' geloeschtam IS NULL' .
        ' ORDER BY' .
        ' news.datum DESC';
$stmt = $db->Query($sql);
$news_items = array();
while ($row = $stmt->fetch(Zend_Db::FETCH_ASSOC)) {
    $news_items[] = $row;
}
$items = '';
foreach ($news_items as $news_item) {
    $item = new TpTemplate('NewsItem.rss');
    $item->Set('TITLE', $news_item['ueberschrift'],
            TpTemplate::CONVERSATION_TYPE_UTF8);
    $item->Set('DESCRIPTION', $news_item['teaser'] . ' [<a href="{LINK}" ' .
            'title="' . $news_item['ueberschrift'] . '">Weiterlesen</a>]',
            TpTemplate::CONVERSATION_TYPE_XML);
    $item->Set('LINK', $config->Get()->global->application_url . 'news/' .
            TpNewsItem::BuildDirName($news_item) . '/',
            TpTemplate::CONVERSATION_TYPE_XML);
    $item->Set('PUB_DATE', $news_item['pub_date'] . ' ' . date('O'),
            TpTemplate::CONVERSATION_TYPE_XML);
    $items .= $item->GetIt();
}
$template->Set('ITEMS', $items);
header('Content-Type: application/rss+xml; charset: UTF-8');
echo $template->GetIt();
