<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class JoinAction extends MemberAction
{
    public function run()
    {
        $id = S::gp('id', 'G', 2);
        $task = Task::model()->findByPk($id);
        if (empty($task))
        {
            throw new iSystemException('不存在的任务');
        }
        if (!$task->status)
        {
            throw new iSystemException('此任务已关闭，不可以再接受');
        }
        $tasker = Tasker::model()->find(array('condition'=>'task_id='.$id.' AND uid='.$this->uid()));
        if (!empty($tasker))
        {
            throw new iSystemException('你已经接受过此任务，不可以重复');
        }

        $new = new Tasker();
        $new->uid = $this->uid();
        $new->username = $this->userName();
        $new->task_id = $id;
        $new->createdate = CDATETIME;
        $new->save();

        $message[] = '恭喜，任务接受成功';
        $message[] = $task->factor.'后将获得'.$task->reward.setting('creditname').'奖励';
        $message[] = ilink('点击马上去做任务', $task->task_url);
        $this->showMessage($message);
    }
}