Skip to content

Commit

Permalink
Avoid doing admin tasks in the module constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Nov 2, 2018
1 parent 0a36624 commit 8b587c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions controllers/admin/AdminPsMboModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function initContent()
'configure_type' => $this->controller_quick_name,
'template_dir' => $this->module->template_dir,
'admin_module_controller_psmbo' => $this->module->controller_name[0],
'admin_module_ajax_url_psmbo' => $this->module->front_controller[0],
'admin_module_ajax_url_psmbo' => $this->module->getControllerLink('AdminPsMboModule'),
'currency_symbol' => Context::getContext()->currency->sign,
'bo_img' => __PS_BASE_URI__ . $admin_webpath . '/themes/default/img/',
'install_url' => $install_url,
Expand Down Expand Up @@ -113,7 +113,7 @@ public function initContent()

$aJsDef = array(
'admin_module_controller_psmbo' => $this->module->controller_name[0],
'admin_module_ajax_url_psmbo' => $this->module->front_controller[0]
'admin_module_ajax_url_psmbo' => $this->module->getControllerLink('AdminPsMboModule')
);

$aCss = array(
Expand Down
37 changes: 29 additions & 8 deletions ps_mbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class ps_mbo extends Module
)
);

/**
* Links to the admin controllers created by the module
*/
protected $front_controller = null;

public function __construct()
{
$this->name = 'ps_mbo';
Expand All @@ -67,10 +72,6 @@ public function __construct()
$this->description = $this->l('Discover the best PrestaShop modules to optimize your online store.');

$this->controller_name = array('AdminPsMboModule', 'AdminPsMboTheme');
$this->front_controller = array(
'index.php?controller=' . $this->controller_name[0] . '&token=' . Tools::getAdminTokenLite($this->controller_name[0]),
'index.php?controller=' . $this->controller_name[1] . '&token=' . Tools::getAdminTokenLite($this->controller_name[1]),
);

$this->template_dir = '../../../../modules/' . $this->name . '/views/templates/admin/';

Expand Down Expand Up @@ -172,7 +173,7 @@ public function fetchModules($controller)
}

$data['controller_name'] = $controller;
$data['admin_module_ajax_url_psmbo'] = $this->front_controller[0];
$data['admin_module_ajax_url_psmbo'] = $this->getControllerLink('AdminPsMboModule');
$data['from'] = 'footer';
$data['modules_list'] = $modules;

Expand Down Expand Up @@ -232,7 +233,7 @@ public function fetchModulesByController($ajax = false)
$data = array(
'panel_id' => $panel_id,
'controller_name' => $controller,
'admin_module_ajax_url_psmbo' => $this->front_controller[0],
'admin_module_ajax_url_psmbo' => $this->getControllerLink('AdminPsMboModule'),
'from' => 'footer',
'modules_list' => $modules,
);
Expand All @@ -246,6 +247,26 @@ public function fetchModulesByController($ajax = false)
return $this->context->smarty->fetch($this->template_dir . '/include/admin-end-content-footer.tpl');
}

/**
* @param string $tab The controller we want a link for.
*
* @return string Admin link with token
*/
protected function getControllerLink($tab)
{
if (null === $this->front_controller) {
$this->front_controller = array();
foreach ($this->controller_name as $name) {
$this->front_controller[$name] = 'index.php?controller=' . $this->name . '&token=' . Tools::getAdminTokenLite($this->name);
}
}

if (isset($this->front_controller[$tab])) {
return $this->front_controller[$tab];
}
throw new Exception('Unknown controller requested.');
}

protected function handleAddonsConnectWithMbo()
{
if (Tools::getValue('controller') !== 'AdminPsMboModule') {
Expand All @@ -268,7 +289,7 @@ protected function handleTheme()
}

$this->context->smarty->assign([
'admin_module_ajax_url_psmbo' => $this->front_controller[0]
'admin_module_ajax_url_psmbo' => $this->getControllerLink('AdminPsMboModule')
]);
return $this->context->smarty->fetch($this->template_dir . '/admin-end-content-theme.tpl');
}
Expand All @@ -292,7 +313,7 @@ public function hookDisplayAdminEndContent()
$controllerWhiteList = array('AdminCarriers', 'AdminPayment');
if (in_array($controller_page, $controllerWhiteList)) {
$this->context->smarty->assign(array(
'admin_module_ajax_url_psmbo' => $this->front_controller[0],
'admin_module_ajax_url_psmbo' => $this->getControllerLink('AdminPsMboModule'),
'controller_page' => $controller_page
));

Expand Down

0 comments on commit 8b587c6

Please sign in to comment.