225 lines
9.3 KiB
PHP
225 lines
9.3 KiB
PHP
<?php
|
|
defined(page_security_key) or exit;
|
|
|
|
// Fallback translations
|
|
if (!isset($button_cancel)) $button_cancel = 'Cancel';
|
|
|
|
$page = 'products_software_version_manage';
|
|
//Check if allowed
|
|
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
|
|
header('location: index.php');
|
|
exit;
|
|
}
|
|
//PAGE Security
|
|
$update_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'U');
|
|
$delete_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'D');
|
|
$create_allowed = isAllowed($page ,$_SESSION['profile'],$_SESSION['permission'],'C');
|
|
|
|
//
|
|
if (isset($_GET['id']) && $_GET['id']!=''){
|
|
$url = 'index.php?page=products_software_version&rowID='.$_GET['id'];
|
|
} else {
|
|
$url = 'index.php?page=products_software_versions';
|
|
}
|
|
|
|
// Default input version values
|
|
$version = [
|
|
'rowID' => '',
|
|
'name' => '',
|
|
'version' => '',
|
|
'description' => '',
|
|
'mandatory' => 0,
|
|
'latest' => 0,
|
|
'hw_version' => '',
|
|
'file_path' => '',
|
|
'status' => 1,
|
|
'created' => '',
|
|
'createdby' => $_SESSION['username'],
|
|
'updated' => '',
|
|
'updatedby' => $_SESSION['username']
|
|
];
|
|
|
|
// If editing, fetch existing data
|
|
if (isset($_GET['id']) && $_GET['id'] != '') {
|
|
$api_url = '/v2/products_software_versions/rowID=' . $_GET['id'];
|
|
$response = ioServer($api_url, '');
|
|
if (!empty($response)) {
|
|
$existing = json_decode($response);
|
|
if (!empty($existing)) {
|
|
$version = (array) $existing[0];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
//CHECK FOR FILE UPLOAD
|
|
$uploaded_file = $_FILES["fileToUpload"]["name"] ?? '';
|
|
|
|
if ($uploaded_file != '' || !empty($uploaded_file)) {
|
|
$extension = strtolower(pathinfo($uploaded_file, PATHINFO_EXTENSION));
|
|
$target_dir = dirname(__FILE__) . "/firmware/";
|
|
$firmware_name = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_FILENAME);
|
|
|
|
if ($extension == 'hex') {
|
|
//READ FILE
|
|
$contents = file_get_contents($_FILES["fileToUpload"]["tmp_name"]);
|
|
$commitCode = compareCommitCodes($firmware_name, "");
|
|
|
|
//IF COMMITCODE IS EMPTY THEN RETURN HEX_FW
|
|
$fw_name = ($commitCode != '' || !empty($commitCode)) ? $commitCode : $firmware_name;
|
|
|
|
//Filename
|
|
$input_file = $target_dir . $firmware_name . '.HEX';
|
|
//store firmware file
|
|
file_put_contents($input_file, $contents);
|
|
$_POST['file_path'] = $firmware_name . '.HEX';
|
|
$_POST['version'] = $fw_name;
|
|
} elseif ($extension == 'bin' || $extension == 'exe') {
|
|
//Extract commit code from filename
|
|
$commitCode = compareCommitCodes($firmware_name, "");
|
|
|
|
//IF COMMITCODE IS EMPTY THEN RETURN FIRMWARE_NAME
|
|
$fw_name = ($commitCode != '' || !empty($commitCode)) ? $commitCode : $firmware_name;
|
|
|
|
//Move uploaded file
|
|
$target_file = $target_dir . $uploaded_file;
|
|
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
|
|
$_POST['file_path'] = $uploaded_file;
|
|
$_POST['version'] = $fw_name;
|
|
} else {
|
|
$target_file = $target_dir . $uploaded_file;
|
|
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
|
|
$_POST['file_path'] = $uploaded_file;
|
|
}
|
|
}
|
|
|
|
$data = [
|
|
'rowID' => $_POST['rowID'] ?? '',
|
|
'name' => $_POST['name'] ?? '',
|
|
'version' => $_POST['version'] ?? '',
|
|
'description' => $_POST['description'] ?? '',
|
|
'mandatory' => isset($_POST['mandatory']) ? 1 : 0,
|
|
'latest' => isset($_POST['latest']) ? 1 : 0,
|
|
'hw_version' => $_POST['hw_version'] ?? '',
|
|
'file_path' => $_POST['file_path'] ?? '',
|
|
'status' => isset($_POST['status']) ? 1 : 0
|
|
];
|
|
|
|
// Handle delete
|
|
if (isset($_POST['delete'])) {
|
|
$data['delete'] = true;
|
|
}
|
|
|
|
// Call API
|
|
$api_url = '/v2/products_software_versions/';
|
|
$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 software version.';
|
|
}
|
|
}
|
|
|
|
template_header('Software Version', 'products_software_version', 'manage');
|
|
|
|
$view ='
|
|
<form action="" method="post" enctype="multipart/form-data">
|
|
<div class="content-title responsive-flex-wrap responsive-pad-bot-3">
|
|
<h2 class="responsive-width-100">'.(isset($_GET['id']) ? 'Edit' : 'Create').' Software Version</h2>
|
|
<a href="' . $url . '" class="btn alt mar-right-2">' . $button_cancel . '</a>
|
|
';
|
|
|
|
if ($delete_allowed === 1 && isset($_GET['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 software version?\')">';
|
|
}
|
|
if (($update_allowed === 1 && isset($_GET['id'])) || ($create_allowed === 1 && !isset($_GET['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="status">Status</label>
|
|
<select id="status" name="status">
|
|
<option value="1" ' . ($version['status'] == 1 ? ' selected' : '') . '>Active</option>
|
|
<option value="0" ' . ($version['status'] == 0 ? ' selected' : '') . '>Inactive</option>
|
|
</select>
|
|
<label for="name"><i class="required">*</i>Name</label>
|
|
<input id="name" type="text" name="name" placeholder="Name" value="' . htmlspecialchars($version['name']) . '" required>
|
|
<label for="version"><i class="required">*</i>Version</label>
|
|
<input id="version" type="text" name="version" placeholder="Version" value="' . htmlspecialchars($version['version']) . '" required>
|
|
<label for="description">Description</label>
|
|
<textarea id="description" name="description" placeholder="Description">' . htmlspecialchars($version['description']) . '</textarea>
|
|
<label for="hw_version">HW Version</label>
|
|
<input id="hw_version" type="text" name="hw_version" placeholder="HW Version" value="' . htmlspecialchars($version['hw_version']) . '">
|
|
<label for="fileToUpload">Upload File</label>
|
|
<div class="file-upload-wrapper">
|
|
<button type="button" class="file-upload-btn" onclick="document.getElementById(\'fileToUpload\').click()">
|
|
<i class="fas fa-cloud-upload-alt"></i>
|
|
<span id="uploadBtnText">Choose File</span>
|
|
</button>
|
|
<input type="file" name="fileToUpload" id="fileToUpload" style="display: none;" onchange="updateFields()" accept=".hex,.bin,.fw,.exe">
|
|
<span class="file-upload-info" id="fileUploadInfo">No file selected</span>
|
|
</div>
|
|
<label for="file_path">File Path</label>
|
|
<input id="file_path" type="text" name="file_path" placeholder="File Path" value="' . htmlspecialchars($version['file_path']) . '" readonly>
|
|
<label class="checkbox">
|
|
<input type="checkbox" name="mandatory" value="1" ' . ($version['mandatory'] ? 'checked' : '') . '>
|
|
<span>Mandatory</span>
|
|
</label>
|
|
<label class="checkbox">
|
|
<input type="checkbox" name="latest" value="1" ' . ($version['latest'] ? 'checked' : '') . '>
|
|
<span>Latest</span>
|
|
</label>
|
|
<input type="hidden" name="rowID" value="' . htmlspecialchars($version['rowID']) . '">
|
|
</div>
|
|
</div>
|
|
<script>
|
|
function updateFields() {
|
|
var fileInput = document.getElementById(\'fileToUpload\');
|
|
var uploadBtnText = document.getElementById(\'uploadBtnText\');
|
|
var fileUploadInfo = document.getElementById(\'fileUploadInfo\');
|
|
var file = fileInput.files[0];
|
|
|
|
if (file) {
|
|
var fileName = file.name;
|
|
var filePathInput = document.getElementById(\'file_path\');
|
|
filePathInput.value = fileName;
|
|
|
|
var versionInput = document.getElementById(\'version\');
|
|
if (!versionInput.value) {
|
|
var nameWithoutExt = fileName.replace(/\.[^/.]+$/, "");
|
|
versionInput.value = nameWithoutExt;
|
|
}
|
|
|
|
// Update button and info text
|
|
uploadBtnText.textContent = \'Change File\';
|
|
fileUploadInfo.textContent = fileName + \' (\' + formatFileSize(file.size) + \')\';
|
|
fileUploadInfo.style.color = \'#28a745\';
|
|
} else {
|
|
uploadBtnText.textContent = \'Choose File\';
|
|
fileUploadInfo.textContent = \'No file selected\';
|
|
fileUploadInfo.style.color = \'#6c757d\';
|
|
}
|
|
}
|
|
|
|
function formatFileSize(bytes) {
|
|
if (bytes === 0) return \'0 Bytes\';
|
|
const k = 1024;
|
|
const sizes = [\'Bytes\', \'KB\', \'MB\', \'GB\'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + \' \' + sizes[i];
|
|
}
|
|
</script>
|
|
';
|
|
|
|
//OUTPUT
|
|
echo $view;
|
|
|
|
template_footer();
|
|
?>
|