public function run() { $model = $this->controller->model; $scenarios = $model->scenarios(); if ($scenarios && $this->modelScenario) { if (isset($scenarios[$this->modelScenario])) { $model->scenario = $this->modelScenario; } } $rr = new RequestResponse(); if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { return $rr->ajaxValidateForm($model); } if ($rr->isRequestPjaxPost()) { if ($model->load(\Yii::$app->request->post()) && $model->save($this->modelValidate)) { \Yii::$app->getSession()->setFlash('success', \Yii::t('app', 'Saved')); if (\Yii::$app->request->post('submit-btn') == 'apply') { } else { return $this->controller->redirect($this->controller->indexUrl); } $model->refresh(); } else { \Yii::$app->getSession()->setFlash('error', \Yii::t('app', 'Could not save')); } } $this->viewParams = ['model' => $model]; return parent::run(); }
public function run() { /** * @var $contentElement CmsContentElement */ $contentElement = $this->controller->model; $model = ShopProduct::find()->where(['id' => $contentElement->id])->one(); $productPrices = []; if (!$model) { $model = new ShopProduct(['id' => $contentElement->id]); } else { if ($typePrices = ShopTypePrice::find()->where(['!=', 'def', Cms::BOOL_Y])->all()) { foreach ($typePrices as $typePrice) { $productPrice = ShopProductPrice::find()->where(['product_id' => $model->id, 'type_price_id' => $typePrice->id])->one(); if (!$productPrice) { $productPrice = new ShopProductPrice(['product_id' => $model->id, 'type_price_id' => $typePrice->id]); } if ($post = \Yii::$app->request->post()) { $data = ArrayHelper::getValue($post, 'prices.' . $typePrice->id); $productPrice->load($data, ""); } $productPrices[] = $productPrice; } } } $rr = new RequestResponse(); if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { return $rr->ajaxValidateForm($model); } if ($rr->isRequestPjaxPost()) { /** * @var $productPrice ShopProductPrice */ foreach ($productPrices as $productPrice) { if ($productPrice->save()) { } else { \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Check the correctness of the prices')); } } if ($model->load(\Yii::$app->request->post()) && $model->save()) { \Yii::$app->getSession()->setFlash('success', 'Saved'); if (\Yii::$app->request->post('submit-btn') == 'apply') { } else { return $this->controller->redirect($this->controller->indexUrl); } $model->refresh(); } else { \Yii::$app->getSession()->setFlash('error', \skeeks\cms\shop\Module::t('app', 'Failed to save')); } } $this->viewParams = ['model' => $model, 'productPrices' => $productPrices]; return parent::run(); }