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;
use Thelia\Core\Event\ImportExport\Export as ExportEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Loop\Export as ExportLoop;
@@ -115,12 +117,24 @@ class ImportExportController extends BaseAdminController
$boundForm->get("formatter")->getData()
);
$formattedContent = $formatter->encode($data);
$filename = $formatter::FILENAME . "." . $formatter->getExtension();
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(
$formattedContent,
@@ -137,21 +151,31 @@ class ImportExportController extends BaseAdminController
$boundForm->get("archive_builder")->getData()
);
/**
* Put the images in the archive
*/
if ($boundForm->get("images")->getData() && $handler instanceof ImagesExportInterface) {
foreach ($handler->getImagesPaths() as $image) {
$archiveBuilder->addFile($image, "images");
$event->setArchiveBuilder($archiveBuilder);
$this->dispatch(TheliaEvents::BEFORE_EXPORT, $event);
$formattedContent = $formatter->encode($data);
$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
);
}
}
/**
* Then the documents
*/
if ($boundForm->get("documents")->getData() && $handler instanceof DocumentsExportInterface) {
foreach ($handler->getDocumentsPaths() as $document) {
$archiveBuilder->addFile($document, "documents");
if ($includeDocuments && $handler instanceof DocumentsExportInterface) {
foreach ($handler->getDocumentsPaths() as $name => $documentPath) {
$archiveBuilder->addFile(
$documentPath,
$handler::DOCUMENTS_DIRECTORY,
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 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 $nodeName = "node";
public $rowName = "row";
/**
* @return string
@@ -85,7 +86,13 @@ class XMLFormatter extends AbstractFormatter
$node = $container->appendChild(new \DOMElement($this->nodeName));
$this->recursiveBuild($entry, $node);
} 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));
$this->recursiveBuild($entry, $newNode);
} 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);
}
}
}