コード例 #1
0
 /**
  * @param $filePath
  */
 public function importImages($filePath)
 {
     $results = new Shopware_Components_CsvIterator($filePath, ';');
     $counter = 0;
     $total = 0;
     $errors = array();
     $articleDetailRepository = $this->getArticleDetailRepository();
     $configuratorGroupRepository = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Group');
     $configuratorOptionRepository = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Option');
     $recreateImagesLater = array();
     foreach ($results as $imageData) {
         if (!empty($imageData['relations'])) {
             $relations = array();
             $results = explode("&", $imageData['relations']);
             foreach ($results as $result) {
                 if ($result !== "") {
                     $result = preg_replace('/{|}/', '', $result);
                     list($group, $option) = explode(":", $result);
                     // Try to get given configurator group/option. Continue, if they don't exist
                     $cGroupModel = $configuratorGroupRepository->findOneBy(array('name' => $group));
                     if ($cGroupModel === null) {
                         continue;
                     }
                     $cOptionModel = $configuratorOptionRepository->findOneBy(array('name' => $option, 'groupId' => $cGroupModel->getId()));
                     if ($cOptionModel === null) {
                         continue;
                     }
                     $relations[] = array("group" => $cGroupModel, "option" => $cOptionModel);
                 }
             }
         }
         if (empty($imageData['ordernumber']) || empty($imageData['image'])) {
             continue;
         }
         $counter++;
         /** @var \Shopware\Models\Article\Detail $articleDetailModel */
         $articleDetailModel = $articleDetailRepository->findOneBy(array('number' => $imageData['ordernumber']));
         if (!$articleDetailModel) {
             continue;
         }
         /** @var \Shopware\Models\Article\Article $article */
         $article = $articleDetailModel->getArticle();
         $image = new \Shopware\Models\Article\Image();
         try {
             $name = pathinfo($imageData['image'], PATHINFO_FILENAME);
             $path = $this->load($imageData['image'], $name);
         } catch (\Exception $e) {
             $errors[] = sprintf("Could not load image {$imageData['image']}: %s", $e->getMessage());
             continue;
         }
         $file = new \Symfony\Component\HttpFoundation\File\File($path);
         $media = new \Shopware\Models\Media\Media();
         $media->setAlbumId(-1);
         $media->setAlbum($this->getManager()->find('Shopware\\Models\\Media\\Album', -1));
         $media->setFile($file);
         $media->setName(pathinfo($imageData['image'], PATHINFO_FILENAME));
         $media->setDescription('');
         $media->setCreated(new \DateTime());
         $media->setUserId(0);
         try {
             //persist the model into the model manager
             $this->getManager()->persist($media);
             $this->getManager()->flush();
         } catch (\Doctrine\ORM\ORMException $e) {
             $errors[] = sprintf("Could not move image: %s", $e->getMessage());
             continue;
         }
         if (empty($imageData['main'])) {
             $imageData['main'] = 1;
         }
         //generate thumbnails
         if ($media->getType() == \Shopware\Models\Media\Media::TYPE_IMAGE) {
             /**@var $manager \Shopware\Components\Thumbnail\Manager */
             $manager = Shopware()->Container()->get('thumbnail_manager');
             $manager->createMediaThumbnail($media, array(), true);
         }
         $image->setArticle($article);
         $image->setDescription($imageData['description']);
         $image->setPosition($imageData['position']);
         $image->setPath($media->getName());
         $image->setExtension($media->getExtension());
         $image->setMedia($media);
         $image->setMain($imageData['main']);
         $this->getManager()->persist($image);
         $this->getManager()->flush($image);
         // Set mappings
         if (!empty($relations)) {
             foreach ($relations as $relation) {
                 $optionModel = $relation['option'];
                 Shopware()->Db()->insert('s_article_img_mappings', array('image_id' => $image->getId()));
                 $mappingID = Shopware()->Db()->lastInsertId();
                 Shopware()->Db()->insert('s_article_img_mapping_rules', array('mapping_id' => $mappingID, 'option_id' => $optionModel->getId()));
             }
             $recreateImagesLater[] = $article->getId();
         }
         // Prevent multiple images from being a preview
         if ((int) $imageData['main'] === 1) {
             Shopware()->Db()->update('s_articles_img', array('main' => 2), array('articleID = ?' => $article->getId(), 'id <> ?' => $image->getId()));
         }
         $total++;
     }
     try {
         // Clear the entity manager and rebuild images in order to get proper variant images
         Shopware()->Models()->clear();
         foreach ($recreateImagesLater as $articleId) {
             $this->recreateVariantImages($articleId);
         }
     } catch (\Exception $e) {
         $errors[] = sprintf("Error building variant images. If no other errors occurred, the images have been\n                uploaded but the image-variant mapping in the shop frontend might fail. Errormessage: %s", $e->getMessage());
     }
     if (!empty($errors)) {
         $errors = $this->toUtf8($errors);
         $message = implode("<br>\n", $errors);
         echo json_encode(array('success' => false, 'message' => sprintf("Errors: {$message}")));
         return;
     }
     echo json_encode(array('success' => true, 'message' => sprintf("Successfully uploaded %s of %s Images", $total, $counter)));
     return;
 }