cleanup & bugfix for DisplayRowAs (formerly RowClass option), added source preview, added focused mode, moved view processing to bottom

This commit is contained in:
Conner Harkness 2025-09-04 11:04:30 -06:00
parent 50bf5bead3
commit e44526aa5a

View File

@ -14,13 +14,15 @@
$strInput = substr($strInput, strlen($strQueryDir) + 1);
}
$strFind = shell_exec("find files/sql | grep -Ei \"\.sql$\"");
$strFind = shell_exec("find {$strQueryDir} | grep -Ei \"\.sql$\"");
$varFiles = explode("\n", $strFind);
$varFiles = array_filter($varFiles);
array_walk($varFiles, "removeQueryDir");
$strSelection = implode("/", Request::getArgs());
$strQueryPath = "{$strQueryDir}/{$strSelection}";
$strQueryView = null;
$varInputs = [[]];
$varRows = null;
@ -31,8 +33,9 @@
if ($strSelection !== null && strlen($strSelection) > 0)
{
$strSelection = urldecode($strSelection);
$strFileData = file_get_contents("{$strQueryDir}/{$strSelection}");
$strSelection = urldecode($strSelection);
$strFileDataSource = file_get_contents("{$strQueryDir}/{$strSelection}");
$strFileData = $strFileDataSource;
// Translate occurrences of {{ Date: next monday }} into yyyy-MM-dd format:
// Can be used in string literals or default values in comments!
@ -158,21 +161,12 @@
try
{
$varRows = $c->query("{$strQueryDir}/{$strSelection}", $varPosted);
$strView = "files/views/{$strSelection}.php";
$varRows = $c->query($strQueryPath, $varPosted);
$strQueryView = "{$strQueryPath}.php";
// Allow returning the data as JSON for APIs, maybe:
if (strtolower($strFormat) == "json")
Respond::json($varRows);
// Load a custom view if there is one:
if (file_exists($strView))
{
ob_clean();
require $strView;
ob_end_flush();
exit;
}
}
catch (Exception $x)
{
@ -190,16 +184,18 @@
}
// Fancier title processing:
$strTitle = null;
$strTitle = "Select Report";
if ($strSelection !== null && strlen($strSelection) > 0)
$strTitle = $strSelection;
if (array_key_exists("title", $varOptions))
$strTitle = $varOptions["title"];
$strTitle = $strTitle ?? $strSelection ?? "Report";
?>
<?php if (array_key_exists("scale", $varOptions)): ?>
<style>
table {
#visible-data {
font-size: <?= $varOptions["scale"]; ?>em;
}
</style>
@ -220,6 +216,14 @@
</div>
</div>
<div class="navbar navbar-expand bg-light navbar-light d-print-none" id="title-navbar">
<div class="container-fluid justify-content-between">
<div class="d-inline-flex align-items-center">
<span class="navbar-brand"><?= $strTitle; ?></span>
</div>
</div>
</div>
<div class="page" id="page-input">
<div class="container my-3">
@ -245,7 +249,7 @@
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<div class="mb-3" data-report-chooser="1">
<label>Choose Query</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-folder"></i></span>
@ -352,7 +356,16 @@
<div class="mb-3">
<label>Actions</label>
<div>
<a class="btn btn-primary" onclick="fnSubmit(this);"><i class="fa fa-fw fa-server me-1"></i> Execute</a>
<?php if ($strSelection !== null && strlen($strSelection) > 0): ?>
<?php
$strButtonLabel = $varOptions["buttonlabel"] ?? "Execute";
$strButtonClass = $varOptions["buttonclass"] ?? "btn-primary";
$strButtonIcon = $varOptions["buttonicon"] ?? "fa-server";
?>
<a class="btn <?= $strButtonClass; ?> me-1" onclick="fnSubmit(this);"><i class="fa fa-fw <?= $strButtonIcon; ?> me-1"></i> <?= $strButtonLabel; ?></a>
<?php endif; ?>
<a class="btn btn-outline-secondary me-1" href="<?= Request::getPath(); ?>"><i class="fa fa-fw fa-arrow-rotate-left me-1"></i> Reset</a>
</div>
</div>
@ -380,10 +393,18 @@
?>
<?php if (count($varDirectories)): ?>
<div class="col-md-6">
<div class="col-md-6" data-report-chooser="1">
<div class="mb-3">
<label>Directories</label>
<div class="">
<div class="border p-3">
<a class="link-underline link-underline-opacity-0" href="/run">
<i class="fa fa-fw fa-home me-2"></i>
Home
</a>
</div>
<?php foreach ($varDirectories as $d => $a): ?>
<div class="border p-3">
@ -401,6 +422,30 @@
</div>
<?php if ($strSelection !== null && strlen($strSelection) > 0): ?>
<?php if (array_key_exists("sourcepreview", $varOptions)): ?>
<style>
#source-preview {
line-height: 1em;
font-family: monospace;
min-height: 2in;
}
</style>
<div class="row">
<div class="col-md-12">
<div class="mb-3">
<label>Source</label>
<div class="w-100">
<textarea class="w-100 border-0 bg-light p-3" id="source-preview" readonly><?= $strFileDataSource; ?></textarea>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
@ -414,10 +459,10 @@
foreach ($varRow1 as $k => $v)
$varColumns[] = $k;
$strRowClass = null;
if (array_key_exists("rowclass", $varOptions))
if (preg_match("/^col\-/i", $varOptions["rowclass"]))
$strRowClass = $varOptions["rowclass"];
$strDisplayRowAs = null;
if (array_key_exists("displayrowas", $varOptions))
if (preg_match("/^col\-/i", $varOptions["displayrowas"]))
$strDisplayRowAs = $varOptions["displayrowas"];
?>
<div class="d-none d-print-block">
@ -443,71 +488,10 @@
</div>
</div>
<?php if ($strRowClass !== null): ?>
<div class="container-fluid">
<div class="row my-3">
<?php foreach ($varRows as $r): ?>
<div class="<?= $strRowClass; ?>">
<table class="table table-sm table-striped table-bordered">
<thead>
<tr>
<th class="bg-black text-white"><?= $varColumns[0]; ?></th>
<th class="bg-black text-white"><?= $r[$varColumns[0]]; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($varColumns as $col): ?>
<?php
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
?>
<tr>
<td><?= $col; ?></td>
<td><?= $r[$col]; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
</div>
<script>
$(function() {
$("#export-csv-button")
.first()
.addClass("d-none");
});
</script>
<?php else: ?>
<div class="table-responsive">
<table class="table table-sm table-striped table-bordered w-100" id="table">
<thead class="sticky-top">
<tr>
<?php foreach ($varColumns as $col): ?>
<?php
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
?>
<th class="text-nowrap sorting"><?= $col; ?> <?php DataTable::sortIcon(); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<div id="visible-data">
<?php if ($strDisplayRowAs !== null): ?>
<div class="container-fluid">
<div class="row my-3">
<?php foreach ($varRows as $r): ?>
<?php
// Allow SQL data to control row classes with _row_class column:
@ -517,38 +501,121 @@
$strNextCellClass = "";
?>
<div class="<?= $strDisplayRowAs; ?>">
<table class="table table-sm table-striped table-bordered">
<thead>
<tr>
<th class="bg-black text-white"><?= $varColumns[0]; ?></th>
<th class="bg-black text-white"><?= $r[$varColumns[0]]; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($varColumns as $col): ?>
<?php
$strCellClass = "";
if (strlen($strRowClass) > 0)
$strCellClass = $strRowClass;
if (strlen($strNextCellClass) > 0)
$strCellClass = $strNextCellClass;
// Allow SQL data to controll next cell class with _next_cell_class column:
if (preg_match("/^_next_cell_class/i", $col))
$strNextCellClass = $r[$col];
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
?>
<tr>
<td><?= $col; ?></td>
<td class="<?= $strCellClass; ?>"><?= $r[$col]; ?></td>
</tr>
<?php $strNextCellClass = ""; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endforeach; ?>
</div>
</div>
<script>
$(function() {
$("#export-csv-button")
.first()
.addClass("d-none");
});
</script>
<?php else: ?>
<div class="table-responsive">
<table class="table table-sm table-striped table-bordered w-100" id="table">
<thead class="sticky-top">
<tr>
<?php foreach ($varColumns as $col): ?>
<?php
$strCellClass = "";
if (strlen($strRowClass) > 0)
$strCellClass = $strRowClass;
if (strlen($strNextCellClass) > 0)
$strCellClass = $strNextCellClass;
// Allow SQL data to controll next cell class with _next_cell_class column:
if (preg_match("/^_next_cell_class/i", $col))
$strNextCellClass = $r[$col];
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
?>
<td class="<?= $strCellClass; ?>"><?= $r[$col]; ?></td>
<?php
$strNextCellClass = "";
?>
<th class="text-nowrap sorting"><?= $col; ?> <?php DataTable::sortIcon(); ?></th>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</thead>
<tbody>
<?php foreach ($varRows as $r): ?>
<?php
// Allow SQL data to control row classes with _row_class column:
$strRowClass = "";
if (array_key_exists("_row_class", $r))
$strRowClass = $r["_row_class"];
$strNextCellClass = "";
?>
<tr>
<?php foreach ($varColumns as $col): ?>
<?php
$strCellClass = "";
if (strlen($strRowClass) > 0)
$strCellClass = $strRowClass;
if (strlen($strNextCellClass) > 0)
$strCellClass = $strNextCellClass;
// Allow SQL data to controll next cell class with _next_cell_class column:
if (preg_match("/^_next_cell_class/i", $col))
$strNextCellClass = $r[$col];
// Hide columns that begin with underscore:
if ($col[0] == "_")
continue;
?>
<td class="<?= $strCellClass; ?>"><?= $r[$col]; ?></td>
<?php
$strNextCellClass = "";
?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
@ -596,6 +663,11 @@
fnShowPage("#page-output");
$("#table").exportCSV({name: "<?= $strSelection; ?>"});
};
fnGetRowAsQueryString = function()
{
};
});
</script>
@ -607,6 +679,15 @@
</script>
<?php DataTable::js("#table"); ?>
<?php if (array_key_exists("outputonly", $varOptions)): ?>
<script>
$(function() {
$(".navbar").addClass("d-none");
// $("#title-navbar").removeClass("d-none");
});
</script>
<?php endif; ?>
<?php endif; ?>
@ -636,3 +717,20 @@
});
</script>
<?php endif; ?>
<?php if (array_key_exists("focusinput", $varOptions)): ?>
<script>
$(function() {
$("[data-report-chooser='1']").addClass("d-none");
});
</script>
<?php endif; ?>
<?php
// Load a custom view if there is one:
if ($strQueryView !== null)
if (file_exists($strQueryView))
require $strQueryView;
?>