<?php

require_once './includes/php/config.php';
require_once './includes/php/functions.php';
require_once './includes/php/calendar.php';

@header('Content-Type: application/rss+xml; charset='.SITE_CHARSET);

echo '<?xml version="1.0" encoding="'.SITE_CHARSET.'"?>
<?xml-stylesheet href="/includes/css/rss.css" type="text/css"?>
<rss version="2.0" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
  <channel>
    <title>'.SITE_DOMAIN.' | Recent Updates</title>
    <link>http://'.SITE_DOMAIN.'/</link>
    <description>Not Coming to a Theater Near You assumes a bias towards older, often unpopular, and sometimes unknown films that merit a second look.</description>
    <lastBuildDate>'.gmdate("D, d M Y H:i:s \G\M\T").'</lastBuildDate>
    <image>
      <title>'.SITE_DOMAIN.' | Recent Updates</title>
      <url>http://'.SITE_DOMAIN.'/images/site-icon.png</url>
      <link>http://'.SITE_DOMAIN.'/</link>
    </image>
    <language>en-us</language>'."\n";


$rows = array();

$Q_2 = "SELECT DATE_FORMAT(sl.insert_date,'%Y/%m') AS yearmonth, sl.insert_date AS date, slt.title, sl.body_text AS body, sl.user AS author, sl.entryID AS screeninglog_id, url FROM screening_log AS sl INNER JOIN screening_log_title AS slt ON sl.screen_log_title_id = slt.screen_log_title_id WHERE sl.published = '1' ORDER BY sl.insert_date DESC LIMIT 20";
$R_2 = mysql_query($Q_2,$MYSQL_LINK);

while ($O_2 = mysql_fetch_object($R_2))
{
  $rows["screeninglogs_".$O_2->screeninglog_id] = array();
  $rows["screeninglogs_".$O_2->screeninglog_id]['date'] = $O_2->date;
  $rows["screeninglogs_".$O_2->screeninglog_id]['image_dir'] = "screeninglog";
  $rows["screeninglogs_".$O_2->screeninglog_id]['image_abbr'] = "";
  $rows["screeninglogs_".$O_2->screeninglog_id]['title'] = $O_2->title;
  $rows["screeninglogs_".$O_2->screeninglog_id]['body'] = $O_2->body;
  $rows["screeninglogs_".$O_2->screeninglog_id]['category'] = "Screening Log";
  $rows["screeninglogs_".$O_2->screeninglog_id]['author'] = $O_2->author;
  $rows["screeninglogs_".$O_2->screeninglog_id]['link'] = "screeninglog/{$O_2->url}/";
  $rows["screeninglogs_".$O_2->screeninglog_id]['type'] = "screeninglogs";
  $rows["screeninglogs_".$O_2->screeninglog_id]['id'] = $O_2->screeninglog_id;
}


$Q_3 = "SELECT * FROM excerpts WHERE active = '1' ORDER BY date DESC LIMIT 0,20";
$R_3 = mysql_query($Q_3,$MYSQL_LINK);

while ($O_3 = mysql_fetch_object($R_3))
{
  $reference = ($O_3->reference == "reviews" ? "review" : "feature")."_id";

  $Q_4 = "SELECT * FROM ".$O_3->reference." WHERE ".($reference)." = '".$O_3->reference_id."' AND active = '1' LIMIT 0,20";
  $R_4 = mysql_query($Q_4,$MYSQL_LINK);
  $O_4 = mysql_fetch_object($R_4);

  $rows[$O_3->reference."_".$O_3->reference_id] = array();
  $rows[$O_3->reference."_".$O_3->reference_id]['date'] = $O_4->date;
  $rows[$O_3->reference."_".$O_3->reference_id]['image_dir'] = $O_3->reference;
  $rows[$O_3->reference."_".$O_3->reference_id]['image_abbr'] = $O_4->image_abbr;
  $rows[$O_3->reference."_".$O_3->reference_id]['title'] = $O_4->title;
  $rows[$O_3->reference."_".$O_3->reference_id]['body'] = $O_3->body;
  $rows[$O_3->reference."_".$O_3->reference_id]['category'] = ucfirst($O_3->reference);
  $rows[$O_3->reference."_".$O_3->reference_id]['author'] = $O_4->author;
  $rows[$O_3->reference."_".$O_3->reference_id]['link'] = $O_3->reference."/".$O_4->abbr."/";
  $rows[$O_3->reference."_".$O_3->reference_id]['type'] = $O_3->reference;
  $rows[$O_3->reference."_".$O_3->reference_id]['id'] = $O_3->reference_id;
}

