<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class DeleteAction extends ForumAction
{
    public function run()
    {
        $uid = S::gp('uid','G',2);
        $member = $this->db()->select()->from(BookMembers::table())->where(array('bookid'=>$this->id(),'uid'=>$uid))->find(true);
        if (empty($member))
        {
            throw new iSystemException('此会员不属于您的书会，不能删除');
        }        
        $this->db()->update(BookMembers::table())->value(array('deleted'=>1,'updatedate'=>CDATETIME))->where(array('bookid'=>$this->id(),'uid'=>$uid))->q();
        
        $joinDate = substr($member['createdate'], 0, 10);
        
        $check = $this->db()->getValue('SELECT COUNT(*) AS c FROM '.BookMembers::table().' WHERE bookid = '.$this->id());
        if ($check)
        {
            $decrField = array('mcount'=>1);
        }
        if ($joinDate == CDATE)
        {
            $decrField['monthmcount'] = 1;
        }
        $this->db()->update(BookForum::table())->decrField($decrField)->where(array('bookid'=>$this->id()))->q();
        updateBookForum($this->id());
        
        $logData = array(
            'uid' => $member['uid'],
            'ouid' => $this->uid(),
            'bookid' => $this->id(),
            'type' => 'DeleteMember',
            'message' => '书友[uid='.$member['uid'].']被[uid='.$this->uid().']提出书会',
        );
        BookManageLog::single()->create($logData);

        $message[] = '成员删除成功';
        $message[] = '返回' . forumLink($this->id()) . '.' . bookLink($this->id());
        $this->showMessage($message);
    }
}