// Improve language links in dispatcher #PSCFV-1765
This commit is contained in:
+32
-19
@@ -179,6 +179,8 @@ class DispatcherCore
|
|||||||
*/
|
*/
|
||||||
protected $front_controller = self::FC_FRONT;
|
protected $front_controller = self::FC_FRONT;
|
||||||
|
|
||||||
|
protected $loaded_languages = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get current instance of dispatcher (singleton)
|
* Get current instance of dispatcher (singleton)
|
||||||
*
|
*
|
||||||
@@ -350,7 +352,6 @@ class DispatcherCore
|
|||||||
*/
|
*/
|
||||||
protected function loadRoutes()
|
protected function loadRoutes()
|
||||||
{
|
{
|
||||||
$context = Context::getContext();
|
|
||||||
foreach ($this->default_routes as $id => $route)
|
foreach ($this->default_routes as $id => $route)
|
||||||
$this->addRoute(
|
$this->addRoute(
|
||||||
$id,
|
$id,
|
||||||
@@ -363,23 +364,7 @@ class DispatcherCore
|
|||||||
if ($this->use_routes)
|
if ($this->use_routes)
|
||||||
{
|
{
|
||||||
// Load routes from meta table
|
// Load routes from meta table
|
||||||
$sql = 'SELECT m.page, ml.url_rewrite
|
$this->loadLangRoutes(Context::getContext()->language->id);
|
||||||
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'],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load custom routes
|
// Load custom routes
|
||||||
foreach ($this->default_routes as $route_id => $route_data)
|
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)
|
* @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 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
|
* @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]))
|
if (!isset($this->routes[$route_id]))
|
||||||
{
|
{
|
||||||
$query = http_build_query($params, '', '&');
|
$query = http_build_query($params, '', '&');
|
||||||
|
|||||||
+2
-2
@@ -385,7 +385,7 @@ class LinkCore
|
|||||||
*/
|
*/
|
||||||
public function getPageLink($controller, $ssl = false, $id_lang = null, $request = null, $request_url_encode = false)
|
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)
|
if (!$id_lang)
|
||||||
$id_lang = (int)Context::getContext()->language->id;
|
$id_lang = (int)Context::getContext()->language->id;
|
||||||
@@ -400,7 +400,7 @@ class LinkCore
|
|||||||
}
|
}
|
||||||
unset($request['controller']);
|
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 = ($ssl && Configuration::get('PS_SSL_ENABLED')) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true);
|
||||||
$url .= __PS_BASE_URI__.$this->getLangLink($id_lang).ltrim($uri_path, '/');
|
$url .= __PS_BASE_URI__.$this->getLangLink($id_lang).ltrim($uri_path, '/');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user