<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class DetailAction extends AuthorAction
{
    public function run()
    {
        $chapter = $this->getController()->getChapter();
        
        if (empty($_GET['id']))
        {
            $first = $this->db()->select(array('id'))->from(Draft::table())->where(array('chapterid'=>$chapter['chapterid']))->ASC('id')->find(true);
            if (empty($first))
            {
                throw new iSystemException('暂无内容');
            }
            $id = $first['id'];
        }
        else
        {
            $id = S::gp('id','G',2);
        }
        $current = $this->db()->select()->from(Draft::table())->where(array('id'=>$id))->find(true);
        $current['content'] = filter($this->saveAnalyze($current['content']));
        
        $sql = "SELECT id,subject FROM ".Draft::table()." WHERE chapterid = {$chapter['chapterid']} AND id > $id ORDER BY id ASC";
        $next = $this->db()->setQueryString($sql)->find(true);
        
        $sql = "SELECT id,subject FROM ".Draft::table()." WHERE chapterid = {$chapter['chapterid']} AND id < $id ORDER BY id DESC";
        $prev = $this->db()->setQueryString($sql)->find(true);
        
        $this->getController()->render('detail', array(
                'chapter' => $chapter,
                'current' => $current,
                'next' => $next,
                'prev' => $prev,
            )
        );
    }
}