<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class CreateAction extends AuthorAction
{
    public function run()
    {
        header('Cache-control: private, must-revalidate');
        session_cache_limiter("private");
        $sql = "SELECT COUNT(1) as count FROM ".Book::table()." WHERE authorid = '{$this->getAuthorId()}' AND size < 100000";
    	$check = $this->db()->getValue($sql);
    	if ($check)
    	{
    		throw new iSystemException('您当前有一本书完成字数不足10W，在这之前您不能添加新作品');
    	}
    
    	if (isset($_POST['bookname']))
    	{
    		$bookname = filter_content(trim(S::gp('bookname','P')));
    		$bookinfo = filter_content(trim(S::gp('bookinfo','P')));
    		$typeid = S::gp('type','P',2);
    		
    		if (empty($bookname) || mb_strlen($bookname, 'utf8') > 20)
    		{
    			throw new iSystemException('书名长度不符号要求(不能大于20或小于0个字)');
    		}
            $checkBookName = $this->db()->getValue('SELECT COUNT(*) FROM '.Book::table().' WHERE bookname='.S::sqlEscape($bookname));
            if ($checkBookName)
            {
                throw new iSystemException('该书名已经存在，请换一个');
            }
            
    		if (empty($bookinfo) || mb_strlen($bookinfo, 'utf8') > 400)
    		{
    			throw new iSystemException('小说简介长度不符号要求(不能大于400或小于0个字)');
    		}
    		$sql = "INSERT INTO `".Book::table()."` (`bookname`, `typeid`, `author`, `authorid`, `bookinfo`, `createdate`, `datatype`, `size`, `view`, `type`) VALUES ('$bookname', '$typeid', '{$this->getAuthorName()}', '{$this->getAuthorId()}', '$bookinfo', NOW(), 1, 0, 0, 2)";
    		$this->db()->query($sql);
    		$bookId = $this->db()->getInsertId();
    
    		$message[] = '恭喜您的作品添加成功，新作品需经过管理员审核以后才能在网站上显示，在这之前您可以先给小说'.ilink('添加章节', $this->getController()->url('system',array('s'=>'author','c'=>'chapter','a'=>'create','bid'=>$bookId)));
    		$message[] = '返回'.ilink('管理章节',$this->getController()->url('system',array('s'=>'author','c'=>'chapter','bid'=>$bookId))).'.'.ilink('作者中心',$this->getController()->url('system',array('s'=>'author')));
    		$this->showMessage($message);
    	}
    	
    	$types = get_type();
    	
        $this->getController()->render('create', array(
                'types' => $types,
            )
        );
    }
}