/**
  * Check each single thumbnail to skip already existing thumbnails
  *
  * @param Media $media
  * @throws \Exception
  */
 private function createMediaThumbnails(Media $media)
 {
     $thumbnails = $media->getThumbnailFilePaths();
     foreach ($thumbnails as $size => $path) {
         if ($this->thumbnailExists($path) && !$this->force) {
             continue;
         }
         $this->generator->createMediaThumbnail($media, array($size), true);
     }
 }
예제 #2
0
 /**
  * @param array $data Contains the array data for the media
  * @return array
  */
 private function getMediaThumbnails(array $data)
 {
     $thumbnailData = $this->thumbnailManager->getMediaThumbnails($data['__media_name'], $data['__media_type'], $data['__media_extension'], explode(';', $data['__mediaSettings_thumbnail_size']));
     $thumbnails = [];
     foreach ($thumbnailData as $row) {
         $retina = $row['retinaSource'];
         if (!$data['__mediaSettings_thumbnail_high_dpi']) {
             $retina = null;
         }
         $thumbnails[] = new Struct\Thumbnail($row['source'], $retina, $row['maxWidth'], $row['maxHeight']);
     }
     return $thumbnails;
 }
 /**
  * Insert/Update data into db
  *
  * @param array $records
  * @throws \Enlight_Event_Exception
  * @throws \Exception
  * @throws \Zend_Db_Adapter_Exception
  */
 public function write($records)
 {
     if (empty($records['default'])) {
         $message = SnippetsHelper::getNamespace()->get('adapters/articlesImages/no_records', 'No new article image records were found.');
         throw new \Exception($message);
     }
     $records = $this->eventManager->filter('Shopware_Components_SwagImportExport_DbAdapters_ArticlesImagesDbAdapter_Write', $records, ['subject' => $this]);
     foreach ($records['default'] as $record) {
         try {
             $record = $this->validator->filterEmptyString($record);
             $this->validator->checkRequiredFields($record);
             /** @var \Shopware\Models\Article\Detail $articleDetailModel */
             $articleDetailModel = $this->manager->getRepository(Detail::class)->findOneBy(['number' => $record['ordernumber']]);
             if (!$articleDetailModel) {
                 $message = SnippetsHelper::getNamespace()->get('adapters/articlesImages/article_not_found', 'Article with number %s does not exists');
                 throw new AdapterException(sprintf($message, $record['ordernumber']));
             }
             $record = $this->dataManager->setDefaultFields($record, $articleDetailModel->getArticle()->getId());
             $this->validator->validate($record, ArticleImageValidator::$mapper);
             $relations = [];
             if (isset($record['relations'])) {
                 $importedRelations = explode("&", $record['relations']);
                 foreach ($importedRelations as $key => $relation) {
                     if ($relation === "") {
                         continue;
                     }
                     $variantConfig = explode('|', preg_replace('/{|}/', '', $relation));
                     foreach ($variantConfig as $config) {
                         list($group, $option) = explode(":", $config);
                         //Get configurator group
                         $cGroupModel = $this->manager->getRepository(Group::class)->findOneBy(['name' => $group]);
                         if ($cGroupModel === null) {
                             continue;
                         }
                         //Get configurator option
                         $cOptionModel = $this->manager->getRepository(Option::class)->findOneBy(['name' => $option, 'groupId' => $cGroupModel->getId()]);
                         if ($cOptionModel === null) {
                             continue;
                         }
                         $relations[$key][] = ["group" => $cGroupModel, "option" => $cOptionModel];
                     }
                 }
             }
             /** @var \Shopware\Models\Article\Article $article */
             $article = $articleDetailModel->getArticle();
             $name = pathinfo($record['image'], PATHINFO_FILENAME);
             $media = false;
             if ($this->imageImportMode === 1) {
                 $mediaRepository = $this->manager->getRepository(Media::class);
                 $media = $mediaRepository->findOneBy(['name' => $name]);
             }
             //create new media
             if ($this->imageImportMode === 2 || empty($media)) {
                 $path = $this->load($record['image'], $name);
                 $file = new File($path);
                 $media = new Media();
                 $media->setAlbumId(-1);
                 $media->setAlbum($this->manager->getRepository(Album::class)->find(-1));
                 $media->setFile($file);
                 $media->setName(pathinfo($record['image'], PATHINFO_FILENAME));
                 $media->setDescription('');
                 $media->setCreated(new \DateTime());
                 $media->setUserId(0);
                 $this->manager->persist($media);
                 $this->manager->flush();
                 //thumbnail flag
                 //TODO: validate thumbnail
                 $thumbnail = (bool) $record['thumbnail'];
                 //generate thumbnails
                 if ($media->getType() == Media::TYPE_IMAGE && $thumbnail) {
                     $this->thumbnailManager->createMediaThumbnail($media, [], true);
                 }
             }
             $image = new Image();
             $image->setArticle($article);
             $image->setPosition($record['position']);
             $image->setPath($media->getName());
             $image->setExtension($media->getExtension());
             $image->setMedia($media);
             $image->setMain($record['main']);
             $image->setDescription($record['description']);
             $this->manager->persist($image);
             $this->manager->flush();
             if ($relations && !empty($relations)) {
                 $this->setImageMappings($relations, $image->getId());
             }
             // Prevent multiple images from being a preview
             if ((int) $record['main'] === 1) {
                 $this->db->update('s_articles_img', ['main' => 2], ['articleID = ?' => $article->getId(), 'id <> ?' => $image->getId()]);
             }
         } catch (AdapterException $e) {
             $message = $e->getMessage();
             $this->saveMessage($message);
         }
     }
 }