Files
assetmgt/products_software_upgrade_paths_manage.php
“VeLiTi” 24481279d5 Refactor user session handling and permissions management
- Updated session variables to use 'authorization' array instead of 'username' for user identification across multiple files.
- Introduced a new function `getUserPermissions` to consolidate user permissions retrieval based on assigned roles.
- Modified API calls to use the new authorization structure and updated endpoints to v2.
- Enhanced language support by adding 'PL' to the list of supported languages.
- Cleaned up redundant code and improved session management during user login and registration processes.
- Added a new API endpoint for fetching user permissions based on user ID.
2026-01-19 15:29:16 +01:00

278 lines
11 KiB
PHP

<?php
defined(page_security_key) or exit;
// Fallback translations
if (!isset($button_cancel)) $button_cancel = 'Cancel';
$page = 'products_software_upgrade_paths_manage';
//Check if allowed
if (isAllowed($page,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'R') === 0){
header('location: index.php');
exit;
}
//PAGE Security
$update_allowed = isAllowed($page ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'U');
$delete_allowed = isAllowed($page ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'D');
$create_allowed = isAllowed($page ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'C');
// Determine redirect URL
if (isset($_GET['id'])) {
$url = 'index.php?page=products_software_version&rowID=' . $_GET['id'];
} else {
$url = 'index.php?page=products_software_versions';
}
// Default input values
$path = [
'rowID' => '',
'from_version_id' => '',
'to_version_id' => '',
'price' => '',
'currency' => 'EUR',
'description' => '',
'is_active' => 1,
'created' => '',
'createdby' => $_SESSION['authorization']['clientID'],
'updated' => '',
'updatedby' => $_SESSION['authorization']['clientID']
];
// Check if coming from version page (id parameter) or editing existing path
$from_version_page = false;
$to_version_fixed = false;
if (isset($_GET['id']) && !isset($_GET['path_id'])) {
// Coming from version page - this is the TO version
$from_version_page = true;
$to_version_fixed = $_GET['id'];
$path['to_version_id'] = $to_version_fixed;
}
// If editing an existing path, load it
if (isset($_GET['path_id']) && $_GET['path_id'] != '') {
$api_url = '/v2/products_software_upgrade_paths/rowID=' . $_GET['path_id'];
$response = ioServer($api_url, '');
if (!empty($response)) {
$existing = json_decode($response);
if (!empty($existing)) {
$path = (array) $existing[0];
}
}
}
// Determine filter version id from URL (for hw_version filtering)
$filter_version_id = $_GET['from_version_id'] ?? $_GET['to_version_id'] ?? $_GET['id'] ?? '';
// Fetch software versions for selects
$api_url = '/v2/products_software_versions/list';
$versions_response = ioServer($api_url, '');
$all_versions = [];
if (!empty($versions_response)) {
$all_versions = json_decode($versions_response);
}
// Determine hw_version for filtering
$filter_hw_version = null;
$selected_versions = [];
if (!empty($path['from_version_id'])) {
$selected_versions[] = $path['from_version_id'];
$api_url = '/v2/products_software_versions/rowID=' . $path['from_version_id'];
$response = ioServer($api_url, '');
if (!empty($response)) {
$ver = json_decode($response);
if (!empty($ver)) {
$filter_hw_version = $ver[0]->hw_version;
}
}
}
if (!empty($path['to_version_id'])) {
$selected_versions[] = $path['to_version_id'];
}
if ($filter_hw_version === null && !empty($filter_version_id)) {
$api_url = '/v2/products_software_versions/rowID=' . $filter_version_id;
$response = ioServer($api_url, '');
if (!empty($response)) {
$ver = json_decode($response);
if (!empty($ver)) {
$filter_hw_version = $ver[0]->hw_version;
}
}
}
// Filter versions to same hw_version
$versions = [];
if ($filter_hw_version !== null) {
foreach ($all_versions as $ver) {
if ($ver->hw_version == $filter_hw_version) {
$versions[] = $ver;
}
}
// Ensure selected versions are included
foreach ($selected_versions as $sel_id) {
$found = false;
foreach ($versions as $ver) {
if ($ver->rowID == $sel_id) {
$found = true;
break;
}
}
if (!$found) {
// Fetch and add
$api_url = '/v2/products_software_versions/rowID=' . $sel_id;
$response = ioServer($api_url, '');
if (!empty($response)) {
$ver = json_decode($response);
if (!empty($ver)) {
$versions[] = $ver[0];
}
}
}
}
} else {
$versions = $all_versions;
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = [
'rowID' => $_POST['rowID'] ?? '',
'from_version_id' => $_POST['from_version_id'] ?? '',
'to_version_id' => $_POST['to_version_id'] ?? '',
'price' => $_POST['price'] ?? '',
'currency' => $_POST['currency'] ?? 'USD',
'description' => $_POST['description'] ?? '',
'is_active' => isset($_POST['is_active']) ? 1 : 0
];
// Handle delete
if (isset($_POST['delete'])) {
$data['delete'] = true;
}
// Call API
$api_url = '/v2/products_software_upgrade_paths/';
$result = ioServer($api_url, json_encode($data));
if ($result !== 'NOK') {
$success = isset($_POST['delete']) ? 3 : (isset($_POST['rowID']) && $_POST['rowID'] != '' ? 2 : 1);
header('Location: ' . $url . '&success_msg=' . $success);
exit;
} else {
$error_msg = 'Failed to save upgrade path.';
}
}
template_header('Upgrade Path', 'products_software_upgrade_paths_manage', 'manage');
$view ='
<form action="" method="post">
<div class="content-title responsive-flex-wrap responsive-pad-bot-3">
<h2 class="responsive-width-100">'.(isset($_GET['id']) ? 'Edit' : 'Create').' Upgrade Path</h2>
<a href="' . $url . '" class="btn alt mar-right-2">' . $button_cancel . '</a>
';
if ($delete_allowed === 1 && isset($_GET['path_id']) && $_GET['path_id'] != ''){
$view .= '<input type="submit" name="delete" value="X" class="btn red mar-right-2" onclick="return confirm(\'Are you sure you want to delete this upgrade path?\')">';
}
if (($update_allowed === 1 && isset($_GET['path_id'])) || ($create_allowed === 1 && !isset($_GET['path_id']))){
$view .= '<input type="submit" name="submit" value="💾" class="btn">';
}
$view .= '</div>';
$view .= '<div class="content-block">
<div class="form responsive-width-100">
<label for="from_version_id"><i class="required">*</i>From Version</label>
<select id="from_version_id" name="from_version_id" required>
<option value="">Select From Version</option>
<option value="9999999"'. ($path['from_version_id'] == 9999999 ? ' selected' : '') .'>Any Version (*)</option>';
if (!empty($versions)) {
foreach ($versions as $ver) {
// Skip the TO version from FROM dropdown to prevent FROM = TO
if ($path['to_version_id'] && $ver->rowID == $path['to_version_id']) {
continue;
}
$selected = ($path['from_version_id'] == $ver->rowID) ? ' selected' : '';
$view .= '<option value="' . $ver->rowID . '"' . $selected . '>' . htmlspecialchars($ver->name . ' (' . $ver->version . ')') . '</option>';
}
}
$view .= ' </select>';
// If TO version is fixed (coming from version page), show it as read-only text
if ($from_version_page && $to_version_fixed) {
$to_version_name = '';
foreach ($versions as $ver) {
if ($ver->rowID == $to_version_fixed) {
$to_version_name = htmlspecialchars($ver->name . ' (' . $ver->version . ')');
break;
}
}
$view .= '
<label for="to_version_display">To Version</label>
<input type="text" id="to_version_display" value="' . $to_version_name . '" disabled>
<input type="hidden" id="to_version_id" name="to_version_id" value="' . $to_version_fixed . '">';
} else {
// Show dropdown for TO version when editing
$view .= '
<label for="to_version_id"><i class="required">*</i>To Version</label>
<select id="to_version_id" name="to_version_id" required>
<option value="">Select To Version</option>';
if (!empty($versions)) {
foreach ($versions as $ver) {
// Skip the FROM version from TO dropdown to prevent FROM = TO
if ($path['from_version_id'] && $ver->rowID == $path['from_version_id']) {
continue;
}
$selected = ($path['to_version_id'] == $ver->rowID) ? ' selected' : '';
$view .= '<option value="' . $ver->rowID . '"' . $selected . '>' . htmlspecialchars($ver->name . ' (' . $ver->version . ')') . '</option>';
}
}
$view .= ' </select>';
}
$view .= '
<label for="price">Price</label>
<input id="price" type="number" step="0.01" name="price" placeholder="Price" value="' . htmlspecialchars($path['price']) . '">
<label for="currency">Currency</label>
<input id="currency" type="text" name="currency" placeholder="Currency" value="' . htmlspecialchars($path['currency']) . '">
<label for="description">Description</label>
<textarea id="description" name="description" placeholder="Description">' . htmlspecialchars($path['description']) . '</textarea>
<label class="checkbox">
<input type="checkbox" name="is_active" value="1" ' . ($path['is_active'] ? 'checked' : '') . '>
<span>Active</span>
</label>
<input type="hidden" name="rowID" value="' . htmlspecialchars($path['rowID']) . '">
</div>
</div>
<script>
// Validate that FROM and TO versions are different
document.querySelector("form").addEventListener("submit", function(e) {
const fromVersion = document.getElementById("from_version_id").value;
const toVersion = document.getElementById("to_version_id").value;
if (fromVersion && toVersion && fromVersion === toVersion) {
e.preventDefault();
alert("Error: FROM version cannot be the same as TO version");
return false;
}
});
// Dynamic filtering: Update dropdowns when selection changes
const fromSelect = document.getElementById("from_version_id");
const toSelect = document.getElementById("to_version_id");
if (fromSelect && toSelect && toSelect.tagName === "SELECT") {
fromSelect.addEventListener("change", function() {
// No need to dynamically filter since PHP already handles it
});
}
</script>
';
//OUTPUT
echo $view;
template_footer();
?>