%PDF- %PDF-
Direktori : /www/varak.net/povidky.varak.net/app/model/ |
Current File : /www/varak.net/povidky.varak.net/app/model/Bookshelf.php |
<?php /** * Created by JetBrains PhpStorm. * User: Waritko * Date: 15.5.13 * Time: 23:03 * To change this template use File | Settings | File Templates. */ class Bookshelf extends Base { public function getAuthorsBySurname($startLetter) { $startLetter = strtoupper(substr($startLetter, 0, 1)); return $this->db->query("select * from (select [id], [name], (select count([author]) from [book_authors] where [author]=[A].[id]) [bookcount] from [authors] A where [name] LIKE %like~) [AU] where [bookcount] > 0 order by [name]", $startLetter); } public function getAuthorsList() { $retVal = array(); $data = $this->db->query("select [id], [name] from authors order by [name]"); foreach($data as $d) { $retVal[$d->id] = $d->name; } return $retVal; } public function getLanguagesList() { $retVal = array(); $data = $this->db->query("select [id], [name] from [languages] order by [name]"); foreach($data as $d) { $retVal[$d->id] = $d->name; } return $retVal; } public function getGenresList() { $retVal = array(); $data = $this->db->query("select [id], [name] from [genres] order by [name]"); foreach($data as $d) { $retVal[$d->name] = array(); $data2 = $this->db->query("select [id], [name] from [subgenres] where [genre]=%i", $d->id); foreach($data2 as $d2) { $retVal[$d2->id] = $d2->name; } } return $retVal; } public function getBooksByAuthor($authorId, $language, $genre) { $filter = ['BA.author' => $authorId]; if($language != 0) $filter['B.language'] = $language; if($genre != 0) $filter['B.genre'] = $genre; return $this->db->query("SELECT BA.[book] [id], B.[name] [bookname], B.[updated], A.[name], BA.[author] [authorid], G.[name] [genre] FROM [books] B JOIN [book_authors] BA ON B.[id] = BA.[book] JOIN [authors] A ON A.[id] = BA.[author] JOIN [subgenres] G ON G.[id] = B.[genre] WHERE %and ORDER BY [bookname]", $filter); } public function getBooksByGenre($genreId) { return $this->db->query("SELECT BA.[book] [id], B.[name] [bookname], B.[updated], A.[name], BA.[author] [authorid], G.[name] [genre] FROM [books] B JOIN [book_authors] BA ON B.[id] = BA.[book] JOIN [authors] A ON A.[id] = BA.[author] JOIN [subgenres] G ON G.[id] = B.[genre] WHERE B.[genre] = %i ORDER BY [bookname]", $genreId); } public function getAuthorLetters() { return $this->db->query("SELECT [letter], count([letter]) [count] from (SELECT UPPER(SUBSTR([name], 1, 1)) [letter], (select count([author]) from [book_authors] where [author]=[A].[id]) [bookcount] FROM [authors] [A]) [AL] WHERE [bookcount] > 0 GROUP BY [letter] ORDER BY [letter]"); } public function getGenres() { return $this->db->query("select * from (select [id], [name], (select count([genre]) from [subgenres] where [genre]=G.[id]) [bookcount] from [genres] G) [GF] where [bookcount] > 0 order by [name]"); } public function getSubgenres($genreId) { return $this->db->query("select * from (select [id], [name], [genre], (select count([genre]) from [books] [B] where [B].[genre]=G.[id]) [bookcount] from [subgenres] G) [GF] where [bookcount] > 0 and [GF].[genre]=%i order by [name]", $genreId); } public function getBookName($bookId) { $obj = $this->db->query("SELECT B.[NAME] [bookname], A.[name] FROM [books] B JOIN [book_authors] BA ON BA.[book]= B.[id] JOIN [authors] A ON A.[id]= BA.[author] WHERE B.[id]= %i", $bookId)->fetchAll(); if(count($obj) > 0) { $bookname = $obj[0]->bookname; } $authors = ""; $multi = false; foreach($obj as $o) { if($multi) { $authors .= ", "; } $multi = true; $authors .= $o->name; } return $authors." - ".$bookname; } public function addBook($name, $authorId, $genreId, $languageId) { $this->db->query("insert into [books]([name], [genre], [language]) VALUES(%s, %i, %i)", $name, $genreId, $languageId); $newid = $this->db->getInsertId(); $this->db->query("insert into [book_authors]([book], [author]) VALUES(%i, %i)", $newid, $authorId); return $newid; } public function addAuthor($name) { $this->db->query("INSERT INTO [authors]([name]) VALUES(%s)", $name); } /*public function addGenre($name) { $this->db->query("INSERT INTO [genres]([name]) VALUES(%s)", $name); }*/ public function addLanguage($name, $tag) { $this->db->query("INSERT INTO [languages]([name], [tag]) VALUES(%s, %s)", $name, $tag); } public function increaseBookDownloadCounter($bookId) { $this->db->query("update [books] set [downloads]=[downloads]+1 where [id]=%i", $bookId); } }