toutpratique/override/classes/Dispatcher.php

212 lines
8.3 KiB
PHP

<?php
include_once(_PS_MODULE_DIR_. 'seourl/classes/seourl.php');
class Dispatcher extends DispatcherCore {
public function dispatch()
{
$controller_class = '';
// Get current controller
$this->getController();
if (!$this->controller)
$this->controller = $this->useDefaultController();
// Dispatch with right front controller
switch ($this->front_controller)
{
// Dispatch front office controller
case self::FC_FRONT :
$controllers = Dispatcher::getControllers(array(_PS_FRONT_CONTROLLER_DIR_, _PS_OVERRIDE_DIR_.'controllers/front/'));
$controllers['index'] = 'IndexController';
if (isset($controllers['auth']))
$controllers['authentication'] = $controllers['auth'];
if (isset($controllers['compare']))
$controllers['productscomparison'] = $controllers['compare'];
if (isset($controllers['contact']))
$controllers['contactform'] = $controllers['contact'];
// SEOURL
if ($this->controller == 'pagenotfound') {
$request = substr($this->request_uri, 1, strlen($this->request_uri));
$request = strtok($request,'?');
$context = Context::getContext();
$current_uri = ManageSeoUrl::getUrl($request, $context->language->id, $context->shop->id);
if(!empty($current_uri['redirect'])){
header("Status: 301 Moved Permanently", false, 301);
header("Location:" . $current_uri['redirect']);
exit();
}
if(!empty($current_uri['canonical'])){
$_POST['seo_canonical'] = $current_uri['canonical'];
}
if(isset($current_uri['type']))
{
if ($current_uri['type'] == ManageSeoUrl::TYPE_PRODUCT) {
$this->controller = 'product';
$_POST['id_product'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_CATEGORY) {
$this->controller = 'category';
$_POST['id_category'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_CATEGORY_CMS) {
$this->controller = 'categorycms';
$_POST['id_category_cms'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_POST_CMS) {
$this->controller = 'postcms';
$_POST['id_post_cms'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_POST_EDITO) {
$this->controller = 'postedito';
$_POST['id_post_edito'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_HOME_EDITO) {
$this->controller = 'homeedito';
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_STORE_HOME) {
$this->controller = 'homestore';
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_VIDEO) {
$this->controller = 'videos';
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_CMS) {
$this->controller = 'cms';
$_POST['id_cms'] = $current_uri['id_element'];
}
elseif ($current_uri['type'] == ManageSeoUrl::TYPE_301) {
header("Status: 301 Moved Permanently", false, 301);
header("Location:" . $current_uri['redirect']);
exit();
}
} else {
// redirect 301 article
$res = substr($request, strlen($request) - 4);
if ($res == ManageSeoUrl::SUFFIX_PHP) {
$segments = explode('/', $request);
if (sizeof($segments) >= 2) {
$last = $segments[(sizeof($segments)-1)];
$data_url = explode('-', $last);
if (Validate::isInt($data_url[0])) {
$url = ManageSeoUrl::getByType($data_url[0], ManageSeoUrl::TYPE_POST_CMS, false, Context::getContext()->language->id);
$redirect = "http://".Context::getContext()->shop->domain.__PS_BASE_URI__;
if (Validate::isLoadedObject($url)) {
header("Status: 301 Moved Permanently", false, 301);
header("Location:" . $redirect.$url->link_rewrite);
exit();
}
}
}
}
}
}
if (!isset($controllers[strtolower($this->controller)]))
$this->controller = $this->controller_not_found;
$controller_class = $controllers[strtolower($this->controller)];
$params_hook_action_dispatcher = array('controller_type' => self::FC_FRONT, 'controller_class' => $controller_class, 'is_module' => 0);
break;
// Dispatch module controller for front office
case self::FC_MODULE :
$module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
$module = Module::getInstanceByName($module_name);
$controller_class = 'PageNotFoundController';
if (Validate::isLoadedObject($module) && $module->active)
{
$controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$module_name.'/controllers/front/');
if (isset($controllers[strtolower($this->controller)]))
{
include_once(_PS_MODULE_DIR_.$module_name.'/controllers/front/'.$this->controller.'.php');
$controller_class = $module_name.$this->controller.'ModuleFrontController';
}
}
$params_hook_action_dispatcher = array('controller_type' => self::FC_FRONT, 'controller_class' => $controller_class, 'is_module' => 1);
break;
// Dispatch back office controller + module back office controller
case self::FC_ADMIN :
if ($this->use_default_controller && !Tools::getValue('token') && Validate::isLoadedObject(Context::getContext()->employee) && Context::getContext()->employee->isLoggedBack())
Tools::redirectAdmin('index.php?controller='.$this->controller.'&token='.Tools::getAdminTokenLite($this->controller));
$tab = Tab::getInstanceFromClassName($this->controller, Configuration::get('PS_LANG_DEFAULT'));
$retrocompatibility_admin_tab = null;
if ($tab->module)
{
if (file_exists(_PS_MODULE_DIR_.$tab->module.'/'.$tab->class_name.'.php'))
$retrocompatibility_admin_tab = _PS_MODULE_DIR_.$tab->module.'/'.$tab->class_name.'.php';
else
{
$controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$tab->module.'/controllers/admin/');
if (!isset($controllers[strtolower($this->controller)]))
{
$this->controller = $this->controller_not_found;
$controller_class = 'AdminNotFoundController';
}
else
{
// Controllers in modules can be named AdminXXX.php or AdminXXXController.php
include_once(_PS_MODULE_DIR_.$tab->module.'/controllers/admin/'.$controllers[strtolower($this->controller)].'.php');
$controller_class = $controllers[strtolower($this->controller)].(strpos($controllers[strtolower($this->controller)], 'Controller') ? '' : 'Controller');
}
}
$params_hook_action_dispatcher = array('controller_type' => self::FC_ADMIN, 'controller_class' => $controller_class, 'is_module' => 1);
}
else
{
$controllers = Dispatcher::getControllers(array(_PS_ADMIN_DIR_.'/tabs/', _PS_ADMIN_CONTROLLER_DIR_, _PS_OVERRIDE_DIR_.'controllers/admin/'));
if (!isset($controllers[strtolower($this->controller)]))
{
// If this is a parent tab, load the first child
if (Validate::isLoadedObject($tab) && $tab->id_parent == 0 && ($tabs = Tab::getTabs(Context::getContext()->language->id, $tab->id)) && isset($tabs[0]))
Tools::redirectAdmin(Context::getContext()->link->getAdminLink($tabs[0]['class_name']));
$this->controller = $this->controller_not_found;
}
$controller_class = $controllers[strtolower($this->controller)];
$params_hook_action_dispatcher = array('controller_type' => self::FC_ADMIN, 'controller_class' => $controller_class, 'is_module' => 0);
if (file_exists(_PS_ADMIN_DIR_.'/tabs/'.$controller_class.'.php'))
$retrocompatibility_admin_tab = _PS_ADMIN_DIR_.'/tabs/'.$controller_class.'.php';
}
// @retrocompatibility with admin/tabs/ old system
if ($retrocompatibility_admin_tab)
{
include_once($retrocompatibility_admin_tab);
include_once(_PS_ADMIN_DIR_.'/functions.php');
runAdminTab($this->controller, !empty($_REQUEST['ajaxMode']));
return;
}
break;
default :
throw new PrestaShopException('Bad front controller chosen');
}
// Instantiate controller
try
{
// Loading controller
$controller = Controller::getController($controller_class);
// Execute hook dispatcher
if (isset($params_hook_action_dispatcher))
Hook::exec('actionDispatcher', $params_hook_action_dispatcher);
// Running controller
$controller->run();
}
catch (PrestaShopException $e)
{
$e->displayMessage();
}
}
}