<?php

Header('Content-Type: text/xml');

include_once('../global.php');
include_once('rss_generator.php');

$NewsCount = 15;

$Items = array();
$Category = '';
$CategoryOption = '';
$CategoryOptionURL = '';
$CategoryName = '';

// Prepare WHERE condition 
if(array_key_exists('select', $_GET)) 
{
  $Where = '';
  $Parts = explode('-', $_GET['select']);
  foreach($Parts as $Part)
  {
    $Where .= 'OR (category='.($Part * 1).')'; 
  }
  $Where = substr($Where, 2);
} else $Where = 1;

// Get category names
$Categories = array();
$DbResult = $Database->select('NewsCategory', '*');
while($Category = $DbResult->fetch_array())
{
  $Categories[$Category['Id']] = $Category['Caption'];
}

// Update news from discussion forum
/*
$ForumCategory = 4;   
$Database->select_db('forum');
$DbResult = $Database->query('SELECT posts.post_time, posts_text.post_subject, posts_text.post_text, users.username, topics.topic_title FROM posts JOIN posts_text ON posts.post_id = posts_text.post_id JOIN users ON users.user_id = posts.poster_id JOIN topics ON topics.topic_id= posts.topic_id ORDER BY post_time DESC LIMIT '.$NewsCount);
$Index = 0;
//echo(DB_NumRows().',');
while($Row = $DbResult->fetch_array())
{
  $Row['post_text'] = StrTr($Row['post_text'], "\x8A\x8D\x8E\x9A\x9D\x9E", "\xA9\xAB\xAE\xB9\xBB\xBE"); 
  $Row['post_text'] = str_replace("\n","<br>", $Row['post_text']); 
  $Row['post_subject'] = StrTr($Row['post_subject'], "\x8A\x8D\x8E\x9A\x9D\x9E", "\xA9\xAB\xAE\xB9\xBB\xBE"); 
  $Row['topic_title'] = StrTr($Row['topic_title'], "\x8A\x8D\x8E\x9A\x9D\x9E", "\xA9\xAB\xAE\xB9\xBB\xBE"); 
  $Index = $Index + 1;    
  
  $Title = $Row['topic_title'].'-'.$Row['post_subject'];
  $Content = $Row['post_text'];
  $Date = date('Y-m-d H:i:s', $Row['post_time']);
  $Author = $Row['username'];
  $Database->select_db('is');
  //echo('category='.$ForumCategory.' AND title="'.addslashes($Title).'" AND content="'.addslashes($Content).'" AND author="'.addslashes($Author).'" AND date="'.$Date.'"');
  $DbResult2 = $Database->select('news', '*', 'category='.$ForumCategory.' AND title="'.addslashes($Title).'" AND content="'.addslashes($Content).'" AND author="'.addslashes($Author).'" AND date="'.$Date.'"');
  if($DbResult2->num_rows == 0) //echo('.'); else echo('x'); 
    $Database->insert('news', array('category' => $ForumCategory, 'title' => $Title, 'content' => $Content, 'author' => $Author, 'date' => $Date));
    //echo($Date);  
  $Database->select_db('forum');
}
$Database->select_db('is');
*/

// Get news from database by selected categories
$UploadedFilesFolder = 'uploads/';
$DbResult = $Database->query('SELECT *, UNIX_TIMESTAMP(Date) FROM News LEFT JOIN `User` ON `User`.`Id`=`News`.`User` WHERE '.$Where.' ORDER BY News.Date DESC LIMIT 0,'.$NewsCount);
while($Row = $DbResult->fetch_assoc())
{
  $EnclosuresText = '';
  if($Row['Enclosure'] != '')
  { 
    $EnclosuresText .= '<br />Přílohy: ';
    $Enclosures = explode(';', $Row['Enclosure']);
    foreach($Enclosures as $Enclosure)
    {
      if(file_exists($UploadedFilesFolder.$Enclosure)) $EnclosuresText .= ' <a href="http://centrala.zdechov.net/aktuality/'.$UploadedFilesFolder.$Enclosure.'">'.$Enclosure.'</a>';
    }
  }
  if($Row['Name'] == '') $Author = $Row['Author'];
    else $Author = $Row['Name'];
  $Items[] = array(
    'Title' => $Categories[$Row['Category']].' - '.$Row['Title'],
    'Link' => 'http://centrala.zdechov.net/aktuality/index.php?category='.$Row['Category'],
    'Description' => $Row['Content'].' ('.$Author.')'.$EnclosuresText,
    'Time' => $Row['UNIX_TIMESTAMP(Date)'],
  );
}

echo(GenerateRSS(array(
  'Title' => $Config['Web']['Title'].' - Aktuality',
  'Link' => 'http://'.$Config['Web']['Host'].'/',
  'Description' => 'Aktuality komunitní počítačové sítě ZděchovNET',
  'WebmasterEmail' => $Config['Web']['AdminEmail'],
  'Items' => $Items)));
  
?>
