d2tools/LevelsEditor_Process.php

81 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2023-09-10 23:54:20 +00:00
<?php
session_start();
// Include necessary files
require_once './config.php';
require_once './_pdo.php';
require_once './src/D2Functions.php';
// Define the database connection
error_reporting(E_ALL);
set_time_limit(-1);
ini_set('max_input_time', '-1');
ini_set('max_execution_time', '0');
define('DB_FILE', $_SESSION['modname'] . ".db");
try {
PDO_Connect("sqlite:" . DB_FILE);
} catch (Exception $e) {
echo "Connection error: " . $e->getMessage();
exit; // stop further execution because of the error
}
// Check if all the expected POST data is available
if (isset($_POST['levelId'], $_POST['selectName'], $_POST['selectedValue'])) {
$levelId = $_POST['levelId'];
$selectName = $_POST['selectName'];
$selectedValue = $_POST['selectedValue'];
// Update the data in the levels table
$query = "UPDATE levels SET $selectName = :selectedValue WHERE Id = :levelId";
$params = [
':selectedValue' => $selectedValue,
':levelId' => $levelId
];
// Execute the query using the PDO_Execute() function
$result = PDO_Execute($query, $params);
if ($result) {
echo json_encode(['status' => 'success', 'message' => 'Data updated successfully.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to update data.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid POST data.']);
}
//$tsvFileName = "Levels";
//$db = new SQLite3(DB_FILE);
//$message = writeToTsvFile($tsvFileName, $db);
//
//echo $message;
$bin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? getcwd() . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "sqlite3.exe" : "/usr/bin/sqlite3";
$dbfile = DB_FILE;
$tablename = "levels";
$outputFile = $_SESSION['path'] . "Levels.txt";
// Build the command to export the SQLite table to a TSV file
$command = sprintf(
'%s %s ".mode tabs" ".headers on" "SELECT * FROM %s;" > "%s"',
$bin,
$dbfile,
$tablename,
$outputFile
);
//ddump($command);
// Execute the command
$commandOutput = shell_exec($command);
if ($commandOutput) {
echo "Error: $commandOutput";
} else {
echo "Table exported successfully!";
}