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:
@@ -37,5 +37,21 @@
|
||||
<description>Export the prices of the products excluding taxes</description>
|
||||
</export_descriptive>
|
||||
</export>
|
||||
|
||||
<export id="thelia.export.product_seo" class="Thelia\ImportExport\Export\Type\ProductSEOExport" category_id="thelia.export.products">
|
||||
<export_descriptive locale="en_US">
|
||||
<title>Product SEO information</title>
|
||||
<description>
|
||||
Export the SEO information ( rewritten url, meta description and keywords, page title ) of your products
|
||||
</description>
|
||||
</export_descriptive>
|
||||
<export_descriptive locale="fr_FR">
|
||||
<title>Informations SEO des produits</title>
|
||||
<description>
|
||||
Exportez les informations SEO de vos produits
|
||||
( url réécrites, meta description et mots clés, titre ) de vos produits
|
||||
</description>
|
||||
</export_descriptive>
|
||||
</export>
|
||||
</exports>
|
||||
</config>
|
||||
|
||||
@@ -180,7 +180,10 @@ class ExportController extends BaseAdminController
|
||||
if ($archiveBuilder === null) {
|
||||
$this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
|
||||
|
||||
$formattedContent = $formatter->encode($data);
|
||||
$formattedContent = $formatter
|
||||
->setOrder($handler->getOrder())
|
||||
->encode($data)
|
||||
;
|
||||
|
||||
$this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
|
||||
|
||||
@@ -197,7 +200,10 @@ class ExportController extends BaseAdminController
|
||||
$event->setArchiveBuilder($archiveBuilder);
|
||||
$this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event);
|
||||
|
||||
$formattedContent = $formatter->encode($data);
|
||||
$formattedContent = $formatter
|
||||
->setOrder($handler->getOrder())
|
||||
->encode($data)
|
||||
;
|
||||
|
||||
$this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent));
|
||||
|
||||
|
||||
@@ -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
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,8 +11,6 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\ImportExport\Export;
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\Join;
|
||||
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
||||
use Thelia\Model\Lang;
|
||||
use Thelia\ImportExport\AbstractHandler;
|
||||
@@ -26,6 +24,9 @@ abstract class ExportHandler extends AbstractHandler
|
||||
{
|
||||
protected $locale;
|
||||
|
||||
/** @var array */
|
||||
protected $order;
|
||||
|
||||
public function addI18nCondition(
|
||||
ModelCriteria $query,
|
||||
$i18nTableName,
|
||||
@@ -80,6 +81,42 @@ abstract class ExportHandler extends AbstractHandler
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* You may override this method to return an array, containing
|
||||
* the order that you want to have for your columns.
|
||||
* The order appliance depends on the formatter
|
||||
*/
|
||||
protected function getDefaultOrder()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*
|
||||
* Use this method to access the order.
|
||||
*
|
||||
*/
|
||||
public function getOrder()
|
||||
{
|
||||
$order = $this->getDefaultOrder();
|
||||
|
||||
if (empty($order)) {
|
||||
$order = $this->order;
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
public function setOrder(array $order)
|
||||
{
|
||||
$this->order = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Thelia\Model\Lang $lang
|
||||
* @return \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
|
||||
@@ -116,11 +116,24 @@ class ProductSEOExport extends ExportHandler
|
||||
])
|
||||
;
|
||||
|
||||
$a = $query->toString();
|
||||
|
||||
$data = new FormatterData($aliases);
|
||||
|
||||
return $data->loadModelCriteria($query);
|
||||
}
|
||||
|
||||
protected function getDefaultOrder()
|
||||
{
|
||||
return [
|
||||
"ref",
|
||||
"product_title",
|
||||
"visible",
|
||||
"url",
|
||||
"content_title",
|
||||
"meta_description",
|
||||
"meta_keywords",
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user