public function removeImage($id) { if (!empty($id)) { $image = ProductImage::findOne($id); if (\Yii::$app->shop_imagable->delete('shop-product', $image->file_name)) { $image->delete(); } else { throw new Exception('Files does not exist'); } } }
public function getImage() { return $this->hasOne(ProductImage::className(), ['product_id' => 'id']); }
/** * Users which have 'updateOwnProduct' permission can delete image only from Product models that have been created by their. * Users which have 'updateProduct' permission can delete image from all Product models. * * @param integer $id * @param integer $languageId * @return mixed * @throws ForbiddenHttpException * @throws Exception */ public function actionDeleteImage($id, $languageId) { if (!empty($id)) { $image = ProductImage::find()->where(['id' => $id])->one(); if (!empty($image)) { $product = Product::findOne($image->product_id); if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) { $product_image = new ProductImage(); $product_image->removeImage($id); if (\Yii::$app->request->isPjax) { return $this->renderPartial('add-image', ['selectedLanguage' => Language::findOne($languageId), 'product' => $product, 'image_form' => new ProductImageForm()]); } else { return $this->redirect(\Yii::$app->request->referrer); } } else { throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.')); } } } else { throw new Exception(); } }