cops/language.php

75 lines
2.5 KiB
PHP
Raw Normal View History

2013-05-21 20:49:21 +03:00
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
2013-05-21 22:27:07 +03:00
* @author Sébastien Lucas <sebastien@slucas.fr>
2013-05-21 20:49:21 +03:00
*/
require_once('base.php');
class language extends Base {
const ALL_LANGUAGES_ID = "cops:languages";
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public $id;
public $lang_code;
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public function __construct($pid, $plang_code) {
$this->id = $pid;
$this->lang_code = $plang_code;
}
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public function getUri () {
return "?page=".parent::PAGE_LANGUAGE_DETAIL."&id=$this->id";
}
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public function getEntryId () {
return self::ALL_LANGUAGES_ID.":".$this->id;
}
2013-12-05 11:52:51 +02:00
public static function getLanguageString ($code) {
$string = localize("languages.".$code);
if (preg_match ("/^languages/", $string)) {
return $code;
}
return $string;
}
2013-05-21 20:49:21 +03:00
public static function getCount() {
$nLanguages = parent::executeQuerySingle ('select count(*) from languages');
if ($nLanguages == 0) return NULL;
2013-12-05 11:52:51 +02:00
$entry = new Entry (localize("languages.title"), self::ALL_LANGUAGES_ID,
str_format (localize("languages.alphabetical", $nLanguages), $nLanguages), "text",
2013-05-21 20:49:21 +03:00
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_LANGUAGES)));
return $entry;
}
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public static function getLanguageById ($languageId) {
$result = parent::getDb ()->prepare('select id, lang_code from languages where id = ?');
$result->execute (array ($languageId));
if ($post = $result->fetchObject ()) {
return new Language ($post->id, Language::getLanguageString ($post->lang_code));
2013-05-21 20:49:21 +03:00
}
return NULL;
}
2013-12-05 11:52:51 +02:00
2013-05-21 20:49:21 +03:00
public static function getAllLanguages() {
2013-05-21 21:39:25 +03:00
$result = parent::getDb ()->query('select languages.id as id, languages.lang_code as lang_code, count(*) as count
2013-05-21 20:49:21 +03:00
from languages, books_languages_link
where languages.id = books_languages_link.lang_code
group by languages.id, books_languages_link.lang_code
order by languages.lang_code');
$entryArray = array();
while ($post = $result->fetchObject ())
{
$language = new Language ($post->id, $post->lang_code);
2013-12-05 11:52:51 +02:00
array_push ($entryArray, new Entry (Language::getLanguageString ($language->lang_code), $language->getEntryId (),
str_format (localize("bookword", $post->count), $post->count), "text",
2013-05-21 20:49:21 +03:00
array ( new LinkNavigation ($language->getUri ()))));
}
return $entryArray;
}
}