mirror of
https://gitlab.com/hashborgir/d2tools.git
synced 2024-11-30 04:26:03 +00:00
Started a new TBL editor
This commit is contained in:
parent
de01891afb
commit
3357f757ea
61
TblEditor.php
Normal file
61
TblEditor.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
session_start();
|
||||
ob_start();
|
||||
|
||||
require_once './config.php';
|
||||
require_once './_pdo.php';
|
||||
|
||||
require_once './src/D2Functions.php';
|
||||
require_once './src/D2Crud.php';
|
||||
|
||||
error_reporting(E_ERROR | E_PARSE);
|
||||
set_time_limit(-1);
|
||||
ini_set('max_input_time', '-1');
|
||||
ini_set('max_execution_time', '0');
|
||||
|
||||
define('DB_FILE', $_SESSION['modname'] . ".db");
|
||||
PDO_Connect("sqlite:" . DB_FILE);
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Check if the "Add" button was clicked
|
||||
if (isset($_POST['add'])) {
|
||||
$tableName = $_POST['tableName'];
|
||||
|
||||
// Retrieve the Key and String values from the form
|
||||
$key = $_POST['key'];
|
||||
$string = $_POST['string'];
|
||||
|
||||
// Insert the new row into the table
|
||||
$inserted = PDO_Execute("INSERT INTO $tableName (Key, String) VALUES (?, ?)", array($key, $string));
|
||||
|
||||
if ($inserted) {
|
||||
// Redirect or display success message
|
||||
header('Location: /TblEditorGUI.php');
|
||||
} else {
|
||||
// Handle the insertion failure
|
||||
echo PDO_ErrorInfo();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Handle the DELETE submission
|
||||
// Check if the "Delete" button was clicked
|
||||
if ($_GET['cmd'] == "delete") {
|
||||
|
||||
$tableName = $_GET['t'];
|
||||
$key = $_GET['k'];
|
||||
|
||||
// Delete the row from the table based on the Key
|
||||
$deleted = PDO_Execute("DELETE FROM $tableName WHERE Key = ?", array($key));
|
||||
|
||||
if ($deleted) {
|
||||
// Redirect or display success message
|
||||
echo "Deleted $key from $tableName";
|
||||
} else {
|
||||
// Handle the deletion failure
|
||||
echo PDO_ErrorInfo();
|
||||
|
||||
}
|
||||
}
|
165
TblEditorGUI.php
Normal file
165
TblEditorGUI.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
session_start();
|
||||
ob_start();
|
||||
|
||||
require_once './config.php';
|
||||
require_once './_pdo.php';
|
||||
|
||||
require_once './src/D2Functions.php';
|
||||
require_once './src/D2Crud.php';
|
||||
|
||||
error_reporting(E_ERROR | E_PARSE);
|
||||
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();
|
||||
}
|
||||
|
||||
$tbl = $_SESSION['tbl'];
|
||||
$tableNames = [];
|
||||
foreach (glob($tbl . "*.tbl") as $filename) {
|
||||
$tableInfo = pathinfo($filename);
|
||||
$tableNames[] = $tableInfo['filename'];
|
||||
}
|
||||
|
||||
$tableNames = ['patchstring'];
|
||||
|
||||
// Create an associative array to store rows for each table
|
||||
$tableRows = [];
|
||||
|
||||
// Iterate over the table names
|
||||
foreach ($tableNames as $tableName) {
|
||||
$crud = new CRUD($tableName);
|
||||
$tableRows[$tableName] = $crud->readAll();
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://bootswatch.com/4/lumen/bootstrap.min.css">
|
||||
<title>Editable Tables</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Editable Tables</h2>
|
||||
<ul class="nav nav-tabs" id="myTab" role="tablist">
|
||||
<?php $firstTab = true; ?>
|
||||
<?php foreach ($tableNames as $tableName): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link <?php echo $firstTab ? 'active' : ''; ?>" id="<?php echo $tableName; ?>-tab" data-toggle="tab" href="#<?php echo $tableName; ?>" role="tab" aria-controls="<?php echo $tableName; ?>" aria-selected="<?php echo $firstTab ? 'true' : 'false'; ?>"><?php echo $tableName; ?></a>
|
||||
</li>
|
||||
<?php $firstTab = false; ?>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<div class="tab-content" id="myTabContent">
|
||||
<?php $firstTab = true; ?>
|
||||
<?php foreach ($tableNames as $tableName): ?>
|
||||
<div class="tab-pane fade <?php echo $firstTab ? 'show active' : ''; ?>" id="<?php echo $tableName; ?>" role="tabpanel" aria-labelledby="<?php echo $tableName; ?>-tab">
|
||||
<form method="post" action="TblEditor.php"> <!-- Replace 'process.php' with the PHP file to handle form submission -->
|
||||
<div class="form-row mb-3">
|
||||
<div class="col">
|
||||
<input type="text" name="key" class="form-control" placeholder="Key" required>
|
||||
<input type="hidden" name="tableName" value="<?php echo $tableName?>">
|
||||
</div>
|
||||
<div class="col">
|
||||
<textarea name="string" class="form-control" placeholder="String" required></textarea>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button type="submit" name="add" class="btn btn-success">Add</button>
|
||||
</div>
|
||||
</div>
|
||||
</form><!-- comment -->
|
||||
<form method="post" action=""> <!-- Replace 'process.php' with the PHP file to handle form submission -->
|
||||
<table class="table table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Key</th>
|
||||
<th>String</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($tableRows[$tableName] as $index => $row){ ?>
|
||||
<tr class="table-row-<?php echo $index; ?>">
|
||||
<td><?php echo $row['rowid']; ?></td>
|
||||
<td><input type="text" name="key[<?php echo $tableName; ?>][]" class="form-control" value="<?php echo $row['Key']; ?>"></td>
|
||||
<td><textarea name="string[<?php echo $tableName; ?>][]" class="form-control"><?php echo $row['String']; ?></textarea></td>
|
||||
<td>
|
||||
<button type="submit" name="update" class="btn btn-primary" value="<?php echo $row['rowid']; ?>">Update</button>
|
||||
<button type="submit" name="delete" class="btn btn-danger" value="<?php echo $row['rowid']; ?>">Delete</button>
|
||||
<input type="hidden" value="<?php echo $row['Key']?>" name="keycode">
|
||||
<input type="hidden" name="keytable" value="<?php echo $tableName?>">
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
</div>
|
||||
<?php $firstTab = false; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
|
||||
// Handle Delete button click
|
||||
$('button[name="delete"]').click(function(event) {
|
||||
event.preventDefault(); // Prevent the default form submission
|
||||
|
||||
var key = $(this).next().val(); // Get the value of the input after the Delete button
|
||||
var tableName = $(this).next().next().val(); // Get the value of the input after the key
|
||||
|
||||
console.log(key);
|
||||
console.log(tableName);
|
||||
|
||||
$.get('/TblEditor.php', { cmd: 'delete', t: tableName, k: key })
|
||||
.done(function(response) {
|
||||
// Handle success response
|
||||
location.reload();
|
||||
})
|
||||
.fail(function(xhr, status, error) {
|
||||
// Handle error response
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Handle Update button event
|
||||
$('button[name="update"]').click(function(event) {
|
||||
event.preventDefault(); // Prevent the default form submission
|
||||
|
||||
var key = $(this).next().val(); // Get the value of the input after the Delete button
|
||||
var tableName = $(this).next().next().val(); // Get the value of the input after the key
|
||||
|
||||
console.log(key);
|
||||
console.log(tableName);
|
||||
|
||||
$.post('/TblEditor.php', { cmd: 'update', t: tableName, k: key })
|
||||
.done(function(response) {
|
||||
// Handle success response
|
||||
location.reload();
|
||||
})
|
||||
.fail(function(xhr, status, error) {
|
||||
// Handle error response
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
83
src/D2Crud.php
Normal file
83
src/D2Crud.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
class CRUD {
|
||||
private $table;
|
||||
|
||||
public function __construct($table) {
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new row into the table
|
||||
*
|
||||
* @param array $data Associative array of column names and values
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function create($data) {
|
||||
$keys = implode(', ', array_keys($data));
|
||||
$values = implode(', ', array_fill(0, count($data), '?'));
|
||||
|
||||
$query = "INSERT INTO {$this->table} ($keys) VALUES ($values)";
|
||||
$params = array_values($data);
|
||||
|
||||
return PDO_Execute($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a row by its ROWID
|
||||
*
|
||||
* @param int $rowid The ROWID of the row to retrieve
|
||||
* @return mixed|null The row as an associative array, or null if not found
|
||||
*/
|
||||
public function read($rowid) {
|
||||
$query = "SELECT * FROM {$this->table} WHERE ROWID = ?";
|
||||
$params = [$rowid];
|
||||
|
||||
return PDO_FetchRow($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all rows from the table
|
||||
*
|
||||
* @return array An array of rows, each represented as an associative array
|
||||
*/
|
||||
public function readAll() {
|
||||
$query = "SELECT ROWID,* FROM {$this->table}";
|
||||
return PDO_FetchAll($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a row by its ROWID
|
||||
*
|
||||
* @param int $rowid The ROWID of the row to update
|
||||
* @param array $data Associative array of column names and new values
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function update($rowid, $data) {
|
||||
$setStatements = [];
|
||||
$params = [$rowid];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$setStatements[] = "$key = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
|
||||
$setClause = implode(', ', $setStatements);
|
||||
$query = "UPDATE {$this->table} SET $setClause WHERE ROWID = ?";
|
||||
|
||||
return PDO_Execute($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a row by its ROWID
|
||||
*
|
||||
* @param int $rowid The ROWID of the row to delete
|
||||
* @return bool True on success, false on failure
|
||||
*/
|
||||
public function delete($rowid) {
|
||||
$query = "DELETE FROM {$this->table} WHERE ROWID = ?";
|
||||
$params = [$rowid];
|
||||
|
||||
return PDO_Execute($query, $params);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user