Working import stock

modifié:         core/lib/Thelia/Controller/Admin/ExportController.php
	modifié:         core/lib/Thelia/Controller/Admin/ImportController.php
	modifié:         core/lib/Thelia/Core/Event/ImportExport.php
	modifié:         core/lib/Thelia/Core/Event/TheliaEvents.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/Formatter/JsonFormatter.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php
	modifié:         core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php
	modifié:         templates/backOffice/default/ajax/import-modal.html
	modifié:         templates/backOffice/default/import-page.html
This commit is contained in:
Benjamin Perche
2014-07-17 17:00:53 +02:00
parent 069d2a07d9
commit cb75c13716
10 changed files with 131 additions and 50 deletions

View File

@@ -166,12 +166,14 @@ class ExportController extends BaseAdminController
$filename = $formatter::FILENAME . "." . $formatter->getExtension();
if ($archiveBuilder === null) {
$this->dispatch(TheliaEvents::BEFORE_EXPORT, $event);
$this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
$formattedContent = $formatter->encode($data);
$this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
return new Response(
$formattedContent,
$event->getContent(),
200,
[
"Content-Type" => $formatter->getMimeType(),
@@ -181,10 +183,12 @@ class ExportController extends BaseAdminController
);
} else {
$event->setArchiveBuilder($archiveBuilder);
$this->dispatch(TheliaEvents::BEFORE_EXPORT, $event);
$this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
$formattedContent = $formatter->encode($data);
$this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
if ($includeImages && $handler instanceof ImagesExportInterface) {
$this->processExportImages($handler, $archiveBuilder);
}
@@ -194,7 +198,7 @@ class ExportController extends BaseAdminController
}
$archiveBuilder->addFileFromString(
$formattedContent, $filename
$event->getContent(), $filename
);
return $archiveBuilder->buildArchiveResponse($formatter::FILENAME);

View File

@@ -11,6 +11,8 @@
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Core\Event\ImportExport as ImportExportEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\FileFormat\Archive\ArchiveBuilderManagerTrait;
use Thelia\Core\FileFormat\Formatting\FormatterManagerTrait;
use Thelia\Core\HttpFoundation\Response;
@@ -97,19 +99,20 @@ class ImportController extends BaseAdminController
/** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder $archiveBuilder */
$archiveBuilder = null;
foreach ($formats as $format) {
foreach ($formats as $objectName => $format) {
$formatLength = strlen($format);
if ($nameLength >= $formatLength && substr($name, -$formatLength) === $formatLength) {
$formatExtension = substr($name, -$formatLength);
if ($nameLength >= $formatLength && $formatExtension === $format) {
$uploadFormat = $format;
$flip = array_flip($format);
try {
$formatter = $formatterManager->get($flip[$format]);
$formatter = $formatterManager->get($objectName);
} catch(\OutOfBoundsException $e) {}
try {
$archiveBuilder = $archiveBuilderManager->get($flip[$format]);
$archiveBuilder = $archiveBuilderManager->get($objectName);
} catch(\OutOfBoundsException $e) {}
break;
@@ -124,8 +127,6 @@ class ImportController extends BaseAdminController
}
if ($uploadFormat === null) {
throw new FormValidationException(
$this->getTranslator()->trans(
"The extension \"%ext\" is not allowed",
@@ -144,14 +145,35 @@ class ImportController extends BaseAdminController
$content = null;
/**
* TODO: HANDLE
* Check expected file names for each formatter
*/
$fileNames = [];
/** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */
foreach ($formatterManager->getFormattersByTypes($types) as $formatter) {
$fileName = $formatter::FILENAME . "." . $formatter->getExtension();
$fileNames[] = $fileName;
if ($archiveBuilder->hasFile($fileName)) {
$content = $archiveBuilder->getFileContent($fileName);
break;
}
}
if ($content === null) {
throw new \ErrorException(
$this->getTranslator()->trans(
"Your archive must contain one of these file and doesn't: %files",
[
"%files" => implode(", ", $fileNames),
]
)
);
}
} elseif ($formatter !== null) {
/**
* If the file isn't
* If the file isn't an archive
*/
$content = file_get_contents($file->getPathname());
} else {
@@ -166,11 +188,28 @@ class ImportController extends BaseAdminController
);
}
$event = new ImportExportEvent($formatter, $handler, null, $archiveBuilder);
$event->setContent($content);
$this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event);
$data = $formatter->decode($content);
// Dispatch event
$event->setContent(null)->setData($data);
$this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event);
$handler->retrieveFromFormatterData($data);
$errors = $handler->retrieveFromFormatterData($data);
if (!empty($errors)) {
throw new \Exception(
$this->getTranslator()->trans(
"Errors occurred while importing the file: %errors",
[
"%errors" => implode(", ", $errors),
]
)
);
}
$successMessage = $this->getTranslator()->trans("Import successfully done");