<?php
class ChangeTypeAction extends BaseAction
{
    public function run()
    {
        $id = isset($_POST['id']) ? Yii::app()->request->getPost('id') : Yii::app()->request->getQuery('id');
        $book = Books::model()->findByPk($id);
        
        if (isset($_POST['type']))
        {
            $newTypeId = Yii::app()->request->getPost('type');
            $book->typeid = $newTypeId;
            $book->save();
            $type = Type::model()->findByPk($newTypeId);
            echo "<a onclick='changeType(".$id.", this);'>".$type->typename.'</a>';
        }
        else
        {
            $types = Type::model()->findAll("type='book'");
            $typeList = CHtml::activeDropDownList($book, 'typeid', CHtml::listData($types, 'typeid', 'typename'), array('onchange'=>'typeOnChange('.$id.', this);'));
        
            echo $typeList;
        }
        Yii::app()->end();
    }
}