From 9a42e5e333b94dd98519c40a48365c00a63e7d3a Mon Sep 17 00:00:00 2001 From: Calvo Date: Mon, 4 Sep 2023 16:27:43 +0200 Subject: [PATCH] Discovery migration. Console visual cahnges --- .../wizards/ManageExtensions.class.php | 148 ++++++++++++++++++ .../include/javascript/manage_extensions.js | 39 +++++ 2 files changed, 187 insertions(+) diff --git a/pandora_console/godmode/wizards/ManageExtensions.class.php b/pandora_console/godmode/wizards/ManageExtensions.class.php index e088278eea..e39f09fa80 100644 --- a/pandora_console/godmode/wizards/ManageExtensions.class.php +++ b/pandora_console/godmode/wizards/ManageExtensions.class.php @@ -37,6 +37,7 @@ class ManageExtensions extends HTML public $AJAXMethods = [ 'getExtensionsInstalled', 'validateIniName', + 'loadMigrateModal', ]; /** @@ -283,6 +284,10 @@ class ManageExtensions extends HTML echo '
'; html_print_input_hidden('upload_disco', 1); html_print_table($table); + // Auxiliar div ant string for migrate modal. + $modal = ''; + echo $modal; + echo '
'; echo '
'; @@ -297,6 +302,7 @@ class ManageExtensions extends HTML "Error": "'.__('Error').'", "Ok": "'.__('Ok').'", "Failed to upload extension": "'.__('Failed to upload extension').'", + "Migrate": "'.__('Migrate').'", }; var url = "'.ui_get_full_url('ajax.php', false, false, false).'"; '; @@ -687,6 +693,7 @@ class ManageExtensions extends HTML ); $data[$key]['actions'] .= html_print_input_hidden('short_name', $row['short_name'], true); $data[$key]['actions'] .= '
'; + if ($this->checkFolderConsole($row['short_name']) === true) { $data[$key]['actions'] .= '
'; $data[$key]['actions'] .= html_print_input_image( @@ -715,6 +722,21 @@ class ManageExtensions extends HTML ], ); } + + $migrationHash = $this->canMigrate($row['short_name']); + if ($migrationHash !== false && empty($migrationHash) !== true) { + // Migrate button + $data[$key]['actions'] .= html_print_input_image( + 'button_migrate', + 'images/reset.png', + '', + '', + true, + [ + 'onclick' => 'show_migration_form(\''.$row['short_name'].'\',\''.$migrationHash.'\')', + ] + ); + } } if (empty($data) === true) { @@ -1053,4 +1075,130 @@ class ManageExtensions extends HTML } + /** + * Checks if the discovery app can be migrated to .disco system. + * If app is migrated or is not in .ini file, it cannot be migrated. + * + * @param string $shortName Short name of the discovery app. + * + * @return string App hash, false in case hash doesnt exist on ini file, or is already migraeted.. + */ + private function canMigrate(string $shortName='') + { + global $config; + + if (empty($shortName) === true) { + return false; + } + + // 1. Check if app is already migrated: + // Get migrated Discovery Apps from config. + $migratedAppsJson = db_get_value('value', 'tconfig', 'token', 'migrated_discovery_apps'); + if ($migratedAppsJson === false) { + return false; + } + + // Decode JSON migrated apps. + $migrateApps = json_decode($migratedAppsJson, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + return false; + } + + // Check app migrated. + if (array_key_exists($shortName, $migrateApps)) { + if (defined($migrateApps[$shortName]) === true && (bool) $migrateApps[$shortName] === true) { + // Already migrated. + return false; + } + } + + // 2. If app not migrated yet, check DiscoveryApplicationsMigrateCodes.ini + // Path to the INI file + $filePath = $this->path.'/DiscoveryApplicationsMigrateCodes.ini'; + + // Parse the INI file + $migrationCodes = parse_ini_file($filePath, true); + + if ($migrationCodes === false) { + return false; + } + + // Check shortname in ini file. + if (array_key_exists($shortName, $migrationCodes) === false) { + return false; + } else { + return $migrationCodes[$shortName]; + } + + // All checks ok, discovery app can be migrated + return false; + + } + + + /** + * Prints html for migrate modal + * + * @param string $shortName Short name of app + * @param string $hash App hash + * + * @return void + */ + public function loadMigrateModal() + { + $shortname = get_parameter('shortname', null); + $hash = get_parameter('hash', null); + + $form = [ + 'action' => '#', + 'id' => 'modal_migrate_form', + 'onsubmit' => 'return false;', + 'class' => 'modal', + ]; + + $inputs = []; + $migrateMessage = __( + 'All ‘legacy‘ tasks for this application will be migrated to the new ‘.disco’ package system. All configurations and executions will be managed with the new system. This process will not be reversible.

' + ); + $migrateMessage .= _('Please check the migration code for the application before proceeding.'); + + $inputs[] = [ + 'wrapper' => 'div', + 'block_id' => 'div_migrate_message', + 'class' => 'hole flex-row flex-items-center w98p', + 'direct' => 1, + 'block_content' => [ + [ + 'label' => $migrateMessage, + 'arguments' => [ + 'class' => 'first_lbl w98p', + 'name' => 'lbl_migrate_message', + 'id' => 'lbl_migrate_message', + ], + ], + ], + ]; + + $inputs[] = [ + 'label' => __('Applicattion hash'), + 'id' => 'div-hash', + 'arguments' => [ + 'name' => 'hash', + 'type' => 'text', + 'value' => $hash, + 'return' => true, + ], + ]; + + $this->printForm( + [ + 'form' => $form, + 'inputs' => $inputs, + ], + false + ); + } + + } diff --git a/pandora_console/include/javascript/manage_extensions.js b/pandora_console/include/javascript/manage_extensions.js index e5c98c6a58..969a9c4ce2 100644 --- a/pandora_console/include/javascript/manage_extensions.js +++ b/pandora_console/include/javascript/manage_extensions.js @@ -70,3 +70,42 @@ $(document).ready(function() { }); }); }); + +/** + * Loads modal from AJAX to add a new key or edit an existing one. + */ +function show_migration_form(shortName, hash) { + var btn_ok_text = textsToTranslate["Migrate"]; + var btn_cancel_text = textsToTranslate["Cancel"]; + var title = textsToTranslate["Migrate"]; + var method = "migrate"; + + load_modal({ + target: $("#migrate_modal"), + form: "modal_migrate_form", + url: url, + modal: { + title: title, + ok: btn_ok_text, + cancel: btn_cancel_text + }, + extradata: [ + { + name: "shortName", + value: shortName + }, + { + name: "hash", + value: hash + } + ], + onshow: { + page: page, + method: "loadMigrateModal" + }, + onsubmit: { + page: page, + method: method + } + }); +}