/** * Adds category SEO data * * @param integer $languageId * @param integer $categoryId * @return mixed * @throws ForbiddenHttpException */ public function actionAddSeo($languageId = null, $categoryId = null) { if (!empty($categoryId)) { $category = Category::findOne($categoryId); $category_translation = CategoryTranslation::find()->where(['category_id' => $categoryId, 'language_id' => $languageId])->one(); if (empty($category_translation)) { $category_translation = new CategoryTranslation(); } } else { $category = new Category(); $category_translation = new CategoryTranslation(); } if (Yii::$app->request->isPost) { $category->load(Yii::$app->request->post()); $category_translation->load(Yii::$app->request->post()); if ($category->validate() && $category_translation->validate()) { $category->save(); $category_translation->category_id = $category->id; $category_translation->language_id = $languageId; $category_translation->save(); Yii::$app->getSession()->setFlash('success', 'Data were successfully modified.'); } } return $this->render('save', ['category' => $category, 'languageId' => $languageId, 'selectedLanguage' => Language::findOne($languageId), 'languages' => Language::findAll(['active' => true]), 'viewName' => 'add-seo', 'params' => ['category' => $category, 'category_translation' => $category_translation, 'languageId' => $languageId]]); }
/** * 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()]]); } }