diff --git a/classes/Dispatcher.php b/classes/Dispatcher.php index 0d964c71e..e8894f88d 100644 --- a/classes/Dispatcher.php +++ b/classes/Dispatcher.php @@ -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; } -} +} \ No newline at end of file diff --git a/classes/Link.php b/classes/Link.php index b39977aff..7c36c0e55 100644 --- a/classes/Link.php +++ b/classes/Link.php @@ -91,7 +91,7 @@ class LinkCore if (!$id_lang) $id_lang = Context::getContext()->language->id; - if (!$id_shop) + if ($id_shop === null) $shop = Context::getContext()->shop; else $shop = new Shop($id_shop); @@ -117,25 +117,25 @@ class LinkCore $params['meta_keywords'] = Tools::str2url($product->getFieldByLang('meta_keywords')); $params['meta_title'] = Tools::str2url($product->getFieldByLang('meta_title')); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'manufacturer', $id_shop)) $params['manufacturer'] = Tools::str2url($product->isFullyLoaded ? $product->manufacturer_name : Manufacturer::getNameById($product->id_manufacturer)); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'supplier', $id_shop)) $params['supplier'] = Tools::str2url($product->isFullyLoaded ? $product->supplier_name : Supplier::getNameById($product->id_supplier)); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'price', $id_shop)) $params['price'] = $product->isFullyLoaded ? $product->price : Product::getPriceStatic($product->id, false, null, 6, null, false, true, 1, false, null, null, null, $product->specificPrice); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'tags', $id_shop)) $params['tags'] = Tools::str2url($product->getTags($id_lang)); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'category', $id_shop)) $params['category'] = (!is_null($product->category) && !empty($product->category)) ? Tools::str2url($product->category) : Tools::str2url($category); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'reference', $id_shop)) $params['reference'] = Tools::str2url($product->reference); - if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories')) + if ($dispatcher->hasKeyword('product_rule', $id_lang, 'categories', $id_shop)) { $params['category'] = (!$category) ? $product->category : $category; $cats = array(); @@ -146,7 +146,7 @@ class LinkCore } $anchor = $ipa ? $product->getAnchor($ipa) : ''; - return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor); + return $url.$dispatcher->createUrl('product_rule', $id_lang, $params, $force_routes, $anchor, $id_shop); } /** @@ -158,11 +158,16 @@ class LinkCore * @param string $selected_filters Url parameter to autocheck filters of the module blocklayered * @return string */ - public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null) + public function getCategoryLink($category, $alias = null, $id_lang = null, $selected_filters = null, $id_shop = null) { if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); if (!is_object($category)) $category = new Category($category, $id_lang); @@ -185,7 +190,7 @@ class LinkCore $params['selected_filters'] = $selected_filters; } - return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow); + return $url.Dispatcher::getInstance()->createUrl($rule, $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -196,17 +201,22 @@ class LinkCore * @param int $id_lang * @return string */ - public function getCMSCategoryLink($cms_category, $alias = null, $id_lang = null) + public function getCMSCategoryLink($cms_category, $alias = null, $id_lang = null, $id_shop = null) { if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); $dispatcher = Dispatcher::getInstance(); if (!is_object($cms_category)) { - if ($alias !== null && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_keywords') && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_title')) - return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, array('id' => (int)$cms_category, 'rewrite' => (string)$alias), $this->allow); + if ($alias !== null && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_category_rule', $id_lang, 'meta_title', $id_shop)) + return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, array('id' => (int)$cms_category, 'rewrite' => (string)$alias), $this->allow, '', $id_shop); $cms_category = new CMSCategory($cms_category, $id_lang); } @@ -217,7 +227,7 @@ class LinkCore $params['meta_keywords'] = Tools::str2url($cms_category->meta_keywords); $params['meta_title'] = Tools::str2url($cms_category->meta_title); - return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, $params, $this->allow); + return $url.$dispatcher->createUrl('cms_category_rule', $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -229,19 +239,25 @@ class LinkCore * @param int $id_lang * @return string */ - public function getCMSLink($cms, $alias = null, $ssl = false, $id_lang = null) + public function getCMSLink($cms, $alias = null, $ssl = false, $id_lang = null, $id_shop = null) { $base = (($ssl && $this->ssl_enable) ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_); if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = $base.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = $base.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); + $dispatcher = Dispatcher::getInstance(); if (!is_object($cms)) { - if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_keywords') && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_title')) - return $url.$dispatcher->createUrl('cms_rule', $id_lang, array('id' => (int)$cms, 'rewrite' => (string)$alias), $this->allow); + if ($alias !== null && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('cms_rule', $id_lang, 'meta_title', $id_shop)) + return $url.$dispatcher->createUrl('cms_rule', $id_lang, array('id' => (int)$cms, 'rewrite' => (string)$alias), $this->allow, '', $id_shop); $cms = new CMS($cms, $id_lang); } @@ -258,7 +274,7 @@ class LinkCore if (isset($cms->meta_title) && !empty($cms->meta_title)) $params['meta_title'] = is_array($cms->meta_title) ? Tools::str2url($cms->meta_title[(int)$id_lang]) : Tools::str2url($cms->meta_title); - return $url.$dispatcher->createUrl('cms_rule', $id_lang, $params, $this->allow); + return $url.$dispatcher->createUrl('cms_rule', $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -269,17 +285,22 @@ class LinkCore * @param int $id_lang * @return string */ - public function getSupplierLink($supplier, $alias = null, $id_lang = null) + public function getSupplierLink($supplier, $alias = null, $id_lang = null, $id_shop = null) { if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); $dispatcher = Dispatcher::getInstance(); if (!is_object($supplier)) { - if ($alias !== null && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_keywords') && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_title')) - return $url.$dispatcher->createUrl('supplier_rule', $id_lang, array('id' => (int)$supplier, 'rewrite' => (string)$alias), $this->allow); + if ($alias !== null && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('supplier_rule', $id_lang, 'meta_title', $id_shop)) + return $url.$dispatcher->createUrl('supplier_rule', $id_lang, array('id' => (int)$supplier, 'rewrite' => (string)$alias), $this->allow, '', $id_shop); $supplier = new Supplier($supplier, $id_lang); } @@ -290,7 +311,7 @@ class LinkCore $params['meta_keywords'] = Tools::str2url($supplier->meta_keywords); $params['meta_title'] = Tools::str2url($supplier->meta_title); - return $url.$dispatcher->createUrl('supplier_rule', $id_lang, $params, $this->allow); + return $url.$dispatcher->createUrl('supplier_rule', $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -301,17 +322,22 @@ class LinkCore * @param int $id_lang * @return string */ - public function getManufacturerLink($manufacturer, $alias = null, $id_lang = null) + public function getManufacturerLink($manufacturer, $alias = null, $id_lang = null, $id_shop = null) { if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = _PS_BASE_URL_.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = 'http://'.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); $dispatcher = Dispatcher::getInstance(); if (!is_object($manufacturer)) { - if ($alias !== null && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_keywords') && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_title')) - return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, array('id' => (int)$manufacturer, 'rewrite' => (string)$alias), $this->allow); + if ($alias !== null && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_keywords', $id_shop) && !$dispatcher->hasKeyword('manufacturer_rule', $id_lang, 'meta_title', $id_shop)) + return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, array('id' => (int)$manufacturer, 'rewrite' => (string)$alias), $this->allow, '', $id_shop); $manufacturer = new Manufacturer($manufacturer, $id_lang); } @@ -322,7 +348,7 @@ class LinkCore $params['meta_keywords'] = Tools::str2url($manufacturer->meta_keywords); $params['meta_title'] = Tools::str2url($manufacturer->meta_title); - return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, $params, $this->allow); + return $url.$dispatcher->createUrl('manufacturer_rule', $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -334,23 +360,28 @@ class LinkCore * @param int $id_lang * @return string */ - public function getModuleLink($module, $controller = 'default', array $params = array(), $ssl = false, $id_lang = null) + public function getModuleLink($module, $controller = 'default', array $params = array(), $ssl = false, $id_lang = null, $id_shop = null) { $base = (($ssl && $this->ssl_enable) ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_); if (!$id_lang) $id_lang = Context::getContext()->language->id; - $url = $base.__PS_BASE_URI__.$this->getLangLink($id_lang); + + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + $url = $base.$shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang); // Set available keywords $params['module'] = $module; $params['controller'] = $controller ? $controller : 'default'; // If the module has its own route ... just use it ! - if (Dispatcher::getInstance()->hasRoute('module-'.$module.'-'.$controller, $id_lang)) + if (Dispatcher::getInstance()->hasRoute('module-'.$module.'-'.$controller, $id_lang, $id_shop)) return $this->getPageLink('module-'.$module.'-'.$controller, $ssl, $id_lang, $params); else - return $url.Dispatcher::getInstance()->createUrl('module', $id_lang, $params, $this->allow); + return $url.Dispatcher::getInstance()->createUrl('module', $id_lang, $params, $this->allow, '', $id_shop); } /** @@ -421,7 +452,7 @@ class LinkCore * * @return string Page link */ - 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, $id_shop = null) { $controller = Tools::strReplaceFirst('.php', '', $controller); @@ -437,9 +468,14 @@ class LinkCore parse_str($request, $request); } - $uri_path = Dispatcher::getInstance()->createUrl($controller, $id_lang, $request); + if ($id_shop === null) + $shop = Context::getContext()->shop; + else + $shop = new Shop($id_shop); + + $uri_path = Dispatcher::getInstance()->createUrl($controller, $id_lang, $request, false, '', $id_shop); $url = ($ssl && $this->ssl_enable) ? Tools::getShopDomainSsl(true) : Tools::getShopDomain(true); - $url .= __PS_BASE_URI__.$this->getLangLink($id_lang).ltrim($uri_path, '/'); + $url .= $shop->domain.$shop->getBaseURI().$this->getLangLink($id_lang).ltrim($uri_path, '/'); return $url; }