<?php

include_once(dirname(__FILE__).'/../../Application/Core.php');
$System = new Core();
$System->ShowPage = false;
$System->Run();

$SourceURL = 'http://www.dkvsetin.cz/rss/kino/8';
$StartText = '<item>';
$TitleSeparator = '<title>';
$DescriptionSeparator = '<description>';
$StartLink = '<link>';
$Category = 15;
$Author = 'Automat';

$doc = new DOMDocument();
$doc->load($SourceURL);
foreach ($doc->getElementsByTagName('item') as $node)
{
  $Title = $node->getElementsByTagName('title')->item(0)->nodeValue;
  $Description = $node->getElementsByTagName('description')->item(0)->nodeValue;
  $Link = $node->getElementsByTagName('link')->item(0)->nodeValue;
  $Date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;

  $Description = strip_tags($Description);
  $Description = str_replace("\r", '', $Description);
  $Description = str_replace("\n", '<br>', $Description);
  //if (($CommaPos = strpos($Date, ',')) !== FALSE)
  //  $Date = substr($Date, $CommaPos + 1);
  $Date = TimeToMysqlDateTime(strtotime($Date));

  $Query = 'SELECT Id FROM News WHERE (`Title`="'.$System->Database->real_escape_string($Title).'") AND (`Category`='.$Category.') AND (`Content` = "'.$System->Database->real_escape_string($Description).'") AND (`Link` = "'.$System->Database->real_escape_string($Link).'")';
  $DbResult = $System->Database->query($Query);
  if ($DbResult->num_rows == 0)
  {
    $System->Database->insert('News', array('Title' => $Title, 'Date' => $Date, 'Author' => $Author, 'Category' => $Category, 'Content' => $Description, 'Link' => $Link));
    echo($System->Database->insert_id.',');
  }
}
