2012-05-28 08:01:33 +03:00
< ? php
/**
* COPS ( Calibre OPDS PHP Server ) class file
*
* @ license GPL 2 ( http :// www . gnu . org / licenses / gpl . html )
* @ author S<EFBFBD> bastien Lucas < sebastien @ slucas . fr >
*/
require_once ( 'base.php' );
class Author extends Base {
2013-10-25 08:59:27 +03:00
const ALL_AUTHORS_ID = " cops:authors " ;
2013-12-05 11:52:51 +02:00
2012-09-30 15:39:53 +03:00
const AUTHOR_COLUMNS = " authors.id as id, authors.name as name, authors.sort as sort, count(*) as count " ;
const SQL_AUTHORS_BY_FIRST_LETTER = " select { 0} from authors, books_authors_link where author = authors.id and upper (authors.sort) like ? group by authors.id, authors.name, authors.sort order by sort " ;
2014-04-30 16:10:37 +03:00
const SQL_AUTHORS_FOR_SEARCH = " select { 0} from authors, books_authors_link where author = authors.id and (upper (authors.sort) like ? or upper (authors.name) like ?) group by authors.id, authors.name, authors.sort order by sort " ;
2012-09-30 15:39:53 +03:00
const SQL_ALL_AUTHORS = " select { 0} from authors, books_authors_link where author = authors.id group by authors.id, authors.name, authors.sort order by sort " ;
2013-12-05 11:52:51 +02:00
2012-05-28 08:01:33 +03:00
public $id ;
public $name ;
public $sort ;
2013-12-05 11:52:51 +02:00
2014-03-27 15:07:50 +02:00
public function __construct ( $pid , $pname , $psort ) {
2012-05-28 08:01:33 +03:00
$this -> id = $pid ;
2014-04-02 23:14:52 +03:00
$this -> name = str_replace ( " | " , " , " , $pname );
2014-03-27 15:07:50 +02:00
$this -> sort = $psort ;
2012-05-28 08:01:33 +03:00
}
2013-12-05 11:52:51 +02:00
2012-05-28 08:01:33 +03:00
public function getUri () {
2012-05-28 08:06:12 +03:00
return " ?page= " . parent :: PAGE_AUTHOR_DETAIL . " &id= $this->id " ;
2012-05-28 08:01:33 +03:00
}
2013-12-05 11:52:51 +02:00
2012-05-28 08:01:33 +03:00
public function getEntryId () {
return self :: ALL_AUTHORS_ID . " : " . $this -> id ;
}
2013-12-05 11:52:51 +02:00
2012-09-29 15:45:27 +03:00
public static function getEntryIdByLetter ( $startingLetter ) {
return self :: ALL_AUTHORS_ID . " :letter: " . $startingLetter ;
}
2012-05-28 08:01:33 +03:00
public static function getCount () {
$nAuthors = parent :: getDb () -> query ( 'select count(*) from authors' ) -> fetchColumn ();
2013-12-05 11:52:51 +02:00
$entry = new Entry ( localize ( " authors.title " ), self :: ALL_AUTHORS_ID ,
str_format ( localize ( " authors.alphabetical " , $nAuthors ), $nAuthors ), " text " ,
2012-05-28 08:06:12 +03:00
array ( new LinkNavigation ( " ?page= " . parent :: PAGE_ALL_AUTHORS )));
2012-05-28 08:05:05 +03:00
return $entry ;
2012-05-28 08:01:33 +03:00
}
2013-12-05 11:52:51 +02:00
2012-09-29 15:45:27 +03:00
public static function getAllAuthorsByFirstLetter () {
$result = parent :: getDb () -> query ( ' select substr ( upper ( sort ), 1 , 1 ) as title , count ( * ) as count
from authors
group by substr ( upper ( sort ), 1 , 1 )
order by substr ( upper ( sort ), 1 , 1 ) ' );
$entryArray = array ();
while ( $post = $result -> fetchObject ())
{
2013-12-05 11:52:51 +02:00
array_push ( $entryArray , new Entry ( $post -> title , Author :: getEntryIdByLetter ( $post -> title ),
str_format ( localize ( " authorword " , $post -> count ), $post -> count ), " text " ,
2012-09-30 15:50:44 +03:00
array ( new LinkNavigation ( " ?page= " . parent :: PAGE_AUTHORS_FIRST_LETTER . " &id= " . rawurlencode ( $post -> title )))));
2012-09-29 15:45:27 +03:00
}
return $entryArray ;
}
2013-12-05 11:52:51 +02:00
2012-09-29 15:45:27 +03:00
public static function getAuthorsByStartingLetter ( $letter ) {
2012-09-30 15:39:53 +03:00
return self :: getEntryArray ( self :: SQL_AUTHORS_BY_FIRST_LETTER , array ( $letter . " % " ));
2012-09-29 15:45:27 +03:00
}
2013-12-05 11:52:51 +02:00
2014-04-30 16:10:37 +03:00
public static function getAuthorsForSearch ( $query ) {
return self :: getEntryArray ( self :: SQL_AUTHORS_FOR_SEARCH , array ( $query . " % " , $query . " % " ));
}
2012-05-28 08:01:33 +03:00
public static function getAllAuthors () {
2012-09-30 15:39:53 +03:00
return self :: getEntryArray ( self :: SQL_ALL_AUTHORS , array ());
}
2013-12-05 11:52:51 +02:00
2012-09-30 15:39:53 +03:00
public static function getEntryArray ( $query , $params ) {
2014-03-10 18:59:57 +02:00
list (, $result ) = parent :: executeQuery ( $query , self :: AUTHOR_COLUMNS , " " , $params , - 1 );
2012-05-28 08:05:05 +03:00
$entryArray = array ();
2012-05-28 08:01:33 +03:00
while ( $post = $result -> fetchObject ())
{
2014-03-27 15:07:50 +02:00
$author = new Author ( $post -> id , $post -> name , $post -> sort );
2013-12-05 11:52:51 +02:00
array_push ( $entryArray , new Entry ( $post -> sort , $author -> getEntryId (),
str_format ( localize ( " bookword " , $post -> count ), $post -> count ), " text " ,
2012-05-28 08:01:33 +03:00
array ( new LinkNavigation ( $author -> getUri ()))));
}
2012-05-28 08:05:05 +03:00
return $entryArray ;
2012-05-28 08:01:33 +03:00
}
2013-12-05 11:52:51 +02:00
2012-05-28 08:07:49 +03:00
public static function getAuthorById ( $authorId ) {
2014-03-27 15:07:50 +02:00
$result = parent :: getDb () -> prepare ( 'select ' . self :: AUTHOR_COLUMNS . ' from authors where id = ?' );
2012-05-28 08:01:33 +03:00
$result -> execute ( array ( $authorId ));
2014-03-27 15:07:50 +02:00
$post = $result -> fetchObject ();
return new Author ( $post -> id , $post -> name , $post -> sort );
2012-05-28 08:01:33 +03:00
}
2013-12-05 11:52:51 +02:00
2012-05-28 08:01:33 +03:00
public static function getAuthorByBookId ( $bookId ) {
2014-03-27 15:07:50 +02:00
$result = parent :: getDb () -> prepare ( 'select ' . self :: AUTHOR_COLUMNS . ' from authors , books_authors_link
2012-05-28 08:01:33 +03:00
where author = authors . id
and book = ? ' );
$result -> execute ( array ( $bookId ));
$authorArray = array ();
while ( $post = $result -> fetchObject ()) {
2014-03-27 15:07:50 +02:00
array_push ( $authorArray , new Author ( $post -> id , $post -> name , $post -> sort ));
2012-05-28 08:01:33 +03:00
}
return $authorArray ;
}
}