Ejemplo n.º 1
0
 private function addImage($item, $src_gallery_types_id, $links_id, $shop_goods_id = null)
 {
     $gallery_groups = array();
     foreach ($item as $image_sxe) {
         $basename_src = basename(strval($image_sxe->{'ПутьКИзображению'}));
         if (!$basename_src) {
             continue;
         }
         $src_image = pathinfo($this->import_file)['dirname'] . '/' . strval($image_sxe->{'ПутьКИзображению'});
         if (!is_file($src_image)) {
             continue;
         }
         $gallery_types_id = false;
         $alt = '';
         foreach (Yii::$app->params['shop']['gallery'] as $name => $id) {
             if (!$gallery_types_id && strripos($basename_src, '_' . $name) !== false) {
                 $gallery_types_id = $id;
                 preg_match('/\\[(.+)\\]/', $basename_src, $matches);
                 $alt = isset($matches[1]) ? $matches[1] : '';
             }
         }
         if (!$gallery_types_id) {
             $gallery_types_id = $src_gallery_types_id;
         }
         if (GalleryTypes::findOne($gallery_types_id)) {
             $gallery_groups[$gallery_types_id][] = ['name' => $basename_src, 'src_image' => $src_image, 'alt' => $alt, 'default' => $image_sxe->{'ОсновноеИзображение'} == 1 ? 1 : 0];
         }
     }
     if (!$gallery_groups) {
         return false;
     }
     $link = Links::findOne($links_id);
     foreach ($gallery_groups as $gallery_types_id => $images) {
         $gallery_group = false;
         if ($shop_goods_id) {
             $good_gallery = ShopGoodGallery::findOne(['shop_goods_id' => $shop_goods_id, 'gallery_types_id' => $gallery_types_id]);
             if (!$good_gallery) {
                 $gallery_group = new GalleryGroups();
                 $gallery_group->gallery_types_id = $gallery_types_id;
                 $gallery_group->save();
                 $good_gallery = new ShopGoodGallery();
                 $good_gallery->shop_goods_id = $shop_goods_id;
                 $good_gallery->gallery_types_id = $gallery_types_id;
                 $good_gallery->gallery_groups_id = $gallery_group->id;
                 $good_gallery->save();
             } else {
                 $gallery_group = GalleryGroups::findOne($good_gallery->gallery_groups_id);
             }
         } elseif (in_array($gallery_types_id, Yii::$app->params['shop']['galleryLink'])) {
             if (!$link->gallery_images_id) {
                 $gallery_group = new GalleryGroups();
                 $gallery_group->gallery_types_id = $gallery_types_id;
                 $gallery_group->save();
             } else {
                 $gallery_group = GalleryGroups::findOne($link->galleryImage->gallery_groups_id);
             }
         }
         if ($gallery_group) {
             foreach ($images as $index => $image) {
                 $gallery_image = GalleryImages::findOne(['gallery_groups_id' => $gallery_group->id, 'name' => $image['name']]);
                 if (!$gallery_image) {
                     $new_image = $this->saveImage($image['src_image'], $gallery_types_id, $gallery_group->id);
                     if (isset($new_image['small']) && isset($new_image['large'])) {
                         $gallery_image = new GalleryImages();
                         $gallery_image->gallery_groups_id = $gallery_group->id;
                         $gallery_image->small = $new_image['small'];
                         $gallery_image->large = $new_image['large'];
                         $gallery_image->name = $image['name'];
                         $gallery_image->alt = $image['alt'];
                         $gallery_image->seq = $new_image['seq'];
                         $gallery_image->save();
                     }
                 } else {
                     if ($gallery_image->alt != $image['alt']) {
                         $gallery_image->alt = $image['alt'];
                         $gallery_image->update();
                     }
                 }
                 if ($image['default'] == 1) {
                     if ($gallery_group->gallery_images_id != $gallery_image->id) {
                         $gallery_group->gallery_images_id = $gallery_image->id;
                         $gallery_group->update();
                     }
                     if ($gallery_image->id && $link->gallery_images_id != $gallery_image->id) {
                         $link->gallery_images_id = $gallery_image->id;
                         $link->update();
                     }
                 }
             }
             $gallery_images = GalleryImages::find()->where(['gallery_groups_id' => $gallery_group->id])->orderBy(['seq' => SORT_ASC])->all();
             $resort_images = false;
             foreach ($gallery_images as $gallery_image) {
                 $delete_image = true;
                 foreach ($images as $index => $image) {
                     if ($image['name'] == $gallery_image->name) {
                         $delete_image = false;
                     }
                 }
                 if ($delete_image) {
                     $resort_images = true;
                     GalleryImages::findOne($gallery_image->id)->delete();
                     unlink(Yii::getAlias('@frontend/web') . $gallery_image->small);
                     unlink(Yii::getAlias('@frontend/web') . $gallery_image->large);
                 }
             }
             if ($resort_images) {
                 $gallery = new Gallery();
                 $gallery->resortImages($gallery_group->id);
             }
         }
     }
 }
Ejemplo n.º 2
0
 public function getShopGoodGallery()
 {
     return $this->hasOne(ShopGoodGallery::className(), ['shop_goods_id' => 'id']);
 }