<?php
$ver = '3.1';
/* -------------------------------------------------------------------
| last update 09/12/2022
| ----------------------------------------------------------------- */
include_once('includes/config.inc.php');
include_once(SITE_ROOT.'includes/functions.inc.php');
//$DEBUG=1;
		$_SESSION['DEBUG_MESSAGE'] .= '<br>News Version: '.$ver;
		
// News Page
		$pageTitle = "News Blog";
		// the name of this page
		$PHP_SELF = $_SERVER['PHP_SELF'];
		$news_image_folder = "/assets/uploads/";
		$absolute_news_image_folder = SITE_ROOT.ASSETS_FOLDER;
		$id = mysqli_real_escape_string($connection,$_GET['id']);
		$mode = ( (empty($id)) || ($id=='~') ) ? '' : 'display';
		$archived = mysqli_real_escape_string($connection,$_GET['archived']);
		$_SESSION['include_archived'] = (empty($archived)) ? 0  : 1;
		$archived_news = check_archived();	
		$list_archived_news	= ($_SESSION['include_archived'] > 0) ? 'A' : '';
		$toggle_archived = ($_SESSION['include_archived']==true) ? '<a href="/news/~/">Show Latest News</a>' : '<a href="/news/~/A">Show Archived News</a>';
		$_SESSION['DEBUG_MESSAGE'] .= '<br>Archived Rows '.$archived_news;
		$_SESSION['DEBUG_MESSAGE'] .= '<br>SESSION Archived '.$_SESSION['include_archived'];
		$_SESSION['DEBUG_MESSAGE'] .= '<br>List Archived '.$list_archived_news;
		$newstype = ($_SESSION['include_archived']==true) ? 'Archived News' : 'Latest News';

// Header file
include_once(SITE_ROOT.'includes/header.inc.php');
include_once(SITE_ROOT.'includes/menu.inc.php');
print <<<end
<div class="container" id="content">
		<h1>$pageTitle</h1>		

	<div class="row">
	  <div class="col-md-12"> 
end;

switch($mode) {

	case 'display':  

 	$query = "SELECT * from ".NEWS_TABLE." WHERE id = '".$id."' LIMIT 1";  
            if (!($result = mysqli_query($connection,$query)))
                	show_mysqli_error('news');

		$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
		$newsdate = getRealDate($row['insert_date']);
		 $text = $row['text'];
		  $news_title = htmlentities(stripslashes($row['title']),ENT_QUOTES);
		   $browser_title = htmlentities(stripslashes($row['browser_title']),ENT_QUOTES);
		    $meta_description = htmlentities(stripslashes($row['meta_description']),ENT_QUOTES);
		     $meta_keywords = htmlentities(stripslashes($row['meta_keywords']),ENT_QUOTES);

		$_SESSION['DEBUG_MESSAGE'] .= "<br />Title: ".$news_title;
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Title: ".$row['title'];
// display the link if exists
    $info = ($row['url']) ? '[ <a href="'.$row['url'].'" target="_blank">Further Information</a> ] ' : '';

// display the image if exists
if ($row['image']) {
 $newsimage = $news_image_folder.$row['image'];
   $caption = $row['caption'];   

print <<<end
<div class="$row[alignment]" style="border:1px dotted #999;padding:5px;">
 <img src="$newsimage" alt="$caption" class="img-responsive" />
 <p style="text-align:center;margin:0;padding:2px;font-size: 1em;">$caption</p>
</div>
end;
}
print <<<end
	<h3>$news_title</h3>
         $text
		 $link
        <p>$info [ <a href="/news/~/$list_archived_news">Back to $pageTitle</a> ]</p>
end;

	break;

	default:
?>
		<div class="row">
			<div class="col-md-12">
				<h2><?=$newstype; ?></h2>
			</div>
		</div>
<?php
	         //$additional_where_clause = "AND publication_date > DATE_SUB(NOW(),INTERVAL 1 YEAR)";
			//$additional_where_clause = "AND archived > 0";	
			$additional_where_clause = ($_SESSION['include_archived']) ? "AND archived > 0" : "AND archived < 1";		
			$query = "SELECT * from ".NEWS_TABLE." 
			WHERE disabled != '1' ". $additional_where_clause ."
			ORDER BY publication_date DESC"; 

			if (!($result = mysqli_query($connection,$query)))
					show_mysqli_error('news 2');
												
			while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {

			$newsdate = getRealDate($row['publication_date']);
			 $title = htmlentities(stripslashes($row['title']),ENT_QUOTES);
			  $short_text = (get_short_text($row['text'],150));
			   $summary = ($short_text) ? '<p>'.$short_text.' ...<em><a href="/news/'.$row['id'].'/'.$archived.'">more</a></em></p>' : '';
			 $_SESSION['DEBUG_MESSAGE'] .= "<br />News Date: ".$newsdate;

		//print the menu
print<<<end
		<div class="row">
			<div class="col-md-2">
			<h4>$newsdate</h4>
			</div>
			<div class="col-md-9">
			<h4><a href="/news/$row[id]/$archived">$title</a></h4>
			$summary
			</div>
		</div>
end;
		
			}
	
		if (mysqli_num_rows($result) < 1) {
			print ("\n<li>Sorry nothing new today, please call back!</li>");
		}
		
		if ($archived_news > 0)	{
print<<<end
		<div class="row">
			<div class="col-md-12">
					<div style="width:auto;text-align:right;margin:10px 0;">
						<p>$toggle_archived</p>
					</div>
			</div>
		</div>
end;
		}
break;
}  //ends switch
?>
		</div>
	</div>
<?php
	//include_once(SITE_ROOT.'includes/services_panel.inc.php');
?>
</div> <!-- ends container-->
<?php
include_once(SITE_ROOT.'includes/footer.inc.php');

/* ------------------------------------------------------------------------- */
function check_archived() {
global $connection;	
	
	$query = "SELECT COUNT(*) AS total FROM `news` WHERE archived=1";
	
						if (!($result = mysqli_query($connection,$query)))
							show_mysqli_error('news 3');
						
						$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
				
			return $row['total'];	
}
?>
