<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class LinkAction extends MemberAction
{
    public function run()
    {
        $id = intval(S::gp('id','G'));
        if (empty($id))
        {
            iforward(url('index'));
        }
        
        $row = $this->db()->select()->from(PmLinks::table())->where('id='.$id)->find(true);
        if (empty($row))
        {
            iforward(url('index'));
        }
        if (0 == $row['status'])
        {
            throw new iSystemException('此链接已经过期');
        }
        
        if ($this->uid())
        {
            $log = $this->db()->select(array('id'))->from(PmLinkLogs::table())->where(array('pmlinkid'=>$row['id'],'uid'=>$this->uid()))->find(true);
            if (empty($log))
            {
                $this->db()->insert(PmLinkLogs::table())->value(array('uid'=>$this->uid(),'username'=>$this->userName(),'pmlinkid'=>$row['id'],'createdate'=>date('Y-m-d H:i:s')))->q();
                iCredits::single($this->uid(), $this->userName())->addMoney($row['award']);
                $this->db()->update(PmLinks::table())->incrField(array('click'=>1))->where(array('id'=>$row['id']))->q();
            }
        }
        iforward($row['url']);
    }
}