<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class WarningAction extends BbsAction
{
    public function run()
    {
        $uid = S::gp('uid', 'G', 2);
        $tid = S::gp('tid', 'G', 2);
        $member = Members::single()->getOne($uid);
        if (empty($member))
        {
            throw new iSystemException('不存在的用户');
        }
        if (isGM($uid))
        {
            throw new iSystemException('您无权警告此用户');
        }
        $thread = $this->getExistsThread($tid);
        $allows = $this->getAllows($thread);
        !$allows['warning'] && $this->NoPermission();

        $isWarning = $this->db()->getValue('SELECT COUNT(*) FROM '.Warning::table().' WHERE uid='.S::sqlEscape($uid).' AND tid='.S::sqlEscape($tid));
        if ($isWarning)
        {
            throw new iSystemException('已经被警告过,请不要重复');
        }

        $thread['uid'] = $uid;
        $remark = $this->confirm('warning', $thread);
        $year = date('Y');
        $week = date('W');
        $insert = array(
            'tid' => $tid,
            'fid' => $thread['fid'],
            'uid' => $uid,
            'ouid' => $this->uid(),
            'subject' => $thread['subject'],
            'remark' => $remark,
            'createdate' => CDATETIME,
            'date' => CDATE,
            'year' => $year,
            'week' => $week,
        );
        $this->db()->insert(Warning::table())->value($insert)->q();

        $check = $this->db()->getValue('SELECT COUNT(*) FROM '.Warning::table().' WHERE uid='.S::sqlEscape($uid).' AND `year` = '.S::sqlEscape($year).' AND `week` = '.S::sqlEscape($week));
        if ($check >= 3)
        {
            $expiry = 24 * 3600 * 3;
            call_user_func_array(array(Members::single(),'ban'),array('member'=>$member,'reason'=>'一周内被警告超过3次,系统自动禁言3天','uid'=>$this->uid(),'type'=>'post','module' => 'bbs', 'id'=>0,'expiry'=>$expiry));
        }
        iforward(url('system', array('s'=>'bbs','a'=>'thread','tid'=>$tid)));
    }
}
