Refactor ExportHandler to allow queries override
modifié: core/lib/Thelia/Controller/Admin/ExportController.php modifié: core/lib/Thelia/ImportExport/Export/ExportHandler.php modifié: core/lib/Thelia/ImportExport/Export/Type/MailingExport.php modifié: core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php modifié: core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php modifié: core/lib/Thelia/Tests/ImportExport/Export/MailingExportTest.php modifié: core/lib/Thelia/Tests/ImportExport/Export/ProductPricesExportTest.php modifié: core/lib/Thelia/Tests/ImportExport/Export/ProductSEOExportTest.php
This commit is contained in:
@@ -169,7 +169,7 @@ class ExportController extends BaseAdminController
|
||||
* Used for specific configuration (e.g: XML node names)
|
||||
*/
|
||||
$data = $handler
|
||||
->buildFormatterData($lang)
|
||||
->buildData($lang)
|
||||
->setLang($lang)
|
||||
;
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\ImportExport\Export;
|
||||
use Propel\Runtime\ActiveQuery\Criterion\Exception\InvalidValueException;
|
||||
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
||||
use Thelia\Core\FileFormat\Formatting\FormatterData;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Model\Lang;
|
||||
use Thelia\ImportExport\AbstractHandler;
|
||||
|
||||
@@ -123,6 +126,40 @@ abstract class ExportHandler extends AbstractHandler
|
||||
*
|
||||
* The method builds the FormatterData for the formatter
|
||||
*/
|
||||
abstract public function buildFormatterData(Lang $lang);
|
||||
public function buildData(Lang $lang)
|
||||
{
|
||||
$data = new FormatterData($this->getAliases());
|
||||
|
||||
$query = $this->buildDataSet($lang);
|
||||
|
||||
if ($query instanceof ModelCriteria) {
|
||||
return $data->loadModelCriteria($query);
|
||||
} elseif (is_array($query)) {
|
||||
return $data->setData($query);
|
||||
}
|
||||
|
||||
throw new InvalidValueException(
|
||||
Translator::getInstance()->trans(
|
||||
"The method \"%class\"::buildDataSet must return an array or a ModelCriteria",
|
||||
[
|
||||
"%class" => get_class($this),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|array
|
||||
*
|
||||
*/
|
||||
protected function getAliases()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Lang $lang
|
||||
* @return ModelCriteria|array
|
||||
*/
|
||||
abstract protected function buildDataSet(Lang $lang);
|
||||
}
|
||||
@@ -28,30 +28,11 @@ use Thelia\Model\NewsletterQuery;
|
||||
class MailingExport extends ExportHandler
|
||||
{
|
||||
/**
|
||||
* @return \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
*
|
||||
* The method builds
|
||||
* @param Lang $lang
|
||||
* @return array|\Propel\Runtime\ActiveQuery\ModelCriteria
|
||||
*/
|
||||
public function buildFormatterData(Lang $lang)
|
||||
public function buildDataSet(Lang $lang)
|
||||
{
|
||||
$email = "email";
|
||||
$lastName = "last_name";
|
||||
$firstName = "first_name";
|
||||
|
||||
|
||||
$aliases = [
|
||||
NewsletterTableMap::EMAIL => $email,
|
||||
CustomerTableMap::EMAIL => $email,
|
||||
|
||||
NewsletterTableMap::LASTNAME => $lastName,
|
||||
CustomerTableMap::LASTNAME => $lastName,
|
||||
|
||||
NewsletterTableMap::FIRSTNAME => $firstName,
|
||||
CustomerTableMap::FIRSTNAME => $firstName,
|
||||
];
|
||||
|
||||
$data = new FormatterData($aliases);
|
||||
|
||||
$newsletter = NewsletterQuery::create()
|
||||
->select([
|
||||
NewsletterTableMap::EMAIL,
|
||||
@@ -72,9 +53,25 @@ class MailingExport extends ExportHandler
|
||||
->toArray()
|
||||
;
|
||||
|
||||
$both = $newsletter + $customers;
|
||||
return $customers + $newsletter;
|
||||
}
|
||||
|
||||
return $data->setData($both);
|
||||
protected function getAliases()
|
||||
{
|
||||
$email = "email";
|
||||
$lastName = "last_name";
|
||||
$firstName = "first_name";
|
||||
|
||||
return [
|
||||
NewsletterTableMap::EMAIL => $email,
|
||||
CustomerTableMap::EMAIL => $email,
|
||||
|
||||
NewsletterTableMap::LASTNAME => $lastName,
|
||||
CustomerTableMap::LASTNAME => $lastName,
|
||||
|
||||
NewsletterTableMap::FIRSTNAME => $firstName,
|
||||
CustomerTableMap::FIRSTNAME => $firstName,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,23 +58,10 @@ class ProductPricesExport extends ExportHandler
|
||||
|
||||
/**
|
||||
* @param Lang $lang
|
||||
* @return \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
*
|
||||
* The method builds the FormatterData for the formatter
|
||||
* @return FormatterData
|
||||
*/
|
||||
public function buildFormatterData(Lang $lang)
|
||||
protected function buildDataSet(Lang $lang)
|
||||
{
|
||||
$aliases = [
|
||||
"product_sale_elements_REF" => "ref",
|
||||
"product_sale_elements_EAN_CODE" => "ean",
|
||||
"price_PRICE" => "price",
|
||||
"price_PROMO_PRICE" => "promo_price",
|
||||
"currency_CODE" => "currency",
|
||||
"product_TITLE" => "title",
|
||||
"attribute_av_i18n_ATTRIBUTES" => "attributes",
|
||||
"product_sale_elements_PROMO" => "promo",
|
||||
];
|
||||
|
||||
$locale = $lang->getLocale();
|
||||
|
||||
$productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN);
|
||||
@@ -122,9 +109,22 @@ class ProductPricesExport extends ExportHandler
|
||||
->groupBy("product_sale_elements_REF")
|
||||
;
|
||||
|
||||
$data = new FormatterData($aliases);
|
||||
|
||||
return $data->loadModelCriteria($query);
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function getAliases()
|
||||
{
|
||||
return [
|
||||
"product_sale_elements_REF" => "ref",
|
||||
"product_sale_elements_EAN_CODE" => "ean",
|
||||
"price_PRICE" => "price",
|
||||
"price_PROMO_PRICE" => "promo_price",
|
||||
"currency_CODE" => "currency",
|
||||
"product_TITLE" => "title",
|
||||
"attribute_av_i18n_ATTRIBUTES" => "attributes",
|
||||
"product_sale_elements_PROMO" => "promo",
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,8 +16,6 @@ use Propel\Runtime\ActiveQuery\Join;
|
||||
use Thelia\Core\FileFormat\Formatting\FormatterData;
|
||||
use Thelia\Core\FileFormat\FormatType;
|
||||
use Thelia\ImportExport\Export\ExportHandler;
|
||||
use Thelia\Model\Map\ContentI18nTableMap;
|
||||
use Thelia\Model\Map\ContentTableMap;
|
||||
use Thelia\Model\Map\ProductI18nTableMap;
|
||||
use Thelia\Model\Map\ProductTableMap;
|
||||
use Thelia\Model\Map\RewritingUrlTableMap;
|
||||
@@ -56,23 +54,11 @@ class ProductSEOExport extends ExportHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Thelia\Model\Lang $lang
|
||||
* @return \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
*
|
||||
* The method builds the FormatterData for the formatter
|
||||
* @param Lang $lang
|
||||
* @return array|\Propel\Runtime\ActiveQuery\ModelCriteria
|
||||
*/
|
||||
public function buildFormatterData(Lang $lang)
|
||||
public function buildDataSet(Lang $lang)
|
||||
{
|
||||
$aliases = [
|
||||
"product_REF" => "ref",
|
||||
"product_VISIBLE" => "visible",
|
||||
"product_i18n_TITLE" => "product_title",
|
||||
"product_URL" => "url",
|
||||
"product_seo_TITLE" => "page_title",
|
||||
"product_seo_META_DESCRIPTION" => "meta_description",
|
||||
"product_seo_META_KEYWORDS" => "meta_keywords",
|
||||
];
|
||||
|
||||
$locale = $this->locale = $lang->getLocale();
|
||||
|
||||
/**
|
||||
@@ -111,9 +97,7 @@ class ProductSEOExport extends ExportHandler
|
||||
])
|
||||
;
|
||||
|
||||
$data = new FormatterData($aliases);
|
||||
|
||||
return $data->loadModelCriteria($query);
|
||||
return $query;
|
||||
}
|
||||
|
||||
protected function getDefaultOrder()
|
||||
@@ -129,6 +113,17 @@ class ProductSEOExport extends ExportHandler
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected function getAliases()
|
||||
{
|
||||
return [
|
||||
"product_REF" => "ref",
|
||||
"product_VISIBLE" => "visible",
|
||||
"product_i18n_TITLE" => "product_title",
|
||||
"product_URL" => "url",
|
||||
"product_seo_TITLE" => "page_title",
|
||||
"product_seo_META_DESCRIPTION" => "meta_description",
|
||||
"product_seo_META_KEYWORDS" => "meta_keywords",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\Container;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Core\FileFormat\FormatType;
|
||||
use Thelia\ImportExport\Export\Type\MailingExport;
|
||||
use Thelia\Model\Lang;
|
||||
|
||||
/**
|
||||
* Class MailingExportTest
|
||||
@@ -37,8 +38,7 @@ class MailingExportTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testExport()
|
||||
{
|
||||
$data = $this->handler->buildFormatterData();
|
||||
|
||||
$data = $this->handler->buildData(Lang::getDefaultLanguage());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ class ProductPricesExportTest extends \PHPUnit_Framework_TestCase
|
||||
new Translator(new Container());
|
||||
$export = new ProductPricesExport(new Container());
|
||||
|
||||
$data = $export->buildFormatterData(Lang::getDefaultLanguage());
|
||||
$data = $export->buildData(Lang::getDefaultLanguage());
|
||||
|
||||
$keys = ["attributes","currency","ean","price","promo","promo_price","ref","title"];
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ class ProductSEOExportTest extends \PHPUnit_Framework_TestCase
|
||||
new Translator(new Container());
|
||||
$export = new ProductSEOExport(new Container());
|
||||
|
||||
$data = $export->buildFormatterData(Lang::getDefaultLanguage());
|
||||
$data = $export->buildData(Lang::getDefaultLanguage());
|
||||
|
||||
$keys=["ref","visible","product_title","url","page_title","meta_description","meta_keywords",];
|
||||
sort($keys);
|
||||
|
||||
Reference in New Issue
Block a user