1) Исправления в связи со сменой API MySQL
[openlib.git] / www / serie.php
1 <?php
2 /**
3  * COPS (Calibre OPDS PHP Server) class file
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Sébastien Lucas <sebastien@slucas.fr>
7  */
8
9 require_once('base.php');
10
11 class Serie extends Base {
12     const ALL_SERIES_ID = "calibre:series";
13     
14     public $id;
15     public $name;
16     
17     public function __construct($pid, $pname) {
18         $this->id = $pid;
19         $this->name = $pname;
20     }
21     
22     public function getUri () {
23         return "?page=".parent::PAGE_SERIE_DETAIL."&id=$this->id";
24     }
25     
26     public function getEntryId () {
27         return self::ALL_SERIES_ID.":".$this->id;
28     }
29
30     public function getEntryIdByLetters ($letters) {
31         return self::ALL_SERIES_ID.":letters:".$letters;
32     }
33
34     public static function getCount() {
35         $nSeries = parent::getDb ()->query('select count(*) from series')->fetchColumn(0);
36         if ($nSeries == 0) return NULL;
37         $entry = new Entry (localize("series.title"), self::ALL_SERIES_ID, 
38             str_format (localize("series.alphabetical", $nSeries), $nSeries), "text", 
39             array ( new LinkNavigation ("?page=".parent::PAGE_ALL_SERIES)));
40         return $entry;
41     }
42     
43     public static function getSerieByBookId ($bookId) {
44         $result = parent::getDb ()->prepare('select  series.id as id, name
45 from books_series_link, series
46 where series.id = series and book = ?');
47         $result->execute (array ($bookId));
48         if ($post = $result->fetchObject ()) {
49             return new Serie ($post->id, $post->name);
50         }
51         return NULL;
52     }
53     
54     public static function getSerieById ($serieId) {
55         $result = parent::getDb ()->prepare('select id, name  from series where id = ?');
56         $result->execute (array ($serieId));
57         if ($post = $result->fetchObject ()) {
58             return new Serie ($post->id, $post->name);
59         }
60         return NULL;
61     }
62
63     public static function getAllSeries($letters) {
64         if (!$letters) { $letters=''; }
65         $len = mb_strlen($letters,'UTF-8')+1;
66         $result = parent::getDb ()->query('select 
67 substr(series.sort,1,'.$len.') as sort, 
68 sum(count) as count, 
69 count(distinct series.id) ser_count, 
70 count(distinct series.sort) sort_cnt, 
71 min(series.id) min_id, min(series.sort) min_sort
72 from series, 
73 (select series,count(*) as count from books_series_link group by series) link
74 where series.id = link.series and series.sort like "'.$letters.'%"
75 group by substr(series.sort,1,'.$len.')
76 order by substr(series.sort,1,'.$len.')');
77         $entryArray = array();
78         while ($post = $result->fetchObject ())
79         {
80             if ($post->ser_count==1) {
81               $serie = new Serie ($post->min_id, $post->min_sort);
82               $title = $serie->name;
83               $link = array ( new LinkNavigation ($serie->getUri ()));
84             } elseif ($post->ser_count > parent::maxItemsPerPage() && $post->sort_cnt>1) {
85               $page = parent::PAGE_ALL_SERIES;
86               $title = $post->sort.'...';
87               $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->sort)));
88             } else {
89               $page = parent::PAGE_SERIES_STARTING_LETTERS;
90               $title = $post->sort.'...';
91               $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->sort)));
92             }
93             array_push ($entryArray, new Entry ($title, Serie::getEntryIdByLetters($post->sort), 
94               str_format (localize("bookword", $post->count), $post->count), "text", 
95               $link ));
96                                             
97         }
98         return $entryArray;
99     }
100
101     
102     public static function getAllSeriesStartingLetters($letters) {
103         $result = parent::getDb ()->query('
104 select 
105 series.id as id, series.name as name, series.sort as sort,
106 sum(count) as count
107 from series, 
108 (select series,count(*) as count from books_series_link group by series) link
109 where series.id = link.series and series.sort like "'.$letters.'%"
110 group by series.id, series.name, series.sort
111 order by series.sort');
112         $entryArray = array();
113         while ($post = $result->fetchObject ())
114         {
115             $serie = new Serie ($post->id, $post->sort);
116             array_push ($entryArray, new Entry ($serie->name, $serie->getEntryId (), 
117                 str_format (localize("bookword", $post->count), $post->count), "text", 
118                 array ( new LinkNavigation ($serie->getUri ()))));
119         }
120         return $entryArray;
121     }
122 }