<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class BanAction extends BbsAction
{
    public function run()
    {
        $uid = S::gp('uid', 'G', 2);
        $tid = $this->getTid();
        $thread = $this->getExistsThread($tid);
        $allows = $this->getAllows($thread);
        !$allows['ban'] && $this->NoPermission();
        
        $userName = userName($uid);
        empty($userName) && $this->showMessage('被操作的用户不存在');
        
        $permissions = Members::single()->getPermissions($this->uid());
        $isBanPost = $permissions['isbanpost'] || isGM($this->uid());
        $isBanVisit = $permissions['isbanvisit'] || isGM($this->uid());
        
        if (isset($_POST['user']))
        {
            $user = S::gp('user','P');
            $utype = S::gp('utype','P');
            $type = S::gp('type','P');
            $expiry = S::gp('expiry','P');
            $range= S::gp('range','P');
            $reason = trim(S::gp('reason'));

            'post' == $type && !$isBanPost && $this->NoPermission();
            'visit' == $type && !$isBanVisit && $this->NoPermission();
            
            0 === $range && (2 !== $isBanPost && 2 !== $isBanVisit) && $this->NoPermission();
            !$range && (!$isBanPost && !$isBanVisit) && $this->NoPermission();
            
            empty($user) && $this->showMessage('请输入被操作用户信息');
            !in_array($type,array('post','visit')) && $this->showMessage('非法操作，请返回');
            empty($reason) && $this->showMessage('请输入操作原因');
            
            $sql = 'SELECT m.*,mf.* FROM '.Members::table().' AS m INNER JOIN '.Memberfields::table().' AS mf on m.uid=mf.uid WHERE ';
            switch ($utype)
            {
                case 'username':
                    $sql .= 'm.username='.S::sqlEscape($user);
                    break;
                case 'uid':
                    $sql .= 'm.uid='.S::sqlEscape(intval($user));
                    break;
            }
            $member = $this->db()->setQueryString($sql)->find(true);
            empty($member) && $this->showMessage('被操作的用户不存在，如果您是输入的用户名，请返回尝试输入UID试试看');

            $expiry *= 24*3600;
            call_user_func_array(array(Members::single(),'ban'),array('member'=>$member,'reason'=>$reason,'uid'=>$this->uid(),'type'=>$type,'module' => 'bbs', 'id'=>$range,'expiry'=>$expiry));
            
            $message[] = '操作成功';
            $message[] = '返回'.ilink('帖子',url('system',array('s'=>'bbs','a'=>'thread','tid'=>$tid))).'.'.ilink('版块',url('system',array('s'=>'bbs','a'=>'forum','fid'=>$thread['fid']))).'.'.memberLink();
            $this->showMessage($message);
        }
        
        $this->getController()->render('ban',array(
                'isBanPost' => $isBanPost,
                'isBanVisit' => $isBanVisit,
                'fid' => $thread['fid'],
                'tid' => $tid,
                'uid' => $uid,
            )
        );
    }
}