Integrate a little better the typeahead search in the normal search page. Heavily based on At-Libitum work. re #34

This commit is contained in:
Sébastien Lucas 2013-12-22 18:40:03 +01:00
parent ebbd830a33
commit 9a15d0e1a4
4 changed files with 122 additions and 92 deletions

111
base.php
View file

@ -281,6 +281,7 @@ class Entry
public $contentType; public $contentType;
public $linkArray; public $linkArray;
public $localUpdated; public $localUpdated;
public $className;
private static $updated = NULL; private static $updated = NULL;
public static $icons = array( public static $icons = array(
@ -318,13 +319,14 @@ class Entry
return array ( "title" => $this->title, "content" => $this->content, "navlink" => $this->getNavLink () ); 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; global $config;
$this->title = $ptitle; $this->title = $ptitle;
$this->id = $pid; $this->id = $pid;
$this->content = $pcontent; $this->content = $pcontent;
$this->contentType = $pcontentType; $this->contentType = $pcontentType;
$this->linkArray = $plinkArray; $this->linkArray = $plinkArray;
$this->className = $pclass;
if ($config['cops_show_icons'] == 1) if ($config['cops_show_icons'] == 1)
{ {
@ -468,7 +470,6 @@ class Page
Base::clearDb (); Base::clearDb ();
} }
} else { } else {
//error_log (var_dump (getCurrentOption ('ignored_categories')));
if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) { if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) {
array_push ($this->entryArray, Author::getCount()); array_push ($this->entryArray, Author::getCount());
} }
@ -539,6 +540,22 @@ class Page
return false; 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 class PageAllAuthors extends Page
@ -732,6 +749,90 @@ class PageQueryResult extends Page
const SCOPE_BOOK = "book"; const SCOPE_BOOK = "book";
const SCOPE_PUBLISHER = "publisher"; 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 () public function InitializeContent ()
{ {
$scope = getURLParam ("scope"); $scope = getURLParam ("scope");
@ -750,7 +851,7 @@ class PageQueryResult extends Page
$crit = "%" . $this->query . "%"; $crit = "%" . $this->query . "%";
// Special case when we are doing a search and no database is selected // Special case when we are doing a search and no database is selected
if (Base::noDatabaseSelected ()) { if (Base::noDatabaseSelected () && !$this->useTypeahead ()) {
$i = 0; $i = 0;
foreach (Base::getDbNameList () as $key) { foreach (Base::getDbNameList () as $key) {
Base::clearDb (); Base::clearDb ();
@ -762,6 +863,10 @@ class PageQueryResult extends Page
} }
return; return;
} }
if (empty ($scope) && $this->useTypeahead ()) {
$this->doSearchByCategory ();
return;
}
switch ($scope) { switch ($scope) {
case self::SCOPE_AUTHOR : case self::SCOPE_AUTHOR :
$this->entryArray = Author::getAuthorsByStartingLetter ('%' . $this->query); $this->entryArray = Author::getAuthorsByStartingLetter ('%' . $this->query);

View file

@ -671,90 +671,13 @@ function getJson ($complete = false) {
$n = getURLParam ("n", "1"); $n = getURLParam ("n", "1");
$database = GetUrlParam (DB); $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 = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent (); $currentPage->InitializeContent ();
if ($search) {
return $currentPage->getContentArrayTypeahead ();
}
$out = array ( "title" => $currentPage->title); $out = array ( "title" => $currentPage->title);
$entries = array (); $entries = array ();
foreach ($currentPage->entryArray as $entry) { foreach ($currentPage->entryArray as $entry) {

View file

@ -294,6 +294,7 @@ class BookTest extends PHPUnit_Framework_TestCase
public function testTypeaheadSearch () public function testTypeaheadSearch ()
{ {
$_GET["page"] = Base::PAGE_OPENSEARCH_QUERY;
$_GET["query"] = "fic"; $_GET["query"] = "fic";
$_GET["search"] = "1"; $_GET["search"] = "1";
@ -343,6 +344,7 @@ class BookTest extends PHPUnit_Framework_TestCase
public function testTypeaheadSearchMultiDatabase () public function testTypeaheadSearchMultiDatabase ()
{ {
global $config; global $config;
$_GET["page"] = Base::PAGE_OPENSEARCH_QUERY;
$_GET["query"] = "art"; $_GET["query"] = "art";
$_GET["search"] = "1"; $_GET["search"] = "1";
$_GET["multi"] = "1"; $_GET["multi"] = "1";

View file

@ -293,7 +293,7 @@ updatePage = function (data) {
limit: 24, limit: 24,
template: templateSuggestion, template: templateSuggestion,
remote: { 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) { replace: function (url, query) {
if (currentData.multipleDatabase === 1 && currentData.databaseId === "") { if (currentData.multipleDatabase === 1 && currentData.databaseId === "") {
return url.replace('%QUERY', query).replace('&db=%DB', ""); return url.replace('%QUERY', query).replace('&db=%DB', "");