/** * Einfügen von Artikelbildern * Hinweis: Falls keine anderen Bilder hinterlegt sind, wird das zuerst eingefügte Bild automatisch zum Hauptbild * * @param array $article_image Array mit Informationen �ber die Bilder die konvertiert und dem Artikel zugewiesen werden müssen * * articleID => ID des Artikels (s_articles.id) * * position => optional, Position des Bildes (fortlaufend 1 bis x) * * descripton => optional, Title/Alt-Text des Bildes * * main => Wenn 1 übergeben wird, wird das Bild zum Hauptbild (für Übersichten etc.) * * image => Http-Link oder lokaler Pfad zum Bild * * <code> * * Beispiel Laden von lokalen Bildern: * foreach ($article["images"] as $image) * { * $image["articleID"] = $image["articleID"]; * $image["main"] = $image["mainpicture"]; * $image["image"] = $api->load("file://".$api->sPath."/engine/connectors/api/sample/images/".$image["file"]); * $import->sArticleImage($image); * } * * Beispiel Laden von externen Bildern: * foreach ($article["images"] as $image) * { * $image["articleID"] = $image["articleID"]; * $image["main"] = $image["mainpicture"]; * $image["image"] = $api->load("ftp://*****:*****@example.org/httpdocs/dbase/images/articles/".$image["file"]); * $import->sArticleImage($image); * } * * </code> * @access public * @return */ public function sArticleImage($article_image = array()) { // Some initial checks if (empty($article_image) || !is_array($article_image)) { return false; } if (isset($article_image["link"])) { $article_image["image"] = $article_image["link"]; } if (isset($article_image["albumID"])) { $article_image["albumID"] = (int) $article_image["albumID"]; } if (isset($article_image['articleID'])) { $article_image['articleID'] = intval($article_image['articleID']); } if (empty($article_image['articleID']) || empty($article_image['image']) && empty($article_image['name'])) { return false; } if (!empty($article_image['position'])) { $article_image['position'] = intval($article_image['position']); } else { $article_image['position'] = 0; } if (empty($article_image['description'])) { $article_image['description'] = ''; } if (empty($article_image['relations'])) { $article_image['relations'] = ''; } // Checks for main flag if (!empty($article_image['main']) && $article_image['main'] == 1) { $article_image['main'] = 1; } elseif (!empty($article_image['main'])) { $article_image['main'] = 2; } // If main is not set and no other article image is main, set this to be main if (empty($article_image['main'])) { $sql = "SELECT id FROM s_articles_img WHERE articleID={$article_image['articleID']} AND main=1"; $row = $this->sDB->GetRow($sql); if (empty($row['id'])) { $article_image['main'] = 1; } else { $article_image['main'] = 2; } // if this article image is set to be main, set all other images to be not-main } elseif ($article_image['main'] == 1) { $sql = "UPDATE s_articles_img SET main=2 WHERE articleID={$article_image['articleID']}"; $this->sDB->Execute($sql); } // First copy the image to temp dir $uploadDir = Shopware()->DocPath('media_' . 'temp'); // If no name is set, choose a random one if (empty($article_image['name'])) { $article_image['name'] = md5(uniqid(mt_rand(), true)); } else { $article_image['name'] = pathinfo($article_image['name'], PATHINFO_FILENAME); } // Copy image to local temp dir if (!empty($article_image['image'])) { $uploadFile = $uploadDir . $article_image['name']; if (!copy($article_image['image'], $uploadFile)) { $this->sAPI->sSetError("Copying image from '{$article_image['image']}' to '{$uploadFile}' did not work", 10400); return false; } // check the copied image if (getimagesize($uploadFile) === false) { unlink($uploadFile); $this->sAPI->sSetError("The file'{$uploadFile}' is not a valid image", 10402); return false; } } else { foreach (array('png', 'gif', 'jpg') as $test) { if (file_exists($uploadDir . $article_image['name'] . '.' . $test)) { $extension = $test; $uploadFile = $uploadDir . $article_image['name'] . '.' . $test; break; } } if (empty($uploadFile)) { $this->sAPI->sSetError("Image source '{$uploadFile}' not found", 10401); return false; } } // Create new Media object and set the image $media = new \Shopware\Models\Media\Media(); $file = new \Symfony\Component\HttpFoundation\File\File($uploadFile); $media->setDescription($article_image['description']); $media->setCreated(new DateTime()); $identity = Shopware()->Auth()->getIdentity(); if ($identity !== null) { $media->setUserId($identity->id); } else { $media->setUserId(0); } //set the upload file into the model. The model saves the file to the directory $media->setFile($file); // Set article $articleRepository = $this->getArticleRepository(); $article = $articleRepository->find((int) $article_image['articleID']); if ($article === null) { $this->sAPI->sSetError("Article '{$article_image['articleID']}' not found", 10404); return false; } // $media->setArticles($article); //set media album, if no one passed set the unsorted album. if (isset($article_image["albumID"])) { $media->setAlbumId($article_image["albumID"]); $media->setAlbum(Shopware()->Models()->find('Shopware\\Models\\Media\\Album', $article_image["albumID"])); } else { $media->setAlbumId(-1); $media->setAlbum(Shopware()->Models()->find('Shopware\\Models\\Media\\Album', -1)); } // Create new article image object and set values $articleImage = new \Shopware\Models\Article\Image(); // Get image infos for the article image list($width, $height, $type, $attr) = getimagesize($uploadFile); // ArticleImage does not get these infos automatically from media, so we set them manually $articleImage->setDescription($article_image['description']); $articleImage->setMedia($media); $articleImage->setArticle($article); $articleImage->setWidth($width); $articleImage->setHeight($height); $articleImage->setHeight($height); $articleImage->setPath($article_image['name']); $articleImage->setExtension($media->getExtension()); $articleImage->setPosition($article_image['position']); $articleImage->setMain($article_image['main']); $articleImage->setRelations($article_image['relations']); $article->setImages($articleImage); Shopware()->Models()->persist($media); Shopware()->Models()->persist($article); Shopware()->Models()->persist($articleImage); Shopware()->Models()->flush(); // if ($article_image['relations'] !== '') { // $this->createImageRelationsFromArticleNumber($articleImage->getId(), $article->getId(), $article_image['relations']); // } return $articleImage->getId(); }