<?php
/**
 * COPS (Calibre OPDS PHP Server) class file
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Sébastien Lucas <sebastien@slucas.fr>
 */

require_once('base.php');

class Serie extends Base {
    const ALL_SERIES_ID = "calibre:series";
    
    public $id;
    public $name;
    
    public function __construct($pid, $pname) {
        $this->id = $pid;
        $this->name = $pname;
    }
    
    public function getUri () {
        return "?page=".parent::PAGE_SERIE_DETAIL."&id=$this->id";
    }
    
    public function getEntryId () {
        return self::ALL_SERIES_ID.":".$this->id;
    }

    public function getEntryIdByLetters ($letters) {
        return self::ALL_SERIES_ID.":letters:".$letters;
    }

    public static function getCount() {
        $nSeries = parent::getDb ()->query('select count(*) from series')->fetchColumn(0);
        if ($nSeries == 0) return NULL;
        $entry = new Entry (localize("series.title"), self::ALL_SERIES_ID, 
            str_format (localize("series.alphabetical", $nSeries), $nSeries), "text", 
            array ( new LinkNavigation ("?page=".parent::PAGE_ALL_SERIES)));
        return $entry;
    }
    
    public static function getSerieByBookId ($bookId) {
        $result = parent::getDb ()->prepare('select  series.id as id, name
from books_series_link, series
where series.id = series and book = ?');
        $result->execute (array ($bookId));
        if ($post = $result->fetchObject ()) {
            return new Serie ($post->id, $post->name);
        }
        return NULL;
    }
    
    public static function getSerieById ($serieId) {
        $result = parent::getDb ()->prepare('select id, name  from series where id = ?');
        $result->execute (array ($serieId));
        if ($post = $result->fetchObject ()) {
            return new Serie ($post->id, $post->name);
        }
        return NULL;
    }

    public static function getAllSeries($letters) {
        if (!$letters) { $letters=''; }
        $len = mb_strlen($letters,'UTF-8')+1;
        $result = parent::getDb ()->query('select 
substr(series.sort,1,'.$len.') as sort, 
sum(count) as count, 
count(distinct series.id) ser_count, 
count(distinct series.sort) sort_cnt, 
min(series.id) min_id, min(series.sort) min_sort
from series, 
(select series,count(*) as count from books_series_link group by series) link
where series.id = link.series and series.sort like "'.$letters.'%"
group by substr(series.sort,1,'.$len.')
order by substr(series.sort,1,'.$len.')');
        $entryArray = array();
        while ($post = $result->fetchObject ())
        {
            if ($post->ser_count==1) {
              $serie = new Serie ($post->min_id, $post->min_sort);
              $title = $serie->name;
              $link = array ( new LinkNavigation ($serie->getUri ()));
            } elseif ($post->ser_count > parent::maxItemsPerPage() && $post->sort_cnt>1) {
              $page = parent::PAGE_ALL_SERIES;
              $title = $post->sort.'...';
              $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->sort)));
            } else {
              $page = parent::PAGE_SERIES_STARTING_LETTERS;
              $title = $post->sort.'...';
              $link = array ( new LinkNavigation ("?page=".$page."&id=". rawurlencode ($post->sort)));
            }
            array_push ($entryArray, new Entry ($title, Serie::getEntryIdByLetters($post->sort), 
              str_format (localize("bookword", $post->count), $post->count), "text", 
              $link ));
                                            
        }
        return $entryArray;
    }

    
    public static function getAllSeriesStartingLetters($letters) {
        $result = parent::getDb ()->query('
select 
series.id as id, series.name as name, series.sort as sort,
sum(count) as count
from series, 
(select series,count(*) as count from books_series_link group by series) link
where series.id = link.series and series.sort like "'.$letters.'%"
group by series.id, series.name, series.sort
order by series.sort');
        $entryArray = array();
        while ($post = $result->fetchObject ())
        {
            $serie = new Serie ($post->id, $post->sort);
            array_push ($entryArray, new Entry ($serie->name, $serie->getEntryId (), 
                str_format (localize("bookword", $post->count), $post->count), "text", 
                array ( new LinkNavigation ($serie->getUri ()))));
        }
        return $entryArray;
    }
}