Refactor to change some verbiage, added ability to POST data (both via FormMethod option or as an API)

This commit is contained in:
Conner Harkness 2025-09-09 14:49:13 -06:00
parent 74a83a4cdf
commit c64dff6923

View File

@ -29,8 +29,9 @@
$strError = null;
$varOptions = [];
$intAllowed = 1;
$intSortable = 1;
$intAllowed = 1; // Default to allowing the user to run the query
$intSortable = 1; // By default, make the tables Datatables sortable
$intSectioned = 0; // By default, the data is unsectioned
if ($strSelection !== null && strlen($strSelection) > 0)
{
@ -145,24 +146,45 @@
$intAllowed = 1;
}
$intRun = Request::getParam("run");
$strFormat = Request::getParam("format");
$varPosted = Request::getParams();
$intRun = 0;
// Merge POST with the GET params:
// POSTing implies running the query, too.
foreach (Request::getPosted() as $k => $v)
{
$varPosted[$k] = $v;
$intRun = 1;
}
$intRun = $varPosted["run"] ?? $intRun;
$strFormat = $varPosted["format"] ?? null;
// Auto-format the output to be JSON when the POSTed Content-Type is JSON:
if (Request::getHeader("Content-Type") == "application/json")
$strFormat = "json";
// Remove these keys from submitted fields:
foreach (["run", "format"] as $k)
if (array_key_exists($k, $varPosted))
unset($varPosted[$k]);
//if (Request::posts("run_query"))
if ($intRun !== null && $intRun == 1 && $intAllowed == 1)
{
$varPosted = Request::getParams();
// Remove these keys from submitted fields:
foreach (["run", "format"] as $k)
if (array_key_exists($k, $varPosted))
unset($varPosted[$k]);
try
{
$varRows = $c->query($strQueryPath, $varPosted);
$strQueryView = "{$strQueryPath}.php";
if (count($varRows) > 0)
{
if (array_key_exists("_section", $varRows[0]))
{
$intSectioned = 1;
$intSortable = 0;
}
}
// Allow returning the data as JSON for APIs, maybe:
if (strtolower($strFormat) == "json")
Respond::json($varRows);
@ -223,9 +245,10 @@
<a class="nav-item btn btn-outline-light me-2" onclick="fnExport();" id="export-csv-button"><i class="fa fa-fw fa-download"></i> <span class="d-none d-lg-inline">.csv</span></a>
<a class="nav-item btn btn-outline-light me-2" onclick="fnPrint();"><i class="fa fa-fw fa-print"></i> <span class="d-none d-lg-inline">Print</span></a>
<div class="input-group">
<span class="input-group-text bg-primary border-white text-white"><i class="fa fa-fw fa-search"></i></span>
<input type="text" class="form-control bg-primary border-white text-white" name="" placeholder="Search rows..." value="" id="table-search"></div>
<span class="input-group-text bg-primary border-white text-white"><i class="fa fa-fw fa-filter"></i></span>
<input type="text" class="form-control bg-primary border-white text-white" placeholder="Filter rows..." value="" id="table-filter-input"></div>
</div>
<?php endif; ?>
</div>
@ -271,7 +294,7 @@
<label>Choose Query</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-folder"></i></span>
<input type="text" class="form-control" name="file" placeholder="Search files..." value="<?= $strSelection; ?>" id="file-search" />
<input type="text" class="form-control" name="file" placeholder="Search files..." value="<?= $strSelection; ?>" id="file-search-input" />
</div>
<div class="file-search-results">
<?php foreach ($varFiles as $f): ?>
@ -306,7 +329,13 @@
<?php endif; ?>
<?php endforeach; ?>
<form method="get">
<?php
$strFormMethod = "get";
if (array_key_exists("formmethod", $varOptions))
$strFormMethod = $varOptions["formmethod"];
?>
<form method="<?= $strFormMethod; ?>">
<?php if (count($varInputs[0]) > 0): ?>
<?php for ($i = 0; $i < count($varInputs[0]); $i++): ?>
<?php
@ -500,7 +529,7 @@
<li><?= $k; ?>: <?= $v; ?></li>
<?php endforeach; ?>
<li>Printed: <?= date("Y-m-d H:i:s"); ?></li>
<li class="d-none">Search: <span id="search-text"></span></li>
<li class="d-none">Filter: <span id="table-filter-text"></span></li>
</ul>
</div>
</div>
@ -521,7 +550,7 @@
$strNextCellClass = "";
?>
<div class="<?= $strDisplayRowAs; ?> searchable">
<div class="<?= $strDisplayRowAs; ?> filterable">
<table class="table table-sm table-striped table-bordered">
@ -581,11 +610,14 @@
<table class="table table-sm table-striped table-bordered w-100" id="table">
<thead class="sticky-top">
<tr>
<?php $intVisibleColumnCount = 0; ?>
<?php foreach ($varColumns as $col): ?>
<?php
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
$intVisibleColumnCount++;
?>
<th class="text-nowrap sorting"><?= $col; ?> <?php if ($intSortable) DataTable::sortIcon(); ?></th>
<?php endforeach; ?>
@ -593,6 +625,13 @@
</thead>
<tbody>
<?php
$strLastSection = "";
$strSectionClass = "bg-black fw-bold text-white";
if (array_key_exists("sectionclass", $varOptions))
$strSectionClass = $varOptions["sectionclass"];
?>
<?php foreach ($varRows as $r): ?>
<?php
// Allow SQL data to control row classes with _row_class column:
@ -602,7 +641,17 @@
$strNextCellClass = "";
?>
<tr class="searchable">
<?php if ($intSectioned && $strLastSection !== $r["_section"]): ?>
<tr>
<td class="<?= $strSectionClass; ?>" colspan="<?= $intVisibleColumnCount; ?>"><?= $r["_section"]; ?></td>
</tr>
<?php
$strLastSection = $r["_section"];
?>
<?php endif; ?>
<tr class="filterable">
<?php foreach ($varColumns as $col): ?>
<?php
$strCellClass = "";
@ -640,16 +689,16 @@
<script>
$(function() {
var fileSearch = $("#file-search").first();
var tableSearch = $("#table-search").first();
var fileSearchInput = $("#file-search-input").first();
var tableFilterInput = $("#table-filter-input").first();
fileSearch.on("focus", function() {
fileSearch.trigger("input");
fileSearch.select();
fileSearchInput.on("focus", function() {
fileSearchInput.trigger("input");
fileSearchInput.select();
});
fileSearch.on("input", function() {
var q = fileSearch.val();
fileSearchInput.on("input", function() {
var q = fileSearchInput.val();
$(".file-search-results div").each(function(i, x) {
x = $(x);
@ -662,41 +711,40 @@
});
});
if (tableSearch)
if (tableFilterInput)
{
var searchText = $("#search-text").first();
var filterText = $("#table-filter-text").first();
tableSearch.on("input", function() {
var q = tableSearch.val();
tableFilterInput.on("input", function() {
var q = tableFilterInput.val();
searchText.html(q);
filterText.html(q);
searchText
filterText
.parents("li")
.first()
.addClass("d-none");
if (q.length > 0)
{
searchText
filterText
.parents("li")
.first()
.removeClass("d-none");
}
$(".searchable").each(function(i, x) {
$(".filterable").each(function(i, x) {
x = $(x);
x.addClass("d-none");
var searchableText = x.text();
var filterableText = x.text();
if (searchableText.includesAll(q))
if (filterableText.includesAll(q))
x.removeClass("d-none");
});
});
}
fnShowPage = function(id)
{
$("div.page").addClass("d-none");
@ -773,7 +821,7 @@
<?php if ($strSearchQuery !== null && strlen($strSearchQuery) > 0): ?>
<script>
$(function() {
$("#file-search")
$("#file-search-input")
.first()
.val("<?= $strSearchQuery; ?>")
.trigger("input");