<?php
class IndexAction extends BaseAction
{
    public function run()
    {
        $medals = Medal::model()->findAll();
        
        if (Yii::app()->request->isPostRequest)
        {
            foreach ($_POST['medals'] as $id => &$post)
            {
                isset($post['available']) && $post['available'] == 0 && $post['available'] = 1;
                !isset($post['available']) && $post['available'] = 0;
                $medal = Medal::model()->findByPk($id);
                if (!empty($medal))
                {
                    $medal->setAttributes($post);
                    $medal->save();
                }
            }
            $this->getController()->redirect(array('index'));
        }

        $list = array();
        foreach ($medals as $medal)
        {
            $list[$medal->type][] = $medal;
        }
        
        $this->getController()->render('index',array(
                'list' => $list,
            )
        );
    }
}