<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yzxh24
 * Date: 11-10-24
 * Time: 上午10:41
 * To change this template use File | Settings | File Templates.
 */
class CopyAction extends BaseAction
{
    public function run()
    {
        $id = Yii::app()->request->getQuery('id');
        $soft = SoftContent::model()->findByPk($id);

        $copy = new SoftContent();
        $copy->name = $soft->name;
        $copy->short_name = $soft->short_name;
        $copy->version = $soft->version;
        $copy->type = $soft->type;
        $copy->cid = $soft->cid;
        $copy->info = $soft->info;
        $copy->images = $soft->images;
        $copy->save();

        // copy picture
        if (!empty($soft->cpic))
        {
            foreach ($soft->cpic as $pic)
            {
                $fileExt = CFileHelper::getExtension($pic->name);
                $newFileName = Helper::guid() . '.' . $fileExt;

                $cpic = new SoftCpic();
                $cpic->date = date('Y-m-d');
                $cpic->cid = $copy->id;
                $cpic->name = $newFileName;
                $cpic->save();

                $file = $pic->getFile();
                if (is_file($file))
                {
                    $content = file_get_contents($file);
                    file_put_contents($cpic->getFile(), $content);
                }
            }
        }

        $this->getController()->redirect(array('list'));
    }
}