<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class TopAction extends BbsAction
{
    public function run()
    {
        $pid = S::gp('pid', 'G', 2);
        empty($pid) && $this->showMessage('非法操作，请返回');
        $post = $this->db()->select(array('id','tid','uid','username','toped'))->from(Posts::table())->where(array('id'=>$pid))->find(true);
        empty($post) && $this->showMessage('不存在的回复');

        $thread = $this->getExistsThread($post['tid']);
        $allows = $this->getAllows($thread);
        !$allows['ptop'] && $this->NoPermission();

        if ($post['toped'])
        {
            throw new iSystemException('请不要重复置顶');
        }

        $check = $this->db()->getValue('SELECT COUNT(*) FROM '.Posts::table().' WHERE tid='.$post['tid'].' AND toped = 1');
        if ($check)
        {
            throw new iSystemException('只能置顶一个回复，如果要置顶新的，请将老的取消掉');
        }

        $this->db()->update(Posts::table())->value(array('toped'=>1))->where(array('id'=>$pid))->q();

        $message[] = '帖子回复置顶成功';
        $message[] = '返回'.ilink('帖子',url('system',array('s'=>'bbs','a'=>'thread','tid'=>$post['tid']))).'.'.ilink('版块',url('system',array('s'=>'bbs','a'=>'forum','fid'=>$thread['fid'])));
        $this->showMessage($message);
    }
}