Add author split by starting letter.

TODO : some code refactoring
This commit is contained in:
Sébastien Lucas 2012-09-29 14:45:27 +02:00
parent 14b06905d5
commit 4d760489dd
3 changed files with 65 additions and 3 deletions

View File

@ -27,7 +27,10 @@ class Author extends Base {
public function getEntryId () {
return self::ALL_AUTHORS_ID.":".$this->id;
}
public static function getEntryIdByLetter ($startingLetter) {
return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
}
public static function getCount() {
$nAuthors = parent::getDb ()->query('select count(*) from authors')->fetchColumn();
@ -37,6 +40,39 @@ class Author extends Base {
return $entry;
}
public static function getAllAuthorsByFirstLetter() {
$result = parent::getDb ()->query('select substr (upper (sort), 1, 1) as title, count(*) as count
from authors
group by substr (upper (sort), 1, 1)
order by substr (upper (sort), 1, 1)');
$entryArray = array();
while ($post = $result->fetchObject ())
{
array_push ($entryArray, new Entry ($post->title, Author::getEntryIdByLetter ($post->title),
str_format (localize("authorword.many"), $post->count), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_AUTHORS_FIRST_LETTER."&id=".$post->title))));
}
return $entryArray;
}
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
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() {
$result = parent::getDb ()->query('select authors.id as id, authors.name as name, authors.sort as sort, count(*) as count
from authors, books_authors_link

View File

@ -200,6 +200,8 @@ class Page
switch ($pageId) {
case Base::PAGE_ALL_AUTHORS :
return new PageAllAuthors ($id, $query, $n);
case Base::PAGE_AUTHORS_FIRST_LETTER :
return new PageAllAuthorsLetter ($id, $query, $n);
case Base::PAGE_AUTHOR_DETAIL :
return new PageAuthorDetail ($id, $query, $n);
case Base::PAGE_ALL_TAGS :
@ -276,12 +278,30 @@ class PageAllAuthors extends Page
{
public function InitializeContent ()
{
global $config;
$this->title = localize("authors.title");
$this->entryArray = Author::getAllAuthors();
if ($config['cops_author_split_first_letter'] == 1) {
$this->entryArray = Author::getAllAuthorsByFirstLetter();
}
else {
$this->entryArray = Author::getAllAuthors();
}
$this->idPage = Author::ALL_AUTHORS_ID;
}
}
class PageAllAuthorsLetter extends Page
{
public function InitializeContent ()
{
global $config;
$this->idPage = Author::getEntryIdByLetter ($this->idGet);
$this->entryArray = Author::getAuthorsByStartingLetter ($this->idGet);
}
}
class PageAuthorDetail extends Page
{
public function InitializeContent ()

View File

@ -100,8 +100,14 @@
/*
* Max number of items per page
* -1 unlimited
*/
$config['cops_max_item_per_page'] = "-1";
/*
* split authors by first letter
* 1 : Yes
* 0 : No
*/
$config['cops_author_split_first_letter'] = "1";
?>