<?php 
require 'dbPDO.php';
$title = "Book Search";
$nav = true;
$content = '';
if (isset($_GET['search'])) {
    $query = $db->prepare("SELECT d.ISBN, title, description, price, GROUP_CONCAT(DISTINCT concat('<a href=\"searchBrowse.php\?search=', namel, '\">', namef, ' ', namel, '</a>') ORDER BY namel SEPARATOR ', ') as authors FROM bookauthors a, bookauthorsbooks ba, bookdescriptions d, bookcategoriesbooks cb, bookcategories c WHERE a.AuthorID = ba.AuthorID AND ba.ISBN = d.ISBN AND d.ISBN = cb.ISBN AND c.CategoryID = cb.CategoryID AND (CategoryName = :search OR title LIKE :search OR description LIKE :search OR publisher LIKE :search OR concat_ws(' ', nameF, nameL) LIKE :search) GROUP BY d.ISBN ORDER BY title;");
    $query->execute(array(':search' => "%$_GET[search]%"));
    $search = htmlspecialchars($_GET['search']);
    $title = "Results for \"$search\"";
    $number = $query->rowCount();
    if ($number == 1) {
        $book = $query->fetch();
        header("location: productPage.php?isbn=$book[ISBN]");
        exit();
    }
    $content .= "<div class=\"center\"><strong>$number</strong> books contain <span class=\"red\">\"$search\"</span>.</div>";
    foreach ($query->fetchAll() as $row) {
        $description = substr($row['description'], 0, strpos($row['description'], ' ', 250));
        $content .= "<article class=\"bookteaser\"><a href=\"productPage.php?isbn=$row[ISBN]\"><h1>$row[title]</h1></a><a href=\"productPage.php?isbn=$row[ISBN]\"><img src=\"//yorktown.cbe.wwu.edu/sandvig/mis314/assignments/bookstore/bookimages/$row[ISBN].01.THUMBZZZ.jpg\"></a><div class=\"authors\">By: $row[authors]</div><section class=\"description\">$description ... <a href=\"productPage.php?isbn=$row[ISBN]\">more</a></section></article>";
    }
} elseif (isset($_GET['catID'])) {
    $catnameQuery = $db->prepare("SELECT CategoryName from bookcategories WHERE CategoryID = ?;"); // Why read CategoryName out of the query string when we have a perfectly good database to look it up out of?
    $catnameQuery->execute(array($_GET['catID']));
    $catName = $catnameQuery->fetch();
    $catName = ($catName) ? $catName['CategoryName'] : 'unknown';
    $title = "$catName";
    $query = $db->prepare("SELECT d.ISBN, title, description, price, GROUP_CONCAT(DISTINCT concat('<a href=\"searchBrowse.php\?search=', namel, '\">', namef, ' ', namel, '</a>') ORDER BY namel SEPARATOR ', ') as authors FROM bookauthors a, bookauthorsbooks ba, bookdescriptions d, bookcategoriesbooks cb, bookcategories c WHERE a.AuthorID = ba.AuthorID AND ba.ISBN = d.ISBN AND d.ISBN = cb.ISBN AND c.CategoryID = cb.CategoryID AND cb.CategoryID = ? GROUP BY d.ISBN ORDER BY title;");
    $query->execute(array($_GET['catID']));
    $number = $query->rowCount();
    $content .= "<div class=\"center\"><strong>$number</strong> books in category <span class=\"red\">$catName</span>.</div>";
    foreach ($query->fetchAll() as $row) {
        $description = substr($row['description'], 0, strpos($row['description'], ' ', 250));
        $content .= "<article class=\"bookteaser\"><a href=\"productPage.php?isbn=$row[ISBN]\"><h1>$row[title]</h1></a><a href=\"productPage.php?isbn=$row[ISBN]\"><img src=\"//yorktown.cbe.wwu.edu/sandvig/mis314/assignments/bookstore/bookimages/$row[ISBN].01.THUMBZZZ.jpg\"></a><div class=\"authors\">By: $row[authors]</div><section class=\"description\">$description ... <a href=\"productPage.php?isbn=$row[ISBN]\">more</a></section></article>";
    }
} else {
    header('location: index.php');
    exit();
}
require 'template.php';
?>