<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\actions;

use Yii;

/**
 * 后台公用的删除动作类
 * 
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Delete extends \yii\base\Action
{
    /**
     * @var class 当前操作所属模型
     */
    public $model;

    public function run()
    {
        $this->model === null && $this->model = $this->controller->model;

        $id = Yii::$app->request->get('id');
        $ids = Yii::$app->request->post('delete');

        $session = Yii::$app->getSession();
        if (empty($id) && empty($ids)) {
            $session->setFlash('error', '要删除的对象不存在');
        } else {
			!empty($id) && $ids[] = $id;
            foreach ($ids as $id) {
                if (!$model = call_user_func([$this->model, 'findOne'], $id)) {
                    $session->setFlash('error', 'ID为' . $id . '的对象不存在');
                    return $this->controller->flash();
                }
                $model->delete();
            }
            $session->setFlash('success', '删除成功');
        }

        return $this->controller->flash();
    }
}