From 78fdce8aee69bc52f54d077b8559c85a79d28a7d Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 4 Aug 2014 08:52:03 +0200 Subject: [PATCH] =?UTF-8?q?Refactor=20addI18nCondition=20method,=20place?= =?UTF-8?q?=20it=20in=20I18n=20tool=20=09modifi=C3=A9:=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20core/lib/Thelia/ImportExport/Export/ExportHandler.php?= =?UTF-8?q?=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/I?= =?UTF-8?q?mportExport/Export/Type/ContentExport.php=20=09modifi=C3=A9:=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20core/lib/Thelia/ImportExport/Export/Typ?= =?UTF-8?q?e/CustomerExport.php=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20core/lib/Thelia/ImportExport/Export/Type/OrderExport.php=20?= =?UTF-8?q?=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Impo?= =?UTF-8?q?rtExport/Export/Type/ProductTaxedPricesExport.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Tools/I18n.ph?= =?UTF-8?q?p?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportExport/Export/ExportHandler.php | 54 ----------------- .../Export/Type/ContentExport.php | 16 ++++- .../Export/Type/CustomerExport.php | 7 ++- .../ImportExport/Export/Type/OrderExport.php | 11 ++-- .../Export/Type/ProductTaxedPricesExport.php | 3 +- core/lib/Thelia/Tools/I18n.php | 60 +++++++++++++++++++ 6 files changed, 85 insertions(+), 66 deletions(-) diff --git a/core/lib/Thelia/ImportExport/Export/ExportHandler.php b/core/lib/Thelia/ImportExport/Export/ExportHandler.php index e051cbf57..31bd8fea2 100644 --- a/core/lib/Thelia/ImportExport/Export/ExportHandler.php +++ b/core/lib/Thelia/ImportExport/Export/ExportHandler.php @@ -123,60 +123,6 @@ abstract class ExportHandler extends AbstractHandler ); } - public function addI18nCondition( - ModelCriteria $query, - $i18nTableName, - $tableIdColumn, - $i18nIdColumn, - $localeColumn, - $locale - ) { - - $locale = $this->real_escape($locale); - $defaultLocale = $this->real_escape($this->defaultLocale); - - $query - ->_and() - ->where( - "CASE WHEN ".$tableIdColumn." IN". - "(SELECT DISTINCT ".$i18nIdColumn." ". - "FROM `".$i18nTableName."` ". - "WHERE locale=$locale) ". - - "THEN ".$localeColumn." = $locale ". - "ELSE ".$localeColumn." = $defaultLocale ". - "END" - ) - ; - } - - /** - * @param $str - * @return string - * - * Really escapes a string for SQL request. - */ - protected function real_escape($str) - { - $str = trim($str, "\"'"); - - $return = "CONCAT("; - $len = strlen($str); - - for ($i = 0; $i < $len; ++$i) { - $return .= "CHAR(".ord($str[$i])."),"; - } - - if ($i > 0) { - $return = substr($return, 0, -1); - } else { - $return = "\"\""; - } - $return .= ")"; - - return $return; - } - public function renderLoop($type, array $args = array()) { $loopsDefinition = $this->container->getParameter("thelia.parser.loops"); diff --git a/core/lib/Thelia/ImportExport/Export/Type/ContentExport.php b/core/lib/Thelia/ImportExport/Export/Type/ContentExport.php index 24cf71319..9f222e413 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/ContentExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/ContentExport.php @@ -72,10 +72,9 @@ class ContentExport extends ExportHandler implements } /** - * @param Lang $lang - * @return ModelCriteria|array|BaseLoop + * @return ContentQuery */ - public function buildDataSet(Lang $lang) + public function getQuery(Lang $lang) { $locale = $lang->getLocale(); @@ -162,6 +161,17 @@ class ContentExport extends ExportHandler implements ->groupBy("folder_ID") ; + return $query; + } + + /** + * @param Lang $lang + * @return ModelCriteria|array|BaseLoop + */ + public function buildDataSet(Lang $lang) + { + $query = $this->getQuery($lang); + $dataSet = $query ->find() ->toArray() diff --git a/core/lib/Thelia/ImportExport/Export/Type/CustomerExport.php b/core/lib/Thelia/ImportExport/Export/Type/CustomerExport.php index ed33077b7..9768c5648 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/CustomerExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/CustomerExport.php @@ -25,6 +25,7 @@ use Thelia\Model\Map\CustomerTableMap; use Thelia\Model\Map\CustomerTitleI18nTableMap; use Thelia\Model\Map\NewsletterTableMap; use Thelia\Model\OrderQuery; +use Thelia\Tools\I18n; /** * Class CustomerExport @@ -132,7 +133,7 @@ class CustomerExport extends ExportHandler ->orderById() ; - $this->addI18nCondition( + I18n::addI18nCondition( $query, CountryI18nTableMap::TABLE_NAME, CountryTableMap::ID, @@ -141,7 +142,7 @@ class CustomerExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, CustomerTitleI18nTableMap::TABLE_NAME, "`customer_title_`.ID", @@ -150,7 +151,7 @@ class CustomerExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, CustomerTitleI18nTableMap::TABLE_NAME, "`address_title`.ID", diff --git a/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php b/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php index e59eb5ea7..885e5dfea 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php @@ -28,6 +28,7 @@ use Thelia\Model\Map\OrderStatusI18nTableMap; use Thelia\Model\Map\OrderStatusTableMap; use Thelia\Model\Map\OrderTableMap; use Thelia\Model\OrderQuery; +use Thelia\Tools\I18n; /** * Class OrderExport @@ -199,7 +200,7 @@ class OrderExport extends ExportHandler ]) ; - $this->addI18nCondition( + I18n::addI18nCondition( $query, CustomerTitleI18nTableMap::TABLE_NAME, "`delivery_address_customer_title_join`.ID", @@ -208,7 +209,7 @@ class OrderExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, CustomerTitleI18nTableMap::TABLE_NAME, "`invoice_address_customer_title_join`.ID", @@ -217,7 +218,7 @@ class OrderExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, CountryI18nTableMap::TABLE_NAME, "`delivery_address_country_join`.ID", @@ -226,7 +227,7 @@ class OrderExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, CountryI18nTableMap::TABLE_NAME, "`invoice_address_country_join`.ID", @@ -235,7 +236,7 @@ class OrderExport extends ExportHandler $locale ); - $this->addI18nCondition( + I18n::addI18nCondition( $query, OrderStatusI18nTableMap::TABLE_NAME, OrderStatusI18nTableMap::ID, diff --git a/core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php b/core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php index 953635c54..329b1f29c 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php @@ -21,6 +21,7 @@ use Thelia\Model\Map\ProductTableMap; use Thelia\Model\Map\TaxRuleI18nTableMap; use Thelia\Model\Map\TaxRuleTableMap; use Thelia\Model\ProductSaleElementsQuery; +use Thelia\Tools\I18n; /** * Class ProductTaxedPricesExport @@ -58,7 +59,7 @@ class ProductTaxedPricesExport extends ProductPricesExport ]) ; - $this->addI18nCondition( + I18n::addI18nCondition( $query, TaxRuleI18nTableMap::TABLE_NAME, TaxRuleTableMap::ID, diff --git a/core/lib/Thelia/Tools/I18n.php b/core/lib/Thelia/Tools/I18n.php index 57bf92e0b..87acbefd4 100644 --- a/core/lib/Thelia/Tools/I18n.php +++ b/core/lib/Thelia/Tools/I18n.php @@ -12,6 +12,7 @@ namespace Thelia\Tools; +use Propel\Runtime\ActiveQuery\ModelCriteria; use Thelia\Model\Lang; /** @@ -27,6 +28,8 @@ use Thelia\Model\Lang; */ class I18n { + protected static $defaultLocale; + /** * Create a \DateTime from a date picker form input * The date format is the same as the one from the current User Session @@ -78,4 +81,61 @@ class I18n return $i18n; } + + public static function addI18nCondition( + ModelCriteria $query, + $i18nTableName, + $tableIdColumn, + $i18nIdColumn, + $localeColumn, + $locale + ) { + if (null === static::$defaultLocale) { + static::$defaultLocale = Lang::getDefaultLanguage()->getLocale(); + } + + $locale = static::real_escape($locale); + $defaultLocale = static::real_escape(static::$defaultLocale); + + $query + ->_and() + ->where( + "CASE WHEN ".$tableIdColumn." IN". + "(SELECT DISTINCT ".$i18nIdColumn." ". + "FROM `".$i18nTableName."` ". + "WHERE locale=$locale) ". + + "THEN ".$localeColumn." = $locale ". + "ELSE ".$localeColumn." = $defaultLocale ". + "END" + ) + ; + } + + /** + * @param $str + * @return string + * + * Really escapes a string for SQL query. + */ + public static function real_escape($str) + { + $str = trim($str, "\"'"); + + $return = "CONCAT("; + $len = strlen($str); + + for ($i = 0; $i < $len; ++$i) { + $return .= "CHAR(".ord($str[$i])."),"; + } + + if ($i > 0) { + $return = substr($return, 0, -1); + } else { + $return = "\"\""; + } + $return .= ")"; + + return $return; + } }