Created
November 11, 2009 13:52
-
-
Save n1k0/231959 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class fooActions extends sfActions | |
{ | |
/** | |
* Changes current user culture, and redirect the user to the translated doctrine | |
* route url he came from (especially useful when you have these pretty things | |
* called translated slugs in your I18nized doctrine models). | |
* | |
* PLEASE NOTE: it might be the most awful piece of code I ever produced, but... | |
* well, it works™ | |
* | |
* @param sfWebRequest $request | |
*/ | |
public function executeChangeCulture(sfWebRequest $request) | |
{ | |
$lang = $request->getParameter('lang', 'en'); | |
$redirect = '@homepage_culture?sf_culture='.$lang; | |
$this->getUser()->setCulture($lang); | |
// no referer available and we can't guess it? so we just change the user | |
// culture and redirect him to translated homepage | |
if (!$referer = $request->getReferer()) | |
{ | |
$this->redirect($redirect); | |
} | |
// retrieve requested script name + path_info stripping protocol, domain and tld (omg) | |
if (!$requestedUrl = substr($referer, strpos($referer, '/', 7))) | |
{ | |
$this->redirect($redirect); | |
} | |
$requestedUrl = str_replace($_SERVER['SCRIPT_NAME'], '', $requestedUrl); // haha, scary | |
$routing = $this->context->getRouting(); | |
$parameters = $routing->parse($requestedUrl); | |
if (isset($parameters['_sf_route'])) | |
{ | |
$route = $parameters['_sf_route']; | |
$routeName = $routing->getCurrentRouteName(); | |
if ($routeName && $route instanceof sfDoctrineRoute) | |
{ | |
try | |
{ | |
$object = $route->getObject(); | |
} | |
catch (Exception $e) | |
{ | |
} | |
if (!$object instanceof Doctrine_Record) | |
{ | |
break; | |
} | |
if ($object->getTable()->hasRelation('Translation')) | |
{ | |
$translatedObject = $object->getTable() | |
->createQuery('o') | |
->leftJoin('o.Translation t WITH t.lang = ?', $lang) | |
->where('o.id = ?', $object->id) | |
->limit(1) // yeah, if you think I'm dumb have a look at code in Doctrine_Query::fetchOne() | |
->fetchOne() | |
; | |
if ($translatedObject) | |
{ | |
$redirect = $this->generateUrl($routeName, $translatedObject); | |
} | |
} | |
else | |
{ | |
$redirect = $this->generateUrl($routeName, $object); | |
} | |
} | |
} | |
else | |
{ | |
$this->getUser()->setCulture($lang); | |
$redirect = preg_replace( | |
sprintf('`^(http|https)://([a-z\.]+)(/(\w)+_(dev|staging)\.php)?(/)?(%s)/?(.*)`', | |
implode('|', sfConfig::get('app_cultures_available', array('en')))), | |
sprintf('$1://$2$3/%s$6$8$9', $lang), | |
$referer | |
); | |
if (preg_match(sprintf('#/%s/$#', $lang), $redirect)) | |
{ | |
$redirect = substr($redirect, 0, strlen($redirect) - 1); | |
} | |
} | |
$this->redirect($redirect); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment