Add BEFORE_EXPORT event and fix bug on modal

modifié:         core/lib/Thelia/Controller/Admin/ImportExportController.php
	nouveau fichier: core/lib/Thelia/Core/Event/ImportExport/Export.php
	modifié:         core/lib/Thelia/Core/Event/TheliaEvents.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/Formatter/XMLFormatter.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-15 11:04:14 +02:00
parent 994eaed7e1
commit 740869eba0
6 changed files with 174 additions and 34 deletions

View File

@@ -11,6 +11,8 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Controller\Admin; namespace Thelia\Controller\Admin;
use Thelia\Core\Event\ImportExport\Export as ExportEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Response; use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Loop\Export as ExportLoop; use Thelia\Core\Template\Loop\Export as ExportLoop;
@@ -115,12 +117,24 @@ class ImportExportController extends BaseAdminController
$boundForm->get("formatter")->getData() $boundForm->get("formatter")->getData()
); );
$formattedContent = $formatter->encode($data);
$filename = $formatter::FILENAME . "." . $formatter->getExtension(); $filename = $formatter::FILENAME . "." . $formatter->getExtension();
if (!$boundForm->get("do_compress")->getData()) { if (!$boundForm->get("do_compress")->getData()) {
/**
* Build an event containing the formatter and the handler.
* Used for specific configuration (e.g: XML node names)
*/
$event = new ExportEvent($formatter, $handler);
if (!$boundForm->get("do_compress")->getData())
{
/**
* Dispatch the event
*/
$this->dispatch(TheliaEvents::BEFORE_EXPORT, $event);
$formattedContent = $formatter->encode($data);
return new Response( return new Response(
$formattedContent, $formattedContent,
@@ -137,21 +151,31 @@ class ImportExportController extends BaseAdminController
$boundForm->get("archive_builder")->getData() $boundForm->get("archive_builder")->getData()
); );
/** $event->setArchiveBuilder($archiveBuilder);
* Put the images in the archive $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event);
*/
if ($boundForm->get("images")->getData() && $handler instanceof ImagesExportInterface) { $formattedContent = $formatter->encode($data);
foreach ($handler->getImagesPaths() as $image) {
$archiveBuilder->addFile($image, "images"); $includeImages = $boundForm->get("images")->getData();
$includeDocuments = $boundForm->get("documents")->getData();
if ($includeImages && $handler instanceof ImagesExportInterface) {
foreach ($handler->getImagesPaths() as $name => $documentPath) {
$archiveBuilder->addFile(
$documentPath,
$handler::IMAGES_DIRECTORY,
is_integer($name) ? null : $name
);
} }
} }
/** if ($includeDocuments && $handler instanceof DocumentsExportInterface) {
* Then the documents foreach ($handler->getDocumentsPaths() as $name => $documentPath) {
*/ $archiveBuilder->addFile(
if ($boundForm->get("documents")->getData() && $handler instanceof DocumentsExportInterface) { $documentPath,
foreach ($handler->getDocumentsPaths() as $document) { $handler::DOCUMENTS_DIRECTORY,
$archiveBuilder->addFile($document, "documents"); is_integer($name) ? null : $name
);
} }
} }

View File

@@ -0,0 +1,101 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Core\Event\ImportExport;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder;
use Thelia\Core\FileFormat\Formatting\AbstractFormatter;
use Thelia\ImportExport\ExportHandler;
/**
* Class Export
* @package Thelia\Core\Event\ImportExport
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class Export extends ActionEvent
{
/** @var \Thelia\ImportExport\ExportHandler */
protected $handler;
/** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter */
protected $formatter;
/** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder */
protected $archiveBuilder;
function __construct(
AbstractFormatter $formatter,
ExportHandler $handler,
AbstractArchiveBuilder $archiveBuilder = null
) {
$this->archiveBuilder = $archiveBuilder;
$this->formatter = $formatter;
$this->handler = $handler;
}
/**
* @param AbstractArchiveBuilder $archiveBuilder
* @return $this
*/
public function setArchiveBuilder(AbstractArchiveBuilder $archiveBuilder)
{
$this->archiveBuilder = $archiveBuilder;
return $this;
}
/**
* @return \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder
*/
public function getArchiveBuilder()
{
return $this->archiveBuilder;
}
/**
* @param AbstractFormatter $formatter
* @return $this
*/
public function setFormatter(AbstractFormatter $formatter)
{
$this->formatter = $formatter;
return $this;
}
/**
* @return \Thelia\Core\FileFormat\Formatting\AbstractFormatter
*/
public function getFormatter()
{
return $this->formatter;
}
/**
* @param ExportHandler $handler
* @return $this
*/
public function setHandler(ExportHandler $handler)
{
$this->handler = $handler;
return $this;
}
/**
* @return \Thelia\ImportExport\ExportHandler
*/
public function getHandler()
{
return $this->handler;
}
}

