Commit cf07505dd1530bb13f3919b4962bd994babd72b9
1 parent
e2128676
Karnovsky 06052016 1200
Showing
7 changed files
with
71 additions
and
36 deletions
Show diff stats
common/modules/product/controllers/ManageController.php
... | ... | @@ -127,23 +127,22 @@ class ManageController extends Controller |
127 | 127 | if ($model->load(Yii::$app->request->post())) { |
128 | 128 | $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); |
129 | 129 | |
130 | - $model->save(); | |
131 | - | |
132 | - foreach($model->images as $image) { | |
133 | - $image->delete(); | |
134 | - } | |
135 | - | |
136 | - if ($model->imagesUpload) { | |
137 | - foreach ($model->imagesUpload as $image) { | |
138 | - $imageModel = new ProductImage(); | |
139 | - $imageModel->product_id = $model->product_id; | |
140 | - $imageModel->image = $image->name; | |
141 | - $imageModel->save(); | |
130 | + if ($model->save()) { | |
131 | + foreach ($model->images as $image) { | |
132 | + $image->delete(); | |
142 | 133 | } |
143 | - } | |
144 | 134 | |
145 | - return $this->redirect(['view', 'id' => $model->product_id]); | |
135 | + if ( ($images = $model->imagesUpload()) !== FALSE) { | |
136 | + foreach ($images as $image) { | |
137 | + $imageModel = new ProductImage(); | |
138 | + $imageModel->product_id = $model->product_id; | |
139 | + $imageModel->image = $image; | |
140 | + $imageModel->save(); | |
141 | + } | |
142 | + } | |
146 | 143 | |
144 | + return $this->redirect(['view', 'id' => $model->product_id]); | |
145 | + } | |
147 | 146 | } else { |
148 | 147 | return $this->render('create', [ |
149 | 148 | 'model' => $model, |
... | ... | @@ -164,22 +163,22 @@ class ManageController extends Controller |
164 | 163 | if ($model->load(Yii::$app->request->post())) { |
165 | 164 | $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); |
166 | 165 | |
167 | - $model->save(); | |
168 | - | |
169 | - foreach($model->images as $image) { | |
170 | - $image->delete(); | |
171 | - } | |
166 | + if ($model->save()) { | |
167 | + foreach ($model->images as $image) { | |
168 | + $image->delete(); | |
169 | + } | |
172 | 170 | |
173 | - if ($model->imagesUpload) { | |
174 | - foreach ($model->imagesUpload as $image) { | |
175 | - $imageModel = new ProductImage(); | |
176 | - $imageModel->product_id = $model->product_id; | |
177 | - $imageModel->image = $image->name; | |
178 | - $imageModel->save(); | |
171 | + if ( ($images = $model->imagesUpload()) !== FALSE) { | |
172 | + foreach ($images as $image) { | |
173 | + $imageModel = new ProductImage(); | |
174 | + $imageModel->product_id = $model->product_id; | |
175 | + $imageModel->image = $image; | |
176 | + $imageModel->save(); | |
177 | + } | |
179 | 178 | } |
180 | - } | |
181 | 179 | |
182 | - return $this->redirect(['view', 'id' => $model->product_id]); | |
180 | + return $this->redirect(['view', 'id' => $model->product_id]); | |
181 | + } | |
183 | 182 | } else { |
184 | 183 | $groups = $model->category->getTaxGroups(); |
185 | 184 | |
... | ... | @@ -203,6 +202,17 @@ class ManageController extends Controller |
203 | 202 | return $this->redirect(['index']); |
204 | 203 | } |
205 | 204 | |
205 | + public function actionDelimg($id) | |
206 | + { | |
207 | + $image = ProductImage::findOne($id); | |
208 | + | |
209 | + if ($image) { | |
210 | + $image->delete(); | |
211 | + } | |
212 | + | |
213 | + return $this->redirect(['index']); | |
214 | + } | |
215 | + | |
206 | 216 | public function actionImport() { |
207 | 217 | $searchModel = new RemoteProductsSearch(); |
208 | 218 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams); | ... | ... |
common/modules/product/models/Product.php
... | ... | @@ -229,10 +229,19 @@ class Product extends \yii\db\ActiveRecord |
229 | 229 | public function imagesUpload() |
230 | 230 | { |
231 | 231 | if ($this->validate()) { |
232 | + $images = []; | |
232 | 233 | foreach ($this->imagesUpload as $image) { |
233 | - $image->saveAs(Yii::getAlias('@frontend/web/images/products/original/' . $image->baseName . '.' . $image->extension)); | |
234 | + $imageName = $image->baseName .'.'. $image->extension; | |
235 | + $i = 0; | |
236 | + while(file_exists(Yii::getAlias('@frontend/web/images/products/' . $imageName))) { | |
237 | + $i++; | |
238 | + $imageName = $image->baseName .'_'. $i .'.'. $image->extension; | |
239 | + } | |
240 | + | |
241 | + $image->saveAs(Yii::getAlias('@frontend/web/images/products/' .$imageName)); | |
242 | + $images[] = $imageName; | |
234 | 243 | } |
235 | - return true; | |
244 | + return $images; | |
236 | 245 | } else { |
237 | 246 | return false; |
238 | 247 | } |
... | ... | @@ -247,4 +256,18 @@ class Product extends \yii\db\ActiveRecord |
247 | 256 | } |
248 | 257 | return $op; |
249 | 258 | } |
259 | + | |
260 | + public function getImagesConfig() { | |
261 | + $op = []; | |
262 | + if ($this->images) { | |
263 | + foreach ($this->images as $image) { | |
264 | + $op[] = [ | |
265 | + 'caption' => $image->image, | |
266 | + 'width' => '120px', | |
267 | + 'url' => \yii\helpers\Url::to(['/product/manage/delimg', 'id' => $image->product_image_id]) | |
268 | + ]; | |
269 | + } | |
270 | + } | |
271 | + return $op; | |
272 | + } | |
250 | 273 | } | ... | ... |
common/modules/product/models/ProductImage.php
... | ... | @@ -97,7 +97,7 @@ class ProductImage extends \yii\db\ActiveRecord |
97 | 97 | */ |
98 | 98 | public function getImageFile() |
99 | 99 | { |
100 | - return isset($this->image) ? Yii::$app->params['uploadPath'] . $this->image : null; | |
100 | + return isset($this->image) ? '/images/products/' . $this->image : null; | |
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
... | ... | @@ -107,8 +107,7 @@ class ProductImage extends \yii\db\ActiveRecord |
107 | 107 | public function getImageUrl() |
108 | 108 | { |
109 | 109 | // return a default image placeholder if your source image is not found |
110 | - $image = isset($this->image) ? $this->image : 'default.jpg'; | |
111 | - return Yii::getAlias('/images/products/original/' . $image); | |
110 | + return isset($this->image) ? '/images/products/'. $this->image : 'default.jpg'; | |
112 | 111 | } |
113 | 112 | |
114 | 113 | /** | ... | ... |
common/modules/product/views/manage/_form.php
... | ... | @@ -55,11 +55,13 @@ use kartik\select2\Select2; |
55 | 55 | 'pluginOptions' => [ |
56 | 56 | 'allowedFileExtensions' => ['jpg', 'gif', 'png'], |
57 | 57 | 'initialPreview' => !empty($model->imagesHTML) ? $model->imagesHTML : [], |
58 | + 'initialPreviewConfig' => $model->imagesConfig, | |
58 | 59 | 'overwriteInitial' => false, |
59 | - 'showRemove' => true, | |
60 | + 'showRemove' => false, | |
60 | 61 | 'showUpload' => false, |
62 | +// 'uploadUrl' => empty($model->product_id) ? null : \yii\helpers\Url::to(['/product/manage/uploadImage']), | |
63 | + 'uploadAsync' => !empty($model->product_id), | |
61 | 64 | 'previewFileType' => 'image', |
62 | -// 'uploadUrl' => \yii\helpers\Url::to(['/site/file-upload']) | |
63 | 65 | ], |
64 | 66 | ]); ?> |
65 | 67 | ... | ... |
common/modules/product/views/manage/view.php
frontend/config/main.php
frontend/views/catalog/product.php
... | ... | @@ -16,7 +16,7 @@ $this->params['breadcrumbs'][] = $product->name .' #'. $product->variant->sku; |
16 | 16 | <?php if (empty($product->image)) :?> |
17 | 17 | <img src="/images/no_photo_big.png" alt="<?= $product->name?>"> |
18 | 18 | <?php else :?> |
19 | - <img src="/images/<?= $product->image->image?>" alt="<?= $product->image->alt ? $product->image->alt : $product->name?>"> | |
19 | + <?= $product->image->imageUrl ? Yii::$app->imageCache->thumb($product->image->imageUrl, 'product') : ''?> | |
20 | 20 | <?php endif?> |
21 | 21 | |
22 | 22 | <!--<span class="new">НОВИНКА</span> | ... | ... |