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.5KB

  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 CustomColumn extends Base {
  10. const ALL_CUSTOMS_ID = "cops:custom";
  11. public $id;
  12. public $name;
  13. public $customId;
  14. public function __construct($pid, $pname, $pcustomId) {
  15. $this->id = $pid;
  16. $this->name = $pname;
  17. $this->customId = $pcustomId;
  18. }
  19. public function getUri () {
  20. return "?page=".parent::PAGE_CUSTOM_DETAIL."&custom={$this->customId}&id={$this->id}";
  21. }
  22. public function getEntryId () {
  23. return self::ALL_CUSTOMS_ID.":".$this->customId.":".$this->id;
  24. }
  25. public static function getTableName ($customId) {
  26. return "custom_column_{$customId}";
  27. }
  28. public static function getTableLinkName ($customId) {
  29. return "books_custom_column_{$customId}_link";
  30. }
  31. public static function getTableLinkColumn ($customId) {
  32. return "value";
  33. }
  34. public static function getAllCustomsId ($customId) {
  35. return self::ALL_CUSTOMS_ID . ":" . $customId;
  36. }
  37. public static function getUriAllCustoms ($customId) {
  38. return "?page=" . parent::PAGE_ALL_CUSTOMS . "&custom={$customId}";
  39. }
  40. public static function getAllTitle ($customId) {
  41. $result = parent::getDb ()->prepare('select name from custom_columns where id = ?');
  42. $result->execute (array ($customId));
  43. $post = $result->fetchObject ();
  44. return $post->name;
  45. }
  46. public static function getCustomId ($lookup) {
  47. $result = parent::getDb ()->prepare('select id from custom_columns where label = ?');
  48. $result->execute (array ($lookup));
  49. if ($post = $result->fetchObject ()) {
  50. return $post->id;
  51. }
  52. return NULL;
  53. }
  54. public static function getCount($customId) {
  55. $nCustoms = parent::executeQuerySingle ('select count(*) from ' . self::getTableName ($customId));
  56. $entry = new Entry (self::getAllTitle ($customId), self::getAllCustomsId ($customId),
  57. str_format (localize("tags.alphabetical", $nCustoms), $nCustoms), "text",
  58. array ( new LinkNavigation (self::getUriAllCustoms ($customId))), "", $nCustoms);
  59. return $entry;
  60. }
  61. public static function getCustomById ($customId, $id) {
  62. $result = parent::getDb ()->prepare('select id, value as name from ' . self::getTableName ($customId) . ' where id = ?');
  63. $result->execute (array ($id));
  64. if ($post = $result->fetchObject ()) {
  65. return new CustomColumn ($post->id, $post->name, $customId);
  66. }
  67. return NULL;
  68. }
  69. public static function getAllCustoms($customId) {
  70. $result = parent::getDb ()->query(str_format ('select {0}.id as id, {0}.value as name, count(*) as count
  71. from {0}, {1}
  72. where {0}.id = {1}.{2}
  73. group by {0}.id, {0}.value
  74. order by {0}.value', self::getTableName ($customId), self::getTableLinkName ($customId), self::getTableLinkColumn ($customId)));
  75. $entryArray = array();
  76. while ($post = $result->fetchObject ())
  77. {
  78. $customColumn = new CustomColumn ($post->id, $post->name, $customId);
  79. array_push ($entryArray, new Entry ($customColumn->name, $customColumn->getEntryId (),
  80. str_format (localize("bookword", $post->count), $post->count), "text",
  81. array ( new LinkNavigation ($customColumn->getUri ())), "", $post->count));
  82. }
  83. return $entryArray;
  84. }
  85. }