[*] Core: that is now easier to get links for another shop

This commit is contained in:
Rémi Gaillard
2013-07-11 16:40:40 +02:00
parent 17ee071bdf
commit 20b5449231
2 changed files with 129 additions and 70 deletions
+54 -31
View File
@@ -379,12 +379,12 @@ class DispatcherCore
/**
* Load default routes group by languages
*/
protected function loadRoutes()
protected function loadRoutes($id_shop = null)
{
$context = Context::getContext();
// Load custom routes from modules
$modules_routes = Hook::exec('moduleRoutes', array(), null, true, false);
$modules_routes = Hook::exec('moduleRoutes', array('id_shop' => $id_shop), null, true, false);
if (is_array($modules_routes) && count($modules_routes))
foreach($modules_routes as $module_route)
foreach($module_route as $route => $route_details)
@@ -405,7 +405,8 @@ class DispatcherCore
$route['controller'],
$lang['id_lang'],
$route['keywords'],
isset($route['params']) ? $route['params'] : array()
isset($route['params']) ? $route['params'] : array(),
$id_shop
);
// Load the custom routes prior the defaults to avoid infinite loops
@@ -420,13 +421,13 @@ class DispatcherCore
// Load routes from meta table
$sql = 'SELECT m.page, ml.url_rewrite, ml.id_lang
FROM `'._DB_PREFIX_.'meta` m
LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta'.Shop::addSqlRestrictionOnLang('ml').')
LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON (m.id_meta = ml.id_meta'.Shop::addSqlRestrictionOnLang('ml', $id_shop).')
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'], $row['id_lang']);
$this->addRoute($row['page'], $row['url_rewrite'], $row['page'], $row['id_lang'], array(), array(), $id_shop);
}
// Set default empty route if no empty route (that's weird I know)
@@ -439,7 +440,7 @@ class DispatcherCore
// Load custom routes
foreach ($this->default_routes as $route_id => $route_data)
if ($custom_route = Configuration::get('PS_ROUTE_'.$route_id))
if ($custom_route = Configuration::get('PS_ROUTE_'.$route_id, null, null, $id_shop))
foreach (Language::getLanguages() as $lang)
$this->addRoute(
$route_id,
@@ -447,7 +448,8 @@ class DispatcherCore
$route_data['controller'],
$lang['id_lang'],
$route_data['keywords'],
isset($route_data['params']) ? $route_data['params'] : array()
isset($route_data['params']) ? $route_data['params'] : array(),
$id_shop
);
}
}
@@ -458,11 +460,15 @@ class DispatcherCore
* @param string $rule Url rule
* @param string $controller Controller to call if request uri match the rule
* @param int $id_lang
* @param int $id_shop
*/
public function addRoute($route_id, $rule, $controller, $id_lang = null, array $keywords = array(), array $params = array())
public function addRoute($route_id, $rule, $controller, $id_lang = null, array $keywords = array(), array $params = array(), $id_shop = null)
{
if (is_null($id_lang))
$id_lang = Context::getContext()->language->id;
if ($id_lang === null)
$id_lang = (int)Context::getContext()->language->id;
if ($id_shop === null)
$id_shop = (int)Context::getContext()->shop->id;
$regexp = preg_quote($rule, '#');
if ($keywords)
@@ -497,10 +503,12 @@ class DispatcherCore
}
$regexp = '#^/'.$regexp.'(\?.*)?$#u';
if (!isset($this->routes[$id_lang]))
$this->routes[$id_lang] = array();
if (!isset($this->routes[$id_shop]))
$this->routes[$id_shop] = array();
if (!isset($this->routes[$id_shop][$id_lang]))
$this->routes[$id_shop][$id_lang] = array();
$this->routes[$id_lang][$route_id] = array(
$this->routes[$id_shop][$id_lang][$route_id] = array(
'rule' => $rule,
'regexp' => $regexp,
'controller' => $controller,
@@ -514,14 +522,17 @@ class DispatcherCore
*
* @param string $route_id
* @param int $id_lang
* @param int $id_shop
* @return bool
*/
public function hasRoute($route_id, $id_lang = null)
public function hasRoute($route_id, $id_lang = null, $id_shop = null)
{
if (is_null($id_lang))
$id_lang = Context::getContext()->language->id;
if ($id_lang === null)
$id_lang = (int)Context::getContext()->language->id;
if ($id_shop === null)
$id_shop = (int)Context::getContext()->shop->id;
return isset($this->routes[$id_lang]) && isset($this->routes[$id_lang][$route_id]);
return isset($this->routes[$id_shop]) && isset($this->routes[$id_shop][$id_lang]) && isset($this->routes[$id_shop][$id_lang][$route_id]);
}
/**
@@ -530,14 +541,18 @@ class DispatcherCore
* @param string $route_id
* @param int $id_lang
* @param string $keyword
* @param int $id_shop
* @return bool
*/
public function hasKeyword($route_id, $id_lang, $keyword)
public function hasKeyword($route_id, $id_lang, $keyword, $id_shop = null)
{
if (!isset($this->routes[$id_lang]) || !isset($this->routes[$id_lang][$route_id]))
if ($id_shop === null)
$id_shop = (int)Context::getContext()->shop->id;
if (!isset($this->routes[$id_shop]) || !isset($this->routes[$id_shop][$id_lang]) || !isset($this->routes[$id_shop][$id_lang][$route_id]))
return false;
return preg_match('#\{([^{}]*:)?'.preg_quote($keyword, '#').'(:[^{}]*)?\}#', $this->routes[$id_lang][$route_id]['rule']);
return preg_match('#\{([^{}]*:)?'.preg_quote($keyword, '#').'(:[^{}]*)?\}#', $this->routes[$id_shop][$id_lang][$route_id]['rule']);
}
/**
@@ -569,18 +584,23 @@ 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, $id_lang = null, array $params = array(), $force_routes = false, $anchor = '')
public function createUrl($route_id, $id_lang = null, array $params = array(), $force_routes = false, $anchor = '', $id_shop = null)
{
if (!$id_lang)
$id_lang = Context::getContext()->language->id;
if ($id_lang === null)
$id_lang = (int)Context::getContext()->language->id;
if ($id_shop === null)
$id_shop = (int)Context::getContext()->shop->id;
if (!isset($this->routes[$id_lang][$route_id]))
if ($this->use_routes && !isset($this->routes[$id_shop]))
$this->loadRoutes($id_shop);
if (!isset($this->routes[$id_shop][$id_lang][$route_id]))
{
$query = http_build_query($params, '', '&');
$index_link = $this->use_routes ? '' : 'index.php';
return ($route_id == 'index') ? $index_link.(($query) ? '?'.$query : '') : 'index.php?controller='.$route_id.(($query) ? '&'.$query : '').$anchor;
}
$route = $this->routes[$id_lang][$route_id];
$route = $this->routes[$id_shop][$id_lang][$route_id];
// Check required fields
$query_params = isset($route['params']) ? $route['params'] : array();
foreach ($route['keywords'] as $key => $data)
@@ -646,7 +666,7 @@ class DispatcherCore
*
* @return string
*/
public function getController()
public function getController($id_shop = null)
{
if (defined('_PS_ADMIN_DIR_'))
$_GET['controllerUri'] = Tools::getvalue('controller');
@@ -655,7 +675,10 @@ class DispatcherCore
$_GET['controller'] = $this->controller;
return $this->controller;
}
if ($id_shop === null)
$id_shop = (int)Context::getContext()->shop->id;
$controller = Tools::getValue('controller');
if (isset($controller) && is_string($controller) && preg_match('/^([0-9a-z_-]+)\?(.*)=(.*)$/Ui', $controller, $m))
@@ -682,10 +705,10 @@ class DispatcherCore
{
// Add empty route as last route to prevent this greedy regexp to match request uri before right time
if ($this->empty_route)
$this->addRoute($this->empty_route['routeID'], $this->empty_route['rule'], $this->empty_route['controller'], Context::getContext()->language->id);
$this->addRoute($this->empty_route['routeID'], $this->empty_route['rule'], $this->empty_route['controller'], Context::getContext()->language->id, array(), array(), $id_shop);
if (isset($this->routes[Context::getContext()->language->id]))
foreach ($this->routes[Context::getContext()->language->id] as $route)
if (isset($this->routes[$id_shop][Context::getContext()->language->id]))
foreach ($this->routes[$id_shop][Context::getContext()->language->id] as $route)
if (preg_match($route['regexp'], $this->request_uri, $m))
{
// Route found ! Now fill $_GET with parameters of uri
@@ -771,4 +794,4 @@ class DispatcherCore
return $controllers;
}
}
}