cops/author.php

112 lines
4.1 KiB
PHP
Raw Normal View History

2012-05-28 08:01:33 +03:00
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author S<EFBFBD>bastien Lucas <sebastien@slucas.fr>
*/
require_once('base.php');
class Author extends Base {
const ALL_AUTHORS_ID = "calibre:authors";
public $id;
public $name;
public $sort;
public function __construct($pid, $pname) {
$this->id = $pid;
$this->name = $pname;
}
public function getUri () {
2012-05-28 08:06:12 +03:00
return "?page=".parent::PAGE_AUTHOR_DETAIL."&id=$this->id";
2012-05-28 08:01:33 +03:00
}
public function getEntryId () {
return self::ALL_AUTHORS_ID.":".$this->id;
}
public static function getEntryIdByLetter ($startingLetter) {
return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
}
2012-05-28 08:01:33 +03:00
public static function getCount() {
$nAuthors = parent::getDb ()->query('select count(*) from authors')->fetchColumn();
$entry = new Entry (localize("authors.title"), self::ALL_AUTHORS_ID,
str_format (localize("authors.alphabetical"), $nAuthors), "text",
2012-05-28 08:06:12 +03:00
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_AUTHORS)));
2012-05-28 08:05:05 +03:00
return $entry;
2012-05-28 08:01:33 +03:00
}
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;
}
2012-05-28 08:01:33 +03:00
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
where author = authors.id
group by authors.id, authors.name, authors.sort
order by sort');
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
while ($post = $result->fetchObject ())
{
$author = new Author ($post->id, $post->sort);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, new Entry ($post->sort, $author->getEntryId (),
str_format (localize("bookword.many"), $post->count), "text",
2012-05-28 08:01:33 +03:00
array ( new LinkNavigation ($author->getUri ()))));
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:07:49 +03:00
public static function getAuthorById ($authorId) {
2012-05-28 08:01:33 +03:00
$result = parent::getDb ()->prepare('select sort from authors where id = ?');
$result->execute (array ($authorId));
2012-05-28 08:07:49 +03:00
return new Author ($authorId, $result->fetchColumn ());
2012-05-28 08:01:33 +03:00
}
public static function getAuthorByBookId ($bookId) {
$result = parent::getDb ()->prepare('select authors.id as id, authors.sort as sort
from authors, books_authors_link
where author = authors.id
and book = ?');
$result->execute (array ($bookId));
$authorArray = array ();
while ($post = $result->fetchObject ()) {
array_push ($authorArray, new Author ($post->id, $post->sort));
}
return $authorArray;
}
}
?>