From 9a15d0e1a47dce492f4362db50b5c376283a2ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Sun, 22 Dec 2013 18:40:03 +0100 Subject: [PATCH] Integrate a little better the typeahead search in the normal search page. Heavily based on At-Libitum work. re #34 --- base.php | 111 ++++++++++++++++++++++++++++++++++++++++++++-- book.php | 93 ++++---------------------------------- test/bookTest.php | 8 ++-- util.js | 2 +- 4 files changed, 122 insertions(+), 92 deletions(-) diff --git a/base.php b/base.php index ecad72b..935da23 100644 --- a/base.php +++ b/base.php @@ -281,6 +281,7 @@ class Entry public $contentType; public $linkArray; public $localUpdated; + public $className; private static $updated = NULL; public static $icons = array( @@ -318,13 +319,14 @@ class Entry return array ( "title" => $this->title, "content" => $this->content, "navlink" => $this->getNavLink () ); } - public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray) { + public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "") { global $config; $this->title = $ptitle; $this->id = $pid; $this->content = $pcontent; $this->contentType = $pcontentType; $this->linkArray = $plinkArray; + $this->className = $pclass; if ($config['cops_show_icons'] == 1) { @@ -468,7 +470,6 @@ class Page Base::clearDb (); } } else { - //error_log (var_dump (getCurrentOption ('ignored_categories'))); if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) { array_push ($this->entryArray, Author::getCount()); } @@ -539,6 +540,22 @@ class Page return false; } + public function getContentArrayTypeahead () { + $out = array (); + foreach ($this->entryArray as $entry) { + if ($entry instanceof EntryBook) { + array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ())); + } else { + if (empty ($entry->className) xor Base::noDatabaseSelected ()) { + array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ())); + } else { + array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ())); + } + } + } + return $out; + } + } class PageAllAuthors extends Page @@ -732,6 +749,90 @@ class PageQueryResult extends Page const SCOPE_BOOK = "book"; const SCOPE_PUBLISHER = "publisher"; + private function useTypeahead () { + return !is_null (getURLParam ("search")); + } + + public function doSearchByCategory () { + $database = GetUrlParam (DB); + $out = array (); + $pagequery = Base::PAGE_OPENSEARCH_QUERY; + $dbArray = array (""); + $d = $database; + $query = $this->query; + // Special case when no databases were chosen, we search on all databases + if (Base::noDatabaseSelected ()) { + $dbArray = Base::getDbNameList (); + $d = 0; + } + foreach ($dbArray as $key) { + if (Base::noDatabaseSelected ()) { + array_push ($this->entryArray, new Entry ($key, DB . ":query:{$d}", + " ", "text", + array ( new LinkNavigation ("?" . DB . "={$d}")), "tt-header")); + Base::getDb ($d); + } + foreach (array (PageQueryResult::SCOPE_BOOK, + PageQueryResult::SCOPE_AUTHOR, + PageQueryResult::SCOPE_SERIES, + PageQueryResult::SCOPE_TAG, + PageQueryResult::SCOPE_PUBLISHER) as $key) { + if (in_array($key, getCurrentOption ('ignored_categories'))) { + continue; + } + switch ($key) { + case "book" : + $array = Book::getBooksByStartingLetter ('%' . $query, 1, NULL, 5); + break; + case "author" : + $array = Author::getAuthorsByStartingLetter ('%' . $query); + break; + case "series" : + $array = Serie::getAllSeriesByQuery ($query); + break; + case "tag" : + $array = Tag::getAllTagsByQuery ($query, 1, NULL, 5); + break; + case "publisher" : + $array = Publisher::getAllPublishersByQuery ($query); + break; + } + + $i = 0; + if (count ($array) == 2 && is_array ($array [0])) { + $total = $array [1]; + $array = $array [0]; + } else { + $total = count($array); + } + if ($total > 0) { + // Comment to help the perl i18n script + // str_format (localize("bookword", count($array)) + // str_format (localize("authorword", count($array)) + // str_format (localize("seriesword", count($array)) + // str_format (localize("tagword", count($array)) + // str_format (localize("publisherword", count($array)) + array_push ($this->entryArray, new Entry (str_format (localize ("search.result.{$key}"), $this->query), DB . ":query:{$d}:{$key}", + str_format (localize("{$key}word", $total), $total), "text", + array ( new LinkNavigation ("?page={$pagequery}&query={$query}&db={$d}&scope={$key}")), + Base::noDatabaseSelected () ? "" : "tt-header")); + } + if (!Base::noDatabaseSelected () && $this->useTypeahead ()) { + foreach ($array as $entry) { + array_push ($this->entryArray, $entry); + $i++; + if ($i > 4) { break; }; + } + } + } + $d++; + if (Base::noDatabaseSelected ()) { + Base::clearDb (); + } + } + return $out; + } + public function InitializeContent () { $scope = getURLParam ("scope"); @@ -750,7 +851,7 @@ class PageQueryResult extends Page $crit = "%" . $this->query . "%"; // Special case when we are doing a search and no database is selected - if (Base::noDatabaseSelected ()) { + if (Base::noDatabaseSelected () && !$this->useTypeahead ()) { $i = 0; foreach (Base::getDbNameList () as $key) { Base::clearDb (); @@ -762,6 +863,10 @@ class PageQueryResult extends Page } return; } + if (empty ($scope) && $this->useTypeahead ()) { + $this->doSearchByCategory (); + return; + } switch ($scope) { case self::SCOPE_AUTHOR : $this->entryArray = Author::getAuthorsByStartingLetter ('%' . $this->query); diff --git a/book.php b/book.php index 7de1802..2e8d42a 100644 --- a/book.php +++ b/book.php @@ -439,16 +439,16 @@ class Book extends Base { echo "Exception : " . $e->getMessage(); } } - + public function getThumbnail ($width, $height, $outputfile = NULL) { if (is_null ($width) && is_null ($height)) { return false; } - + // In case something bad happen below set a default size $nw = "160"; $nh = "120"; - + $file = $this->getFilePath ("jpg"); // get image size if ($size = GetImageSize($file)) { @@ -473,7 +473,7 @@ class Book extends Base { imagejpeg($dst_img,$outputfile,80); imagedestroy($src_img); imagedestroy($dst_img); - + return true; } @@ -671,90 +671,13 @@ function getJson ($complete = false) { $n = getURLParam ("n", "1"); $database = GetUrlParam (DB); - if ($search) { - $out = array (); - $pagequery = Base::PAGE_OPENSEARCH_QUERY; - $dbArray = array (""); - $d = $database; - // Special case when no databases were chosen, we search on all databases - if (Base::noDatabaseSelected ()) { - $dbArray = Base::getDbNameList (); - $d = 0; - } - foreach ($dbArray as $key) { - if (Base::noDatabaseSelected ()) { - array_push ($out, array ("title" => $key, - "class" => "tt-header", - "navlink" => "index.php?db={$d}")); - Base::getDb ($d); - } - foreach (array (PageQueryResult::SCOPE_BOOK, - PageQueryResult::SCOPE_AUTHOR, - PageQueryResult::SCOPE_SERIES, - PageQueryResult::SCOPE_TAG, - PageQueryResult::SCOPE_PUBLISHER) as $key) { - if (in_array($key, getCurrentOption ('ignored_categories'))) { - continue; - } - switch ($key) { - case "book" : - $array = Book::getBooksByStartingLetter ('%' . $query, 1, NULL, 5); - break; - case "author" : - $array = Author::getAuthorsByStartingLetter ('%' . $query); - break; - case "series" : - $array = Serie::getAllSeriesByQuery ($query); - break; - case "tag" : - $array = Tag::getAllTagsByQuery ($query, 1, NULL, 5); - break; - case "publisher" : - $array = Publisher::getAllPublishersByQuery ($query); - break; - } - - $i = 0; - if (count ($array) == 2 && is_array ($array [0])) { - $total = $array [1]; - $array = $array [0]; - } else { - $total = count($array); - } - if ($total > 0) { - // Comment to help the perl i18n script - // str_format (localize("bookword", count($array)) - // str_format (localize("authorword", count($array)) - // str_format (localize("seriesword", count($array)) - // str_format (localize("tagword", count($array)) - // str_format (localize("publisherword", count($array)) - array_push ($out, array ("title" => str_format (localize("{$key}word", $total), $total), - "class" => Base::noDatabaseSelected () ? "" : "tt-header", - "navlink" => "index.php?page={$pagequery}&query={$query}&db={$d}&scope={$key}")); - } - if (!Base::noDatabaseSelected ()) { - foreach ($array as $entry) { - if ($entry instanceof EntryBook) { - array_push ($out, array ("class" => "", "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ())); - } else { - array_push ($out, array ("class" => "", "title" => $entry->title, "navlink" => $entry->getNavLink ())); - } - $i++; - if ($i > 4) { break; }; - } - } - } - $d++; - if (Base::noDatabaseSelected ()) { - Base::clearDb (); - } - } - return $out; - } - $currentPage = Page::getPage ($page, $qid, $query, $n); $currentPage->InitializeContent (); + if ($search) { + return $currentPage->getContentArrayTypeahead (); + } + $out = array ( "title" => $currentPage->title); $entries = array (); foreach ($currentPage->entryArray as $entry) { diff --git a/test/bookTest.php b/test/bookTest.php index ba52a9f..d6d7860 100644 --- a/test/bookTest.php +++ b/test/bookTest.php @@ -277,10 +277,10 @@ class BookTest extends PHPUnit_Framework_TestCase $this->assertEquals ("Lewis Carroll/Alice's Adventures in Wonderland (17)/Alice's Adventures in Wonderland - Lewis Carroll.epub", $book->getFilePath ("epub", 20, true)); $this->assertEquals ("Lewis Carroll/Alice's Adventures in Wonderland (17)/Alice's Adventures in Wonderland - Lewis Carroll.mobi", $book->getFilePath ("mobi", 17, true)); } - + public function testGetDataFormat () { $book = Book::getBookById(17); - + // Get Alice MOBI=>17, PDF=>19, EPUB=>20 $data = $book->getDataFormat ("EPUB"); $this->assertEquals (20, $data->id); @@ -288,12 +288,13 @@ class BookTest extends PHPUnit_Framework_TestCase $this->assertEquals (17, $data->id); $data = $book->getDataFormat ("PDF"); $this->assertEquals (19, $data->id); - + $this->assertNull ($book->getDataFormat ("FB2")); } public function testTypeaheadSearch () { + $_GET["page"] = Base::PAGE_OPENSEARCH_QUERY; $_GET["query"] = "fic"; $_GET["search"] = "1"; @@ -343,6 +344,7 @@ class BookTest extends PHPUnit_Framework_TestCase public function testTypeaheadSearchMultiDatabase () { global $config; + $_GET["page"] = Base::PAGE_OPENSEARCH_QUERY; $_GET["query"] = "art"; $_GET["search"] = "1"; $_GET["multi"] = "1"; diff --git a/util.js b/util.js index 4f7e907..01c3b73 100644 --- a/util.js +++ b/util.js @@ -293,7 +293,7 @@ updatePage = function (data) { limit: 24, template: templateSuggestion, remote: { - url: 'getJSON.php?search=1&db=%DB&query=%QUERY', + url: 'getJSON.php?page=9&search=1&db=%DB&query=%QUERY', replace: function (url, query) { if (currentData.multipleDatabase === 1 && currentData.databaseId === "") { return url.replace('%QUERY', query).replace('&db=%DB', "");