<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yzxh24
 * Date: 12-2-5
 * Time: 下午9:53
 * To change this template use File | Settings | File Templates.
 */
class MedalsAction extends BaseAction
{
    public function run()
    {
        $uid = Yii::app()->request->getQuery('uid');
        $field = MemberData::model()->findByPk($uid);
        $medals = array();

        if ($field->medals)
        {
            $tmp = explode("\t", $field->medals);
            foreach ($tmp as $key => $medalid)
            {
                list($medalid, $expire) = explode("|", $medalid);
                $medals[] = Front::getMedal($medalid) + array('expire' => $expire);
            }
        }

        if (Yii::app()->request->isAjaxRequest && isset($_POST['post']))
        {
            $m = array();
            foreach ($_POST as $key => $value)
            {
                if (preg_match('/medal_(\d+)/i', $key, $match))
                {
                    $m[] = $match[1] . "|" . strtotime($value);
                }
            }
            if (array() !== $m)
            {
                $field->medals = implode("\t", $m);
                $field->save();
            }
            echo CJSON::encode(1);
            Yii::app()->end();
        }

        $this->getController()->renderPartial('medals', array(
                'fields' => $field,
                'medals' => $medals,
            ),
            false,
            true
        );
    }
}
