From 96b3de7c8485a285c639219968cecedd3480bae4 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 26 Aug 2013 12:38:23 +0200 Subject: [PATCH] backend trasnlation in loops --- .../Thelia/Core/Template/Element/BaseLoop.php | 1 + .../Thelia/Core/Template/Loop/Attribute.php | 14 +++++++- .../Template/Loop/AttributeAvailability.php | 1 + .../Thelia/Core/Template/Loop/Category.php | 1 + .../lib/Thelia/Core/Template/Loop/Content.php | 1 + .../lib/Thelia/Core/Template/Loop/Country.php | 3 +- .../Thelia/Core/Template/Loop/Currency.php | 3 +- .../lib/Thelia/Core/Template/Loop/Feature.php | 1 + .../Template/Loop/FeatureAvailability.php | 1 + core/lib/Thelia/Core/Template/Loop/Folder.php | 1 + .../lib/Thelia/Core/Template/Loop/Product.php | 1 + core/lib/Thelia/Core/Template/Loop/Title.php | 3 +- .../Thelia/Model/Tools/ModelCriteriaTools.php | 29 +++++++++++++++ templates/default/debug.html | 35 +++++++------------ 14 files changed, 69 insertions(+), 26 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 9f3a03d5d..c3e2c5d36 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -80,6 +80,7 @@ abstract class BaseLoop Argument::createIntTypeArgument('offset', 0), Argument::createIntTypeArgument('page'), Argument::createIntTypeArgument('limit', PHP_INT_MAX), + Argument::createIntTypeArgument('backend_context', false), ); } diff --git a/core/lib/Thelia/Core/Template/Loop/Attribute.php b/core/lib/Thelia/Core/Template/Loop/Attribute.php index 84549aaa5..d71d73dfb 100755 --- a/core/lib/Thelia/Core/Template/Loop/Attribute.php +++ b/core/lib/Thelia/Core/Template/Loop/Attribute.php @@ -33,6 +33,7 @@ use Thelia\Core\Template\Loop\Argument\ArgumentCollection; use Thelia\Core\Template\Loop\Argument\Argument; use Thelia\Log\Tlog; +use Thelia\Model\Base\LangQuery; use Thelia\Model\Tools\ModelCriteriaTools; use Thelia\Model\Base\CategoryQuery; @@ -66,6 +67,7 @@ class Attribute extends BaseLoop Argument::createIntListTypeArgument('category'), Argument::createBooleanOrBothTypeArgument('visible', 1), Argument::createIntListTypeArgument('exclude'), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( @@ -85,8 +87,18 @@ class Attribute extends BaseLoop { $search = AttributeQuery::create(); + $backendContext = $this->getBackend_context(); + + $lang = $this->getLang(); + + $x = LangQuery::create()->findOneById($lang); + /* manage translations */ - ModelCriteriaTools::getI18n($search, ConfigQuery::read("default_lang_without_translation", 1), $this->request->getSession()->getLocale()); + if($backendContext) { + ModelCriteriaTools::getBackEndI18n($search, $lang === null ? $this->request->getSession()->getLocale() : $x); + } else { + ModelCriteriaTools::getI18n($search, ConfigQuery::read("default_lang_without_translation", 1), $this->request->getSession()->getLocale()); + } $id = $this->getId(); diff --git a/core/lib/Thelia/Core/Template/Loop/AttributeAvailability.php b/core/lib/Thelia/Core/Template/Loop/AttributeAvailability.php index ade493efa..5050f2060 100755 --- a/core/lib/Thelia/Core/Template/Loop/AttributeAvailability.php +++ b/core/lib/Thelia/Core/Template/Loop/AttributeAvailability.php @@ -59,6 +59,7 @@ class AttributeAvailability extends BaseLoop Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('attribute'), Argument::createIntListTypeArgument('exclude'), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 1233ccd4e..5448d6e29 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -77,6 +77,7 @@ class Category extends BaseLoop Argument::createBooleanTypeArgument('current'), Argument::createBooleanTypeArgument('not_empty', 0), Argument::createBooleanOrBothTypeArgument('visible', 1), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Content.php b/core/lib/Thelia/Core/Template/Loop/Content.php index 3a81ff9f2..b35a545d0 100755 --- a/core/lib/Thelia/Core/Template/Loop/Content.php +++ b/core/lib/Thelia/Core/Template/Loop/Content.php @@ -65,6 +65,7 @@ class Content extends BaseLoop Argument::createBooleanTypeArgument('current_folder'), Argument::createIntTypeArgument('depth', 1), Argument::createBooleanOrBothTypeArgument('visible', 1), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Country.php b/core/lib/Thelia/Core/Template/Loop/Country.php index d25951fb8..62a0029ff 100755 --- a/core/lib/Thelia/Core/Template/Loop/Country.php +++ b/core/lib/Thelia/Core/Template/Loop/Country.php @@ -56,7 +56,8 @@ class Country extends BaseLoop Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('area'), Argument::createBooleanTypeArgument('with_area'), - Argument::createIntListTypeArgument('exclude') + Argument::createIntListTypeArgument('exclude'), + Argument::createIntTypeArgument('lang') ); } diff --git a/core/lib/Thelia/Core/Template/Loop/Currency.php b/core/lib/Thelia/Core/Template/Loop/Currency.php index 08eacca08..922669250 100755 --- a/core/lib/Thelia/Core/Template/Loop/Currency.php +++ b/core/lib/Thelia/Core/Template/Loop/Currency.php @@ -55,7 +55,8 @@ class Currency extends BaseLoop return new ArgumentCollection( Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('exclude'), - Argument::createBooleanTypeArgument('default_only', false) + Argument::createBooleanTypeArgument('default_only', false), + Argument::createIntTypeArgument('lang') ); } diff --git a/core/lib/Thelia/Core/Template/Loop/Feature.php b/core/lib/Thelia/Core/Template/Loop/Feature.php index 0693a303a..dba4ccaaa 100755 --- a/core/lib/Thelia/Core/Template/Loop/Feature.php +++ b/core/lib/Thelia/Core/Template/Loop/Feature.php @@ -64,6 +64,7 @@ class Feature extends BaseLoop Argument::createIntListTypeArgument('category'), Argument::createBooleanOrBothTypeArgument('visible', 1), Argument::createIntListTypeArgument('exclude'), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/FeatureAvailability.php b/core/lib/Thelia/Core/Template/Loop/FeatureAvailability.php index 272bb5211..a16be7743 100755 --- a/core/lib/Thelia/Core/Template/Loop/FeatureAvailability.php +++ b/core/lib/Thelia/Core/Template/Loop/FeatureAvailability.php @@ -57,6 +57,7 @@ class FeatureAvailability extends BaseLoop Argument::createIntListTypeArgument('id'), Argument::createIntListTypeArgument('feature'), Argument::createIntListTypeArgument('exclude'), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Folder.php b/core/lib/Thelia/Core/Template/Loop/Folder.php index 7062a55b4..d14c31892 100755 --- a/core/lib/Thelia/Core/Template/Loop/Folder.php +++ b/core/lib/Thelia/Core/Template/Loop/Folder.php @@ -59,6 +59,7 @@ class Folder extends BaseLoop Argument::createBooleanTypeArgument('current'), Argument::createBooleanTypeArgument('not_empty', 0), Argument::createBooleanOrBothTypeArgument('visible', 1), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index 9c22a4987..f9752646b 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -85,6 +85,7 @@ class Product extends BaseLoop Argument::createBooleanTypeArgument('current_category'), Argument::createIntTypeArgument('depth', 1), Argument::createBooleanOrBothTypeArgument('visible', 1), + Argument::createIntTypeArgument('lang'), new Argument( 'order', new TypeCollection( diff --git a/core/lib/Thelia/Core/Template/Loop/Title.php b/core/lib/Thelia/Core/Template/Loop/Title.php index 843e29334..2402dc318 100755 --- a/core/lib/Thelia/Core/Template/Loop/Title.php +++ b/core/lib/Thelia/Core/Template/Loop/Title.php @@ -53,7 +53,8 @@ class Title extends BaseLoop protected function getArgDefinitions() { return new ArgumentCollection( - Argument::createIntListTypeArgument('id') + Argument::createIntListTypeArgument('id'), + Argument::createIntTypeArgument('lang') ); } diff --git a/core/lib/Thelia/Model/Tools/ModelCriteriaTools.php b/core/lib/Thelia/Model/Tools/ModelCriteriaTools.php index fd04ef3d9..656be5848 100755 --- a/core/lib/Thelia/Model/Tools/ModelCriteriaTools.php +++ b/core/lib/Thelia/Model/Tools/ModelCriteriaTools.php @@ -43,6 +43,8 @@ class ModelCriteriaTools $search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias) ->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR); + $search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'TRANSLATION_EXISTS'); + foreach($columns as $column) { $search->withColumn('`' . $askedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column); } @@ -63,6 +65,8 @@ class ModelCriteriaTools $search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias) ->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR); + $search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'TRANSLATION_EXISTS'); + $search->where('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.ID)')->_or()->where('NOT ISNULL(`' . $defaultLocaleI18nAlias . '`.ID)'); foreach($columns as $column) { @@ -70,4 +74,29 @@ class ModelCriteriaTools } } } + + public static function getBackEndI18n(ModelCriteria &$search, $askedLocale, $columns = array('TITLE', 'CHAPO', 'DESCRIPTION', 'POSTSCRIPTUM'), $foreignTable = null, $foreignKey = 'ID') + { + if($foreignTable === null) { + $foreignTable = $search->getTableMap()->getName(); + $aliasPrefix = ''; + } else { + $aliasPrefix = $foreignTable . '_'; + } + + $askedLocaleI18nAlias = 'asked_locale_i18n'; + + $askedLocaleJoin = new Join(); + $askedLocaleJoin->addExplicitCondition($search->getTableMap()->getName(), $foreignKey, null, $foreignTable . '_i18n', 'ID', $askedLocaleI18nAlias); + $askedLocaleJoin->setJoinType(Criteria::LEFT_JOIN); + + $search->addJoinObject($askedLocaleJoin, $askedLocaleI18nAlias) + ->addJoinCondition($askedLocaleI18nAlias ,'`' . $askedLocaleI18nAlias . '`.LOCALE = ?', $askedLocale, null, \PDO::PARAM_STR); + + $search->withColumn('NOT ISNULL(`' . $askedLocaleI18nAlias . '`.`ID`)', $aliasPrefix . 'TRANSLATION_EXISTS'); + + foreach($columns as $column) { + $search->withColumn('`' . $askedLocaleI18nAlias . '`.`' . $column . '`', $aliasPrefix . 'i18n_' . $column); + } + } } diff --git a/templates/default/debug.html b/templates/default/debug.html index 251a9b891..62966680a 100755 --- a/templates/default/debug.html +++ b/templates/default/debug.html @@ -1,23 +1,14 @@ -{*loop name="product" type="product" new="on" promo="on" min_stock="20" attribute_non_strict_match="min_stock,promo"} -

PRODUCT : #REF / #TITLE (#ID)

-{/loop*} +

ALL ATTRIBUTES AND THEIR AVAILABILITY

-{*loop name="product" type="product" min_weight="1000" max_weight="6000" attribute_non_strict_match="*"} -

PRODUCT : #REF / #TITLE (#ID)

-{/loop*} - -{*loop name="product" type="product" min_price="100" max_price="300" min_stock="4" min_weight="6000" max_weight="7000" attribute_non_strict_match="*" promo="on"} -

PRODUCT : #REF / #TITLE (#ID)

-{/loop*} - -{*loop name="product" type="product" promo="0" min_stock="4" order="promo,min_price"} -

PRODUCT : #REF / #TITLE (#ID)

-{/loop*} - -{*loop name="product" type="product" order="promo,min_price"} -

PRODUCT : #REF / #TITLE (#ID) / NEW : #NEW / PROMO : #PROMO / best price : #BEST_PRICE

-{/loop*} - -{loop name="product" type="product"} -

PRODUCT : #REF / #TITLE

-{/loop} \ No newline at end of file + \ No newline at end of file