1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace carono\yii2crud\actions; |
4
|
|
|
|
5
|
|
|
use carono\yii2crud\CrudController; |
6
|
|
|
use carono\yii2crud\Event; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\helpers\Html; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class UpdateAction |
12
|
|
|
* |
13
|
|
|
* @package carono\yii2crud\actions |
14
|
|
|
* @property CrudController $controller |
15
|
|
|
* @method getMessageOnUpdate(\yii\db\ActiveRecord $model) |
16
|
|
|
*/ |
17
|
|
|
class UpdateAction extends Action |
18
|
|
|
{ |
19
|
|
|
public $view = 'update'; |
20
|
|
|
public $messageOnUpdate = 'Model Successful Updated'; |
21
|
|
|
public $redirect; |
22
|
|
|
|
23
|
|
|
public function run() |
24
|
|
|
{ |
25
|
|
|
$model = $this->findModel($this->modelClass ?: $this->controller->updateClass); |
26
|
|
|
$this->controller->trigger(CrudController::EVENT_BEFORE_UPDATE_LOAD, new Event(['model' => $model])); |
27
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
|
|
|
28
|
|
|
$this->controller->trigger(CrudController::EVENT_AFTER_UPDATE_LOAD, new Event(['model' => $model])); |
29
|
|
|
if ($model->save()) { |
30
|
|
|
$this->controller->trigger(CrudController::EVENT_AFTER_UPDATE, new Event(['model' => $model])); |
31
|
|
|
Yii::$app->session->setFlash('success', $this->getMessageOnUpdate($model)); |
|
|
|
|
32
|
|
|
if ($this->redirect instanceof \Closure) { |
33
|
|
|
$url = call_user_func($this->redirect, $model); |
34
|
|
|
} else { |
35
|
|
|
return $this->controller->refresh(); |
36
|
|
|
} |
37
|
|
|
if (Yii::$app->request->isPjax) { |
38
|
|
|
return Yii::$app->response->redirect($url, 302, false); |
39
|
|
|
} |
40
|
|
|
return $this->controller->redirect($url); |
41
|
|
|
} else { |
42
|
|
|
$this->controller->trigger(CrudController::EVENT_ERROR_UPDATE, new Event(['model' => $model])); |
43
|
|
|
} |
44
|
|
|
Yii::$app->session->setFlash('error', Html::errorSummary($model)); |
45
|
|
|
} |
46
|
|
|
return $this->render($this->view, ['model' => $model]); |
47
|
|
|
} |
48
|
|
|
} |