<?php
$title = "Latest News";
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . ini_get('include_path'));
include_once("include/header.php");
?>

    <div class="imagesInsideFloatRight">
        <h2>OceanCurrent News</h2>

        <div class="alert alert-info" role="alert">Noticed something interesting, on this website, or on the water?<BR>
            Please
            join the list of <a href="acknowl.php">contributors.</a>
        </div>
<?php
		// Do a loop through the sorted articles
		$ndir = $DOCROOT.$BASEDIR.'news';
		//$ndir = '/home/vamp1/pub_web/analysis/news';
		$dirs = scandir( $ndir, 1 );
		$today = date("Ymd");
		foreach( $dirs as $d ) {
			if (strlen($d) !== 8) continue;
			if (!is_numeric($d)) continue;
			//echo "<code>$d</code>\n";
			if ($d > $today) continue;
			$file = $ndir.'/'.$d.'/item.publish';
			if (!file_exists( $file )) continue;
			$file = $ndir.'/'.$d.'/item.xml';
			if (!file_exists( $file )) continue;
			// Also check the size
			if (filesize($file)==0) continue;
			$dom = new DOMDocument;
			$dom->loadHTMLFile( $file );
			$xpath = new DOMXPath($dom);
			$title = $xpath->query('//article/title/text()')->item(0)->nodeValue;

			$xdom = new DOMDocument;
			$xnode = $xdom->createTextNode( $title );
			$xdom->appendChild($xnode);
			$title = $xdom->saveHTML();

			$author_node = $xpath->query('//article/author');
			$date = $xpath->query('//article/date/text()')->item(0)->nodeValue;
			$text = $xpath->query('//article/text/*');
			echo "<article>\n";
				//$titletext = preg_replace('/[^a-z0-9]+/', '_', strtolower($title));
				$titletext = preg_replace('/\_$/','',preg_replace('/[^A-Za-z0-9]+/', '_', $title));
				echo '<a name="'.$date.'"></a>';
				echo "<h3 id=\"$titletext\"><a href=\"news.php#$titletext\">$title</a></h3>\n";
				if ( $author_node->length > 0 ) {
					$author = $xpath->query('//article/author/text()')->item(0)->nodeValue;
					if ( strlen($author) > 0 ) {
						echo '<h5><em>'.$author.'</em></h5>';
					}
				}
				$datestr = strftime( '%e %B, %Y', strtotime( $date ));
				//echo '<a name="'.$date.'"><em>'.$datestr.'</em></a>';
				echo '<a><em>'.$datestr.'</em></a>';
				$ndom = new DOMDocument;
				foreach ($text as $t) {
					$node = $ndom->importNode($t,true);
					$ndom->appendChild($node);
				}
				echo $ndom->saveHTML();
			echo "</article>\n";
		}
?>

		<a href="news_archive.php">[Archive]</a>
		<a href="technews.php">[Technical news archive]</a>

    </div>

<?php
include("include/footer.php");
?>
