Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...) http://blog.slucas.fr/en/oss/calibre-opds-php-server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.9KB

  1. <?php
  2. /**
  3. * COPS (Calibre OPDS PHP Server) class file
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Sébastien Lucas <sebastien@slucas.fr>
  7. */
  8. require_once('base.php');
  9. class Author extends Base {
  10. const ALL_AUTHORS_ID = "cops:authors";
  11. const AUTHOR_COLUMNS = "authors.id as id, authors.name as name, authors.sort as sort, count(*) as count";
  12. 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";
  13. 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";
  14. public $id;
  15. public $name;
  16. public $sort;
  17. public function __construct($pid, $pname) {
  18. $this->id = $pid;
  19. $this->name = $pname;
  20. }
  21. public function getUri () {
  22. return "?page=".parent::PAGE_AUTHOR_DETAIL."&id=$this->id";
  23. }
  24. public function getEntryId () {
  25. return self::ALL_AUTHORS_ID.":".$this->id;
  26. }
  27. public static function getEntryIdByLetter ($startingLetter) {
  28. return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
  29. }
  30. public static function getCount() {
  31. $nAuthors = parent::getDb ()->query('select count(*) from authors')->fetchColumn();
  32. $entry = new Entry (localize("authors.title"), self::ALL_AUTHORS_ID,
  33. str_format (localize("authors.alphabetical", $nAuthors), $nAuthors), "text",
  34. array ( new LinkNavigation ("?page=".parent::PAGE_ALL_AUTHORS)));
  35. return $entry;
  36. }
  37. public static function getAllAuthorsByFirstLetter() {
  38. $result = parent::getDb ()->query('select substr (upper (sort), 1, 1) as title, count(*) as count
  39. from authors
  40. group by substr (upper (sort), 1, 1)
  41. order by substr (upper (sort), 1, 1)');
  42. $entryArray = array();
  43. while ($post = $result->fetchObject ())
  44. {
  45. array_push ($entryArray, new Entry ($post->title, Author::getEntryIdByLetter ($post->title),
  46. str_format (localize("authorword", $post->count), $post->count), "text",
  47. array ( new LinkNavigation ("?page=".parent::PAGE_AUTHORS_FIRST_LETTER."&id=". rawurlencode ($post->title)))));
  48. }
  49. return $entryArray;
  50. }
  51. public static function getAuthorsByStartingLetter($letter) {
  52. return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
  53. }
  54. public static function getAllAuthors() {
  55. return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
  56. }
  57. public static function getEntryArray ($query, $params) {
  58. list (, $result) = parent::executeQuery ($query, self::AUTHOR_COLUMNS, "", $params, -1);
  59. $entryArray = array();
  60. while ($post = $result->fetchObject ())
  61. {
  62. $author = new Author ($post->id, $post->sort);
  63. array_push ($entryArray, new Entry ($post->sort, $author->getEntryId (),
  64. str_format (localize("bookword", $post->count), $post->count), "text",
  65. array ( new LinkNavigation ($author->getUri ()))));
  66. }
  67. return $entryArray;
  68. }
  69. public static function getAuthorById ($authorId) {
  70. $result = parent::getDb ()->prepare('select sort from authors where id = ?');
  71. $result->execute (array ($authorId));
  72. return new Author ($authorId, $result->fetchColumn ());
  73. }
  74. public static function getAuthorByBookId ($bookId) {
  75. $result = parent::getDb ()->prepare('select authors.id as id, authors.sort as sort
  76. from authors, books_authors_link
  77. where author = authors.id
  78. and book = ?');
  79. $result->execute (array ($bookId));
  80. $authorArray = array ();
  81. while ($post = $result->fetchObject ()) {
  82. array_push ($authorArray, new Author ($post->id, $post->sort));
  83. }
  84. return $authorArray;
  85. }
  86. }