| @@ 22-80 (lines=59) @@ | ||
| 19 | * @author iRipVanWinkle <[email protected]> |
|
| 20 | * @since 1.0 |
|
| 21 | */ |
|
| 22 | class Create extends BaseAction |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var string the scenario to be assigned to the new model |
|
| 26 | */ |
|
| 27 | public $scenario = Model::SCENARIO_DEFAULT; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @var string name of the view |
|
| 31 | */ |
|
| 32 | public $view = 'create'; |
|
| 33 | ||
| 34 | /** |
|
| 35 | * @var string|array|callable a PHP callable that will be called to create the new model or class name |
|
| 36 | * The callable should return the new model instance. |
|
| 37 | */ |
|
| 38 | public $newModel; |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @var string the name variable model in the view template |
|
| 42 | */ |
|
| 43 | public $nameVariableModel = 'model'; |
|
| 44 | ||
| 45 | /** |
|
| 46 | * Creates new model instance. |
|
| 47 | * @return ActiveRecord new model instance. |
|
| 48 | * @throws InvalidConfigException on invalid configuration. |
|
| 49 | */ |
|
| 50 | public function newModel() |
|
| 51 | { |
|
| 52 | return \Yii::createObject($this->newModel); |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Creates new record. |
|
| 57 | * @return mixed response |
|
| 58 | */ |
|
| 59 | public function run() |
|
| 60 | { |
|
| 61 | $model = $this->newModel(); |
|
| 62 | $model->setScenario($this->scenario); |
|
| 63 | if ($model->load(\Yii::$app->getRequest()->post())) { |
|
| 64 | if (\Yii::$app->getRequest()->isAjax) { |
|
| 65 | \Yii::$app->getResponse()->format = Response::FORMAT_JSON; |
|
| 66 | return ActiveForm::validate($model); |
|
| 67 | } |
|
| 68 | if ($model->save()) { |
|
| 69 | $this->addSuccessFlash(); |
|
| 70 | return $this->redirect($model); |
|
| 71 | } else { |
|
| 72 | $this->addErrorFlash(); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||
| 76 | return $this->controller->render($this->view, [ |
|
| 77 | $this->nameVariableModel => $model, |
|
| 78 | ]); |
|
| 79 | } |
|
| 80 | } |
|
| 81 | ||
| @@ 21-64 (lines=44) @@ | ||
| 18 | * @author iRipVanWinkle <[email protected]> |
|
| 19 | * @since 1.0 |
|
| 20 | */ |
|
| 21 | class Update extends BaseAction |
|
| 22 | { |
|
| 23 | /** |
|
| 24 | * @var string the scenario to be assigned to the new model |
|
| 25 | */ |
|
| 26 | public $scenario = Model::SCENARIO_DEFAULT; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @var string name of the view |
|
| 30 | */ |
|
| 31 | public $view = 'update'; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * @var string the name variable model in the view template |
|
| 35 | */ |
|
| 36 | public $nameVariableModel = 'model'; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * Updates existing record. |
|
| 40 | * @param mixed $id ID of the model to be updated. |
|
| 41 | * @return mixed response |
|
| 42 | */ |
|
| 43 | public function run($id) |
|
| 44 | { |
|
| 45 | $model = $this->getModel($id); |
|
| 46 | $model->setScenario($this->scenario); |
|
| 47 | if ($model->load(Yii::$app->getRequest()->post())) { |
|
| 48 | if (Yii::$app->getRequest()->isAjax) { |
|
| 49 | Yii::$app->getResponse()->format = Response::FORMAT_JSON; |
|
| 50 | return ActiveForm::validate($model); |
|
| 51 | } |
|
| 52 | if ($model->save()) { |
|
| 53 | $this->addSuccessFlash(); |
|
| 54 | return $this->redirect($model); |
|
| 55 | } else { |
|
| 56 | $this->addErrorFlash(); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| 60 | return $this->controller->render($this->view, [ |
|
| 61 | $this->nameVariableModel => $model, |
|
| 62 | ]); |
|
| 63 | } |
|
| 64 | } |
|
| 65 | ||