<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class MoveAction extends BbsAction
{
    public function run()
    {
        $tid = $this->getTid();
        $thread = $this->getExistsThread($tid);
        $allows = $this->getAllows($thread);
        !$allows['move'] && $this->NoPermission();

        $this->confirm('move', $thread);
        if (isset($_POST['forum']))
        {
            $fid = S::gp('forum','P');
            $forum = Forums::single()->getOne($fid);
            if (empty($forum))
            {
                throw new iSystemException('不存在的版块');
            }
            if ('category' == $forum['type'])
            {
                throw new iSystemException($forum['name'].'为版块，不能移动帖子');
            }
            
            $this->db()->update(Threads::table())->value(array('fid'=>$fid))->where('id='.$tid)->q();
            iforward(url('system',array('s'=>'bbs','a'=>'thread','tid'=>$tid)));
        }
        $forums = Forums::single()->getForumsCache();
        $forumList = array();
        foreach ($forums as $forum)
        {
            if ($forum['type'] != 'category')
            {
                $forumList[$forum['fid']] = $forum['name'];
            }
        }
        
        $this->getController()->render('move',array(
                'tid' => $tid,
                'thread' => $thread,
                'forumList' => $forumList,
            )
        );
    }
}