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

namespace home\actions;

use Yii;
use common\models\Common;
use common\models\PermissionScope;

/**
 * 前台公用的更新动作类
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */

class Update extends \yii\base\Action
{
	/**
	 * @var class 当前操作所属模型 
	 */
    public $model; 
	/**
	 * @var string 当前操作视图名称
	 */
    public $view = 'create';
    /**
	 * @var string 成功提示信息
	 */
    public $successMsg = '保存成功';
	/**
	 * @var string 模型的场景 
	 */
	public $scenario = 'default';
	/**
	 * @var string 当前操作所渲染的布局 
	 */
	public $layout;
	/**
	 * @var array 要跳转的页面
	 */
	public $to;
    /**
	 * @var bool 是否开启权限范围控制
	 */
	public $enableScopeAccess = true;
    /**
	 * @var array 范围控制字段,默认有user_id，如果有多个，采用or的关系
     */
    public $scopeAccessFields = ['user_id'];


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

		$id = (int)Yii::$app->request->post('id');
        $from = Yii::$app->request->post('from');
		if (empty($id) || !$this->model = call_user_func([$this->model, 'findOne'] ,$id)) {
			Yii::$app->getSession()->setFlash('error', '要更新的对象不存在');
			return $this->controller->flash();
		}
		$this->model->scenario = $this->scenario;

		if ($this->model->load(Yii::$app->request->post())) {
            //权限范围过滤
            if ($this->enableScopeAccess) {
                $permission = Common::getActionPermission($this);
                $scopeUser = PermissionScope::getScopeUser($permission);
                $canUpdate = false;
                if (!empty($scopeUser)) {
                    foreach($this->scopeAccessFields as $field) {
                        if (in_array($this->model->$field, $scopeUser)) {
                            $canUpdate = true;
                            break;
                        }
                    }
                }
                else {
                    $canUpdate = true;
                }
                if (!$canUpdate) {
                    Yii::$app->getSession()->setFlash('error', '没有权限更新');
                    return $this->controller->flash();
                }
            }

            if ($this->model->save()) {
                Yii::$app->getSession()->setFlash('success', $this->successMsg);
                //return $this->controller->flash(['url' => [$this->controller->id.'/view', 'id' => $id]]);
                /*
                if (!empty($from)) {
                    return Yii::$app->getResponse()->redirect($from);
                }
                elseif (!empty($this->to)) {
                    return $this->controller->redirect($this->to);
                }
                 */
            }
            elseif($this->model->hasErrors()) {
                $firstError = reset($this->model->getFirstErrors());
                Yii::$app->getSession()->setFlash('error', $firstError);
                //return $this->controller->redirect(['site/flash']);
            }
        }

        //修改当前动作的布局
        if($this->layout !== null){
            $this->controller->layout = $this->layout;
        }
        //        \common\components\Tool::dump($this);
        //        exit;
        return $this->controller->render($this->view, [
            'model' => $this->model,
            'action' => [$this->controller->id.'/update'],
            'to' => $this->to,
            'from' => $from,
        ]);
    } 

}
