diff --git a/core/lib/Thelia/ImportExport/Export/ExportHandler.php b/core/lib/Thelia/ImportExport/Export/ExportHandler.php index 43cf64fd3..2a0be8579 100644 --- a/core/lib/Thelia/ImportExport/Export/ExportHandler.php +++ b/core/lib/Thelia/ImportExport/Export/ExportHandler.php @@ -11,6 +11,8 @@ /*************************************************************************************/ 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; @@ -22,6 +24,8 @@ use Thelia\ImportExport\AbstractHandler; */ abstract class ExportHandler extends AbstractHandler { + protected $locale; + public function addI18nCondition( ModelCriteria $query, $i18nTableName, @@ -49,7 +53,6 @@ abstract class ExportHandler extends AbstractHandler ; } - /** * @param $str * @return string @@ -58,6 +61,8 @@ abstract class ExportHandler extends AbstractHandler */ protected function real_escape($str) { + $str = trim($str, "\"'"); + $return = "CONCAT("; $len = strlen($str); diff --git a/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php b/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php index b4dad51d6..25519fd64 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/ProductPricesExport.php @@ -12,6 +12,7 @@ namespace Thelia\ImportExport\Export\Type; use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\ActiveQuery\Join; use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\FormatType; use Thelia\ImportExport\Export\ExportHandler; @@ -76,6 +77,9 @@ class ProductPricesExport extends ExportHandler $locale = $lang->getLocale(); + $productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN); + $attributeAvJoin = new Join(AttributeAvTableMap::ID, AttributeAvI18nTableMap::ID, Criteria::LEFT_JOIN); + $query = AttributeCombinationQuery::create() ->useProductSaleElementsQuery() ->useProductPriceQuery() @@ -86,21 +90,24 @@ class ProductPricesExport extends ExportHandler ->addAsColumn("price_PROMO_PRICE", ProductPriceTableMap::PROMO_PRICE) ->endUse() ->useProductQuery() - ->useProductI18nQuery() - ->addAsColumn("product_TITLE", ProductI18nTableMap::TITLE) - ->endUse() + ->addJoinObject($productJoin, "product_join") + ->addJoinCondition("product_join", ProductI18nTableMap::LOCALE . "=" . $this->real_escape($locale)) + ->addAsColumn("product_TITLE", ProductI18nTableMap::TITLE) ->endUse() ->addAsColumn("product_sale_elements_REF", ProductSaleElementsTableMap::REF) ->addAsColumn("product_sale_elements_EAN_CODE", ProductSaleElementsTableMap::EAN_CODE) ->addAsColumn("product_sale_elements_PROMO", ProductSaleElementsTableMap::PROMO) ->endUse() ->useAttributeAvQuery() - ->useAttributeAvI18nQuery(null, Criteria::INNER_JOIN) - ->addAsColumn( - "attribute_av_i18n_ATTRIBUTES", - "GROUP_CONCAT(DISTINCT ".AttributeAvI18nTableMap::TITLE.")" - ) - ->endUse() + ->addJoinObject($attributeAvJoin, "attribute_av_join") + ->addJoinCondition( + "attribute_av_join", + AttributeAvI18nTableMap::LOCALE . "=" . $this->real_escape($locale) + ) + ->addAsColumn( + "attribute_av_i18n_ATTRIBUTES", + "GROUP_CONCAT(DISTINCT ".AttributeAvI18nTableMap::TITLE.")" + ) ->endUse() ->select([ "product_sale_elements_REF", @@ -115,24 +122,6 @@ class ProductPricesExport extends ExportHandler ->groupBy("product_sale_elements_REF") ; - $this->addI18nCondition( - $query, - ProductI18nTableMap::TABLE_NAME, - ProductTableMap::ID, - AttributeAvI18nTableMap::ID, - ProductI18nTableMap::LOCALE, - $locale - ); - - $this->addI18nCondition( - $query, - AttributeAvI18nTableMap::TABLE_NAME, - AttributeAvTableMap::ID, - AttributeAvI18nTableMap::ID, - AttributeAvI18nTableMap::LOCALE, - $locale - ); - $data = new FormatterData($aliases); return $data->loadModelCriteria($query); diff --git a/core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php b/core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php index 6e01834c2..8113cc2a7 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/ProductSEOExport.php @@ -11,6 +11,8 @@ /*************************************************************************************/ namespace Thelia\ImportExport\Export\Type; +use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\ActiveQuery\Join; use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\FormatType; use Thelia\ImportExport\Export\ExportHandler; @@ -18,6 +20,8 @@ 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; +use Thelia\Model\Product; use Thelia\Model\ProductAssociatedContentQuery; use Thelia\Model\Lang; @@ -63,58 +67,56 @@ class ProductSEOExport extends ExportHandler "product_REF" => "ref", "product_VISIBLE" => "visible", "product_i18n_TITLE" => "product_title", - "content_URL" => "url", - "content_TITLE" => "page_title", + "product_URL" => "url", + "content_TITLE" => "content_title", "content_META_DESCRIPTION" => "meta_description", "content_META_KEYWORDS" => "meta_keywords", ]; - $locale = $lang->getLocale(); + $locale = $this->locale = $lang->getLocale(); + + /** + * Join objects + */ + $urlJoin = new Join(ProductTableMap::ID, RewritingUrlTableMap::VIEW_ID, Criteria::LEFT_JOIN); + $contentJoin = new Join(ContentTableMap::ID, ContentI18nTableMap::ID, Criteria::LEFT_JOIN); + $productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN); $query = ProductAssociatedContentQuery::create() ->useContentQuery() - ->useContentI18nQuery() - ->addAsColumn("content_URL", "") - ->addAsColumn("content_TITLE", ContentI18nTableMap::TITLE) - ->addAsColumn("content_META_DESCRIPTION", ContentI18nTableMap::META_DESCRIPTION) - ->addAsColumn("content_META_KEYWORDS", ContentI18nTableMap::META_KEYWORDS) - ->endUse() + ->addJoinObject($contentJoin, "content_join") + ->addJoinCondition("content_join", ContentI18nTableMap::LOCALE . "=" . $this->real_escape($locale)) + ->addAsColumn("content_TITLE", ContentI18nTableMap::TITLE) + ->addAsColumn("content_META_DESCRIPTION", ContentI18nTableMap::META_DESCRIPTION) + ->addAsColumn("content_META_KEYWORDS", ContentI18nTableMap::META_KEYWORDS) ->endUse() ->useProductQuery() - ->useProductI18nQuery() - ->addAsColumn("product_i18n_TITLE", ProductI18nTableMap::TITLE) - ->endUse() + ->addJoinObject($productJoin, "product_join") + ->addJoinCondition("product_join", ProductI18nTableMap::LOCALE . "=" . $this->real_escape($locale)) + ->addAsColumn("product_i18n_TITLE", ProductI18nTableMap::TITLE) ->addAsColumn("product_REF", ProductTableMap::REF) ->addAsColumn("product_VISIBLE", ProductTableMap::VISIBLE) ->endUse() + ->addJoinObject($urlJoin, "rewriting_url_join") + ->addJoinCondition("rewriting_url_join", RewritingUrlTableMap::VIEW_LOCALE . "=" . $this->real_escape($locale)) + ->addJoinCondition( + "rewriting_url_join", + RewritingUrlTableMap::VIEW . "=" . $this->real_escape((new Product())->getRewrittenUrlViewName()) + ) + ->addJoinCondition("rewriting_url_join", "ISNULL(".RewritingUrlTableMap::REDIRECTED.")") + ->addAsColumn("product_URL", RewritingUrlTableMap::URL) ->select([ "product_REF", "product_VISIBLE", "product_i18n_TITLE", - "content_URL", + "product_URL", "content_TITLE", "content_META_DESCRIPTION", "content_META_KEYWORDS", ]) ; - $this->addI18nCondition( - $query, - ContentI18nTableMap::TABLE_NAME, - ContentTableMap::ID, - ContentI18nTableMap::ID, - ContentI18nTableMap::LOCALE, - $locale - ); - - $this->addI18nCondition( - $query, - ProductI18nTableMap::TABLE_NAME, - ProductTableMap::ID, - ProductI18nTableMap::ID, - ProductI18nTableMap::LOCALE, - $locale - ); + $a = $query->toString(); $data = new FormatterData($aliases); diff --git a/core/lib/Thelia/Tests/ImportExport/Export/ProductSEOExportTest.php b/core/lib/Thelia/Tests/ImportExport/Export/ProductSEOExportTest.php new file mode 100644 index 000000000..bfeedcbef --- /dev/null +++ b/core/lib/Thelia/Tests/ImportExport/Export/ProductSEOExportTest.php @@ -0,0 +1,85 @@ + + */ +class ProductSEOExportTest extends \PHPUnit_Framework_TestCase +{ + public function testQuery() + { + new Translator(new Container()); + $export = new ProductSEOExport(new Container()); + + $data = $export->buildFormatterData(Lang::getDefaultLanguage()); + + $keys=["ref","visible","product_title","url","content_title","meta_description","meta_keywords",]; + sort($keys); + $rawData = $data->getData(); + + $max = count($rawData); + + /** + * If there's more that 50 entries, + * just pick 50, it would be faster and as tested as if we test 1000 entries. + */ + if ($max > 50) { + $max = 50; + } + + for ($i = 0; $i < $max; ++$i) + { + $row = $rawData[$i]; + $rowKeys = array_keys($row); + + $this->assertTrue(sort($rowKeys)); + $this->assertEquals($keys, $rowKeys); + + $product = ProductQuery::create()->findOneByRef($row["ref"]); + $this->assertNotNull($product); + + $contentI18n = ContentI18nQuery::create() + ->filterByTitle($row["content_title"]) + ->filterByLocale("en_US") + ->findOne(); + + $contentsObj = ProductAssociatedContentQuery::create() + ->filterByProduct($product) + ; + + if (null !== $contentI18n) { + $contentsObj->filterByContentId($contentI18n->getId()); + } + + $contentsObj->findOne(); + + $this->assertEquals($product->getVisible(), $row["visible"]); + $this->assertEquals($product->getTitle(), $row["product_title"]); + + $this->assertEquals($contentI18n->getMetaDescription(), $row["meta_description"]); + $this->assertEquals($contentI18n->getMetaKeywords(), $row["meta_keywords"]); + } + } +} \ No newline at end of file