/** * Users which have 'updateOwnProduct' permission can add image only for Product models that have been created by their. * Users which have 'updateProduct' permission can add image for all Product models. * * @param integer $id * @param integer $languageId * @return mixed * @throws ForbiddenHttpException */ public function actionAddImage($id, $languageId) { if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($id)->owner])) { $product = Product::findOne($id); $image_form = new ProductImageForm(); $image = new ProductImage(); if (Yii::$app->request->isPost) { $image_form->load(Yii::$app->request->post()); $image_form->image = UploadedFile::getInstance($image_form, 'image'); if (!empty($image_form->image)) { $uploadedImageName = $image_form->upload(); $image->file_name = $uploadedImageName; $image->alt = $image_form->alt2; $image->product_id = $product->id; if ($image->validate()) { $image->save(); } else { die(var_dump($image->errors)); } } if (!empty($image_form->link)) { $image_name = $image_form->copy($image_form->link); $image->file_name = $image_name; $image->alt = $image_form->alt1; $image->product_id = $product->id; if ($image->validate()) { $image->save(); } } } if (Yii::$app->request->isPjax) { return $this->renderPartial('add-image', ['selectedLanguage' => Language::findOne($languageId), 'product' => $product, 'image_form' => new ProductImageForm()]); } return $this->render('save', ['languages' => Language::find()->all(), 'viewName' => 'add-image', 'selectedLanguage' => Language::findOne($languageId), 'product' => $product, 'params' => ['selectedLanguage' => Language::findOne($languageId), 'product' => $product, 'image_form' => new ProductImageForm()]]); } else { throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.')); } }