d2tools/TblEditorGUI.php

248 lines
9.4 KiB
PHP
Raw Normal View History

2023-06-02 05:35:10 +00:00
<?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'];
}
?>
<!DOCTYPE html>
<html lang="en">
2023-06-13 07:14:37 +00:00
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.min.css">
2023-06-13 07:14:37 +00:00
<!-- JS -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script>
<!-- Bootstrap CSS (if not already included) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
<!-- Bootstrap DataTables CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/dataTables.bootstrap4.min.css">
<!-- Bootstrap JS (if not already included) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<!-- Bootstrap DataTables JS -->
<script src="https://cdn.datatables.net/1.11.4/js/dataTables.bootstrap4.min.js"></script>
<title>Editable Tables</title>
<link rel="stylesheet" href="https://bootswatch.com/4/lumen/bootstrap.min.css">
2023-06-13 07:14:37 +00:00
</head>
<body>
<div class="container">
<h2>Select .tbl File</h2>
<select name="tableName" id="tableName" class="form-control">
<?php foreach ($tableNames as $tableName): ?>
<option value="<?php echo $tableName; ?>" <?php echo ($tableName == "patchstring") ? "selected" : ""; ?>><?php echo $tableName; ?></option>
<?php endforeach; ?>
</select>
<h2>Table</h2>
<div class="form-group">
Add new String
<label for="inputKey">Key</label>
<input type="text" class="form-control" id="inputKey" placeholder="Enter Key">
</div>
<div class="form-group">
<label for="inputString">String</label>
<textarea class="form-control" id="inputString" placeholder="Enter String"></textarea>
</div>
<button type="button" class="btn btn-primary" id="addLink">Add</button>
<br>
<hr>
<div class="tablediv">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Key</th>
<th>String</th>
<th></th>
</tr>
</thead>
<tbody>
<!-- Additional rows -->
</tbody>
</table>
</div>
2023-06-02 05:35:10 +00:00
</div>
2023-06-13 07:14:37 +00:00
<script>
$(document).ready(function () {
// Get the initial selected table
var selectedTable = $('#tableName').val();
// Send GET request to TblEditor.php?getTable with selected table name
var request = $.get('TblEditor.php', {cmd: 'getTable', tableName: selectedTable});
request.done(function (response) {
// Handle the response here
$('.tablediv tbody').html(response).promise().done(function () {
// Callback function after HTML content is loaded
// Initialize DataTables on the table
$('.table').DataTable();
});
});
// Select element change event handler
$('#tableName').change(function () {
$('.table').DataTable().destroy();
2023-06-13 07:14:37 +00:00
var selectedTable = $(this).val();
// Send GET request to TblEditor.php?getTable with selected table name
var request = $.get('TblEditor.php', {cmd: 'getTable', tableName: selectedTable});
request.done(function (response) {
// Handle the response here
$('.tablediv tbody').html(response).promise().done(function () {
// Callback function after HTML content is loaded
// Initialize DataTables on the table
$('.table').DataTable();
});
});
});
// Delete link click event handler
$(document).on('click', '.delete-link', function (event) {
event.preventDefault();
var selectedTable = $('select[name="tableName"]').val();
// Get the rowid from the custom attribute data-rowid
var rowid = $(this).data('rowid');
// Remove the deleted row from the table
$('.row-' + rowid).remove();
// Send GET request to TblEditor.php?cmd=delete&rowid={rowid}
$.get('TblEditor.php', {cmd: 'delete', tableName: selectedTable, rowid: rowid}, function (response) {
// Handle the response here
// Wait until the response is loaded, then remove the row
$(response).ready(function () {
// Update DataTable
});
});
});
// Update link click event handler
$(document).on('click', '.update-link', function (event) {
event.preventDefault();
var selectedTable = $('select[name="tableName"]').val();
// Get the rowid from the custom attribute data-rowid
var rowid = $(this).data('rowid');
// Get the updated values from the corresponding input and textarea
var key = $('.row-' + rowid + ' input').val();
var string = $('.row-' + rowid + ' textarea').val();
string = string.replace(/&#13;/g, '\\n');
string = string.replace(/\n/g, '\\n');
2023-06-13 07:14:37 +00:00
// Send POST request to TblEditor.php with the updated values
$.post('TblEditor.php', {cmd: 'update', tableName: selectedTable, rowid: rowid, key: key, string: string}, function (response) {
// Handle the response here
// Optional: Update any UI elements if needed
2023-06-02 05:35:10 +00:00
});
2023-06-13 07:14:37 +00:00
});
// Add link click event handler
$('#addLink').click(function () {
var selectedTable = $('#tableName').val();
var key = $('#inputKey').val();
var string = $('#inputString').val();
// Send POST request to TblEditor.php with parameters
$.post('TblEditor.php', {cmd: 'add', tableName: selectedTable, Key: key, String: string}, function (response) {
// Handle the response here
rowid = response;
// Create the HTML markup for the new row
var newRow = '<tr class="row-' + rowid + '">' +
'<td>' + rowid + '</td>' +
'<td><input type="text" value="' + key + '" class="form-control"></td>' +
'<td><textarea class="form-control">' + string + '</textarea></td>' +
'<td><div>' +
'<a href="#" class="update-link btn btn-primary" data-rowid="' + rowid + '">Update</a>' +
'<a href="#" class="delete-link btn btn-danger" data-rowid="' + rowid + '">Delete</a>' +
'</div></td>' +
'</tr>';
;
2023-06-13 07:14:37 +00:00
// Append the new row to the table
$('.table').DataTable().destroy();
2023-06-13 07:14:37 +00:00
$('.table tbody').append(newRow);
2023-06-13 07:14:37 +00:00
$('.table').DataTable();
2023-06-02 05:35:10 +00:00
});
2023-06-13 07:14:37 +00:00
});
});
$(document).ready(function () {
// Iterate over each div with the form-control class
$('.form-control').each(function () {
// Get the text content of the div
var text = $(this).val();
// Set the height of the div to fit the text content
$(this).height('auto'); // Reset the height to auto
var contentHeight = $(this).prop('scrollHeight');
$(this).height(contentHeight);
});
});
2023-06-02 05:35:10 +00:00
2023-06-13 07:14:37 +00:00
</script>
<style>
div.dataTables_wrapper div.dataTables_length select {
width: 75px !important;
}
</style>
</body>
2023-06-02 05:35:10 +00:00
</html>