3  * COPS (Calibre OPDS PHP Server) class file
 
   5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 
   6  * @author     Sébastien Lucas <sebastien@slucas.fr>
 
   9 require_once('base.php');
 
  11 class Author extends Base {
 
  12     const ALL_AUTHORS_ID = "calibre:authors";
 
  14     const AUTHOR_COLUMNS = "authors.id as id, authors.name as name, authors.sort as sort, link.count as count";
 
  15     const SQL_AUTHORS_BY_FIRST_LETTER = "select {0} from authors, (select author,count(*) as count from books_authors_link group by author) link where link.author = authors.id and authors.sort like ? order by authors.sort";
 
  16     const SQL_ALL_AUTHORS = "select {0} from authors, (select author,count(*) as count from books_authors_link group by author) link where link.author = authors.id order by authors.sort ";
 
  22     public function __construct($pid, $pname) {
 
  27     public function getUri () {
 
  28         return "?page=".parent::PAGE_AUTHOR_DETAIL."&id=$this->id";
 
  31     public function getEntryId () {
 
  32         return self::ALL_AUTHORS_ID.":".$this->id;
 
  35     public static function getEntryIdByLetter ($startingLetter) {
 
  36         return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
 
  39     public static function getCount() {
 
  40         $nAuthors = parent::getDb ()->query('select count(*) from authors')->fetchColumn(0);
 
  41         $entry = new Entry (localize("authors.title"), self::ALL_AUTHORS_ID, 
 
  42             str_format (localize("authors.alphabetical", $nAuthors), $nAuthors), "text", 
 
  43             array ( new LinkNavigation ("?page=".parent::PAGE_AUTHORS_FIRST_LETTER)));
 
  47     public static function getAuthorsByFirstLetter($letter) {
 
  48         if (!$letter) { $letter = ""; }
 
  49         $len = mb_strlen($letter,'UTF-8')+1;
 
  50         $result = parent::getDb ()->query('select substring(sort, 1, '.$len.') as title, count(distinct sort) sort_cnt,count(*) as count,min(id) min_id,min(sort) min_sort
 
  52 where sort like "'.$letter.'%"
 
  53 group by substring(sort, 1, '.$len.')
 
  54 order by substring(sort, 1, '.$len.')');
 
  55         $entryArray = array();
 
  56         while ($post = $result->fetchObject ())
 
  58             if ($post->count==1) {
 
  59               $author = new Author ($post->min_id, $post->min_sort);
 
  60               $title = $post->min_sort;
 
  61               $entryid = $author->getEntryId();
 
  62               $link = array ( new LinkNavigation ($author->getUri ()));
 
  63             } elseif ($post->count > parent::maxItemsPerPage() && $post->sort_cnt>1) {
 
  64               $page = parent::PAGE_AUTHORS_FIRST_LETTER;
 
  65               $title= $post->title.'...';
 
  66               $entryid = Author::getEntryIdByLetter ($post->title);
 
  67               $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->title)));
 
  69               $page = parent::PAGE_AUTHORS_STARTING_LETTERS;
 
  70               $title = $post->title.'...';
 
  71               $entryid = Author::getEntryIdByLetter ($post->title);
 
  72               $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->title)));            
 
  74             array_push ($entryArray, new Entry ($title, $entryid, 
 
  75                 str_format (localize("authorword", $post->count), $post->count), "text", 
 
  82     public static function getAuthorsByStartingLetter($letter) {
 
  83         return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
 
  86     public static function getAllAuthors() {
 
  87         return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
 
  90     public static function getEntryArray ($query, $params) {
 
  91         list ($totalNumber, $result) = parent::executeQuery ($query, self::AUTHOR_COLUMNS, "", $params, -1);
 
  92         $entryArray = array();
 
  93         while ($post = $result->fetchObject ())
 
  95             $author = new Author ($post->id, $post->sort);
 
  96             array_push ($entryArray, new Entry ($post->sort, $author->getEntryId (), 
 
  97                 str_format (localize("bookword", $post->count), $post->count), "text", 
 
  98                 array ( new LinkNavigation ($author->getUri ()))));
 
 103     public static function getAuthorById ($authorId) {
 
 104         $result = parent::getDb ()->prepare('select sort from authors where id = ?');
 
 105         $result->execute (array ($authorId));
 
 106         return new Author ($authorId, $result->fetchColumn (0));
 
 109     public static function getAuthorByBookId ($bookId) {
 
 110         $result = parent::getDb ()->prepare('select authors.id as id, authors.sort as sort
 
 111 from authors, books_authors_link
 
 112 where author = authors.id
 
 114         $result->execute (array ($bookId));
 
 115         $authorArray = array ();
 
 116         while ($post = $result->fetchObject ()) {
 
 117             array_push ($authorArray, new Author ($post->id, $post->sort));