Commit 83be3cfb57df129f132aa7793949c02d1842778b
1 parent
34b5f15a
-
Showing
5 changed files
with
56 additions
and
17 deletions
Show diff stats
common/config/main.php
common/modules/product/controllers/ManageController.php
@@ -157,12 +157,17 @@ class ManageController extends Controller | @@ -157,12 +157,17 @@ class ManageController extends Controller | ||
157 | $model = $this->findModel($id); | 157 | $model = $this->findModel($id); |
158 | if ($model->load(Yii::$app->request->post())) { | 158 | if ($model->load(Yii::$app->request->post())) { |
159 | $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); | 159 | $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); |
160 | - | 160 | + $variantImagesUpload = UploadedFile::getInstances($model, 'variants'); |
161 | + $model->variantImagesUpload = []; | ||
162 | + foreach ($_POST['Product']['variants'] as $i => $variant) { | ||
163 | + if (!empty($variant['product_variant_id'])) { | ||
164 | + $model->variantImagesUpload[$variant['product_variant_id']] = $variantImagesUpload[$i]; | ||
165 | + } | ||
166 | + } | ||
161 | if ($model->save()) { | 167 | if ($model->save()) { |
162 | // foreach ($model->images as $image) { | 168 | // foreach ($model->images as $image) { |
163 | // $image->delete(); | 169 | // $image->delete(); |
164 | // } | 170 | // } |
165 | - | ||
166 | if ( ($images = $model->imagesUpload()) !== FALSE) { | 171 | if ( ($images = $model->imagesUpload()) !== FALSE) { |
167 | foreach ($images as $image) { | 172 | foreach ($images as $image) { |
168 | $imageModel = new ProductImage(); | 173 | $imageModel = new ProductImage(); |
@@ -172,6 +177,16 @@ class ManageController extends Controller | @@ -172,6 +177,16 @@ class ManageController extends Controller | ||
172 | } | 177 | } |
173 | } | 178 | } |
174 | 179 | ||
180 | + if ( ($images = $model->variantImagesUpload()) !== FALSE) { | ||
181 | + foreach ($images as $product_variant_id => $image) { | ||
182 | + $imageModel = new ProductImage(); | ||
183 | + $imageModel->product_id = $model->product_id; | ||
184 | + $imageModel->product_variant_id = $product_variant_id; | ||
185 | + $imageModel->image = $image; | ||
186 | + $imageModel->save(); | ||
187 | + } | ||
188 | + } | ||
189 | + | ||
175 | return $this->redirect(['view', 'id' => $model->product_id]); | 190 | return $this->redirect(['view', 'id' => $model->product_id]); |
176 | } | 191 | } |
177 | } else { | 192 | } else { |
common/modules/product/models/Product.php
@@ -34,6 +34,7 @@ class Product extends \yii\db\ActiveRecord | @@ -34,6 +34,7 @@ class Product extends \yii\db\ActiveRecord | ||
34 | 34 | ||
35 | /** @var array $_images */ | 35 | /** @var array $_images */ |
36 | public $imagesUpload = []; | 36 | public $imagesUpload = []; |
37 | + public $variantImagesUpload = []; | ||
37 | /** | 38 | /** |
38 | * @inheritdoc | 39 | * @inheritdoc |
39 | */ | 40 | */ |
@@ -154,7 +155,7 @@ class Product extends \yii\db\ActiveRecord | @@ -154,7 +155,7 @@ class Product extends \yii\db\ActiveRecord | ||
154 | */ | 155 | */ |
155 | public function getVariants() | 156 | public function getVariants() |
156 | { | 157 | { |
157 | - return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->orderBy('product_variant_id'); | 158 | + return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->orderBy('product_variant.product_variant_id', SORT_ASC); |
158 | } | 159 | } |
159 | 160 | ||
160 | public function setVariants($variants) { | 161 | public function setVariants($variants) { |
@@ -233,9 +234,9 @@ class Product extends \yii\db\ActiveRecord | @@ -233,9 +234,9 @@ class Product extends \yii\db\ActiveRecord | ||
233 | $model->product_id = $this->product_id; | 234 | $model->product_id = $this->product_id; |
234 | $model->save(); | 235 | $model->save(); |
235 | } | 236 | } |
236 | - if (!empty($todel)) { | ||
237 | - ProductVariant::deleteAll(['product_variant_id' => $todel]); | ||
238 | - } | 237 | +// if (!empty($todel)) { |
238 | +// ProductVariant::deleteAll(['product_variant_id' => $todel]); | ||
239 | +// } | ||
239 | } | 240 | } |
240 | 241 | ||
241 | public function beforeDelete() { | 242 | public function beforeDelete() { |
@@ -244,6 +245,27 @@ class Product extends \yii\db\ActiveRecord | @@ -244,6 +245,27 @@ class Product extends \yii\db\ActiveRecord | ||
244 | ProductVariant::deleteAll(['product_id' => $this->product_id]); | 245 | ProductVariant::deleteAll(['product_id' => $this->product_id]); |
245 | } | 246 | } |
246 | 247 | ||
248 | + public function variantImagesUpload() | ||
249 | + { | ||
250 | + if ($this->validate()) { | ||
251 | + $images = []; | ||
252 | + foreach ($this->variantImagesUpload as $product_variant_id => $image) { | ||
253 | + $imageName = $image->baseName .'.'. $image->extension; | ||
254 | + $i = 0; | ||
255 | + while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { | ||
256 | + $i++; | ||
257 | + $imageName = $image->baseName .'_'. $i .'.'. $image->extension; | ||
258 | + } | ||
259 | + | ||
260 | + $image->saveAs(Yii::getAlias('@imagesDir/products/' .$imageName)); | ||
261 | + $images[$product_variant_id] = $imageName; | ||
262 | + } | ||
263 | + return $images; | ||
264 | + } else { | ||
265 | + return false; | ||
266 | + } | ||
267 | + } | ||
268 | + | ||
247 | public function imagesUpload() | 269 | public function imagesUpload() |
248 | { | 270 | { |
249 | if ($this->validate()) { | 271 | if ($this->validate()) { |
@@ -251,12 +273,12 @@ class Product extends \yii\db\ActiveRecord | @@ -251,12 +273,12 @@ class Product extends \yii\db\ActiveRecord | ||
251 | foreach ($this->imagesUpload as $image) { | 273 | foreach ($this->imagesUpload as $image) { |
252 | $imageName = $image->baseName .'.'. $image->extension; | 274 | $imageName = $image->baseName .'.'. $image->extension; |
253 | $i = 0; | 275 | $i = 0; |
254 | - while(file_exists(Yii::getAlias('@frontend/web/images/products/' . $imageName))) { | 276 | + while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { |
255 | $i++; | 277 | $i++; |
256 | $imageName = $image->baseName .'_'. $i .'.'. $image->extension; | 278 | $imageName = $image->baseName .'_'. $i .'.'. $image->extension; |
257 | } | 279 | } |
258 | 280 | ||
259 | - $image->saveAs(Yii::getAlias('@frontend/web/images/products/' .$imageName)); | 281 | + $image->saveAs(Yii::getAlias('@imagesDir/products/' .$imageName)); |
260 | $images[] = $imageName; | 282 | $images[] = $imageName; |
261 | } | 283 | } |
262 | return $images; | 284 | return $images; |
@@ -269,7 +291,7 @@ class Product extends \yii\db\ActiveRecord | @@ -269,7 +291,7 @@ class Product extends \yii\db\ActiveRecord | ||
269 | $op = []; | 291 | $op = []; |
270 | if ($this->images) { | 292 | if ($this->images) { |
271 | foreach ($this->images as $image) { | 293 | foreach ($this->images as $image) { |
272 | - $op[] = Html::img($image->imageUrl); | 294 | + $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); |
273 | } | 295 | } |
274 | } | 296 | } |
275 | return $op; | 297 | return $op; |
frontend/views/catalog/product.php
@@ -99,10 +99,10 @@ $this->registerJs (" | @@ -99,10 +99,10 @@ $this->registerJs (" | ||
99 | <h1><?= $product->fullname ?></h1> | 99 | <h1><?= $product->fullname ?></h1> |
100 | <div class="begin">Цветовые решения</div> | 100 | <div class="begin">Цветовые решения</div> |
101 | <ul class="product_mod"> | 101 | <ul class="product_mod"> |
102 | - <?php foreach ($product->variants as $variant): ?> | 102 | + <?php foreach (array_reverse($product->variants) as $variant): ?> |
103 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> | 103 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> |
104 | <li> | 104 | <li> |
105 | - <a id='m<?= $variant->product_variant_id ?>' href="#" | 105 | + <a id='m<?= $variant->product_variant_id ?>' href="#<?=$variant->product_variant_id ?>" |
106 | data-cost="<?= $variant->price ?>" | 106 | data-cost="<?= $variant->price ?>" |
107 | data-old_cost="<?= $variant->price_old ?>" data-id="<?= $variant->product_variant_id ?>" data-art="<?= $variant->sku ?>" | 107 | data-old_cost="<?= $variant->price_old ?>" data-id="<?= $variant->product_variant_id ?>" data-art="<?= $variant->sku ?>" |
108 | data-color="<?= $variant->name ?>" | 108 | data-color="<?= $variant->name ?>" |
frontend/views/catalog/product_item.php
@@ -51,15 +51,12 @@ use yii\helpers\Url; | @@ -51,15 +51,12 @@ use yii\helpers\Url; | ||
51 | 51 | ||
52 | <div class="mycarousel"> | 52 | <div class="mycarousel"> |
53 | <ul class="jcarousel jcarousel-skin-tango"> | 53 | <ul class="jcarousel jcarousel-skin-tango"> |
54 | - <?php foreach ($product->variants as $variant) : ?> | 54 | + <?php foreach (array_reverse($product->variants) as $variant) : ?> |
55 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> | 55 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> |
56 | <?php if (!empty($variant->image)) :?> | 56 | <?php if (!empty($variant->image)) :?> |
57 | <li> | 57 | <li> |
58 | - <a href="<?= Url::to([ | ||
59 | - 'catalog/product', | ||
60 | - 'product' => $product, | ||
61 | - '#' => 'm' . $variant->product_variant_id]) ?>"> | ||
62 | - <?= \common\components\artboximage\ArtboxImageHelper::getImage($variant->image->imageUrl, 'product_variant')?> | 58 | + <a href="<?= Url::to(['catalog/product', 'product' => $product, '#' => 'm' . $variant->product_variant_id]) ?>"> |
59 | + <?= \common\components\artboximage\ArtboxImageHelper::getImage($variant->imageUrl, 'product_variant')?> | ||
63 | </a> | 60 | </a> |
64 | </li> | 61 | </li> |
65 | <?php endif; ?> | 62 | <?php endif; ?> |