From 4473037ae8ebbedd6a588a854826ddf67e992435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Tue, 26 Jun 2012 14:27:06 +0200 Subject: [PATCH] Move link handling in a seperate class --- book.php | 59 +++++++++++++++++++---------------------------- data.php | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 data.php diff --git a/book.php b/book.php index 332681d..3a9a071 100644 --- a/book.php +++ b/book.php @@ -10,6 +10,7 @@ require_once('base.php'); require_once('serie.php'); require_once('author.php'); require_once('tag.php'); +require_once('data.php'); class Book extends Base { const ALL_BOOKS_UUID = "urn:uuid"; @@ -27,15 +28,12 @@ class Book extends Base { public $relativePath; public $seriesIndex; public $comment; + public $datas = NULL; public $authors = NULL; public $serie = NULL; public $tags = NULL; public $format = array (); - public static $mimetypes = array( - 'epub' => 'application/epub+zip', - 'mobi' => 'application/x-mobipocket-ebook', - 'pdf' => 'application/pdf' - ); + public function __construct($line) { global $config; @@ -109,6 +107,23 @@ class Book extends Base { return $this->tags; } + public function getDatas () + { + if (is_null ($this->datas)) { + $this->datas = array (); + + $result = parent::getDb ()->prepare('select id, format, name + from data where book = ?'); + $result->execute (array ($this->id)); + + while ($post = $result->fetchObject ()) + { + array_push ($this->datas, new Data ($post, $this)); + } + } + return $this->datas; + } + public function getTagsName () { $tagList = null; foreach ($this->getTags () as $tag) { @@ -168,26 +183,6 @@ class Book extends Base { } } - private function getLink ($type, $mime, $rel, $filename, $idData, $title = NULL) - { - global $config; - - $textData = ""; - if (!is_null ($idData)) - { - $textData = "&data=" . $idData; - } - - if (preg_match ('/^\//', $config['calibre_directory'])) - { - return new Link ("fetch.php?id=$this->id" . $textData . "&type=" . $type, $mime, $rel, $title); - } - else - { - return new Link (str_replace('%2F','/',rawurlencode ($this->path."/".$filename)), $mime, $rel, $title); - } - } - public function getLinkArray () { global $config; @@ -195,7 +190,7 @@ class Book extends Base { if ($this->hasCover) { - array_push ($linkArray, $this->getLink ("jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)); + array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)); $height = "50"; if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) { $height = $config['cops_opds_thumbnail_height']; @@ -207,17 +202,11 @@ class Book extends Base { array_push ($linkArray, new Link ("fetch.php?id=$this->id&height=" . $height, "image/jpeg", Link::OPDS_THUMBNAIL_TYPE)); } - $result = parent::getDb ()->prepare('select id, format, name -from data where book = ?'); - $result->execute (array ($this->id)); - - while ($post = $result->fetchObject ()) + foreach ($this->getDatas () as $data) { - $ext = strtolower (str_replace ("ORIGINAL_", "", $post->format)); - if (array_key_exists ($ext, self::$mimetypes)) + if ($data->isKnownType ()) { - array_push ($linkArray, $this->getLink ($ext, self::$mimetypes [$ext], Link::OPDS_ACQUISITION_TYPE, $post->name . "." . strtolower ($post->format), $post->id, "Download")); - $this->format [$post->format] = array ($post->id, $post->name . "." . strtolower ($post->format)); + array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, "Download")); } } diff --git a/data.php b/data.php new file mode 100644 index 0000000..815e6d5 --- /dev/null +++ b/data.php @@ -0,0 +1,70 @@ + + */ + +require_once('base.php'); + +class Data extends Base { + public $id; + public $name; + public $format; + public $realFormat; + public $extension; + public $book; + + public static $mimetypes = array( + 'epub' => 'application/epub+zip', + 'mobi' => 'application/x-mobipocket-ebook', + 'pdf' => 'application/pdf' + ); + + public function __construct($post, $book = null) { + $this->id = $post->id; + $this->name = $post->name; + $this->format = $post->format; + $this->realFormat = str_replace ("ORIGINAL_", "", $post->format); + $this->extension = strtolower ($this->realFormat); + $this->book = $book; + } + + public function isKnownType () { + return array_key_exists ($this->extension, self::$mimetypes); + } + + public function getMimeType () { + return self::$mimetypes [$this->extension]; + } + + public function getFilename () { + $this->name . "." . strtolower ($this->format); + } + + public function getDataLink ($rel, $title = NULL) { + return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title); + } + + public static function getLink ($book, $type, $mime, $rel, $filename, $idData, $title = NULL) + { + global $config; + + $textData = ""; + if (!is_null ($idData)) + { + $textData = "&data=" . $idData; + } + + if (preg_match ('/^\//', $config['calibre_directory'])) + { + return new Link ("fetch.php?id=$book->id" . $textData . "&type=" . $type, $mime, $rel, $title); + } + else + { + return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title); + } + } +} +?> \ No newline at end of file