Author can now be searched by sort or by name (Carroll, Lewis or Lewis Carroll will work). fix #155

This commit is contained in:
Sébastien Lucas 2014-04-30 15:10:37 +02:00
parent d45bbb7ff6
commit 15e78a852a
3 changed files with 42 additions and 1 deletions

View file

@ -13,6 +13,7 @@ class Author extends Base {
const AUTHOR_COLUMNS = "authors.id as id, authors.name as name, authors.sort as sort, count(*) as count"; const AUTHOR_COLUMNS = "authors.id as id, authors.name as name, authors.sort as sort, count(*) as count";
const SQL_AUTHORS_BY_FIRST_LETTER = "select {0} from authors, books_authors_link where author = authors.id and upper (authors.sort) like ? group by authors.id, authors.name, authors.sort order by sort"; const SQL_AUTHORS_BY_FIRST_LETTER = "select {0} from authors, books_authors_link where author = authors.id and upper (authors.sort) like ? group by authors.id, authors.name, authors.sort order by sort";
const SQL_AUTHORS_FOR_SEARCH = "select {0} from authors, books_authors_link where author = authors.id and (upper (authors.sort) like ? or upper (authors.name) like ?) group by authors.id, authors.name, authors.sort order by sort";
const SQL_ALL_AUTHORS = "select {0} from authors, books_authors_link where author = authors.id group by authors.id, authors.name, authors.sort order by sort"; const SQL_ALL_AUTHORS = "select {0} from authors, books_authors_link where author = authors.id group by authors.id, authors.name, authors.sort order by sort";
public $id; public $id;
@ -64,6 +65,10 @@ order by substr (upper (sort), 1, 1)');
return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%")); return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
} }
public static function getAuthorsForSearch($query) {
return self::getEntryArray (self::SQL_AUTHORS_FOR_SEARCH, array ($query . "%", $query . "%"));
}
public static function getAllAuthors() { public static function getAllAuthors() {
return self::getEntryArray (self::SQL_ALL_AUTHORS, array ()); return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
} }

View file

@ -801,7 +801,7 @@ class PageQueryResult extends Page
$array = Book::getBooksByStartingLetter ('%' . $this->query, $n, NULL, $numberPerPage); $array = Book::getBooksByStartingLetter ('%' . $this->query, $n, NULL, $numberPerPage);
break; break;
case self::SCOPE_AUTHOR : case self::SCOPE_AUTHOR :
$array = Author::getAuthorsByStartingLetter ('%' . $this->query); $array = Author::getAuthorsForSearch ('%' . $this->query);
break; break;
case self::SCOPE_SERIES : case self::SCOPE_SERIES :
$array = Serie::getAllSeriesByQuery ($this->query); $array = Serie::getAllSeriesByQuery ($this->query);

View file

@ -681,6 +681,42 @@ class PageTest extends PHPUnit_Framework_TestCase
$this->assertFalse ($currentPage->ContainsBook ()); $this->assertFalse ($currentPage->ContainsBook ());
} }
public function testAuthorSearch_ByName ()
{
global $config;
$page = Base::PAGE_OPENSEARCH_QUERY;
$query = "Lewis Carroll";
$_GET ["scope"] = "author";
$qid = NULL;
$n = "1";
$currentPage = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent ();
$this->assertEquals ("Search result for *Lewis Carroll* in authors", $currentPage->title);
$this->assertCount (1, $currentPage->entryArray);
$this->assertEquals ("Carroll, Lewis", $currentPage->entryArray [0]->title);
$this->assertFalse ($currentPage->ContainsBook ());
}
public function testAuthorSearch_BySort ()
{
global $config;
$page = Base::PAGE_OPENSEARCH_QUERY;
$query = "Carroll, Lewis";
$_GET ["scope"] = "author";
$qid = NULL;
$n = "1";
$currentPage = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent ();
$this->assertEquals ("Search result for *Carroll, Lewis* in authors", $currentPage->title);
$this->assertCount (1, $currentPage->entryArray);
$this->assertEquals ("Carroll, Lewis", $currentPage->entryArray [0]->title);
$this->assertFalse ($currentPage->ContainsBook ());
}
public function testPageSearchScopeAuthors () public function testPageSearchScopeAuthors ()
{ {
$page = Base::PAGE_OPENSEARCH_QUERY; $page = Base::PAGE_OPENSEARCH_QUERY;