%PDF- %PDF-
| Direktori : /www/varak.net/catalog.varak.net/app/model/ |
| Current File : /www/varak.net/catalog.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], [surname], (select count([author]) from [book_authors] where [author]=[A].[id]) [bookcount] from [authors] A where [surname] LIKE %like~) [AU] where [bookcount] > 0 order by [surname], [name]", $startLetter);
}
public function getAuthorsList()
{
$retVal = array();
$data = $this->db->query("select [id], [name], [surname] from authors order by [surname], [name]");
foreach($data as $d)
{
$retVal[$d->id] = $d->surname.", ".$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->id] = $d->name;
}
return $retVal;
}
public function getAuthorsRaw()
{
return $this->db->query("select [id], [name], [surname] from authors order by [surname], [name]");
}
public function getGenresRaw()
{
return $this->db->query("select [id], [name] from [genres] order by [name]");
}
public function getLanguagesRaw()
{
return $this->db->query("select [id], [tag], [name] from [languages] order by [tag]");
}
public function getSeriesRaw()
{
return $this->db->query("select [id], [name], [language], [genre] from [series] order by [name]");
}
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], A.[surname], 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 [genres] 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], A.[surname], 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 [genres] 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([surname], 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 [books] where [genre]=G.[id]) [bookcount] from [genres] G) [GF] where [bookcount] > 0 order by [name]");
}
public function getSeriesLetters()
{
return $this->db->query("SELECT [letter], count([letter]) [count] from (SELECT UPPER(SUBSTR([name], 1, 1)) [letter], (select count([serie]) from [series_books] where [serie]=[S].[id]) [bookcount] FROM [series] [S]) [SL] WHERE [bookcount] > 0 GROUP BY [letter] ORDER BY [letter]");
}
public function getSeriesByName($startLetter)
{
$startLetter = strtoupper(substr($startLetter, 0, 1));
return $this->db->query("select * from (select [id], [name], (select count([serie]) from [series_books] where [serie]=[S].[id]) [bookcount] from [series] [S] where [name] LIKE %like~) [SE] where [bookcount] > 0 order by [name]", $startLetter);
}
public function getBooksBySerie($serieId)
{
return $this->db->query("SELECT [BA].[book] [id], [B].[name] [bookname], [B].[updated], [A].[name], [A].[surname], [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 [genres] [G] ON [G].[id] = [B].[genre] WHERE [B].[id] in (select [book] from [series_books] where [serie]=%i) ORDER BY [bookname]", $serieId);
}
public function getSeriesGenres()
{
return $this->db->query("select * from (select [id], [name], (select count([genre]) from [series] where [genre]=G.[id]) [seriecount] from [genres] G) [GF] where [seriecount] > 0 order by [name]");
}
public function getSeriesByGenre($genreId)
{
return $this->db->query("select * from (select [id], [name], (select count([serie]) from [series_books] where [serie]=[S].[id]) [bookcount] from [series] [S] where [genre]=%i) [SE] where [bookcount] > 0 order by [name]", $genreId);
}
public function getSeriesAuthorsLetters()
{
return $this->db->query("SELECT [letter], count([letter]) [count] from (SELECT UPPER(SUBSTR([surname], 1, 1)) [letter], (select count([author]) from [series_authors] where [author]=[A].[id]) [bookcount] FROM [authors] [A]) [AL] WHERE [bookcount] > 0 GROUP BY [letter] ORDER BY [letter]");
}
public function getSeriesByAuthorLetter($startLetter)
{
$startLetter = strtoupper(substr($startLetter, 0, 1));
return $this->db->query("select * from (select [id], [name], [surname], (select count([author]) from [series_authors] where [author]=[A].[id]) [seriecount] from [authors] A where [surname] LIKE %like~) [AU] where [seriecount] > 0 order by [surname], [name]", $startLetter);
}
public function getSeriesByAuthor($authorId)
{
return $this->db->query("select * from (select [id], [name], (select count([serie]) from [series_books] where [serie]=[S].[id]) [bookcount] from [series] [S] where [id] in (select distinct [serie] from [series_authors] where [author]=%i)) [SE] where [bookcount] > 0 order by [name]", $authorId);
}
public function getBookName($bookId)
{
$obj = $this->db->query("SELECT B.[NAME] [bookname], A.[surname], 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->surname." ".$o->name;
}
return $authors." - ".$bookname;
}
public function addBook($name, $authorId, $genreId, $languageId)
{
$this->db->query("insert into [books]([name], [genre], [language], [downloads]) VALUES(%s, %i, %i, 0)", $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($surname, $name)
{
$this->db->query("INSERT INTO [authors]([surname], [name]) VALUES(%s, %s)", $surname, $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);
}
}