Example #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTranslations()
 {
     return $this->hasMany(ProductPriceTranslation::className(), ['price_id' => 'id']);
 }
 /**
  * Users which have 'updateOwnProduct' permission can add price only for Product models that have been created by their.
  * Users which have 'updateProduct' permission can add price for all Product models.
  *
  * @param integer $id
  * @param integer $languageId
  * @return mixed
  * @throws ForbiddenHttpException
  */
 public function actionAddPrice($id, $languageId)
 {
     if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($id)->owner])) {
         $price = new ProductPrice();
         $priceTranslation = new ProductPriceTranslation();
         $product = Product::findOne($id);
         $selectedLanguage = Language::findOne($languageId);
         if (\Yii::$app->request->isPost) {
             $post = \Yii::$app->request->post();
             if ($price->load($post) && $priceTranslation->load($post)) {
                 $price->product_id = $product->id;
                 if ($price->save()) {
                     $priceTranslation->price_id = $price->id;
                     $priceTranslation->language_id = $selectedLanguage->id;
                     if ($priceTranslation->save()) {
                         $price = new ProductPrice();
                         $priceTranslation = new ProductPriceTranslation();
                     }
                 }
             }
         }
         if (Yii::$app->request->isPjax) {
             return $this->renderPartial('add-price', ['priceList' => $product->prices, 'priceModel' => $price, 'priceTranslationModel' => $priceTranslation, 'product' => $product, 'languages' => Language::findAll(['active' => true]), 'language' => $selectedLanguage]);
         }
         return $this->render('save', ['viewName' => 'add-price', 'selectedLanguage' => Language::findOne($languageId), 'product' => $product, 'languages' => Language::find()->all(), 'params' => ['priceList' => $product->prices, 'priceModel' => $price, 'priceTranslationModel' => $priceTranslation, 'product' => $product, 'languages' => Language::findAll(['active' => true]), 'language' => $selectedLanguage]]);
     } else {
         throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
     }
 }