From: arno Date: Fri, 18 Mar 2011 15:15:41 +0000 (+0100) Subject: store lang preference in session instead of url X-Git-Tag: v0.3~18 X-Git-Url: https://dev.renevier.net/?p=syj.git;a=commitdiff_plain;h=627f937d7241dc4b9311759fa446c3738d05f5a9 store lang preference in session instead of url --- diff --git a/application/controllers/helpers/SyjSession.php b/application/controllers/helpers/SyjSession.php index 11c369f..06949f0 100644 --- a/application/controllers/helpers/SyjSession.php +++ b/application/controllers/helpers/SyjSession.php @@ -13,29 +13,20 @@ class Syj_Controller_Action_Helper_SyjSession extends Zend_Controller_Action_Hel throw new Zend_Exception(); } - $storage = Zend_Auth::getInstance()->getStorage(); - $storage->clear(); - $storage->write(array('user' => $user->id)); - Zend_Session::rememberMe(); // zend default expiration delay is 2 weeks. Ok, use that value + $storage = new Zend_Session_Namespace('userSettings'); + $storage->user = $user->id; + Zend_Session::rememberMe(); } static public function logout() { - Zend_Session::start(); - Zend_Session::destroy(); + $storage = new Zend_Session_Namespace('userSettings'); + unset($storage->user); + Zend_Session::rememberMe(); } static public function user() { - try { - $sessionStorage = Zend_Auth::getInstance()->getStorage(); - } catch(Exception $e) { - return null; - } - $sessionData = $sessionStorage->read(); - if ($sessionStorage->isEmpty()) { - return null; - } - - $id = $sessionData['user']; + $storage = new Zend_Session_Namespace('userSettings'); + $id = $storage->user; if (!isset($id)) { return null; } @@ -63,16 +54,9 @@ class Syj_Controller_Action_Helper_SyjSession extends Zend_Controller_Action_Hel $view = $viewRenderer->view; $request = $this->getRequest(); - $encodeduri = implode('/', array_map('urlencode', explode('/', $request->getRequestUri()))); + $encodeduri = $view->UriPath(true); $loginurl = $view->addParamToUrl($view->baseUrl() . '/' . 'login', 'redirect', $encodeduri); $translator = Zend_Registry::get('Zend_Translate'); - $lang = $request->getQuery('lang'); - if ($lang) { - $adapter = $translator->getAdapter(); - if ($adapter->isAvailable($lang)) { - $loginurl = $view->addParamToUrl($loginurl, 'lang', $lang); - } - } $this->getActionController()->getHelper('Redirector')->gotoURL($loginurl, array('prependBase' => false)); } } diff --git a/application/controllers/plugins/SyjLocale.php b/application/controllers/plugins/SyjLocale.php index 19af8ec..52dd491 100644 --- a/application/controllers/plugins/SyjLocale.php +++ b/application/controllers/plugins/SyjLocale.php @@ -7,11 +7,14 @@ class Syj_Controllers_Plugins_SyjLocale extends Zend_Controller_Plugin_Abstract public function preDispatch(Zend_Controller_Request_Abstract $request) { $lang = $request->getQuery('lang'); - if (is_null($lang)) { - return; + + if ($lang) { + setcookie("syj_lang", $lang, 0, "", "", false, true); + } else { + $lang = $request->getCookie('syj_lang'); } - if (!Zend_Registry::isRegistered('Zend_Translate')) { + if (!$lang) { return; } diff --git a/application/forms/Login.php b/application/forms/Login.php index b42f8ba..ab3a5f6 100644 --- a/application/forms/Login.php +++ b/application/forms/Login.php @@ -13,10 +13,7 @@ class Syj_Form_Login extends Syj_Form_TableAbstract $this->setMainElements(array($user, $pass)) ->addElement('Hidden', 'login_geom_id', array( 'decorators' => array('ViewHelper'))); - $currentUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(); - if (strpos($currentUri, '?') !== false) { - $currentUri = strstr($currentUri, '?', true); - } + $currentUri = $this->getView()->UriPath(true); $href = $this->getView()->addParamToUrl('newpwd', 'redirect', $currentUri, true); $anchor = $this->getView()->Anchor($href, diff --git a/application/views/helpers/Anchor.php b/application/views/helpers/Anchor.php index 55cae33..6cc59bf 100644 --- a/application/views/helpers/Anchor.php +++ b/application/views/helpers/Anchor.php @@ -5,15 +5,6 @@ class Syj_View_Helper_Anchor extends Zend_View_Helper_HtmlElement { public function Anchor($href, $text = null, array $attribs=array(), $escape = true) { - $lang = Zend_Controller_Front::getInstance()->getRequest()->getQuery('lang'); - if ($lang) { - $translator = Zend_Registry::get('Zend_Translate'); - $adapter = $translator->getAdapter(); - if ($adapter->isAvailable($lang)) { - $href = $this->view->addParamToUrl($href, 'lang', $lang); - } - } - $attribs = array_merge(array('href' => $href), $attribs); if (!isset($text)) { $text = $href; diff --git a/application/views/helpers/FooterLink.php b/application/views/helpers/FooterLink.php index cebae50..0a2a475 100644 --- a/application/views/helpers/FooterLink.php +++ b/application/views/helpers/FooterLink.php @@ -11,10 +11,7 @@ class Syj_View_Helper_FooterLink extends Zend_View_Helper_Abstract } else { $href = $page->getHRef(); if ($redirect) { - $currentUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(); - if (($pos = strpos($currentUri, '?')) !== false) { - $currentUri = substr($currentUri, 0, $pos); - } + $currentUri = $this->view->UriPath(true); $href = $this->view->addParamToUrl($href, 'redirect', $currentUri, true); } $link = $this->view->anchor($href, $text, array('class' => 'footer-anchor')); diff --git a/application/views/helpers/LocaleSwitcher.php b/application/views/helpers/LocaleSwitcher.php index cd4e26d..f5b9042 100644 --- a/application/views/helpers/LocaleSwitcher.php +++ b/application/views/helpers/LocaleSwitcher.php @@ -9,7 +9,7 @@ class Syj_View_Helper_LocaleSwitcher extends Zend_View_Helper_Abstract $availables = $translator->getList(); $current = $translator->getLocale(); - $requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(); + $requestUri = $this->view->UriPath(); $links = array(); foreach ($availables as $lang) { diff --git a/application/views/helpers/LogoutLink.php b/application/views/helpers/LogoutLink.php index 146276e..307c55c 100644 --- a/application/views/helpers/LogoutLink.php +++ b/application/views/helpers/LogoutLink.php @@ -5,8 +5,7 @@ class Syj_View_Helper_LogoutLink extends Zend_View_Helper_Abstract { public function logoutLink() { - $currentUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(); - $encodeduri = implode('/', array_map('urlencode', explode('/', $currentUri))); + $encodeduri = $this->view->UriPath(true); $translatedString = $this->view->translate('logout'); $href = $this->view->addParamToUrl('logout', 'redirect', $encodeduri, true); return $this->view->anchor($href, $translatedString, array('id' => 'logout', 'class' => 'menu-item')); diff --git a/application/views/helpers/UriPath.php b/application/views/helpers/UriPath.php new file mode 100644 index 0000000..8d267d5 --- /dev/null +++ b/application/views/helpers/UriPath.php @@ -0,0 +1,17 @@ +getRequest()->getRequestUri(); + if (($pos = strpos($uri, '?')) !== false) { + $uri = substr($uri, 0, $pos); + } + if ($encoded) { + $uri = implode('/', array_map('urlencode', explode('/', $uri))); + } + return $uri; + } +}