<?php 
#include 'goallinefunctionsJSON.php';
#include '/AHA/headerJSON.php'
include_once 'connection.php';

function NewsBuilder($Newsid) {
	$pdoConnect = createNDBH();
	$NewsFetch = $pdoConnect->prepare('SELECT idNews, NewsTitle, NewsItem, NewsDate, NewsAuthor, NewsAuthorLink FROM news WHERE idNews = :idnews LIMIT 1');
	$NewsFetch->bindParam(':idnews', $Newsid, PDO::PARAM_INT);
	$NewsFetch->execute();
	$NewsSelected = $NewsFetch->fetch(PDO::FETCH_ASSOC);

return $NewsSelected;
}

function BlogStoryList($blogID) {
	$pdoConnect = createNDBH();
	$BlogFetch = $pdoConnect->prepare('SELECT idNews, NewsTitle, NewsDate, Target, Targetid FROM newsdeliver JOIN news ON newsdeliver.NewsidRef = news.idNews WHERE Target = "blog" AND Targetid = :idBlog ORDER BY NewsDate DESC');
	$BlogFetch->bindParam(':idBlog', $blogID, PDO::PARAM_INT);
	$BlogFetch->execute();
	$BlogSelected = $BlogFetch->fetchAll(PDO::FETCH_ASSOC);
return $BlogSelected;
}

function BlogDesc($blogID) {
	$pdoConnect = createNDBH();
	$BlogFetch = $pdoConnect->prepare('SELECT blogName, blogDesc FROM blog WHERE idblog = :idBlog LIMIT 1');
	$BlogFetch->bindParam(':idBlog', $blogID, PDO::PARAM_INT);
	$BlogFetch->execute();
	$BlogSelected = $BlogFetch->fetch(PDO::FETCH_ASSOC);
return $BlogSelected;
}

/*
function VideoBuilder($width, $height, $youtubeid) {
	$videooutput = '<iframe width="'.$width.'" height="'.$height.'" src="https://www.youtube.com/embed/'.$youtubeid.'" frameborder="0" allowfullscreen></iframe>';
	return $videooutput;
}
*/
#Initial variables - requires a news id
$Blogid = $_GET['blogid'];
$BlogList = BlogStoryList($Blogid);
$BlogData = BlogDesc($Blogid);
$BlogStory = array('blogTitle' => $BlogData['blogName'], 'blogDesc' => $BlogData['blogDesc']);

$blogArticles = array();
foreach ($BlogList as $blogstory) {
	#print_r($blogstory);
	$NewsFetcher = NewsBuilder($blogstory['idNews']);
	#print_r($NewsFetcher);
	#News Entry Title control
	$NewsTitle = $NewsFetcher['NewsTitle'];
	$NewsAuthor = $NewsFetcher['NewsAuthor'];
	$NewsDate = date("M d, Y", strtotime($NewsFetcher['NewsDate']));

	#News Sections
	$NewsHeader = '<h1>'.$NewsTitle.'</h1>';
	$NewsCreated = 'Written by '.$NewsAuthor.' on '.$NewsDate.'<br />';
	#$NewsBody = newline2p($NewsFetcher['NewsItem']); 
	$NewsBody = array_filter(explode("\n", $NewsFetcher['NewsItem']));

	$blogArticles[] = array(
		'newsID' => $blogstory['idNews'],
		'newsTitle' => $NewsTitle,
		'newsAuthor' => $NewsAuthor,
		'newsDate' => $NewsDate,
		'newsBody' => $NewsBody
	);
}
	$BlogStory['articles'] = $blogArticles;
echo json_encode($BlogStory);
?>