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.

96 lines
3.8KB

  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_AUTHORS_FOR_SEARCH = "select {0} from authors, books_authors_link where author = authors.id and (upper (authors.sort) like ? or upper (authors.name) like ?) group by authors.id, authors.name, authors.sort order by sort";
  14. 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";
  15. public $id;
  16. public $name;
  17. public $sort;
  18. public function __construct($post) {
  19. $this->id = $post->id;
  20. $this->name = str_replace("|", ",", $post->name);
  21. $this->sort = $post->sort;
  22. }
  23. public function getUri () {
  24. return "?page=".parent::PAGE_AUTHOR_DETAIL."&id=$this->id";
  25. }
  26. public function getEntryId () {
  27. return self::ALL_AUTHORS_ID.":".$this->id;
  28. }
  29. public static function getEntryIdByLetter ($startingLetter) {
  30. return self::ALL_AUTHORS_ID.":letter:".$startingLetter;
  31. }
  32. public static function getCount() {
  33. // str_format (localize("authors.alphabetical", count(array))
  34. return parent::getCountGeneric ("authors", self::ALL_AUTHORS_ID, parent::PAGE_ALL_AUTHORS);
  35. }
  36. public static function getAllAuthorsByFirstLetter() {
  37. list (, $result) = parent::executeQuery ("select {0}
  38. from authors
  39. group by substr (upper (sort), 1, 1)
  40. order by substr (upper (sort), 1, 1)", "substr (upper (sort), 1, 1) as title, count(*) as count", "", array (), -1);
  41. $entryArray = array();
  42. while ($post = $result->fetchObject ())
  43. {
  44. array_push ($entryArray, new Entry ($post->title, Author::getEntryIdByLetter ($post->title),
  45. str_format (localize("authorword", $post->count), $post->count), "text",
  46. array ( new LinkNavigation ("?page=".parent::PAGE_AUTHORS_FIRST_LETTER."&id=". rawurlencode ($post->title))), "", $post->count));
  47. }
  48. return $entryArray;
  49. }
  50. public static function getAuthorsByStartingLetter($letter) {
  51. return self::getEntryArray (self::SQL_AUTHORS_BY_FIRST_LETTER, array ($letter . "%"));
  52. }
  53. public static function getAuthorsForSearch($query) {
  54. return self::getEntryArray (self::SQL_AUTHORS_FOR_SEARCH, array ($query . "%", $query . "%"));
  55. }
  56. public static function getAllAuthors() {
  57. return self::getEntryArray (self::SQL_ALL_AUTHORS, array ());
  58. }
  59. public static function getEntryArray ($query, $params) {
  60. return Base::getEntryArrayWithBookNumber ($query, self::AUTHOR_COLUMNS, $params, "Author");
  61. }
  62. public static function getAuthorById ($authorId) {
  63. $result = parent::getDb ()->prepare('select ' . self::AUTHOR_COLUMNS . ' from authors where id = ?');
  64. $result->execute (array ($authorId));
  65. $post = $result->fetchObject ();
  66. return new Author ($post);
  67. }
  68. public static function getAuthorByBookId ($bookId) {
  69. $result = parent::getDb ()->prepare('select authors.id as id, authors.name as name, authors.sort as sort from authors, books_authors_link
  70. where author = authors.id
  71. and book = ?');
  72. $result->execute (array ($bookId));
  73. $authorArray = array ();
  74. while ($post = $result->fetchObject ()) {
  75. array_push ($authorArray, new Author ($post));
  76. }
  77. return $authorArray;
  78. }
  79. }