<?php

include_once "settings.php";

// Get and use a basic title search for pulling records.
$keywordsearch = htmlspecialchars($_GET["kw"]);
$authorsearch = htmlspecialchars($_GET["au"]);
$typesearch = htmlspecialchars($_GET["ty"]);

$socialkw = mb_convert_case($keywordsearch, MB_CASE_TITLE, "UTF-8");
$socialau = mb_convert_case($authorsearch, MB_CASE_TITLE, "UTF-8");
$socialty = mb_convert_case($typesearch, MB_CASE_TITLE, "UTF-8");

if (!empty($keywordsearch)) {
	$searchtopic = 'Keyword: '.$socialkw;
} elseif (!empty($authorsearch)) {
	$searchtopic = 'Author: '.$socialau;
} else {
	$searchtopic = 'Type: '.$socialty;
}


// -------------------- BEGIN DATABASE QUERIES --------------------

// Establish atabase connection
$db = new SQLite3('metadata.sqlite');

$keywordquery = $db->query("SELECT
DISTINCT books.id AS id,
books.title AS title,
SUBSTR(comments.text,0,120) AS excerpt
FROM books
INNER JOIN
comments ON comments.book = books.id
INNER JOIN
books_tags_link ON books_tags_link.book = books.id
INNER JOIN
tags ON tags.id = books_tags_link.tag
WHERE books.title LIKE '%$keywordsearch%'
OR books.author_sort LIKE '%$keywordsearch%'
OR comments.text LIKE '%$keywordsearch%'
OR tags.name LIKE '%$keywordsearch%'
ORDER BY books.timestamp DESC");

$authorquery = $db->query("SELECT
DISTINCT books.id AS id,
books.title AS title,
SUBSTR(comments.text,0,120) AS excerpt
FROM books
INNER JOIN
comments ON comments.book = books.id
INNER JOIN
books_tags_link ON books_tags_link.book = books.id
WHERE books.author_sort LIKE '%$authorsearch%'
ORDER BY books.timestamp DESC");

$typequery = $db->query("SELECT
DISTINCT books.id AS id,
books.title AS title,
SUBSTR(comments.text,0,120) AS excerpt
FROM books
INNER JOIN
comments ON comments.book = books.id
INNER JOIN
books_custom_column_1_link ON books_custom_column_1_link.book = books.id
INNER JOIN
custom_column_1 ON custom_column_1.id = books_custom_column_1_link.value
WHERE
custom_column_1.value = '$typesearch'
ORDER BY books.timestamp DESC");
?>


<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
	<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Infopump - Recently Added - <?php echo $searchtopic; ?></title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="description" content="Free HTML5 Template by FREEHTML5.CO" />
	<meta name="keywords" content="free html5, free template, free bootstrap, html5, css3, mobile first, responsive" />
	<meta name="author" content="FREEHTML5.CO" />

  <!-- 
	//////////////////////////////////////////////////////

	FREE HTML5 TEMPLATE 
	DESIGNED & DEVELOPED by FREEHTML5.CO
		
	Website: 		http://freehtml5.co/
	Email: 			info@freehtml5.co
	Twitter: 		http://twitter.com/fh5co
	Facebook: 		https://www.facebook.com/fh5co

	//////////////////////////////////////////////////////
	 -->

  	<!-- Facebook and Twitter integration -->
	<meta property="og:title" content=<?php echo '"'.$SiteName.' - Recently Added - '.$searchtopic.'" />';?>

	<meta property="og:image" content=<?php echo '"'.$SiteURL.'/images/og-site-avatar.jpg" />';?>

	<?php
		if (!empty($keywordsearch)) {
			echo '<meta property="og:url" content="'.$SiteURL.'/recent.php?kw='.$socialkw.'" />';
			echo '<meta name="twitter:url" content="'.$SiteURL.'/recent.php?kw='.$socialkw.'" />';
		} elseif (!empty($authorsearch)) {
			echo '<meta property="og:url" content="'.$SiteURL.'/recent.php?au='.$socialau.'" />';
			echo '<meta name="twitter:url" content="'.$SiteURL.'/recent.php?au='.$socialau.'" />';
		} else {
			echo '<meta property="og:url" content="'.$SiteURL.'/recent.php?ty='.$socialty.'" />';
			echo '<meta name="twitter:url" content="'.$SiteURL.'/recent.php?ty='.$socialty.'" />';
		}
	?>

	<meta property="og:site_name" content=<?php echo '"'.$SiteName.' - Recently Added" />';?>

	<meta property="og:description" content=<?php echo '"'.$SubName.'" />';?>

	<meta name="twitter:title" content=<?php echo '"'.$SiteName.' - Recently Added - '.$searchtopic.'" />';?>

	<meta name="twitter:image" content=<?php echo '"'.$SiteURL.'/images/og-site-avatar.jpg" />';?>

	

	<meta name="twitter:card" content="summary" />

	<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
	<link rel="shortcut icon" href="favicon.ico">
	<!-- Google Fonts -->
	<link href='http://fonts.googleapis.com/css?family=Playfair+Display:400,700,400italic|Roboto:400,300,700' rel='stylesheet' type='text/css'>
	<!-- Animate -->
	<link rel="stylesheet" href="css/animate.css">
	<!-- Icomoon -->
	<link rel="stylesheet" href="css/icomoon.css">
	<!-- Bootstrap  -->
	<link rel="stylesheet" href="css/bootstrap.css">

	<link rel="stylesheet" href="css/style.css">


	<!-- Modernizr JS -->
	<script src="js/modernizr-2.6.2.min.js"></script>
	<!-- FOR IE9 below -->
	<!--[if lt IE 9]>
	<script src="js/respond.min.js"></script>
	<![endif]-->

	</head>
	<body>
	<div id="fh5co-offcanvas">
		<a href="#" class="fh5co-close-offcanvas js-fh5co-close-offcanvas"><span><i class="icon-cross3"></i> <span>Close</span></span></a>
		<div class="fh5co-bio">
			<figure>
				<a href="index.php"><img src="images/avatar.jpg" alt="Infopump Avatar" class="img-responsive"></a>
			</figure>
			<h3 class="heading">About the Project</h3>
			<a href="index.php"><h2>Infopump</h2></a>
			<p>A bibliographic management and display system.</p>
			<hr>
			<p>A free, open source project from:<br />
				<a href="https://rss.com/podcasts/l0wl1f3podcast/">The L0WL1F3 Podcast</a><br />
				<a href="https://www.neondystopia.com/">Neon Dystopia</a><br />
				<a href="https://cyberpunklibrarian.com">Cyberpunk Librarian</a>
			</p>
			<ul class="fh5co-social">
				<!--<li><a href="#"><i class="icon-twitter"></i></a></li>-->
				<!--<li><a href="#"><i class="icon-facebook"></i></a></li>
				<li><a href="#"><i class="icon-instagram"></i></a></li>-->
			</ul>
		</div>

		<div class="fh5co-menu">
			<div class="fh5co-box">
				<h3 class="heading">Categories</h3>
				<ul>
					<li><a href="#">Books</a></li>
					<li><a href="#">Movies</a></li>
					<li><a href="#">Television</a></li>
					<li><a href="#">Video Games</a></li>
					<li><a href="#">Other</a></li>
				</ul>
			</div>
			<div class="fh5co-box">
				<h3 class="heading">Search</h3>
				<form action="results.php" method="get">
					<div class="form-group">
						<input type="text" class="form-control" name="kw" placeholder="Keyword search">
					</div>
				</form>
			</div>
		</div>
	</div>

	<!-- END #fh5co-offcanvas -->
	<header id="fh5co-header">
		
		<div class="container-fluid">

			<div class="row">
				<a href="#" class="js-fh5co-nav-toggle fh5co-nav-toggle"><i></i></a>
				<!-- <ul class="fh5co-social">
					<li><a href="#"><i class="icon-twitter"></i></a></li>
					<li><a href="#"><i class="icon-facebook"></i></a></li>
					<li><a href="#"><i class="icon-instagram"></i></a></li>
				</ul> -->
				<div class="col-lg-12 col-md-12 text-center">
					<h1 id="fh5co-logo"><a href="index.php">Recently Added<br /><br /><?php echo $searchtopic; ?></a></h1>
				</div>

			</div>
		
		</div>

	</header>
	<!-- END #fh5co-header -->
	<div class="container-fluid">
		<div class="row fh5co-post-entry single-entry">
			<article class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-12 col-xs-offset-0">
				<div class="col-lg-12 col-lg-offset-0 col-md-12 col-md-offset-0 col-sm-12 col-sm-offset-0 col-xs-12 col-xs-offset-0 text-left content-article">
					<div class="row rp-b">
						<div class="col-md-12 animate-box">
							<?php
							if ($keywordsearch != '') {
							while ($row = $keywordquery->fetchArray()) {
								$row_id = $row['id'];
								$row_title = $row['title'];
								$row_excerpt = $row['excerpt'];
							
								echo '<p style="padding:25px 0 35px 0;"><img style="float:left; max-height: 120px; padding: 10px 10px" src="images/'.$row_id.'.jpg"><strong><em><a href="itemrecord.php?itemid='.$row_id.'">'.$row_title.'</em></a> :</strong> '.strip_tags($row_excerpt).'...</p>';
							}
						} elseif ($typesearch != '') {
							while ($row = $typequery->fetchArray()) {
								$row_id = $row['id'];
								$row_title = $row['title'];
								$row_excerpt = $row['excerpt'];
							
								echo '<p style="padding:25px 0 35px 0;"><img style="float:left; max-height: 120px; padding: 10px 10px" src="images/'.$row_id.'.jpg"><strong><em><a href="itemrecord.php?itemid='.$row_id.'">'.$row_title.'</em></a> :</strong> '.strip_tags($row_excerpt).'...</p>';								
						}
						} else {
							while ($row = $authorquery->fetchArray()) {
								$row_id = $row['id'];
								$row_title = $row['title'];
								$row_excerpt = $row['excerpt'];
							
								echo '<p style="padding:25px 0 35px 0;"><img style="float:left; max-height: 120px; padding: 10px 10px" src="images/'.$row_id.'.jpg"><strong><em><a href="itemrecord.php?itemid='.$row_id.'">'.$row_title.'</em></a> :</strong> '.strip_tags($row_excerpt).'...</p>';
							}
						}
							?>
						</div>
					</div>
					
				</div>
			</article>
		</div>
	</div>

	<footer id="fh5co-footer">
	<p><small>&copy; Creative Commons By-NC-SA<br> Design by <a href="http://freehtml5.co" target="_blank">FREEHTML5.co</a></small></p>
	</footer>
	
	<!-- jQuery -->
	<script src="js/jquery.min.js"></script>
	<!-- jQuery Easing -->
	<script src="js/jquery.easing.1.3.js"></script>
	<!-- Bootstrap -->
	<script src="js/bootstrap.min.js"></script>
	<!-- Waypoints -->
	<script src="js/jquery.waypoints.min.js"></script>
	<!-- Main JS -->
	<script src="js/main.js"></script>

	</body>
</html>

