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:
Benjamin Perche
2014-07-23 16:36:02 +02:00
parent 9dfff70838
commit c5c8b4c9e4
8 changed files with 98 additions and 69 deletions

View File

@@ -169,7 +169,7 @@ class ExportController extends BaseAdminController
* Used for specific configuration (e.g: XML node names) * Used for specific configuration (e.g: XML node names)
*/ */
$data = $handler $data = $handler
->buildFormatterData($lang) ->buildData($lang)
->setLang($lang) ->setLang($lang)
; ;

View File

@@ -11,7 +11,10 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\ImportExport\Export; namespace Thelia\ImportExport\Export;
use Propel\Runtime\ActiveQuery\Criterion\Exception\InvalidValueException;
use Propel\Runtime\ActiveQuery\ModelCriteria; use Propel\Runtime\ActiveQuery\ModelCriteria;
use Thelia\Core\FileFormat\Formatting\FormatterData;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Lang; use Thelia\Model\Lang;
use Thelia\ImportExport\AbstractHandler; use Thelia\ImportExport\AbstractHandler;
@@ -123,6 +126,40 @@ abstract class ExportHandler extends AbstractHandler
* *
* The method builds the FormatterData for the formatter * 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);
} }

View File

@@ -28,30 +28,11 @@ use Thelia\Model\NewsletterQuery;
class MailingExport extends ExportHandler class MailingExport extends ExportHandler
{ {
/** /**
* @return \Thelia\Core\FileFormat\Formatting\FormatterData * @param Lang $lang
* * @return array|\Propel\Runtime\ActiveQuery\ModelCriteria
* The method builds
*/ */
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() $newsletter = NewsletterQuery::create()
->select([ ->select([
NewsletterTableMap::EMAIL, NewsletterTableMap::EMAIL,
@@ -72,9 +53,25 @@ class MailingExport extends ExportHandler
->toArray() ->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,
];
} }
/** /**

View File

@@ -58,23 +58,10 @@ class ProductPricesExport extends ExportHandler
/** /**
* @param Lang $lang * @param Lang $lang
* @return \Thelia\Core\FileFormat\Formatting\FormatterData * @return FormatterData
*
* The method builds the FormatterData for the formatter
*/ */
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(); $locale = $lang->getLocale();
$productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN); $productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN);
@@ -122,9 +109,22 @@ class ProductPricesExport extends ExportHandler
->groupBy("product_sale_elements_REF") ->groupBy("product_sale_elements_REF")
; ;
$data = new FormatterData($aliases); return $query;
return $data->loadModelCriteria($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",
];
}
} }

View File

@@ -16,8 +16,6 @@ use Propel\Runtime\ActiveQuery\Join;
use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\Formatting\FormatterData;
use Thelia\Core\FileFormat\FormatType; use Thelia\Core\FileFormat\FormatType;
use Thelia\ImportExport\Export\ExportHandler; use Thelia\ImportExport\Export\ExportHandler;
use Thelia\Model\Map\ContentI18nTableMap;
use Thelia\Model\Map\ContentTableMap;
use Thelia\Model\Map\ProductI18nTableMap; use Thelia\Model\Map\ProductI18nTableMap;
use Thelia\Model\Map\ProductTableMap; use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\Map\RewritingUrlTableMap; use Thelia\Model\Map\RewritingUrlTableMap;
@@ -56,23 +54,11 @@ class ProductSEOExport extends ExportHandler
} }
/** /**
* @param \Thelia\Model\Lang $lang * @param Lang $lang
* @return \Thelia\Core\FileFormat\Formatting\FormatterData * @return array|\Propel\Runtime\ActiveQuery\ModelCriteria
*
* The method builds the FormatterData for the formatter
*/ */
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(); $locale = $this->locale = $lang->getLocale();
/** /**
@@ -111,9 +97,7 @@ class ProductSEOExport extends ExportHandler
]) ])
; ;
$data = new FormatterData($aliases); return $query;
return $data->loadModelCriteria($query);
} }
protected function getDefaultOrder() 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",
];
}
} }

View File

@@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\Container;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Core\FileFormat\FormatType; use Thelia\Core\FileFormat\FormatType;
use Thelia\ImportExport\Export\Type\MailingExport; use Thelia\ImportExport\Export\Type\MailingExport;
use Thelia\Model\Lang;
/** /**
* Class MailingExportTest * Class MailingExportTest
@@ -37,8 +38,7 @@ class MailingExportTest extends \PHPUnit_Framework_TestCase
public function testExport() public function testExport()
{ {
$data = $this->handler->buildFormatterData(); $data = $this->handler->buildData(Lang::getDefaultLanguage());
} }

View File

@@ -31,7 +31,7 @@ class ProductPricesExportTest extends \PHPUnit_Framework_TestCase
new Translator(new Container()); new Translator(new Container());
$export = new ProductPricesExport(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"]; $keys = ["attributes","currency","ean","price","promo","promo_price","ref","title"];

View File

@@ -33,7 +33,7 @@ class ProductSEOExportTest extends \PHPUnit_Framework_TestCase
new Translator(new Container()); new Translator(new Container());
$export = new ProductSEOExport(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",]; $keys=["ref","visible","product_title","url","page_title","meta_description","meta_keywords",];
sort($keys); sort($keys);