From cf07505dd1530bb13f3919b4962bd994babd72b9 Mon Sep 17 00:00:00 2001 From: Karnovsky A Date: Fri, 6 May 2016 12:27:42 +0300 Subject: [PATCH] Karnovsky 06052016 1200 --- common/modules/product/controllers/ManageController.php | 64 +++++++++++++++++++++++++++++++++++++--------------------------- common/modules/product/models/Product.php | 27 +++++++++++++++++++++++++-- common/modules/product/models/ProductImage.php | 5 ++--- common/modules/product/views/manage/_form.php | 6 ++++-- common/modules/product/views/manage/view.php | 1 + frontend/config/main.php | 2 +- frontend/views/catalog/product.php | 2 +- 7 files changed, 71 insertions(+), 36 deletions(-) diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index 1cd3662..56695a6 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -127,23 +127,22 @@ class ManageController extends Controller if ($model->load(Yii::$app->request->post())) { $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); - $model->save(); - - foreach($model->images as $image) { - $image->delete(); - } - - if ($model->imagesUpload) { - foreach ($model->imagesUpload as $image) { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->image = $image->name; - $imageModel->save(); + if ($model->save()) { + foreach ($model->images as $image) { + $image->delete(); } - } - return $this->redirect(['view', 'id' => $model->product_id]); + if ( ($images = $model->imagesUpload()) !== FALSE) { + foreach ($images as $image) { + $imageModel = new ProductImage(); + $imageModel->product_id = $model->product_id; + $imageModel->image = $image; + $imageModel->save(); + } + } + return $this->redirect(['view', 'id' => $model->product_id]); + } } else { return $this->render('create', [ 'model' => $model, @@ -164,22 +163,22 @@ class ManageController extends Controller if ($model->load(Yii::$app->request->post())) { $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); - $model->save(); - - foreach($model->images as $image) { - $image->delete(); - } + if ($model->save()) { + foreach ($model->images as $image) { + $image->delete(); + } - if ($model->imagesUpload) { - foreach ($model->imagesUpload as $image) { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->image = $image->name; - $imageModel->save(); + if ( ($images = $model->imagesUpload()) !== FALSE) { + foreach ($images as $image) { + $imageModel = new ProductImage(); + $imageModel->product_id = $model->product_id; + $imageModel->image = $image; + $imageModel->save(); + } } - } - return $this->redirect(['view', 'id' => $model->product_id]); + return $this->redirect(['view', 'id' => $model->product_id]); + } } else { $groups = $model->category->getTaxGroups(); @@ -203,6 +202,17 @@ class ManageController extends Controller return $this->redirect(['index']); } + public function actionDelimg($id) + { + $image = ProductImage::findOne($id); + + if ($image) { + $image->delete(); + } + + return $this->redirect(['index']); + } + public function actionImport() { $searchModel = new RemoteProductsSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index cc36f31..d4cf85a 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -229,10 +229,19 @@ class Product extends \yii\db\ActiveRecord public function imagesUpload() { if ($this->validate()) { + $images = []; foreach ($this->imagesUpload as $image) { - $image->saveAs(Yii::getAlias('@frontend/web/images/products/original/' . $image->baseName . '.' . $image->extension)); + $imageName = $image->baseName .'.'. $image->extension; + $i = 0; + while(file_exists(Yii::getAlias('@frontend/web/images/products/' . $imageName))) { + $i++; + $imageName = $image->baseName .'_'. $i .'.'. $image->extension; + } + + $image->saveAs(Yii::getAlias('@frontend/web/images/products/' .$imageName)); + $images[] = $imageName; } - return true; + return $images; } else { return false; } @@ -247,4 +256,18 @@ class Product extends \yii\db\ActiveRecord } return $op; } + + public function getImagesConfig() { + $op = []; + if ($this->images) { + foreach ($this->images as $image) { + $op[] = [ + 'caption' => $image->image, + 'width' => '120px', + 'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]) + ]; + } + } + return $op; + } } diff --git a/common/modules/product/models/ProductImage.php b/common/modules/product/models/ProductImage.php index 97e558e..307433a 100755 --- a/common/modules/product/models/ProductImage.php +++ b/common/modules/product/models/ProductImage.php @@ -97,7 +97,7 @@ class ProductImage extends \yii\db\ActiveRecord */ public function getImageFile() { - return isset($this->image) ? Yii::$app->params['uploadPath'] . $this->image : null; + return isset($this->image) ? '/images/products/' . $this->image : null; } /** @@ -107,8 +107,7 @@ class ProductImage extends \yii\db\ActiveRecord public function getImageUrl() { // return a default image placeholder if your source image is not found - $image = isset($this->image) ? $this->image : 'default.jpg'; - return Yii::getAlias('/images/products/original/' . $image); + return isset($this->image) ? '/images/products/'. $this->image : 'default.jpg'; } /** diff --git a/common/modules/product/views/manage/_form.php b/common/modules/product/views/manage/_form.php index 9de7c8b..3d7ff90 100755 --- a/common/modules/product/views/manage/_form.php +++ b/common/modules/product/views/manage/_form.php @@ -55,11 +55,13 @@ use kartik\select2\Select2; 'pluginOptions' => [ 'allowedFileExtensions' => ['jpg', 'gif', 'png'], 'initialPreview' => !empty($model->imagesHTML) ? $model->imagesHTML : [], + 'initialPreviewConfig' => $model->imagesConfig, 'overwriteInitial' => false, - 'showRemove' => true, + 'showRemove' => false, 'showUpload' => false, +// 'uploadUrl' => empty($model->product_id) ? null : \yii\helpers\Url::to(['/product/manage/uploadImage']), + 'uploadAsync' => !empty($model->product_id), 'previewFileType' => 'image', -// 'uploadUrl' => \yii\helpers\Url::to(['/site/file-upload']) ], ]); ?> diff --git a/common/modules/product/views/manage/view.php b/common/modules/product/views/manage/view.php index 9cb1d2a..7c5a8ae 100755 --- a/common/modules/product/views/manage/view.php +++ b/common/modules/product/views/manage/view.php @@ -33,6 +33,7 @@ $this->params['breadcrumbs'][] = $this->title; 'fullname', 'brand.name', 'category.name', + 'image.imageUrl:image' ], ]) ?> diff --git a/frontend/config/main.php b/frontend/config/main.php index 3173027..1bb4321 100755 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -47,7 +47,7 @@ return [ 'sourceUrl' => '@web/images', 'sizes' => [ 'brandlist' => [130, 70], - 'product_item' => [130, 70], + 'product' => [128, 128], 'product_list' => [130, 70], 'product_list2' => [130, 70], 'mainmenu' => [160, 170], diff --git a/frontend/views/catalog/product.php b/frontend/views/catalog/product.php index 4bb4db7..34cb651 100755 --- a/frontend/views/catalog/product.php +++ b/frontend/views/catalog/product.php @@ -16,7 +16,7 @@ $this->params['breadcrumbs'][] = $product->name .' #'. $product->variant->sku; image)) :?> <?= $product->name?> - <?= $product->image->alt ? $product->image->alt : $product->name?> + image->imageUrl ? Yii::$app->imageCache->thumb($product->image->imageUrl, 'product') : ''?>