$reference_date = strtotime("Feb 3 2013");
$today = time();
$oneweek = 60 * 60 * 24 * 7;

$calendar = new calendar();
$cities = $calendar->GetCities();

$cityCount = 0;
while($row = mysql_fetch_array($cities))
{
  $cityName[$cityCount] = "{$row['City']}";
  $cityAbbr[$cityCount] = "{$row['Abbr']}";
  $cityCount++;
}

$cityString = "<a href=\"/calendar/$cityAbbr[0]\">{$cityName[0]}</a>";
            for($i = 1; $i < $cityCount; $i++)
            {
              if( $i == $cityCount - 1 && $i != 1 )
                $cityString .= ", and <a href=\"/calendar/$cityAbbr[$i]\">{$cityName[$i]}</a>";
              else if( $i == 1 )
                $cityString .= ", <a href=\"/calendar/$cityAbbr[$i]\">{$cityName[$i]}</a>";
              else
                $cityString .= ", <a href=\"/calendar/$cityAbbr[$i]\">{$cityName[$i]}</a>";
            }

$calendar_description_text = "This calendar is a new addition to NotComing.com, and is updated each Sunday with suggested screenings for the upcoming week. Currently, the calendar contains listings for $cityString. Others will be added in the future, and if you're interested in curating a calendar in your part of the world, <a href=\"/contact\">do get in touch</a>!";

if($today > $reference_date) {
	$num_weeks = floor(($today - $reference_date) / $oneweek);
	$new_date = $reference_date;

	if($num_weeks >= 1) {
		$new_date = $reference_date + ($num_weeks * $oneweek);
		$next_week_date = $new_date + $oneweek - (60 * 60 * 24);
	}

	$nice_date = gmdate("F d",$new_date);
	$nice_next_week_date = gmdate('F d, Y',$next_week_date);
	$pub_date = gmdate("D, d M Y H:i:s \G\M\T",$new_date);
	$url_date = gmdate("M_d_Y",$new_date);

  echo "<item>
      <guid>http://NotComing.com/calendar/index.php?start=$url_date&amp;rss_redirect</guid>
      <title>Recommended Cinema for $nice_date-$nice_next_week_date</title>
      <link>http://NotComing.com/calendar</link>
      <description>$calendar_description_text</description>
      <category>Calendar</category>
      <dc:creator>Not Coming to a Theater Near You</dc:creator>
      <pubDate>$pub_date</pubDate>
    </item>";
}

arsort($rows);

$int = 0;

foreach ($rows as $row)
{

  $int++;

  if ($int <= 20)
  {
    if ($row['image_abbr'])
    {
      $image = generate_image("images/".$row['image_dir']."/",$row['image_abbr'],"l");
    }

    $row['body'] = preg_replace('!href="/(.*)"!','href="http://notcoming.com/\1"',$row['body']);
    $row['body'] = preg_replace('!href="([^(http://)]+)(.*)"!','href="http://notcoming.com/\1\2"',$row['body']);
    $body = trim($row['body']);

    echo "<item>
      <guid>http://".SITE_DOMAIN."/".$row['link']."</guid>
      <title>".htmlspecialchars(trim($row['title']))."</title>
      <link>http://".SITE_DOMAIN."/".$row['link']."</link>
      <description>".htmlspecialchars(($row['image_abbr'] ? '<img src="http://'.SITE_DOMAIN.'/'.$image.'" alt="'.$row['title'].'" />' : "")). htmlspecialchars($body) ."</description>
      <category>".ucfirst($row['category'])."</category>
      <dc:creator>".ucfirst($row['author'])."</dc:creator>\n";

    // Display comments page
    $Q_5 = "SELECT COUNT(*) AS c FROM comments WHERE reference_id = '".$row['id']."' AND reference = '".$row['type']."' AND active = '1'";
    $R_5 = mysql_query($Q_5,$MYSQL_LINK);
    $O_5 = mysql_fetch_object($R_5);



    if ($row['link'] && $O_5->c >= 1)
    {
      echo "      <comments>http://".SITE_DOMAIN."/".$row['link']."#comments</comments>\n";
    }
    elseif ($row['link'] && $O_5->c == 0)
    {
      echo "      <comments>http://".SITE_DOMAIN."/".$row['link']."#submit-comment</comments>\n";
    }

    echo "      <pubDate>".gmdate("D, d M Y H:i:s \G\M\T",strtotime($row['date']))."</pubDate>
    </item>\n";
  }
}


/*********************/


echo "  </channel>
</rss>";


@mysql_close($MYSQL_LINK);

?>
