<?php
if (MING != "merciless") require ('index.php'); // this keeps people from executing "news.php" directly
else {

function news_readmore_link_doaction ($arg = FALSE)
{
	global $theme_readmore_link_template;
	if ($arg !== FALSE )
	{
		$url = URL_PREPARED;
		$url .= 'readmore=yes&article=' . urlencode($arg);
		$replace_array = Array (
		'%url%' => $url
		);
		$text = main_replace($theme_readmore_link_template, $replace_array);
		outblurt($text, POWER_DEFAULT);
		return TRUE;
	}
	else
	{
		outblurt('Error [news_01]: News readmore link bad call.', POWER_DEBUG );
		return FALSE;
	}
	
}
function news_full_story_doaction ( $directory = '' )
{
	global $news_theme_full_template, $news_directory;
	$rqst = $_GET['article'];
	if ( trim($directory) != '') $directory = $news_directory . '/' . $directory;
	else $directory = $news_directory;
	$file = main_get_file( $rqst, $directory);
	if ($file !== FALSE)
	{
		$file->outblurtWithTemplate($news_theme_full_template, TRUE); // outblurts the full story with the template as a capable file.
		return TRUE;
	}
	else
	{
		outblurt ('<strong>404 - File Not Found</strong><br />The file you were looking for does not exist. <!-- Error [news:full_story 1] -->');
		// ERROR!
	}
}

function news_listing_doaction ( $directory = '')
{
	global $news_directory, $news_date_format, $news_theme_format, $news_folding, $news_articles_per_page, 
		$news_fold_get_variable, $news_should_reverse_list;
	if (strlen($directory)>0) $dir = $directory;
	else $dir = $news_directory;
	$news_file_list = main_dir_contents ($dir);
	$ordered_new_list = Array();
	$previous_and_next_links = '';
	
	foreach ($news_file_list as $file)
	{
		$date = $file->unix_date;
		$title = $file->title;
		$type = $file->type;
		$contents = main_get_file_contents($file, $dir);
		$ordered_news_list[] = Array( $date, $title, $type, $contents, $file->raw_title );
	}
	
	sort($ordered_news_list); //TODO: use usort() to sort instead of this. Also, don't waste time making two lists.
	if($news_should_reverse_list) $ordered_news_list = array_reverse($ordered_news_list);
	$folded = FALSE;
	$newsfold = $_GET[$news_fold_get_variable];
	$total_number_of_stories = sizeof($ordered_news_list);
	if ($news_folding === TRUE && $news_articles_per_page > 0 && $news_articles_per_page < $total_number_of_stories) {
		$folded = TRUE;
		$ordered_news_list = array_slice($ordered_news_list, $newsfold, $news_articles_per_page);
		/*
			TODO: Make the theme layout more customizable via a generalized template like the
					one in the content module
		*/
		global $news_fold_get_variable, $news_theme_folding_link, $news_theme_folding_info, $theme_undisplay_element;
		$next_articles = urlencode($newsfold + $news_articles_per_page);
		$base_url = ereg_replace("\&?$news_fold_get_variable=[0-9]+", '', URL_PREPARED); // this gets rid of the old &articles=X in the url
		$previous_articles = urlencode($news_articles_per_page < $newsfold ? $newsfold - $news_articles_per_page : 0);
		
		$next_link = ($newsfold + $news_articles_per_page >= $total_number_of_stories) ? $theme_undisplay_element : ''; // if we've reached the end, make the link
		$previous_link = ($newsfold <= 0) ? $theme_undisplay_element : '';
		
		$replace_list = Array (
		'%next_url%' => $base_url . $news_fold_get_variable . "=$next_articles",
		'%previous_url%' => $base_url . $news_fold_get_variable . "=$previous_articles",
		'%articles_per_fold%' => $news_articles_per_page,
		'%next%' => $next_link,
		'%previous%' => $previous_link		
		);
		$previous_and_next_links = main_replace($news_theme_folding_link, $replace_list);
		outblurt ($previous_and_next_links , POWER_DEFAULT);
		#############################
	}
	foreach ($ordered_news_list as $item)
	{
		
		list ($date, $title, $type, $contents, $raw_title) = $item;
		$posted_on = theme_generate_date_line($date, $news_date_format);
		$raw_title_url = urlencode($raw_title);
		$replace_list2 = Array (
			'%posted_on%' => $posted_on,
			'%contents%' => $contents,
			'%title%' => $title,
			'%file_name%' => $file_name,
			'%raw_title%' => $raw_title,
			'%readmore_link%' => '<!--[[ news : readmore_link : ' . $raw_title . ']]-->'
			);
		$exploded = explode( '%contents%', $news_theme_format, 2);
		
		$first_half = $exploded[0];
		$second_half = $exploded[1];
		
		$first = main_replace ($first_half, $replace_list2);
		$middle = $contents;
		$last = main_replace ($second_half, $replace_list2);
		outblurt ($first, POWER_DEFAULT, TRUE );
		outblurt ($middle, $type);
		outblurt ($last, POWER_DEFAULT, TRUE );
	}
	outblurt($previous_and_next_links, POWER_DEFAULT);
	
}

function news_main_doaction ( $directory = '' )
{
	if ($_GET['readmore'] == 'yes') news_full_story_doaction($directory);
	else news_listing_doaction( $directory );
}

} // this closing brace is from the else clause in the bad_file checker above
?>
