Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs installation fix during upgrade #976

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 126 additions & 29 deletions upgrade/Upgrade-6.2.3.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @see https://github.com/mollie/PrestaShop
*/

use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Config\Config;
use PrestaShop\PrestaShop\Adapter\Module\Tab\ModuleTabRegister;
use Symfony\Component\HttpFoundation\ParameterBag;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -36,37 +36,134 @@ function upgrade_module_6_2_3(Mollie $module): bool
return false; // If the table creation fails, return false
}

// Install the necessary tabs
$tabs = [
[
'name' => $module->l('Logs'),
'class_name' => $module::ADMIN_MOLLIE_LOGS_PARENT_CONTROLLER,
'parent_class_name' => $module::ADMIN_MOLLIE_CONTROLLER,
'module_tab' => true,
],
if (!deleteOldTabs623($module)) {
return false;
}

// only for 1.7.6 version
if (version_compare(_PS_VERSION_, '1.7.7.0', '<')) {
if (!deleteOldTabAuthorizationRoles623($module)) {
return false;
}
}

/** @var Mollie\Install\Installer $installer */
$installer = $module->getService(Mollie\Install\Installer::class);

$installer->installSpecificTabs();

/** @var ModuleTabRegister $tabRegister */
$tabRegister = $module->getService('prestashop.adapter.module.tab.register');

$moduleAdapter = new \PrestaShop\PrestaShop\Adapter\Module\Module();
$moduleAdapter->instance = $module;
$moduleAdapter->disk = new ParameterBag(
[
'name' => $module->l('Logs'),
'class_name' => $module::ADMIN_MOLLIE_LOGS_CONTROLLER,
'parent_class_name' => $module::ADMIN_MOLLIE_TAB_CONTROLLER,
'module_tab' => true,
],
];

foreach ($tabs as $tabData) {
$tab = new Tab();
$tab->name = [];
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = $tabData['name'];
'filemtype' => 0,
'is_present' => 1,
'is_valid' => 1,
'version' => null,
'path' => '',
]
);

$moduleAdapter->attributes->set('name', $module->name);

$tabRegister->registerTabs($moduleAdapter);

return $isTableCreated;
}

function deleteOldTabs623($module): bool
{
$classNames = [$module::ADMIN_MOLLIE_CONTROLLER, $module::ADMIN_MOLLIE_AJAX_CONTROLLER];
$preparedClassNames = [];

foreach ($classNames as $className) {
$preparedClassNames[] = pSQL($className);
}

$preparedClassNames = implode("', '", $preparedClassNames);

$sql = '
SELECT id_tab
FROM `' . _DB_PREFIX_ . 'tab`
WHERE class_name IN (\'' . $preparedClassNames . '\');
';

try {
$tabIds = Db::getInstance()->executeS($sql);
} catch (Exception $e) {
return false;
}

if (empty($tabIds)) {
return true;
}

$preparedTabIds = [];

foreach ($tabIds as $tabId) {
$preparedTabIds[] = (int) $tabId['id_tab'];
}

$preparedTabIds = implode(", ", $preparedTabIds);

$sql = '
DELETE FROM `' . _DB_PREFIX_ . 'tab`
WHERE id_tab IN (' . $preparedTabIds . ');
';

try {
if (!Db::getInstance()->execute($sql)) {
return false;
}
$tab->class_name = $tabData['class_name'];
$tab->id_parent = Tab::getIdFromClassName($tabData['parent_class_name']);
$tab->module = $module->name;
$tab->active = 1;
} catch (Exception $e) {
return false;
}

if (!$tab->add()) {
return false; // Return false if any tab creation fails
$sql = '
DELETE FROM `' . _DB_PREFIX_ . 'tab_lang`
WHERE id_tab IN (' . $preparedTabIds . ');
';

try {
if (!Db::getInstance()->execute($sql)) {
return false;
}
} catch (Exception $e) {
return false;
}

return $isTableCreated;
return true;
}

function deleteOldTabAuthorizationRoles623($module): bool
{
$controllers = [$module::ADMIN_MOLLIE_CONTROLLER, $module::ADMIN_MOLLIE_AJAX_CONTROLLER];
$preparedTabs = [];

foreach ($controllers as $controller) {
$preparedTabs[] = 'ROLE_MOD_TAB_' . pSQL($controller) . '_CREATE';
$preparedTabs[] = 'ROLE_MOD_TAB_' . pSQL($controller) . '_READ';
$preparedTabs[] = 'ROLE_MOD_TAB_' . pSQL($controller) . '_UPDATE';
$preparedTabs[] = 'ROLE_MOD_TAB_' . pSQL($controller) . '_DELETE';
}

$preparedTabs = implode("', '", $preparedTabs);

$sql = '
DELETE FROM `' . _DB_PREFIX_ . 'authorization_role`
WHERE slug IN (\'' . $preparedTabs . '\');
';

try {
if (!Db::getInstance()->execute($sql)) {
return false;
}
} catch (Exception $e) {
return false;
}

return true;
}
Loading