Author split is finished with this little refactoring. fix #13

This commit is contained in:
Sébastien Lucas 2012-09-30 14:39:53 +02:00
parent da7bb34a2c
commit 14199cf9b3
2 changed files with 11 additions and 21 deletions

View file

@ -11,6 +11,10 @@ require_once('base.php');
class Author extends Base { class Author extends Base {
const ALL_AUTHORS_ID = "calibre:authors"; const ALL_AUTHORS_ID = "calibre:authors";
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_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;
public $name; public $name;
public $sort; public $sort;
@ -56,29 +60,15 @@ order by substr (upper (sort), 1, 1)');
} }
public static function getAuthorsByStartingLetter($letter) { public static function getAuthorsByStartingLetter($letter) {
$result = parent::getDb ()->prepare('select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
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');
$entryArray = array();
$result->execute (array ($letter . "%"));
while ($post = $result->fetchObject ())
{
$author = new Author ($post->id, $post->sort);
array_push ($entryArray, new Entry ($post->sort, $author->getEntryId (),
str_format (localize("bookword.many"), $post->count), "text",
array ( new LinkNavigation ($author->getUri ()))));
}
return $entryArray;
} }
public static function getAllAuthors() { public static function getAllAuthors() {
$result = parent::getDb ()->query('select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
from authors, books_authors_link }
where author = authors.id
group by authors.id, authors.name, authors.sort public static function getEntryArray ($query, $params) {
order by sort'); list ($totalNumber, $result) = parent::executeQuery ($query, self::AUTHOR_COLUMNS, $params, -1);
$entryArray = array(); $entryArray = array();
while ($post = $result->fetchObject ()) while ($post = $result->fetchObject ())
{ {

View file

@ -431,7 +431,7 @@ abstract class Base
global $config; global $config;
$totalResult = -1; $totalResult = -1;
if ($config['cops_max_item_per_page'] != -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(*)")); $result = self::getDb ()->prepare (str_format ($query, "count(*)"));