// Improve language links in dispatcher #PSCFV-1765

This commit is contained in:
rMalie
2012-05-14 09:47:07 +00:00
parent 325e254bcd
commit b403776bbb
2 changed files with 34 additions and 21 deletions
+32 -19
View File
@@ -179,6 +179,8 @@ class DispatcherCore
*/
protected $front_controller = self::FC_FRONT;
protected $loaded_languages = array();
/**
* Get current instance of dispatcher (singleton)
*
@@ -350,7 +352,6 @@ class DispatcherCore
*/
protected function loadRoutes()
{
$context = Context::getContext();
foreach ($this->default_routes as $id => $route)
$this->addRoute(
$id,
@@ -363,23 +364,7 @@ class DispatcherCore
if ($this->use_routes)
{
// Load routes from meta table
$sql = 'SELECT m.page, ml.url_rewrite
FROM `'._DB_PREFIX_.'meta` m
LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta'.Shop::addSqlRestrictionOnLang('ml').')
WHERE id_lang = '.(int)$context->language->id.'
ORDER BY LENGTH(ml.url_rewrite) DESC';
if ($results = Db::getInstance()->executeS($sql))
foreach ($results as $row)
{
if ($row['url_rewrite'])
$this->addRoute($row['page'], $row['url_rewrite'], $row['page']);
else
$this->empty_route = array(
'routeID' => $row['page'],
'rule' => $row['url_rewrite'],
'controller' => $row['page'],
);
}
$this->loadLangRoutes(Context::getContext()->language->id);
// Load custom routes
foreach ($this->default_routes as $route_id => $route_data)
@@ -394,6 +379,30 @@ class DispatcherCore
}
}
public function loadLangRoutes($id_lang)
{
if (in_array($id_lang, $this->loaded_languages))
$this->loaded_languages[] = $id_lang;
$sql = 'SELECT m.page, ml.url_rewrite
FROM `'._DB_PREFIX_.'meta` m
LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta'.Shop::addSqlRestrictionOnLang('ml').')
WHERE id_lang = '.(int)$id_lang.'
ORDER BY LENGTH(ml.url_rewrite) DESC';
if ($results = Db::getInstance()->executeS($sql))
foreach ($results as $row)
{
if ($row['url_rewrite'])
$this->addRoute(str_replace('-', '', $row['page']).'_'.$id_lang, $row['url_rewrite'], $row['page']);
else
$this->empty_route = array(
'routeID' => $row['page'].'_'.$id_lang,
'rule' => $row['url_rewrite'],
'controller' => $row['page'],
);
}
}
/**
*
* @param string $id Name of the route (need to be uniq, a second route with same name will override the first)
@@ -498,8 +507,12 @@ class DispatcherCore
* @param bool $use_routes If false, don't use to create this url
* @param string $anchor Optional anchor to add at the end of this url
*/
public function createUrl($route_id, array $params = array(), $use_routes = true, $anchor = '')
public function createUrl($route_id, array $params = array(), $use_routes = true, $anchor = '', $id_lang = null)
{
if (!$id_lang)
$id_lang = Context::getContext()->language->id;
$this->loadLangRoutes($id_lang);
if (!isset($this->routes[$route_id]))
{
$query = http_build_query($params, '', '&');
+2 -2
View File
@@ -385,7 +385,7 @@ class LinkCore
*/
public function getPageLink($controller, $ssl = false, $id_lang = null, $request = null, $request_url_encode = false)
{
$controller = str_replace('.php', '', $controller);
$controller = str_replace(array('-', '.php'), '', $controller);
if (!$id_lang)
$id_lang = (int)Context::getContext()->language->id;
@@ -400,7 +400,7 @@ class LinkCore
}
unset($request['controller']);
$uri_path = Dispatcher::getInstance()->createUrl($controller, $request);
$uri_path = Dispatcher::getInstance()->createUrl($controller.'_'.$id_lang, $request, true, '', $id_lang);
$url = ($ssl && Configuration::get('PS_SSL_ENABLED')) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true);
$url .= __PS_BASE_URI__.$this->getLangLink($id_lang).ltrim($uri_path, '/');