/** * Internal helper function which is used to upload the passed image link * to the server and create a media object for the image. * * @param $link * @param $albumId * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return MediaModel */ public function internalCreateMediaByFileLink($link, $albumId = -1) { $name = pathinfo($link, PATHINFO_FILENAME); $path = $this->load($link, $name); $name = pathinfo($path, PATHINFO_FILENAME); $file = new File($path); $media = new MediaModel(); $media->setAlbumId($albumId); $media->setFile($file); $media->setName($name); $media->setDescription(''); $media->setCreated(new \DateTime()); $media->setUserId(0); /**@var $album Album*/ $album = $this->getManager()->find('Shopware\\Models\\Media\\Album', $albumId); if (!$album) { throw new ApiException\CustomValidationException(sprintf("Album by id %s not found", $albumId)); } $media->setAlbum($album); try { //persist the model into the model manager this uploads and resizes the image $this->getManager()->persist($media); } catch (\Doctrine\ORM\ORMException $e) { throw new ApiException\CustomValidationException(sprintf("Some error occurred while loading your image")); } if ($media->getType() === MediaModel::TYPE_IMAGE) { /**@var $manager Manager */ $manager = Shopware()->Container()->get('thumbnail_manager'); $manager->createMediaThumbnail($media, array(), true); } return $media; }
/** * 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); } } }
/** * {@inheritDoc} */ public function setName($name) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setName', array($name)); return parent::setName($name); }