Refactor addI18nCondition method, place it in I18n tool

modifié:         core/lib/Thelia/ImportExport/Export/ExportHandler.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/ContentExport.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/CustomerExport.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/OrderExport.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php
	modifié:         core/lib/Thelia/Tools/I18n.php
This commit is contained in:
Benjamin Perche
2014-08-04 08:52:03 +02:00
parent df27aff534
commit 78fdce8aee
6 changed files with 85 additions and 66 deletions

View File

@@ -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;
}
}