1) Исправления в связи со сменой API MySQL
[openlib.git] / www / language.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 language extends Base {
12     const ALL_LANGUAGES_ID = "calibre:languages";
13     
14     public $id;
15     public $lang_code;
16     
17     public function __construct($pid, $plang_code) {
18         $this->id = $pid;
19         $this->lang_code = $plang_code;
20     }
21     
22     public function getUri () {
23         return "?page=".parent::PAGE_LANGUAGE_DETAIL."&id=$this->id";
24     }
25     
26     public function getEntryId () {
27         return self::ALL_LANGUAGES_ID.":".$this->id;
28     }
29     
30     public static function getLanguageString ($code) {
31         $string = localize("languages.".$code);
32         if (preg_match ("/^languages/", $string)) {
33             return $code;
34         }
35         return $string;
36     }
37
38     public static function getCount() {
39         $nLanguages = parent::getDb ()->query('select count(*) from languages')->fetchColumn(0);
40         if ($nLanguages == 0) return NULL;
41         $entry = new Entry (localize("languages.title"), self::ALL_LANGUAGES_ID, 
42             str_format (localize("languages.alphabetical", $nLanguages), $nLanguages), "text", 
43             array ( new LinkNavigation ("?page=".parent::PAGE_ALL_LANGUAGES)));
44         return $entry;
45     }
46        
47     public static function getLanguageById ($languageId) {
48         $result = parent::getDb ()->prepare('select id, lang_code  from languages where id = ?');
49         $result->execute (array ($languageId));
50         if ($post = $result->fetchObject ()) {
51             return new Language ($post->id, Language::getLanguageString ($post->lang_code));
52         }
53         return NULL;
54     }
55     
56
57
58     public static function getAllLanguages() {
59         $result = parent::getDb ()->query('select languages.id as id, languages.lang_code as lang_code, link.count as count
60 from languages, 
61 (select lang_code,count(*) as count from books_languages_link group by lang_code) link
62 where languages.id = link.lang_code
63 group by languages.id, link.lang_code
64 order by languages.lang_code');
65         $entryArray = array();
66         while ($post = $result->fetchObject ())
67         {
68             $language = new Language ($post->id, $post->lang_code);
69             array_push ($entryArray, new Entry (Language::getLanguageString ($language->lang_code), $language->getEntryId (), 
70                 str_format (localize("bookword", $post->count), $post->count), "text", 
71                 array ( new LinkNavigation ($language->getUri ()))));
72         }
73         return $entryArray;
74     }
75 }