<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class JoinAction extends ForumAction
{
    public function run()
    {
        $forumInfo = $this->forumInfo();
        if (empty($forumInfo))
        {
            throw new iSystemException('不存在的书会');
        }
        
        if ($_GET['confirm'])
        {
            $this->getController()->isLogin();
            $permissions = Members::single()->getPermissions($this->uid());
            !$permissions['isjoinforum'] && $this->NoPermission();
            $sql = 'SELECT BF.forumname,BM.`bookid` FROM `'.BookMembers::table().'` AS BM INNER JOIN `'.BookForum::table()."` AS BF ON BF.`bookid` = BM.bookid WHERE BM.`uid` = {$this->uid()} AND deleted = 0";
            $row = $this->db()->setQueryString($sql)->find(true);
            if (!empty($row))
            {
                $message[] = '您已经是‘' . ilink($this->getController()->filter($row['forumname']), url('system',array('s'=>'forum','id'=>$row['bookid']))) . '’书会成员，如果要加入本书会，需要先退出原来的书会！';
                $this->showMessage($message);
            }
            
            $updatedate = $this->db()->getValue('SELECT updatedate FROM '.BookMembers::table()." WHERE bookid = '{$this->id()}' AND uid = '{$this->uid()}' ORDER BY updatedate DESC");
            if ($updatedate)
            {
                if ((CTIME - strtotime($updatedate)) < (30 * 24 * 3600))
                {
                    throw new iSystemException('同一个书会一个月内只能加入一次');
                }
            }

            $count = $this->db()->getValue('SELECT COUNT(*) FROM '.BookMembers::table().' WHERE bookid='.$this->id().' AND deleted = 0');
            $count += 1;
            $this->db()->query("INSERT INTO `".BookMembers::table()."` (`bookid`, `uid`, `username`, `createdate`) VALUES ({$this->id()}, {$this->uid()}, '{$this->userName()}', NOW())");
            $this->db()->query("UPDATE `".BookForum::table()."` SET `mcount` = {$count}, `monthmcount` = `monthmcount` + 1, `income` = `income` + 5 WHERE `bookid` = {$this->id()}");
            updateBookForum($this->id());

            $logs = '书友' . $this->userName() . '[ID:' . $this->uid() . ']加入本书会';
            $this->db()->insert(BookManageLog::table())
                 ->value(array('bookid'=>$this->id(),'uid'=>$this->uid(),'username'=>$this->userName(),'message'=>$logs,'createdate'=>date("Y-m-d H:i:s")))
                 ->q();
            $feed = '欢迎{{'.$this->uid().'}}加入[url='.staticUrl('system',array('s'=>'forum','id'=>$this->id())).']'.$forumInfo['forumname'].'[/url]书会';
            iFeed::single()->create($feed, $this->uid(), 'forum');
                
            $message[] = '欢迎您加入‘'. $this->getController()->filter($forumInfo['forumname']) . '’书友会！';
            $message[] = '返回' . forumLink($this->id()) . '.' . ilink('书页',url('bookshow',array('bid'=>$this->id()))).'.'.memberLink();
            $this->showMessage($message);
        }
        
        $this->getController()->render('join', array(
                'bookid' => $this->id(),
                'forumInfo' => $forumInfo,
            )
        );
    }
}