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 | 157 | $model = $this->findModel($id); |
| 158 | 158 | if ($model->load(Yii::$app->request->post())) { |
| 159 | 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 | 167 | if ($model->save()) { |
| 162 | 168 | // foreach ($model->images as $image) { |
| 163 | 169 | // $image->delete(); |
| 164 | 170 | // } |
| 165 | - | |
| 166 | 171 | if ( ($images = $model->imagesUpload()) !== FALSE) { |
| 167 | 172 | foreach ($images as $image) { |
| 168 | 173 | $imageModel = new ProductImage(); |
| ... | ... | @@ -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 | 190 | return $this->redirect(['view', 'id' => $model->product_id]); |
| 176 | 191 | } |
| 177 | 192 | } else { | ... | ... |
common/modules/product/models/Product.php
| ... | ... | @@ -34,6 +34,7 @@ class Product extends \yii\db\ActiveRecord |
| 34 | 34 | |
| 35 | 35 | /** @var array $_images */ |
| 36 | 36 | public $imagesUpload = []; |
| 37 | + public $variantImagesUpload = []; | |
| 37 | 38 | /** |
| 38 | 39 | * @inheritdoc |
| 39 | 40 | */ |
| ... | ... | @@ -154,7 +155,7 @@ class Product extends \yii\db\ActiveRecord |
| 154 | 155 | */ |
| 155 | 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 | 161 | public function setVariants($variants) { |
| ... | ... | @@ -233,9 +234,9 @@ class Product extends \yii\db\ActiveRecord |
| 233 | 234 | $model->product_id = $this->product_id; |
| 234 | 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 | 242 | public function beforeDelete() { |
| ... | ... | @@ -244,6 +245,27 @@ class Product extends \yii\db\ActiveRecord |
| 244 | 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 | 269 | public function imagesUpload() |
| 248 | 270 | { |
| 249 | 271 | if ($this->validate()) { |
| ... | ... | @@ -251,12 +273,12 @@ class Product extends \yii\db\ActiveRecord |
| 251 | 273 | foreach ($this->imagesUpload as $image) { |
| 252 | 274 | $imageName = $image->baseName .'.'. $image->extension; |
| 253 | 275 | $i = 0; |
| 254 | - while(file_exists(Yii::getAlias('@frontend/web/images/products/' . $imageName))) { | |
| 276 | + while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { | |
| 255 | 277 | $i++; |
| 256 | 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 | 282 | $images[] = $imageName; |
| 261 | 283 | } |
| 262 | 284 | return $images; |
| ... | ... | @@ -269,7 +291,7 @@ class Product extends \yii\db\ActiveRecord |
| 269 | 291 | $op = []; |
| 270 | 292 | if ($this->images) { |
| 271 | 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 | 297 | return $op; | ... | ... |
frontend/views/catalog/product.php
| ... | ... | @@ -99,10 +99,10 @@ $this->registerJs (" |
| 99 | 99 | <h1><?= $product->fullname ?></h1> |
| 100 | 100 | <div class="begin">Цветовые решения</div> |
| 101 | 101 | <ul class="product_mod"> |
| 102 | - <?php foreach ($product->variants as $variant): ?> | |
| 102 | + <?php foreach (array_reverse($product->variants) as $variant): ?> | |
| 103 | 103 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> |
| 104 | 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 | 106 | data-cost="<?= $variant->price ?>" |
| 107 | 107 | data-old_cost="<?= $variant->price_old ?>" data-id="<?= $variant->product_variant_id ?>" data-art="<?= $variant->sku ?>" |
| 108 | 108 | data-color="<?= $variant->name ?>" | ... | ... |
frontend/views/catalog/product_item.php
| ... | ... | @@ -51,15 +51,12 @@ use yii\helpers\Url; |
| 51 | 51 | |
| 52 | 52 | <div class="mycarousel"> |
| 53 | 53 | <ul class="jcarousel jcarousel-skin-tango"> |
| 54 | - <?php foreach ($product->variants as $variant) : ?> | |
| 54 | + <?php foreach (array_reverse($product->variants) as $variant) : ?> | |
| 55 | 55 | <?php if (!is_null($variant->stock) && empty($variant->stock)) continue;?> |
| 56 | 56 | <?php if (!empty($variant->image)) :?> |
| 57 | 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 | 60 | </a> |
| 64 | 61 | </li> |
| 65 | 62 | <?php endif; ?> | ... | ... |