Untitled diff

Created Diff never expires
6 removals
472 lines
9 additions
473 lines
<?php
<?php
/**
/**
* COPS (Calibre OPDS PHP Server) class file
* COPS (Calibre OPDS PHP Server) class file
*
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author S�bastien Lucas <sebastien@slucas.fr>
* @author S�bastien Lucas <sebastien@slucas.fr>
*/
*/
require_once('base.php');
require_once('base.php');
require_once('serie.php');
require_once('serie.php');
require_once('author.php');
require_once('author.php');
require_once('tag.php');
require_once('tag.php');
require_once ("customcolumn.php");
require_once ("customcolumn.php");
require_once('data.php');
require_once('data.php');
require_once('resources/php-epub-meta/epub.php');
require_once('resources/php-epub-meta/epub.php');
// Silly thing because PHP forbid string concatenation in class const
// Silly thing because PHP forbid string concatenation in class const
define ('SQL_BOOKS_LEFT_JOIN', "left outer join comments on comments.book = books.id
define ('SQL_BOOKS_LEFT_JOIN', "left outer join comments on comments.book = books.id
left outer join books_ratings_link on books_ratings_link.book = books.id
left outer join books_ratings_link on books_ratings_link.book = books.id
left outer join ratings on books_ratings_link.rating = ratings.id ");
left outer join ratings on books_ratings_link.rating = ratings.id ");
define ('SQL_BOOKS_BY_FIRST_LETTER', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_BY_FIRST_LETTER', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
where upper (books.sort) like ? order by books.sort");
where upper (books.sort) like ? order by books.sort");
define ('SQL_BOOKS_BY_AUTHOR', "select {0} from books_authors_link, books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_BY_AUTHOR', "select {0} from books_authors_link, books " . SQL_BOOKS_LEFT_JOIN . "
where books_authors_link.book = books.id and author = ? {1} order by pubdate");
where books_authors_link.book = books.id and author = ? {1} order by pubdate");
define ('SQL_BOOKS_BY_SERIE', "select {0} from books_series_link, books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_BY_SERIE', "select {0} from books_series_link, books " . SQL_BOOKS_LEFT_JOIN . "
where books_series_link.book = books.id and series = ? {1} order by series_index");
where books_series_link.book = books.id and series = ? {1} order by series_index");
define ('SQL_BOOKS_BY_TAG', "select {0} from books_tags_link, books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_BY_TAG', "select {0} from books_tags_link, books " . SQL_BOOKS_LEFT_JOIN . "
where books_tags_link.book = books.id and tag = ? {1} order by sort");
where books_tags_link.book = books.id and tag = ? {1} order by sort");
define ('SQL_BOOKS_BY_CUSTOM', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_BY_CUSTOM', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
where {2}.book = books.id and {2}.{3} = ? {1} order by sort");
where {2}.book = books.id and {2}.{3} = ? {1} order by sort");
define ('SQL_BOOKS_QUERY', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_QUERY', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
where (exists (select null from authors, books_authors_link where book = books.id and author = authors.id and authors.name like ?) or title like ?) {1} order by books.sort");
where (exists (select null from authors, books_authors_link where book = books.id and author = authors.id and authors.name like ?) or title like ?) {1} order by books.sort");
define ('SQL_BOOKS_RECENT', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
define ('SQL_BOOKS_RECENT', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
where 1=1 {1} order by timestamp desc limit ");
where 1=1 {1} order by timestamp desc limit ");
class Book extends Base {
class Book extends Base {
const ALL_BOOKS_UUID = "urn:uuid";
const ALL_BOOKS_UUID = "urn:uuid";
const ALL_BOOKS_ID = "calibre:books";
const ALL_BOOKS_ID = "calibre:books";
const ALL_RECENT_BOOKS_ID = "calibre:recentbooks";
const ALL_RECENT_BOOKS_ID = "calibre:recentbooks";
const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating";
const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating";
const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN;
const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN;
const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER;
const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER;
const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR;
const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR;
const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE;
const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE;
const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG;
const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG;
const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM;
const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM;
const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY;
const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY;
const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT;
const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT;
public $id;
public $id;
public $title;
public $title;
public $timestamp;
public $timestamp;
public $pubdate;
public $pubdate;
public $path;
public $path;
public $uuid;
public $uuid;
public $hasCover;
public $hasCover;
public $relativePath;
public $relativePath;
public $seriesIndex;
public $seriesIndex;
public $comment;
public $comment;
public $rating;
public $rating;
public $datas = NULL;
public $datas = NULL;
public $authors = NULL;
public $authors = NULL;
public $serie = NULL;
public $serie = NULL;
public $tags = NULL;
public $tags = NULL;
public $format = array ();
public $format = array ();
public function __construct($line) {
public function __construct($line) {
global $config;
global $config;
$this->id = $line->id;
$this->id = $line->id;
$this->title = $line->title;
$this->title = $line->title;
$this->timestamp = strtotime ($line->timestamp);
$this->timestamp = $line->timestamp;
$this->pubdate = strtotime ($line->pubdate);
$this->pubdate = $line->pubdate;
$this->path = Base::getDbDirectory () . $line->path;
$this->path = Base::getDbDirectory () . $line->path;
$this->relativePath = $line->path;
$this->relativePath = $line->path;
$this->seriesIndex = $line->series_index;
$this->seriesIndex = $line->series_index;
$this->comment = $line->comment;
$this->comment = $line->comment;
$this->uuid = $line->uuid;
$this->uuid = $line->uuid;
$this->hasCover = $line->has_cover;
$this->hasCover = $line->has_cover;
if (!file_exists ($this->getFilePath ("jpg"))) {
// if (!file_exists ($this->getFilePath ("jpg"))) {
// double check
// double check
$this->hasCover = 0;
$this->hasCover = 0;
}
// }
$this->rating = $line->rating;
$this->rating = $line->rating;
}
}
public function getEntryId () {
public function getEntryId () {
return self::ALL_BOOKS_UUID.":".$this->uuid;
return self::ALL_BOOKS_UUID.":".$this->uuid;
}
}
public static function getEntryIdByLetter ($startingLetter) {
public static function getEntryIdByLetter ($startingLetter) {
return self::ALL_BOOKS_ID.":letter:".$startingLetter;
return self::ALL_BOOKS_ID.":letter:".$startingLetter;
}
}
public function getUri () {
public function getUri () {
return "?page=".parent::PAGE_BOOK_DETAIL."&id=$this->id";
return "?page=".parent::PAGE_BOOK_DETAIL."&id=$this->id";
}
}
public function getDetailUrl ($permalink = false) {
public function getDetailUrl ($permalink = false) {
global $config;
global $config;
$urlParam = $this->getUri ();
$urlParam = $this->getUri ();
if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
$urlParam = str_replace ("&", "&amp;", $urlParam);
$urlParam = str_replace ("&", "&amp;", $urlParam);
if ($permalink || $config['cops_use_fancyapps'] == 0) {
if ($permalink || $config['cops_use_fancyapps'] == 0) {
return 'index.php' . $urlParam;
return 'index.php' . $urlParam;
} else {
} else {
return 'bookdetail.php' . $urlParam;
return 'bookdetail.php' . $urlParam;
}
}
}
}
public function getTitle () {
public function getTitle () {
return $this->title;
return $this->title;
}
}
public function getAuthors () {
public function getAuthors () {
if (is_null ($this->authors)) {
if (is_null ($this->authors)) {
$this->authors = Author::getAuthorByBookId ($this->id);
$this->authors = Author::getAuthorByBookId ($this->id);
}
}
return $this->authors;
return $this->authors;
}
}
public static function getFilterString () {
public static function getFilterString () {
$filter = getURLParam ("tag", NULL);
$filter = getURLParam ("tag", NULL);
if (empty ($filter)) return "";
if (empty ($filter)) return "";
$exists = true;
$exists = true;
if (preg_match ("/^!(.*)$/", $filter, $matches)) {
if (preg_match ("/^!(.*)$/", $filter, $matches)) {
$exists = false;
$exists = false;
$filter = $matches[1];
$filter = $matches[1];
}
}
$result = "exists (select null from books_tags_link, tags where books_tags_link.book = books.id and books_tags_link.tag = tags.id and tags.name = '" . $filter . "')";
$result = "exists (select null from books_tags_link, tags where books_tags_link.book = books.id and books_tags_link.tag = tags.id and tags.name = '" . $filter . "')";
if (!$exists) {
if (!$exists) {
$result = "not " . $result;
$result = "not " . $result;
}
}
return "and " . $result;
return "and " . $result;
}
}
public function getAuthorsName () {
public function getAuthorsName () {
return implode (", ", array_map (function ($author) { return $author->name; }, $this->getAuthors ()));
return implode (", ", array_map (function ($author) { return $author->name; }, $this->getAuthors ()));
}
}
public function getSerie () {
public function getSerie () {
if (is_null ($this->serie)) {
if (is_null ($this->serie)) {
$this->serie = Serie::getSerieByBookId ($this->id);
$this->serie = Serie::getSerieByBookId ($this->id);
}
}
return $this->serie;
return $this->serie;
}
}
public function getLanguages () {
public function getLanguages () {
$lang = array ();
$lang = array ();
$result = parent::getDb ()->prepare('select languages.lang_code
$result = parent::getDb ()->prepare('select languages.lang_code
from books_languages_link, languages
from books_languages_link, languages
where books_languages_link.lang_code = languages.id
where books_languages_link.lang_code = languages.id
and book = ?
and book = ?
order by item_order');
order by item_order');
$result->execute (array ($this->id));
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
array_push ($lang, $post->lang_code);
array_push ($lang, $post->lang_code);
}
}
return implode (", ", $lang);
return implode (", ", $lang);
}
}
public function getTags () {
public function getTags () {
if (is_null ($this->tags)) {
if (is_null ($this->tags)) {
$this->tags = array ();
$this->tags = array ();
$result = parent::getDb ()->prepare('select tags.id as id, name
$result = parent::getDb ()->prepare('select tags.id as id, name
from books_tags_link, tags
from books_tags_link, tags
where tag = tags.id
where tag = tags.id
and book = ?
and book = ?
order by name');
order by name');
$result->execute (array ($this->id));
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
array_push ($this->tags, new Tag ($post->id, $post->name));
array_push ($this->tags, new Tag ($post->id, $post->name));
}
}
}
}
return $this->tags;
return $this->tags;
}
}
public function getDatas ()
public function getDatas ()
{
{
if (is_null ($this->datas)) {
if (is_null ($this->datas)) {
$this->datas = array ();
$this->datas = array ();
$result = parent::getDb ()->prepare('select id, format, name
$result = parent::getDb ()->prepare('select id, format, name
from data where book = ?');
from data where book = ?');
$result->execute (array ($this->id));
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
array_push ($this->datas, new Data ($post, $this));
array_push ($this->datas, new Data ($post, $this));
}
}
}
}
return $this->datas;
return $this->datas;
}
}
public function getDataById ($idData)
public function getDataById ($idData)
{
{
foreach ($this->getDatas () as $data) {
foreach ($this->getDatas () as $data) {
if ($data->id == $idData) {
if ($data->id == $idData) {
return $data;
return $data;
}
}
}
}
return NULL;
return NULL;
}
}
public function getTagsName () {
public function getTagsName () {
return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ()));
return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ()));
}
}
public function getRating () {
public function getRating () {
if (is_null ($this->rating) || $this->rating == 0) {
if (is_null ($this->rating) || $this->rating == 0) {
return "";
return "";
}
}
$retour = "";
$retour = "";
for ($i = 0; $i < $this->rating / 2; $i++) {
for ($i = 0; $i < $this->rating / 2; $i++) {
$retour .= "&#9733;";
$retour .= "&#9733;";
}
}
for ($i = 0; $i < 5 - $this->rating / 2; $i++) {
for ($i = 0; $i < 5 - $this->rating / 2; $i++) {
$retour .= "&#9734;";
$retour .= "&#9734;";
}
}
return $retour;
return $retour;
}
}
public function getPubDate () {
public function getPubDate () {
if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) {
if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) {
return "";
return "";
}
}
else {
else {
return date ("Y", $this->pubdate);
return "";
}
}
}
}
public function getComment ($withSerie = true) {
public function getComment ($withSerie = true) {
$addition = "";
$addition = "";
$se = $this->getSerie ();
$se = $this->getSerie ();
if (!is_null ($se) && $withSerie) {
if (!is_null ($se) && $withSerie) {
$addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n";
$addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n";
}
}
if (preg_match ("/<\/(div|p|a|span)>/", $this->comment))
if (preg_match ("/<\/(div|p|a|span)>/", $this->comment))
{
{
return $addition . html2xhtml ($this->comment);
return $addition . html2xhtml ($this->comment);
}
}
else
else
{
{
return $addition . htmlspecialchars ($this->comment);
return $addition . htmlspecialchars ($this->comment);
}
}
}
}
public function getDataFormat ($format) {
public function getDataFormat ($format) {
foreach ($this->getDatas () as $data)
foreach ($this->getDatas () as $data)
{
{
if ($data->format == $format)
if ($data->format == $format)
{
{
return $data;
return $data;
}
}
}
}
return NULL;
return NULL;
}
}
public function getFilePath ($extension, $idData = NULL, $relative = false)
public function getFilePath ($extension, $idData = NULL, $relative = false)
{
{
$file = NULL;
$file = NULL;
if ($extension == "jpg")
if ($extension == "jpg")
{
{
$file = "cover.jpg";
$file = "cover.jpg";
}
}
else
else
{
{
$data = $this->getDataById ($idData);
$data = $this->getDataById ($idData);
$file = $data->name . "." . strtolower ($data->format);
$file = $data->name . "." . strtolower ($data->format);
}
}
if ($relative)
if ($relative)
{
{
return $this->relativePath."/".$file;
return $this->relativePath."/".$file;
}
}
else
else
{
{
return $this->path."/".$file;
return $this->path."/".$file;
}
}
}
}
public function getUpdatedEpub ($idData)
public function getUpdatedEpub ($idData)
{
{
global $config;
global $config;
$data = $this->getDataById ($idData);
$data = $this->getDataById ($idData);
try
try
{
{
$epub = new EPub ($data->getLocalPath ());
$epub = new EPub ($data->getLocalPath ());
$epub->Title ($this->title);
$epub->Title ($this->title);
$authorArray = array ();
$authorArray = array ();
foreach ($this->getAuthors() as $author) {
foreach ($this->getAuthors() as $author) {
$authorArray [$author->sort] = $author->name;
$authorArray [$author->sort] = $author->name;
}
}
$epub->Authors ($authorArray);
$epub->Authors ($authorArray);
$epub->Language ($this->getLanguages ());
$epub->Language ($this->getLanguages ());
$epub->Description ($this->getComment (false));
$epub->Description ($this->getComment (false));
$epub->Subjects ($this->getTagsName ());
$epub->Subjects ($this->getTagsName ());
$epub->Cover2 ($this->getFilePath ("jpg"), "image/jpeg");
$epub->Cover2 ($this->getFilePath ("jpg"), "image/jpeg");
$epub->Calibre ($this->uuid);
$epub->Calibre ($this->uuid);
$se = $this->getSerie ();
$se = $this->getSerie ();
if (!is_null ($se)) {
if (!is_null ($se)) {
$epub->Serie ($se->name);
$epub->Serie ($se->name);
$epub->SerieIndex ($this->seriesIndex);
$epub->SerieIndex ($this->seriesIndex);
}
}
if ($config['cops_provide_kepub'] == "1" && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
if ($config['cops_provide_kepub'] == "1" && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
$epub->updateForKepub ();
$epub->updateForKepub ();
}
}
$epub->download ($data->getUpdatedFilenameEpub ());
$epub->download ($data->getUpdatedFilenameEpub ());
}
}
catch (Exception $e)
catch (Exception $e)
{
{
echo "Exception : " . $e->getMessage();
echo "Exception : " . $e->getMessage();
}
}
}
}
public function getLinkArray ()
public function getLinkArray ()
{
{
global $config;
global $config;
$linkArray = array();
$linkArray = array();
if ($this->hasCover)
if ($this->hasCover)
{
{
array_push ($linkArray, Data::getLink ($this, "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));
array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL));
array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL));
}
}
foreach ($this->getDatas () as $data)
foreach ($this->getDatas () as $data)
{
{
if ($data->isKnownType ())
if ($data->isKnownType ())
{
{
array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, "Download"));
array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, "Download"));
}
}
}
}
foreach ($this->getAuthors () as $author) {
foreach ($this->getAuthors () as $author) {
array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name)));
array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name)));
}
}
$serie = $this->getSerie ();
$serie = $this->getSerie ();
if (!is_null ($serie)) {
if (!is_null ($serie)) {
array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name)));
array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name)));
}
}
return $linkArray;
return $linkArray;
}
}
public function getEntry () {
public function getEntry () {
return new EntryBook ($this->getTitle (), $this->getEntryId (),
return new EntryBook ($this->getTitle (), $this->getEntryId (),
$this->getComment (), "text/html",
$this->getComment (), "text/html",
$this->getLinkArray (), $this);
$this->getLinkArray (), $this);
}
}
public static function getCount() {
public static function getCount() {
global $config;
global $config;
$nBooks = parent::getDb ()->query('select count(*) from books')->fetchColumn();
$nBooks = parent::getDb ()->query('select count(*) from books')->fetchColumn();
$result = array();
$result = array();
$entry = new Entry (localize ("allbooks.title"),
$entry = new Entry (localize ("allbooks.title"),
self::ALL_BOOKS_ID,
self::ALL_BOOKS_ID,
str_format (localize ("allbooks.alphabetical", $nBooks), $nBooks), "text",
str_format (localize ("allbooks.alphabetical", $nBooks), $nBooks), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)));
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)));
array_push ($result, $entry);
array_push ($result, $entry);
$entry = new Entry (localize ("recent.title"),
$entry = new Entry (localize ("recent.title"),
self::ALL_RECENT_BOOKS_ID,
self::ALL_RECENT_BOOKS_ID,
str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text",
str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)));
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)));
array_push ($result, $entry);
array_push ($result, $entry);
return $result;
return $result;
}
}
public static function getBooksByAuthor($authorId, $n) {
public static function getBooksByAuthor($authorId, $n) {
return self::getEntryArray (self::SQL_BOOKS_BY_AUTHOR, array ($authorId), $n);
return self::getEntryArray (self::SQL_BOOKS_BY_AUTHOR, array ($authorId), $n);
}
}
public static function getBooksBySeries($serieId, $n) {
public static function getBooksBySeries($serieId, $n) {
return self::getEntryArray (self::SQL_BOOKS_BY_SERIE, array ($serieId), $n);
return self::getEntryArray (self::SQL_BOOKS_BY_SERIE, array ($serieId), $n);
}
}
public static function getBooksByTag($tagId, $n) {
public static function getBooksByTag($tagId, $n) {
return self::getEntryArray (self::SQL_BOOKS_BY_TAG, array ($tagId), $n);
return self::getEntryArray (self::SQL_BOOKS_BY_TAG, array ($tagId), $n);
}
}
public static function getBooksByCustom($customId, $id, $n) {
public static function getBooksByCustom($customId, $id, $n) {
$query = str_format (self::SQL_BOOKS_BY_CUSTOM, "{0}", "{1}", CustomColumn::getTableLinkName ($customId), CustomColumn::getTableLinkColumn ($customId));
$query = str_format (self::SQL_BOOKS_BY_CUSTOM, "{0}", "{1}", CustomColumn::getTableLinkName ($customId), CustomColumn::getTableLinkColumn ($customId));
return self::getEntryArray ($query, array ($id), $n);
return self::getEntryArray ($query, array ($id), $n);
}
}
public static function getBookById($bookId) {
public static function getBookById($bookId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
from books ' . self::SQL_BOOKS_LEFT_JOIN . '
from books ' . self::SQL_BOOKS_LEFT_JOIN . '
where books.id = ?');
where books.id = ?');
$result->execute (array ($bookId));
$result->execute (array ($bookId));
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
$book = new Book ($post);
$book = new Book ($post);
return $book;
return $book;
}
}
return NULL;
return NULL;
}
}
public static function getBookByDataId($dataId) {
public static function getBookByDataId($dataId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ', data.name, data.format
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ', data.name, data.format
from data, books ' . self::SQL_BOOKS_LEFT_JOIN . '
from data, books ' . self::SQL_BOOKS_LEFT_JOIN . '
where data.book = books.id and data.id = ?');
where data.book = books.id and data.id = ?');
$result->execute (array ($dataId));
$result->execute (array ($dataId));
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
$book = new Book ($post);
$book = new Book ($post);
$data = new Data ($post, $book);
$data = new Data ($post, $book);
$data->id = $dataId;
$data->id = $dataId;
$book->datas = array ($data);
$book->datas = array ($data);
return $book;
return $book;
}
}
return NULL;
return NULL;
}
}
public static function getBooksByQuery($query, $n, $database = NULL) {
public static function getBooksByQuery($query, $n, $database = NULL) {
return self::getEntryArray (self::SQL_BOOKS_QUERY, array ("%" . $query . "%", "%" . $query . "%"), $n, $database);
return self::getEntryArray (self::SQL_BOOKS_QUERY, array ("%" . $query . "%", "%" . $query . "%"), $n, $database);
}
}
public static function getAllBooks() {
public static function getAllBooks() {
$result = parent::getDb ()->query("select substr (upper (sort), 1, 1) as title, count(*) as count
$result = parent::getDb ()->query("select substr (upper (sort), 1, 1) as title, count(*) as count
from books
from books
group by substr (upper (sort), 1, 1)
group by substr (upper (sort), 1, 1)
order by substr (upper (sort), 1, 1)");
order by substr (upper (sort), 1, 1)");
$entryArray = array();
$entryArray = array();
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
array_push ($entryArray, new Entry ($post->title, Book::getEntryIdByLetter ($post->title),
array_push ($entryArray, new Entry ($post->title, Book::getEntryIdByLetter ($post->title),
str_format (localize("bookword", $post->count), $post->count), "text",
str_format (localize("bookword", $post->count), $post->count), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS_LETTER."&id=". rawurlencode ($post->title)))));
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS_LETTER."&id=". rawurlencode ($post->title)))));
}
}
return $entryArray;
return $entryArray;
}
}
public static function getBooksByStartingLetter($letter, $n) {
public static function getBooksByStartingLetter($letter, $n) {
return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n);
return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n);
}
}
public static function getEntryArray ($query, $params, $n, $database = NULL) {
public static function getEntryArray ($query, $params, $n, $database = NULL) {
list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database);
list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database);
$entryArray = array();
$entryArray = array();
while ($post = $result->fetchObject ())
while ($post = $result->fetchObject ())
{
{
$book = new Book ($post);
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
array_push ($entryArray, $book->getEntry ());
}
}
return array ($entryArray, $totalNumber);
return array ($entryArray, $totalNumber);
}
}
public static function getAllRecentBooks() {
public static function getAllRecentBooks() {
global $config;
global $config;
list ($entryArray, $totalNumber) = self::getEntryArray (self::SQL_BOOKS_RECENT . $config['cops_recentbooks_limit'], array (), -1);
list ($entryArray, $totalNumber) = self::getEntryArray (self::SQL_BOOKS_RECENT . $config['cops_recentbooks_limit'], array (), -1);
return $entryArray;
return $entryArray;
}
}
}
}
?>
?>