Fix search when no database is selected. re #40

This commit is contained in:
Sébastien Lucas 2013-04-29 16:46:52 +02:00
parent b3cd76d0b5
commit 33cad7ad98
2 changed files with 29 additions and 9 deletions

View file

@ -576,8 +576,24 @@ class PageQueryResult extends Page
{ {
public function InitializeContent () public function InitializeContent ()
{ {
global $config;
$this->title = str_format (localize ("search.result"), $this->query); $this->title = str_format (localize ("search.result"), $this->query);
$currentPage = getURLParam ("current", NULL); $currentPage = getURLParam ("current", NULL);
// Special case when we are doing a search and no database is selected
if (is_array ($config['calibre_directory']) && is_null (GetUrlParam (DB))) {
$i = 0;
foreach ($config['calibre_directory'] as $key => $value) {
Base::clearDb ();
list ($array, $totalNumber) = Book::getBooksByQuery ($this->query, $this->n, $i);
array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
count($array) . " book found", "text",
array ( new LinkNavigation ("?" . DB . "={$i}&page=9&query=" . $this->query))));
$i++;
}
return;
}
switch ($currentPage) { switch ($currentPage) {
case Base::PAGE_ALL_AUTHORS : case Base::PAGE_ALL_AUTHORS :
case Base::PAGE_AUTHORS_FIRST_LETTER : case Base::PAGE_AUTHORS_FIRST_LETTER :
@ -665,11 +681,11 @@ abstract class Base
return self::getDbDirectory ($database) .'metadata.db'; return self::getDbDirectory ($database) .'metadata.db';
} }
public static function getDb () { public static function getDb ($database = NULL) {
global $config; global $config;
if (is_null (self::$db)) { if (is_null (self::$db)) {
try { try {
self::$db = new PDO('sqlite:'. self::getDbFileName ()); self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
} catch (Exception $e) { } catch (Exception $e) {
header("location: checkconfig.php?err=1"); header("location: checkconfig.php?err=1");
exit(); exit();
@ -678,14 +694,18 @@ abstract class Base
return self::$db; return self::$db;
} }
public static function executeQuery($query, $columns, $filter, $params, $n) { public static function clearDb () {
self::$db = NULL;
}
public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL) {
global $config; global $config;
$totalResult = -1; $totalResult = -1;
if ($config['cops_max_item_per_page'] != -1 && $n != -1) if ($config['cops_max_item_per_page'] != -1 && $n != -1)
{ {
// First check total number of results // First check total number of results
$result = self::getDb ()->prepare (str_format ($query, "count(*)", $filter)); $result = self::getDb ($database)->prepare (str_format ($query, "count(*)", $filter));
$result->execute ($params); $result->execute ($params);
$totalResult = $result->fetchColumn (); $totalResult = $result->fetchColumn ();
@ -694,7 +714,7 @@ abstract class Base
array_push ($params, ($n - 1) * $config['cops_max_item_per_page'], $config['cops_max_item_per_page']); array_push ($params, ($n - 1) * $config['cops_max_item_per_page'], $config['cops_max_item_per_page']);
} }
$result = self::getDb ()->prepare(str_format ($query, $columns, $filter)); $result = self::getDb ($database)->prepare(str_format ($query, $columns, $filter));
$result->execute ($params); $result->execute ($params);
return array ($totalResult, $result); return array ($totalResult, $result);
} }

View file

@ -427,8 +427,8 @@ where data.book = books.id and data.id = ?');
return NULL; return NULL;
} }
public static function getBooksByQuery($query, $n) { public static function getBooksByQuery($query, $n, $database = NULL) {
return self::getEntryArray (self::SQL_BOOKS_QUERY, array ("%" . $query . "%", "%" . $query . "%"), $n); return self::getEntryArray (self::SQL_BOOKS_QUERY, array ("%" . $query . "%", "%" . $query . "%"), $n, $database);
} }
public static function getAllBooks() { public static function getAllBooks() {
@ -450,8 +450,8 @@ order by substr (upper (sort), 1, 1)");
return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n); return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n);
} }
public static function getEntryArray ($query, $params, $n) { public static function getEntryArray ($query, $params, $n, $database = NULL) {
list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n); list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database);
$entryArray = array(); $entryArray = array();
while ($post = $result->fetchObject ()) while ($post = $result->fetchObject ())
{ {