Fix setting cookie

This commit is contained in:
Nicolas Le Goff
2013-10-28 10:30:39 +01:00
parent 496b5145a7
commit 7b256eae61
4 changed files with 37 additions and 13 deletions

View File

@@ -14,7 +14,9 @@ namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Silex\Application;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class PhraseaLocaleSubscriber implements EventSubscriberInterface
{
@@ -34,18 +36,13 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface
// symfony locale is set on 16 priority, let's override it
array('addLocale', 17),
array('addLocale', 15),
array('removePhraseanetLocale', 14),
),
KernelEvents::RESPONSE => array(
array('addLocaleCookie', 8)
)
);
}
public function removePhraseanetLocale(GetResponseEvent $event)
{
if (isset($this->locale)) {
unset($this->locale);
}
}
public function addLocale(GetResponseEvent $event)
{
if (isset($this->locale)) {
@@ -66,7 +63,7 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface
)
);
$this->app['locale'] = $this->locale = $this->app->share(function(Application $app) use ($event) {
$this->app['locale'] = $this->app->share(function(Application $app) use ($event) {
if (isset($app['phraseanet.registry'])) {
$event->getRequest()->setDefaultLocale(
$app['phraseanet.registry']->get('GV_default_lng', 'en_GB')
@@ -102,8 +99,16 @@ class PhraseaLocaleSubscriber implements EventSubscriberInterface
return $event->getRequest()->getLocale();
});
\phrasea::use_i18n($this->app['locale']);
$this->locale = $this->app['locale'];
\phrasea::use_i18n($this->locale);
}
$event->getResponse()->headers->setCookie(new Cookie('locale', $this->app['locale'], 0, '/', null, false, false));
public function addLocaleCookie(FilterResponseEvent $event)
{
$cookies = $event->getRequest()->cookies;
if (isset($this->locale) && (false === $cookies->has('locale') || $cookies->get('locale') !== $this->locale)) {
$event->getResponse()->headers->setCookie(new Cookie('locale', $this->locale, 0, '/', null, false, false));
}
}
}

View File

@@ -57,6 +57,23 @@ class PhraseaLocaleSubscriberTest extends \PhraseanetPHPUnitAbstract
$this->assertEquals('fr_FR', $client->getResponse()->getContent());
}
public function testCookieIsSet()
{
$client = new Client(self::$DI['app']);
$client->request('GET', '/', array(), array(), array('HTTP_ACCEPT_LANGUAGE' => 'fr-FR,fr;q=0.9'));
$settedCookie = null;
foreach($client->getResponse()->headers->getCookies() as $cookie) {
if($cookie->getName() === 'locale') {
$settedCookie = $cookie;
break;
}
}
$this->assertNotNull($settedCookie);
$this->assertEquals('fr_FR', $settedCookie->getValue());
}
/**
* @covers Alchemy\Phrasea\Application
*/

View File

@@ -31,7 +31,9 @@ define([
$leftBlock : $(".left-block", this.$bottom),
$rightBlock : $(".right-block", this.$bottom),
fieldsToDelete : [],
lng : window.p4.lng || "en",
lng : function() {
return typeof p4 === "undefined" ? "en" : (p4.lng || "en");
},
resizeListBlock: function () {
var listBlock = $(".list-block", AdminFieldApp.$leftBlock);
listBlock.height(AdminFieldApp.$window.height() - listBlock.offset().top - 10);

View File

@@ -35,7 +35,7 @@ define([
render: function() {
var self = this;
var template = _.template($("#edit_template").html(), {
lng: AdminFieldApp.lng,
lng: AdminFieldApp.lng(),
field: this.model.toJSON(),
vocabularyTypes: AdminFieldApp.vocabularyCollection.toJSON(),
modelErrors: AdminFieldApp.errorManager.getModelError(this.model),