'', '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 ='

'.(isset($_GET['id']) ? 'Edit' : 'Create').' Software Version

' . $button_cancel . ' '; if ($delete_allowed === 1 && isset($_GET['id'])){ $view .= ''; } if (($update_allowed === 1 && isset($_GET['id'])) || ($create_allowed === 1 && !isset($_GET['id']))){ $view .= ''; } $view .= '
'; $view .= '
No file selected
'; //OUTPUT echo $view; template_footer(); ?>