<?php

// Yahoo Yql Google Books Search Tutorial - RJM Programming - 2014

$selname = "";
$maxresults = "10";
if (isset($_POST['selname'])) $selname = str_replace(" ","%20",$_POST['selname']);
if (isset($_GET['selname'])) $selname = str_replace(" ","%20",$_GET['selname']);
if (isset($_POST['maxresults'])) $maxresults = str_replace(" ","%20",$_POST['maxresults']);
if (isset($_GET['maxresults'])) $maxresults = str_replace(" ","%20",$_GET['maxresults']);
$bcolours = array("white", "pink", "aqua", "lime", "silver", "lightyellow", "cyan", "fuchsia", "orange", "lightblue", "lightgreen", "lightgray");

if ($selname != "") {
 $pageContent = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20google.books%20WHERE%20q%3D%22".$selname."%22%20AND%20maxResults%3D". $maxresults ."&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");

 $parts = explode("<!-- total:", $pageContent);

 $json_output = json_decode($pageContent);  
  
 // Confirm that results were returned before parsing
 if (!is_null($json_output->query->results)) {
            //echo "yessir";
			date_default_timezone_set('Australia/Sydney');
			$date = date('d/m/Y h:i:s a', time());     
			$i=1;       
			echo "<!doctype HTML><html><head><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'></head><body align='center' style='background-color:yellow;'><h1 align='center'>" . urldecode($selname) . " ... Google Books ... " . $date . " AEST</h1><br>\n<div align='center'>";
            foreach($json_output->query->results->json->items as $event){
                try {
                echo "\n<div align='center' style='background-color:" . $bcolours[rand(0,(sizeof($bcolours)-1))], ";'>" . $i . "<table align='center' border=1 cellspacing=5 cellpadding=5><tr><td style='padding: 8 8 8 8;'><p align='center' style='border: red 12px solid; padding: 8 8 8 8;font-weight:bold;'>&nbsp;&nbsp;<a style='font-weight:bold;' target=_blank align='center' title='New Window of Information' href='".$event->selfLink."' title='" . str_replace("'", "", $event->volumeInfo->title) . "'>".$event->volumeInfo->title."";
                if (str_replace("null", "", $event->volumeInfo->imageLinks->thumbnail) != "") echo "<img align='center' style='padding-left:25px;margin-top:20px;margin-bottom:20px;' src='".$event->volumeInfo->imageLinks->thumbnail."' title='" . str_replace("'", "", $event->volumeInfo->title) . "' />";
                echo "</a>&nbsp;&nbsp;</p>";
                //if (1 == 2) {
                if  (isset($event->volumeInfo->authors[0])) {
                $ais = $event->volumeInfo->authors[0];
                if (strlen($ais) == 1) {
                echo "<p align='center' padding: 8 8 8 8;'>Author: ".$event->volumeInfo->authors."</p>";
                } else {
                echo "<p align='center' padding: 8 8 8 8;'>Authors: ".$event->volumeInfo->authors[0];
                $k = 1;
                while (isset($event->volumeInfo->authors[$k])) {
                echo ", ".$event->volumeInfo->authors[$k];
                $k++;
                }
                echo "</p>";
                }
                } else if (isset($event->volumeInfo->authors)) {
                echo "<p align='center' padding: 8 8 8 8;'>Author: ".$event->volumeInfo->authors."</p>";
                if (isset($event->volumeInfo->industryIdentifiers->identifier)) {
                echo "<p align='center' padding: 8 8 8 8;'>ISBN: ".$event->volumeInfo->industryIdentifiers->identifier."</p>";
                } else if (isset($event->volumeInfo->industryIdentifiers[1])) {
                echo "<p align='center' padding: 8 8 8 8;'>ISBNs: ".$event->volumeInfo->industryIdentifiers[0]->identifier."</p>";
                echo "<p align='center' padding: 8 8 8 8;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ".$event->volumeInfo->industryIdentifiers[1]->identifier."</p>";
                } else if (isset($event->volumeInfo->industryIdentifiers[0])) {
                echo "<p align='center' padding: 8 8 8 8;'>ISBN: ".$event->volumeInfo->industryIdentifiers[0]->identifier."</p>";
                }
                }
                if (isset($event->accessInfo->webReaderLink) && ($event->saleInfo->isEbook == "true" || $event->saleInfo->isEbook)) {
                echo "<p align='center' padding: 8 8 8 8;'>Web Reading Link: <a target=_blank title='Web Reader Link' href='".$event->accessInfo->webReaderLink."'>Web Reader</a></p>";
                }
                if (isset($event->volumeInfo->previewLink)) {
                echo "<p align='center' padding: 8 8 8 8;'>Preview Link: <a target=_blank title='Preview Link' href='".$event->volumeInfo->previewLink."'>Preview</a></p>";
                }
                if (isset($event->volumeInfo->infoLink)) {
                echo "<p align='center' padding: 8 8 8 8;'>Information Link: <a target=_blank title='Information Link' href='".$event->volumeInfo->infoLink."'>Information</a></p>";
                }
                if (isset($event->volumeInfo->canonicalVolumeLink)) {
                echo "<p align='center' padding: 8 8 8 8;'>Canonical Volume Link: <a target=_blank title='Canonical Volume Link' href='".$event->volumeInfo->canonicalVolumeLink."'>Canonical Volume</a></p>";
                }
                if (isset($event->searchInfo->textSnippet)) {
                if ($event->volumeInfo->description != $event->searchInfo->textSnippet) echo "<p align='center' padding: 8 8 8 8;'>Text Snippet: ".$event->searchInfo->textSnippet."</p>";
                }
                echo "<p align='center' padding: 8 8 8 8;'>Publisher: ".$event->volumeInfo->publisher."</p>";
                echo "<p align='center' padding: 8 8 8 8;'>Published Date: ".$event->volumeInfo->publishedDate."</p>";
                echo "<p align='center' style='border: lightgreen 14px solid; padding: 8 8 8 8; background-color:white;'>Description: " . str_replace("null", "", $event->volumeInfo->description) . "</p>";

                //}
                echo "</td></tr></table></div>\n";
       
                } catch (Exception $huh) {
                echo "\n<p>".$huh->message."</p>\n";
                }
                $i++;
            }
 			echo "</td></tr></table></div></body></html>";
 }

}
?> 

