Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced parameter "locale" with "lang" in generated URL #1722

Merged
merged 1 commit into from Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/lib/Thelia/Core/Stack/ParamInitMiddleware.php
Expand Up @@ -134,10 +134,16 @@ protected function defineCurrency(TheliaRequest $request)
protected function detectLang(TheliaRequest $request)
{
// first priority => lang parameter present in request (get or post)
if ($request->query->has("lang")) {
// The lang parameter may contains a lang code (fr, en, ru) for Thelia < 2.2,
// or a locale (fr_FR, en_US, etc.) for Thelia > 2.2.beta1
$requestedLangCodeOrLocale = $request->query->get("lang");
$requestedLangCodeOrLocale = $request->query->get("lang");

// add a fallback on locale parameter
if (null === $requestedLangCodeOrLocale) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to compare with false. The has method will return true or false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or don't use the has but the get method.

$requestedLangCodeOrLocale = $request->query->get("locale");
}

// The lang parameter may contains a lang code (fr, en, ru) for Thelia < 2.2,
// or a locale (fr_FR, en_US, etc.) for Thelia > 2.2.beta1
if (null !== $requestedLangCodeOrLocale) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


if (strlen($requestedLangCodeOrLocale) > 2) {
$lang = LangQuery::create()->findOneByLocale($requestedLangCodeOrLocale);
Expand Down
16 changes: 14 additions & 2 deletions core/lib/Thelia/Model/RewritingUrlQuery.php
Expand Up @@ -57,7 +57,7 @@ public function getViewUrlQuery($view, $viewLocale, $viewId)
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
->where('ISNULL(`ra`.REWRITING_URL_ID)')
->filterByView($view)
->filterByViewLocale($viewLocale)
->filterByViewLocale($this->retrieveLocale($viewLocale))
->filterByViewId($viewId)
->filterByRedirected(null)
->orderById(Criteria::DESC)
Expand All @@ -78,7 +78,7 @@ public function getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParam
->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID')
->filterByView($view)
->filterByViewLocale($viewLocale)
->filterByViewLocale($this->retrieveLocale($viewLocale))
->filterByViewId($viewId)
->filterByRedirected(null)
->orderById(Criteria::DESC);
Expand Down Expand Up @@ -109,5 +109,17 @@ public function getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParam

return $urlQuery->findOne();
}

protected function retrieveLocale($viewLocale)
{
if (strlen($viewLocale) == 2) {
if (null !== $lang = LangQuery::create()->findOneByCode($viewLocale)) {
$viewLocale = $lang->getLocale();
}
}

return $viewLocale;
}

}
// RewritingUrlQuery
4 changes: 2 additions & 2 deletions core/lib/Thelia/Rewriting/RewritingRetriever.php
Expand Up @@ -50,7 +50,7 @@ public function loadViewUrl($view, $viewLocale = null, $viewId = null)

$allParametersWithoutView = array();
if (null !== $viewLocale) {
$allParametersWithoutView['locale'] = $viewLocale;
$allParametersWithoutView['lang'] = $viewLocale;
}
if (null !== $viewId) {
$allParametersWithoutView[$view . '_id'] = $viewId;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function loadSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherPa
$this->search = $this->rewritingUrlQuery->getSpecificUrlQuery($view, $viewLocale, $viewId, $viewOtherParameters);

$allParametersWithoutView = $viewOtherParameters;
$allParametersWithoutView['locale'] = $viewLocale;
$allParametersWithoutView['lang'] = $viewLocale;
if (null !== $viewId) {
$allParametersWithoutView[$view . '_id'] = $viewId;
}
Expand Down
25 changes: 23 additions & 2 deletions core/lib/Thelia/Tools/URL.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Validator\Constraints\UrlValidator;
use Thelia\Model\ConfigQuery;
use Thelia\Model\LangQuery;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Rewriting\RewritingRetriever;
use Thelia\Core\HttpFoundation\Request;
Expand Down Expand Up @@ -242,7 +243,7 @@ public function retrieve($view, $viewId, $viewLocale)
$this->retriever->loadViewUrl($view, $viewLocale, $viewId);
} else {
$allParametersWithoutView = array();
$allParametersWithoutView['locale'] = $viewLocale;
$allParametersWithoutView['lang'] = $viewLocale;
if (null !== $viewId) {
$allParametersWithoutView[$view . '_id'] = $viewId;
}
Expand All @@ -264,7 +265,9 @@ public function retrieveCurrent(Request $request)
{
if (ConfigQuery::isRewritingEnable()) {
$view = $request->attributes->get('_view', null);
$viewLocale = $request->query->get('locale', null);

$viewLocale = $this->getViewLocale($request);

$viewId = $view === null ? null : $request->query->get($view . '_id', null);

$allOtherParameters = $request->query->all();
Expand All @@ -275,6 +278,7 @@ public function retrieveCurrent(Request $request)
}
}
if ($viewLocale !== null) {
unset($allOtherParameters['lang']);
unset($allOtherParameters['locale']);
}

Expand Down Expand Up @@ -331,4 +335,21 @@ public static function checkUrl($url, array $protocols = ["http", "https"])

return (bool) preg_match($pattern, $url);
}

