Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 014a0db7 authored by Nicolas's avatar Nicolas
Browse files

Fonctionnalité d'upload de fichier terminée

parent 32ba1017
Branches wip-game
No related tags found
No related merge requests found
......@@ -11,7 +11,9 @@ use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \NicolasBejean\Importer\Model\ImageUploader;
use \Exception;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\InputException;
use \Magento\Framework\Exception\LocalizedException;
......@@ -64,6 +66,11 @@ class Uploader extends Action
*/
protected $delimiter;
/**
* @var ImageUploader
*/
protected $imageUploader;
/**
* Uploader constructor.
*
......@@ -73,6 +80,7 @@ class Uploader extends Action
* @param ProductImageProcessor $productImageProcessor
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param ImageUploader $imageUploader
*/
public function __construct(
Context $context,
......@@ -80,18 +88,21 @@ class Uploader extends Action
ProductRepositoryInterface $productInterface,
ProductImageProcessor $productImageProcessor,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
ImageUploader $imageUploader
) {
parent::__construct($context);
$this->requestHttp = $requestHttp;
$this->requestHttp = $requestHttp;
$this->productInterface = $productInterface;
$this->productImageProcessor = $productImageProcessor;
$this->productInterface = $productInterface;
$this->productImageProcessor = $productImageProcessor;
/* Pour récupérer les paramétres du module */
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->imageUploader = $imageUploader;
$this->delimiter = $this->getDelimiter();
}
......@@ -115,31 +126,19 @@ class Uploader extends Action
{
if ($this->requestHttp->getParam('isAjax')) {
$files = $this->requestHttp->getFiles();
/** @var array $file */
foreach ($files as $file) {
$sku = $this->getSku($file);
/* Recherche des produits existants */
try {
/** @var Product $product */
$product = $this->productInterface->get($sku);
} catch (NoSuchEntityException $e) {
echo 'no-product';
exit;
}
/* Supprime tous les rôles assignés aux images d'un produit */
$this->clearMediaAttribute($product);
/* Supprime toutes les images d'un produit */
$this->deleteProductGallery($product);
/* Récupère le fichier envoyé dans la requête */
$file = $this->getRequest()->getFiles('file');
echo 'product-found';
try {
/* Enregistre le fichier dans un dossier temporaire */
$result = $this->imageUploader->saveFileToTmpDir($file);
} catch (Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
echo 'upload-error';
exit();
}
echo 'upload-ok';
}
}
......@@ -157,56 +156,4 @@ class Uploader extends Action
return $this->scopeConfig->getValue('importimagesproduct/pattern/delimiter', ScopeInterface::SCOPE_STORE, $storeId);
}
/**
* Retourne le SKU en fonction du nom du fichier
*
* @param array $file
* @return string
*/
private function getSku ($file) {
/* On récupère le nom du fichier */
list($fileName) = explode('.', $file['name']);
/* On récupère le sku */
list($sku) = explode($this->delimiter, $fileName);
return $sku;
}
/**
* Supprime tous les rôles assignés aux images d'un produit
*
* @param Product $product
*/
private function clearMediaAttribute (Product $product) {
$this->productImageProcessor->clearMediaAttribute($product, $this->productImageProcessor->getMediaAttributeCodes());
try {
$this->productInterface->save($product);
} catch (CouldNotSaveException $e) {
} catch (InputException $e) {
} catch (StateException $e) {
}
}
/**
* Supprime toutes les images d'un produit
*
* @param Product $product
*/
private function deleteProductGallery (Product $product) {
$existingGallery = $product->getMediaGalleryImages();
foreach ($existingGallery as $image) {
$this->productImageProcessor->removeImage($product, $image->getFile());
}
try {
$this->productInterface->save($product);
} catch (CouldNotSaveException $e) {
} catch (InputException $e) {
} catch (StateException $e) {
}
}
}
......@@ -52,8 +52,8 @@ use \NicolasBejean\Importer\Block\Adminhtml\ProductImages\Index;
</div>
</div>
<div class="alert-modal-content no-product">
<p>No product</p>
<div class="alert-modal-content upload-error">
<p><?php echo __('Upload error') ?></p>
</div>
<div class="alert-modal-content product-found">
<p>Product Found</p>
......
......@@ -68,12 +68,11 @@ require([
});
myDropzone.on("success", function (file, response) {
if (response === 'no-product') {
if (response === 'upload-error') {
/* Ouvre la modal correspondante */
$('.alert-modal-content.no-product').modal("openModal");
} else if (response === 'product-found') {
/* Ouvre la modal correspondante */
$('.alert-modal-content.product-found').modal("openModal");
$('.alert-modal-content.upload-error').modal("openModal");
} else if (response === 'upload-ok') {
myDropzone.removeFile(file);
}
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment