Fix form hidden fields and begin export controller

modifié:         core/lib/Thelia/Controller/Admin/ImportExportController.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php
	modifié:         templates/backOffice/default/ajax/export-modal.html
	modifié:         templates/backOffice/default/export-page.html
This commit is contained in:
Benjamin Perche
2014-07-11 16:59:26 +02:00
parent b6937ab421
commit 7e995e38d5
4 changed files with 126 additions and 24 deletions

View File

@@ -11,9 +11,12 @@
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Loop\Export as ExportLoop;
use Thelia\Core\Template\Loop\Import as ImportLoop;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Form\ExportForm;
use Thelia\Model\ExportQuery;
/**
@@ -23,6 +26,19 @@ use Thelia\Model\ExportQuery;
*/
class ImportExportController extends BaseAdminController
{
/** @var \Thelia\Core\FileFormat\Archive\ArchiveBuilderManager */
protected $archiveBuilderManager;
/** @var \Thelia\Core\FileFormat\Formatting\FormatterManager */
protected $formatterManager;
public function hydrate()
{
$this->archiveBuilderManager = $this->container->get("thelia.manager.archive_builder_manager");
$this->formatterManager = $this->container->get("thelia.manager.formatter_manager");
}
public function import($id)
{
@@ -30,7 +46,93 @@ class ImportExportController extends BaseAdminController
public function export($id)
{
if (null === $export = $this->getExport($id)) {
return $this->render("404");
}
/**
* Get needed services
*/
$this->hydrate();
/**
* Get the archive builders
*/
$archiveBuilders = [];
foreach ($this->archiveBuilderManager->getNames() as $archiveBuilder) {
$archiveBuilders[$archiveBuilder] = $archiveBuilder;
}
/**
* Get the allowed formatters to inject them into the form
*/
$handler = $export->getHandleClassInstance($this->container);
$types = $handler->getHandledType();
if (!is_array($types)) {
$types = [$types];
}
$formatters = [];
/** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */
foreach ($this->formatterManager->getAll() as $formatter) {
if (in_array($formatter->getExportType(), $types)) {
$formatters[$formatter->getName()] = $formatter->getName();
}
}
/**
* Define and validate the form
*/
$form = new ExportForm($this->getRequest());
$form->setArchiveBuilderNames($archiveBuilders);
$form->setFormatters($formatters);
$errorMessage = null;
try {
$boundForm = $this->validateForm($form);
$data = $handler->buildFormatterData();
$formatter = $this->formatterManager->get(
$boundForm->get("formatter")->getData()
);
$formattedContent = $formatter->encode($data);
if (!$boundForm->get("do_compress")->getData())
{
return new Response(
$formattedContent,
200,
[
"Content-Type" => $formatter->getMimeType(),
"Content-Disposition" => $formatter::FILENAME . "." . $formatter->getExtension(),
]
);
}
} catch(FormValidationException $e) {
$errorMessage = $this->createStandardFormValidationErrorMessage($e);
} catch(\Exception $e) {
$errorMessage = $e->getMessage();
}
/**
* If has an error, display it
*/
if (null !== $errorMessage) {
$form->setErrorMessage($errorMessage);
$this->getParserContext()
->addForm($form)
->setGeneralError($errorMessage)
;
}
return $this->exportView($id);
}
public function importView($id)

View File

@@ -22,31 +22,18 @@ use Thelia\Log\Tlog;
*/
abstract class AbstractFormatter implements FormatInterface, FormatterInterface
{
const FILENAME = "export";
/** @var \Thelia\Core\Translation\Translator */
protected $translator;
/** @var \Thelia\Log\Tlog */
protected $logger;
/** @var array */
protected $aliases = array();
public function __construct()
{
$this->translator = Translator::getInstance();
$this->logger = Tlog::getInstance();
}
public function setAliases(array $aliases)
{
$this->aliases = $aliases;
return $this;
}
public function getAliases()
{
return $this->aliases;
}
}

View File

@@ -1,5 +1,6 @@
{form name="thelia.export"}
<form action="{$URL}" method="post" {form_enctype form=$form}>
{form_hidden_fields form=$form}
<div class="modal fade" id="real-export-modal" tabindex="-1" role="dialog" aria-labelledby="export-modal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">

View File

@@ -21,6 +21,16 @@
</ul>
</nav>
{form name="thelia.export"}
{if $form_error}
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger">{$form_error_message}</div>
</div>
</div>
{/if}
<div class="row">
<div class="col-md-6">
<div class="general-block-decorator">
@@ -29,21 +39,22 @@
{intl l='Export: '}{$TITLE}
</div>
{form name="thelia.export"}
<form action="{$URL}" method="post" {form_enctype form=$form}>
{form_hidden_fields form=$form}
<div class="row">
<div class="col-md-12">
{ifloop rel="export-formatters"}
<div class="row">
<div class="col-md-6">
{custom_render_form_field form=$form field="formatter"}
<select>
{loop name="export-formatters" type="formatter" export=$ID}
<option value="{$NAME}" {if $value == $NAME}selected{/if}>
{$NAME} (.{$EXTENSION})
</option>
{/loop}
</select>
<select>
{loop name="export-formatters" type="formatter" export=$ID}
<option value="{$NAME}" {if $value == $NAME}selected{/if}>
{$NAME} (.{$EXTENSION})
</option>
{/loop}
</select>
{/custom_render_form_field}
</div>
<div class="col-md-6">
@@ -107,11 +118,12 @@
<button type="submit" class="btn btn-primary">{intl l="Export"}</button>
</form>
{/form}
</div>
</div>
</div>
</div>
{/form}
{/block}
{block name="javascript-initialization"}