/** * Adds basic info for product model. * * Users which have 'updateOwnProduct' permission can add or edit basic info only for Product models that have been created by their. * Users which have 'updateProduct' permission can can add or edit basic info for all Product models. * * @param integer $id * @param integer $languageId * @return mixed * @throws ForbiddenHttpException */ public function actionAddBasic($id = null, int $languageId) { if (!empty($languageId)) { $selectedLanguage = Language::findOne($languageId); } else { $selectedLanguage = Language::getCurrent(); } if (!empty($id)) { $product = Product::findOne($id); if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) { $products_translation = ProductTranslation::find()->where(['product_id' => $id, 'language_id' => $languageId])->one(); if (empty($products_translation)) { $products_translation = new ProductTranslation(); } } else { throw new ForbiddenHttpException(); } } else { if (\Yii::$app->user->can('createProduct')) { $this->trigger(self::EVENT_BEFORE_CREATE_PRODUCT); $product = new Product(); $products_translation = new ProductTranslation(); } else { throw new ForbiddenHttpException(); } } if (Yii::$app->request->isPost) { $product->load(Yii::$app->request->post()); if ($product->isNewRecord) { $product->owner = Yii::$app->user->id; if (\Yii::$app->user->can('createProductWithoutModeration')) { $product->status = Product::STATUS_SUCCESS; } if ($product->validate()) { $product->save(); $this->trigger(self::EVENT_AFTER_CREATE_PRODUCT, new ProductEvent(['productId' => $product->id, 'userId' => Yii::$app->user->id, 'time' => $product->creation_time])); } } $this->trigger(self::EVENT_BEFORE_EDIT_PRODUCT, new ProductEvent(['productId' => $product->id, 'userId' => Yii::$app->user->id, 'time' => $products_translation->update_time])); $products_translation->load(Yii::$app->request->post()); if ($product->validate() && $products_translation->validate()) { if (empty($products_translation->seoUrl)) { $products_translation->seoUrl = Inflector::slug($products_translation->title); } $products_translation->product_id = $product->id; $products_translation->language_id = $selectedLanguage->id; $products_translation->save(); $product->save(); $this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent(['productId' => $product->id, 'userId' => Yii::$app->user->id, 'time' => $products_translation->update_time])); } } if (Yii::$app->request->isPjax) { return $this->renderPartial('add-basic', ['languages' => Language::find()->all(), 'selectedLanguage' => $selectedLanguage, 'product' => $product, 'products_translation' => $products_translation, 'params_translation' => new ParamTranslation()]); } else { return $this->render('save', ['viewName' => 'add-basic', 'selectedLanguage' => $selectedLanguage, 'product' => $product, 'languages' => Language::find()->all(), 'params' => ['languages' => Language::find()->all(), 'selectedLanguage' => $selectedLanguage, 'product' => $product, 'products_translation' => $products_translation, 'categories' => CategoryTranslation::find()->where(['language_id' => $selectedLanguage->id])->all(), 'params_translation' => new ParamTranslation()]]); } }