Refactor to change some verbiage, added ability to POST data (both via FormMethod option or as an API)
This commit is contained in:
parent
74a83a4cdf
commit
c64dff6923
126
pages/run.php
126
pages/run.php
@ -29,8 +29,9 @@
|
|||||||
$strError = null;
|
$strError = null;
|
||||||
$varOptions = [];
|
$varOptions = [];
|
||||||
|
|
||||||
$intAllowed = 1;
|
$intAllowed = 1; // Default to allowing the user to run the query
|
||||||
$intSortable = 1;
|
$intSortable = 1; // By default, make the tables Datatables sortable
|
||||||
|
$intSectioned = 0; // By default, the data is unsectioned
|
||||||
|
|
||||||
if ($strSelection !== null && strlen($strSelection) > 0)
|
if ($strSelection !== null && strlen($strSelection) > 0)
|
||||||
{
|
{
|
||||||
@ -144,25 +145,46 @@
|
|||||||
if (in_array($strObject, $varUserObjects))
|
if (in_array($strObject, $varUserObjects))
|
||||||
$intAllowed = 1;
|
$intAllowed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$intRun = Request::getParam("run");
|
|
||||||
$strFormat = Request::getParam("format");
|
|
||||||
|
|
||||||
//if (Request::posts("run_query"))
|
$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 ($intRun !== null && $intRun == 1 && $intAllowed == 1)
|
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
|
try
|
||||||
{
|
{
|
||||||
$varRows = $c->query($strQueryPath, $varPosted);
|
$varRows = $c->query($strQueryPath, $varPosted);
|
||||||
$strQueryView = "{$strQueryPath}.php";
|
$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:
|
// Allow returning the data as JSON for APIs, maybe:
|
||||||
if (strtolower($strFormat) == "json")
|
if (strtolower($strFormat) == "json")
|
||||||
Respond::json($varRows);
|
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="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>
|
<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">
|
<div class="input-group">
|
||||||
<span class="input-group-text bg-primary border-white text-white"><i class="fa fa-fw fa-search"></i></span>
|
<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" name="" placeholder="Search rows..." value="" id="table-search"></div>
|
<input type="text" class="form-control bg-primary border-white text-white" placeholder="Filter rows..." value="" id="table-filter-input"></div>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
@ -271,7 +294,7 @@
|
|||||||
<label>Choose Query</label>
|
<label>Choose Query</label>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<span class="input-group-text"><i class="fa fa-fw fa-folder"></i></span>
|
<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>
|
||||||
<div class="file-search-results">
|
<div class="file-search-results">
|
||||||
<?php foreach ($varFiles as $f): ?>
|
<?php foreach ($varFiles as $f): ?>
|
||||||
@ -306,7 +329,13 @@
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php endforeach; ?>
|
<?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 if (count($varInputs[0]) > 0): ?>
|
||||||
<?php for ($i = 0; $i < count($varInputs[0]); $i++): ?>
|
<?php for ($i = 0; $i < count($varInputs[0]); $i++): ?>
|
||||||
<?php
|
<?php
|
||||||
@ -500,7 +529,7 @@
|
|||||||
<li><?= $k; ?>: <?= $v; ?></li>
|
<li><?= $k; ?>: <?= $v; ?></li>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<li>Printed: <?= date("Y-m-d H:i:s"); ?></li>
|
<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>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -521,7 +550,7 @@
|
|||||||
$strNextCellClass = "";
|
$strNextCellClass = "";
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<div class="<?= $strDisplayRowAs; ?> searchable">
|
<div class="<?= $strDisplayRowAs; ?> filterable">
|
||||||
|
|
||||||
<table class="table table-sm table-striped table-bordered">
|
<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">
|
<table class="table table-sm table-striped table-bordered w-100" id="table">
|
||||||
<thead class="sticky-top">
|
<thead class="sticky-top">
|
||||||
<tr>
|
<tr>
|
||||||
|
<?php $intVisibleColumnCount = 0; ?>
|
||||||
<?php foreach ($varColumns as $col): ?>
|
<?php foreach ($varColumns as $col): ?>
|
||||||
<?php
|
<?php
|
||||||
// Hide columns that begin with underscore:
|
// Hide columns that begin with underscore:
|
||||||
if ($col[0] == "_")
|
if ($col[0] == "_")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
$intVisibleColumnCount++;
|
||||||
?>
|
?>
|
||||||
<th class="text-nowrap sorting"><?= $col; ?> <?php if ($intSortable) DataTable::sortIcon(); ?></th>
|
<th class="text-nowrap sorting"><?= $col; ?> <?php if ($intSortable) DataTable::sortIcon(); ?></th>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
@ -593,6 +625,13 @@
|
|||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tbody>
|
<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 foreach ($varRows as $r): ?>
|
||||||
<?php
|
<?php
|
||||||
// Allow SQL data to control row classes with _row_class column:
|
// Allow SQL data to control row classes with _row_class column:
|
||||||
@ -602,7 +641,17 @@
|
|||||||
|
|
||||||
$strNextCellClass = "";
|
$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 foreach ($varColumns as $col): ?>
|
||||||
<?php
|
<?php
|
||||||
$strCellClass = "";
|
$strCellClass = "";
|
||||||
@ -640,16 +689,16 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
$(function() {
|
$(function() {
|
||||||
var fileSearch = $("#file-search").first();
|
var fileSearchInput = $("#file-search-input").first();
|
||||||
var tableSearch = $("#table-search").first();
|
var tableFilterInput = $("#table-filter-input").first();
|
||||||
|
|
||||||
fileSearch.on("focus", function() {
|
fileSearchInput.on("focus", function() {
|
||||||
fileSearch.trigger("input");
|
fileSearchInput.trigger("input");
|
||||||
fileSearch.select();
|
fileSearchInput.select();
|
||||||
});
|
});
|
||||||
|
|
||||||
fileSearch.on("input", function() {
|
fileSearchInput.on("input", function() {
|
||||||
var q = fileSearch.val();
|
var q = fileSearchInput.val();
|
||||||
|
|
||||||
$(".file-search-results div").each(function(i, x) {
|
$(".file-search-results div").each(function(i, x) {
|
||||||
x = $(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() {
|
tableFilterInput.on("input", function() {
|
||||||
var q = tableSearch.val();
|
var q = tableFilterInput.val();
|
||||||
|
|
||||||
searchText.html(q);
|
filterText.html(q);
|
||||||
|
|
||||||
searchText
|
filterText
|
||||||
.parents("li")
|
.parents("li")
|
||||||
.first()
|
.first()
|
||||||
.addClass("d-none");
|
.addClass("d-none");
|
||||||
|
|
||||||
if (q.length > 0)
|
if (q.length > 0)
|
||||||
{
|
{
|
||||||
searchText
|
filterText
|
||||||
.parents("li")
|
.parents("li")
|
||||||
.first()
|
.first()
|
||||||
.removeClass("d-none");
|
.removeClass("d-none");
|
||||||
}
|
}
|
||||||
|
|
||||||
$(".searchable").each(function(i, x) {
|
$(".filterable").each(function(i, x) {
|
||||||
x = $(x);
|
x = $(x);
|
||||||
x.addClass("d-none");
|
x.addClass("d-none");
|
||||||
|
|
||||||
var searchableText = x.text();
|
var filterableText = x.text();
|
||||||
|
|
||||||
if (searchableText.includesAll(q))
|
if (filterableText.includesAll(q))
|
||||||
x.removeClass("d-none");
|
x.removeClass("d-none");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fnShowPage = function(id)
|
fnShowPage = function(id)
|
||||||
{
|
{
|
||||||
$("div.page").addClass("d-none");
|
$("div.page").addClass("d-none");
|
||||||
@ -773,7 +821,7 @@
|
|||||||
<?php if ($strSearchQuery !== null && strlen($strSearchQuery) > 0): ?>
|
<?php if ($strSearchQuery !== null && strlen($strSearchQuery) > 0): ?>
|
||||||
<script>
|
<script>
|
||||||
$(function() {
|
$(function() {
|
||||||
$("#file-search")
|
$("#file-search-input")
|
||||||
.first()
|
.first()
|
||||||
.val("<?= $strSearchQuery; ?>")
|
.val("<?= $strSearchQuery; ?>")
|
||||||
.trigger("input");
|
.trigger("input");
|
||||||
|
Loading…
Reference in New Issue
Block a user