View File

@@ -766,4 +766,8 @@ final class TheliaEvents
const BEFORE_UPDATEBRAND = "action.before_updateBrand"; const BEFORE_UPDATEBRAND = "action.before_updateBrand";
const AFTER_UPDATEBRAND = "action.after_updateBrand"; const AFTER_UPDATEBRAND = "action.after_updateBrand";
// -- Export ----------------------------------------------
const BEFORE_EXPORT = "Thelia.before.export";
} }

View File

@@ -25,6 +25,7 @@ class XMLFormatter extends AbstractFormatter
{ {
public $root = "data"; public $root = "data";
public $nodeName = "node"; public $nodeName = "node";
public $rowName = "row";
/** /**
* @return string * @return string
@@ -85,7 +86,13 @@ class XMLFormatter extends AbstractFormatter
$node = $container->appendChild(new \DOMElement($this->nodeName)); $node = $container->appendChild(new \DOMElement($this->nodeName));
$this->recursiveBuild($entry, $node); $this->recursiveBuild($entry, $node);
} else { } else {
$container->appendChild(new \DOMElement($key, $entry)); $node = new \DOMElement($this->nodeName);
$container->appendChild($node);
/** @var \DOMElement $lastChild */
$lastChild = $container->lastChild;
$lastChild->setAttribute("name",$key);
$lastChild->setAttribute("value", $entry);
} }
} }
@@ -102,7 +109,13 @@ class XMLFormatter extends AbstractFormatter
$newNode = $node->appendChild(new \DOMElement($key)); $newNode = $node->appendChild(new \DOMElement($key));
$this->recursiveBuild($entry, $newNode); $this->recursiveBuild($entry, $newNode);
} else { } else {
$node->appendChild(new \DOMElement($key, $entry)); $inputNode = new \DOMElement($this->rowName);
$node->appendChild($inputNode);
/** @var \DOMElement $lastChild */
$lastChild = $node->lastChild;
$lastChild->setAttribute("name",$key);
$lastChild->setAttribute("value", $entry);
} }
} }
} }

View File

@@ -15,21 +15,16 @@
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-6">
{form_field form=$form field="formatter"} {form_field form=$form field="formatter"}
<div class="form-group {if $error}has-error{/if}"> <label for="{$label_attr.for}">
<label for="{$label_attr.for}"> {$label}
{$label} </label>
</label> <select name="{$name}" id="{$label_attr.for}">
<select name="{$name}" id="{$label_attr.for}"> {loop name="export-formatters" type="formatter" export=$ID}
{loop name="export-formatters" type="formatter" export=$ID} <option value="{$NAME}" {if $value == $NAME}selected{/if}>
<option value="{$NAME}" {if $value == $NAME}selected{/if}> {$NAME} (.{$EXTENSION})
{$NAME} (.{$EXTENSION}) </option>
</option> {/loop}
{/loop} </select>
</select>
{if $error}
<div class="error-field">{$message}</div>
{/if}
</div>
{/form_field} {/form_field}
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
@@ -45,7 +40,10 @@
</div> </div>
<div class="row export-compression-selection-row"> <div class="row export-compression-selection-row">
<div class="col-md-4"> <div class="col-md-4">
{custom_render_form_field form=$form field="archive_builder"} {form_field form=$form field="archive_builder"}
<label for="{$label_attr.for}">
{$label}
</label>
<select name="{$name}" id="{$label_attr.for}"> <select name="{$name}" id="{$label_attr.for}">
{loop name="export-archive-builder" type="archive-builder"} {loop name="export-archive-builder" type="archive-builder"}
<option value="{$NAME}" {if $value == $NAME}selected{/if}> <option value="{$NAME}" {if $value == $NAME}selected{/if}>
@@ -53,7 +51,7 @@
</option> </option>
{/loop} {/loop}
</select> </select>
{/custom_render_form_field} {/form_field}
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
{form_field form=$form field="images"} {form_field form=$form field="images"}

View File

@@ -1,7 +1,7 @@
{extends file="admin-layout.tpl"} {extends file="admin-layout.tpl"}
{block name="no-return-functions"} {block name="no-return-functions"}
{$admin_current_location = 'modules'} {$admin_current_location = 'tools'}
{/block} {/block}
{block name="page-title"}{intl l='Export'}: {$TITLE}{/block} {block name="page-title"}{intl l='Export'}: {$TITLE}{/block}