|
|
|
|
@@ -6,6 +6,7 @@ use Propel\Runtime\ActiveQuery\Criteria;
|
|
|
|
|
use Propel\Runtime\ActiveQuery\Join;
|
|
|
|
|
use Propel\Runtime\ActiveQuery\ModelCriteria;
|
|
|
|
|
use Thelia\Model\Base\LangQuery;
|
|
|
|
|
use Thelia\Model\ConfigQuery;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ModelCriteriaTools
|
|
|
|
|
@@ -17,13 +18,12 @@ class ModelCriteriaTools
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param ModelCriteria $search
|
|
|
|
|
* @param $defaultLangWithoutTranslation
|
|
|
|
|
* @param $askedLocale
|
|
|
|
|
* @param $requestedLocale
|
|
|
|
|
* @param array $columns
|
|
|
|
|
* @param null $foreignTable
|
|
|
|
|
* @param string $foreignKey
|
|
|
|
|
*/
|
|
|
|
|
public static function getFrontEndI18n(ModelCriteria &$search, $defaultLangWithoutTranslation, $askedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID', $forceReturn = false)
|
|
|
|
|
public static function getFrontEndI18n(ModelCriteria &$search, $requestedLocale, $columns, $foreignTable, $foreignKey, $forceReturn = false)
|
|
|
|
|
{
|
|
|
|
|
if($foreignTable === null) {
|
|
|
|
|
$foreignTable = $search->getTableMap()->getName();
|
|
|
|
|
@@ -32,21 +32,24 @@ class ModelCriteriaTools
|
|
|
|
|
$aliasPrefix = $foreignTable . '_';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$askedLocaleI18nAlias = $aliasPrefix . 'asked_locale_i18n';
|
|
|
|
|
$defaultLangWithoutTranslation = ConfigQuery::getDefaultLangWhenNoTranslationAvailable();
|
|
|
|
|
|
|
|
|
|
$requestedLocaleI18nAlias = $aliasPrefix . 'requested_locale_i18n';
|
|
|
|
|
$defaultLocaleI18nAlias = $aliasPrefix . 'default_locale_i18n';
|
|
|
|
|
|
|
|
|
|
if($defaultLangWithoutTranslation == 0) {
|
|
|
|
|
$askedLocaleJoin = new Join();
|
|
|
|
|
$askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias);
|
|
|
|
|
$askedLocaleJoin->setJoinType($forceReturn === false ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN);
|
|
|
|
|
|
|
|
|
|
$requestedLocaleJoin = new Join();
|
|
|
|
|
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
|
|
|
|
|
$requestedLocaleJoin->setJoinType($forceReturn === false ? Criteria::INNER_JOIN : Criteria::LEFT_JOIN);
|
|
|
|
|
|
|
|
|
|
$search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
|
|
|
|
|
foreach($columns as $column) {
|
|
|
|
|
$search->withColumn('`' . $askedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
$search->withColumn('`' . $requestedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$defaultLocale = LangQuery::create()->findOneById($defaultLangWithoutTranslation)->getLocale();
|
|
|
|
|
@@ -58,26 +61,26 @@ class ModelCriteriaTools
|
|
|
|
|
$search->addJoinObject($defaultLocaleJoin, $defaultLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($defaultLocaleI18nAlias ,'`' . $defaultLocaleI18nAlias . '`.LOCALE = ?', $defaultLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
|
|
|
|
|
$askedLocaleJoin = new Join();
|
|
|
|
|
$askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias);
|
|
|
|
|
$askedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
|
|
|
|
|
$requestedLocaleJoin = new Join();
|
|
|
|
|
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
|
|
|
|
|
$requestedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
|
|
|
|
|
|
|
|
|
|
$search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
|
|
|
|
|
if(!$forceReturn) {
|
|
|
|
|
$search->where('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)');
|
|
|
|
|
if($forceReturn === false) {
|
|
|
|
|
$search->where('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach($columns as $column) {
|
|
|
|
|
$search->withColumn('CASE WHEN NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID) THEN `' . $askedLocaleI18nAlias . '`.`' . $column . '` ELSE `' . $defaultLocaleI18nAlias . '`.`' . $column . '` END', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
$search->withColumn('CASE WHEN NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.ID) THEN `' . $requestedLocaleI18nAlias . '`.`' . $column . '` ELSE `' . $defaultLocaleI18nAlias . '`.`' . $column . '` END', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getBackEndI18n(ModelCriteria &$search, $askedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID')
|
|
|
|
|
public static function getBackEndI18n(ModelCriteria &$search, $requestedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID')
|
|
|
|
|
{
|
|
|
|
|
if($foreignTable === null) {
|
|
|
|
|
$foreignTable = $search->getTableMap()->getName();
|
|
|
|
|
@@ -86,39 +89,46 @@ class ModelCriteriaTools
|
|
|
|
|
$aliasPrefix = $foreignTable . '_';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$askedLocaleI18nAlias = $aliasPrefix . 'asked_locale_i18n';
|
|
|
|
|
$requestedLocaleI18nAlias = 'requested_locale_i18n';
|
|
|
|
|
|
|
|
|
|
$askedLocaleJoin = new Join();
|
|
|
|
|
$askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias);
|
|
|
|
|
$askedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
|
|
|
|
|
$requestedLocaleJoin = new Join();
|
|
|
|
|
$requestedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $requestedLocaleI18nAlias);
|
|
|
|
|
$requestedLocaleJoin->setJoinType(Criteria::LEFT_JOIN);
|
|
|
|
|
|
|
|
|
|
$search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
$search->addJoinObject($requestedLocaleJoin, $requestedLocaleI18nAlias)
|
|
|
|
|
->addJoinCondition($requestedLocaleI18nAlias ,'`' . $requestedLocaleI18nAlias . '`.LOCALE = ?', $requestedLocale, null, \PDO::PARAM_STR);
|
|
|
|
|
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
$search->withColumn('NOT ISNULL(`' . $requestedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'IS_TRANSLATED');
|
|
|
|
|
|
|
|
|
|
foreach($columns as $column) {
|
|
|
|
|
$search->withColumn('`' . $askedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
$search->withColumn('`' . $requestedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getI18n($backendContext, $lang, ModelCriteria &$search, $defaultLangWithoutTranslation, $currentLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID')
|
|
|
|
|
public static function getI18n($backendContext, $requestedLangId, ModelCriteria &$search, $currentLocale, $columns, $foreignTable, $foreignKey, $forceReturn = false)
|
|
|
|
|
{
|
|
|
|
|
if($lang !== null) {
|
|
|
|
|
$localeSearch = LangQuery::create()->findOneById($lang);
|
|
|
|
|
if($localeSearch === null) {
|
|
|
|
|
throw new \InvalidArgumentException('Incorrect lang argument given in attribute loop');
|
|
|
|
|
// If a lang has been requested, find the related Lang object, and get the locale
|
|
|
|
|
if ($requestedLangId !== null) {
|
|
|
|
|
$localeSearch = LangQuery::create()->findOneById($requestedLangId);
|
|
|
|
|
|
|
|
|
|
if ($localeSearch === null) {
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Incorrect lang argument given in attribute loop: lang ID %d not found', $requestedLangId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$locale = $localeSearch->getLocale();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Use the currently defined locale
|
|
|
|
|
$locale = $currentLocale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$askedLocale = $lang === null ? $currentLocale : $localeSearch->getLocale();
|
|
|
|
|
|
|
|
|
|
if($backendContext) {
|
|
|
|
|
self::getBackEndI18n($search, $askedLocale, $columns, $foreignTable, $foreignKey);
|
|
|
|
|
// Call the proper method depending on the context: front or back
|
|
|
|
|
if ($backendContext) {
|
|
|
|
|
self::getBackEndI18n($search, $locale, $columns, $foreignTable, $foreignKey);
|
|
|
|
|
} else {
|
|
|
|
|
self::getFrontEndI18n($search, $defaultLangWithoutTranslation, $askedLocale, $columns, $foreignTable, $foreignKey);
|
|
|
|
|
self::getFrontEndI18n($search, $locale, $columns, $foreignTable, $foreignKey, $forceReturn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $askedLocale;
|
|
|
|
|
return $locale;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|