Add export order for CSV

modifié:         core/lib/Thelia/Config/Resources/export.xml
	modifié:         core/lib/Thelia/Controller/Admin/ExportController.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php
	modifié:         core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php
	modifié:         core/lib/Thelia/ImportExport/Export/ExportHandler.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php
This commit is contained in:
Benjamin Perche
2014-07-23 14:37:29 +02:00
parent f4f3a54e9c
commit 77db0d7408
6 changed files with 146 additions and 12 deletions

View File

@@ -30,10 +30,41 @@ abstract class AbstractFormatter implements FormatInterface, FormatterInterface
/** @var \Thelia\Log\Tlog */
protected $logger;
/** @var array */
protected $order;
public function __construct()
{
$this->translator = Translator::getInstance();
$this->logger = Tlog::getInstance();
}
public function getOrder()
{
return $this->order;
}
public function setOrder(array $order)
{
$this->order = $order;
return $this;
}
public function checkOrders(array $values)
{
foreach($this->order as $order) {
if (!array_key_exists($order, $values)) {
throw new \ErrorException(
$this->translator->trans(
"The column %column that you want to sort doesn't exist",
[
"%column" => $order
]
)
);
}
}
}
}

View File

@@ -77,10 +77,18 @@ class CSVFormatter extends AbstractFormatter
{
$string = "";
/**
* Get the first row and delimiters lengths
*/
$firstRow = $data->getRow();
$delimiterLength = strlen($this->delimiter);
$lineReturnLength = strlen($this->lineReturn);
/**
* check if $this->order doesn't have non-existing rows
*/
$this->checkOrders($firstRow);
if (false !== $firstRow) {
$rawKeys = array_keys($firstRow);
$keys = [];
@@ -93,20 +101,43 @@ class CSVFormatter extends AbstractFormatter
array_unshift($values, $keys);
while (null !== $row = array_shift($values)) {
foreach ($keys as $key) {
if (!is_scalar($row[$key])) {
$row[$key] = serialize($row[$key]);
}
/**
* First put the sorted ones
*/
foreach ($this->order as $order) {
$string .= $this->formatField($row[$order]);
unset($row[$order]);
}
$string .= $this->stringDelimiter . addslashes($row[$key]) . $this->stringDelimiter . $this->delimiter;
/**
* Then place the fields,
* order by name
*/
ksort($row);
foreach ($keys as $key) {
$string .= $this->formatField($row[$key]);
}
$string = substr($string,0, -$delimiterLength) . $this->lineReturn;
}
} else {
$lineReturnLength = 0;
}
return substr($string,0, -$lineReturnLength);
return substr($string, 0, -$lineReturnLength);
}
protected function formatField($value)
{
if($value === null) {
$value = "";
} else if (!is_scalar($value)) {
$value = serialize($value);
}
return $this->stringDelimiter . addslashes($value) . $this->stringDelimiter . $this->delimiter;
}
/**