/**
* Get the locale code from the lang attribute in URL.
*
* @param Request $request
* @return null|string
*/
private function getViewLocale(Request $request)
{
$viewLocale = $request->query->get('lang', null);
if (null === $viewLocale) {
// fallback for old parameter
$viewLocale = $request->query->get('locale', null);
}

return $viewLocale;
}
}
8 changes: 4 additions & 4 deletions templates/frontOffice/default/sitemap.html
Expand Up @@ -21,7 +21,7 @@
{if $_lang_ == "" || $_lang_ == $CODE }
{loop type="category" name="category" lang="$ID"}
<url>
<loc>{$URL nofilter}</loc>
<loc>{$URL}</loc>
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
</url>
{/loop}
Expand All @@ -33,7 +33,7 @@
{if $_lang_ == "" || $_lang_ == $CODE }
{loop type="product" name="product" lang="$ID"}
<url>
<loc>{$URL nofilter}</loc>
<loc>{$URL}</loc>
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
</url>
{/loop}
Expand All @@ -48,7 +48,7 @@
{if $_lang_ == "" || $_lang_ == $CODE }
{loop type="folder" name="folder" lang="$ID"}
<url>
<loc>{$URL nofilter}</loc>
<loc>{$URL}</loc>
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
</url>
{/loop}
Expand All @@ -60,7 +60,7 @@
{if $_lang_ == "" || $_lang_ == $CODE }
{loop type="content" name="content" lang="$ID"}
<url>
<loc>{$URL nofilter}</loc>
<loc>{$URL}</loc>
<lastmod>{format_date date=$UPDATE_DATE format="c"}</lastmod>
</url>
{/loop}
Expand Down
Expand Up @@ -91,7 +91,7 @@ public function testLoadViewUrl()
$retriever->loadViewUrl('view', 'fr_FR', 1);

$this->assertEquals(URL::getInstance()->absoluteUrl('foo.html'), $retriever->rewrittenUrl);
$this->assertEquals(URL::getInstance()->viewUrl('view', array('locale' => 'fr_FR', 'view_id' => 1)), $retriever->url);
$this->assertEquals(URL::getInstance()->viewUrl('view', array('lang' => 'fr_FR', 'view_id' => 1)), $retriever->url);
}

public function testLoadSpecificUrl()
Expand All @@ -113,6 +113,6 @@ public function testLoadSpecificUrl()
$retriever->loadSpecificUrl('view', 'fr_FR', 1, array('foo0' => 'bar0', 'foo1' => 'bar1'));

$this->assertEquals('foo.html', $retriever->rewrittenUrl);
$this->assertEquals(URL::getInstance()->viewUrl('view', array('foo0' => 'bar0', 'foo1' => 'bar1', 'locale' => 'fr_FR', 'view_id' => 1)), $retriever->url);
$this->assertEquals(URL::getInstance()->viewUrl('view', array('foo0' => 'bar0', 'foo1' => 'bar1', 'lang' => 'fr_FR', 'view_id' => 1)), $retriever->url);
}
}