pics/templates/TabularData.php

223 lines
5.3 KiB
PHP

<?php
/*****************************************************************************
* TabularData.php
* Contains the template that displays tabular data.
*
* Global Data Lab code (C) Radboud University Nijmegen
* Programming (C) Aaron van Geffen, 2015-2022
*****************************************************************************/
class TabularData extends SubTemplate
{
private GenericTable $_t;
public function __construct(GenericTable $table)
{
$this->_t = $table;
}
protected function html_content()
{
$title = $this->_t->getTitle();
if (!empty($title))
{
$titleclass = $this->_t->getTitleClass();
echo '
<div class="generic-table', !empty($titleclass) ? ' ' . $titleclass : '', '">
<h1>', htmlspecialchars($title), '</h1>';
}
foreach ($this->_subtemplates as $template)
$template->html_main();
// Showing an inline form?
$pager = $this->_t->getPageIndex();
if (!empty($pager) || isset($this->_t->form_above))
{
echo '
<div class="row clearfix justify-content-end">';
// Page index?
if (!empty($pager))
PageIndexWidget::paginate($pager);
// Form controls?
if (isset($this->_t->form_above))
$this->showForm($this->_t->form_above);
echo '
</div>';
}
$tableClass = $this->_t->getTableClass();
if ($tableClass)
echo '
<div class="', $tableClass, '">';
// Build the table!
echo '
<table class="table table-striped table-condensed">
<thead>
<tr>';
// Show all headers in their full glory!
$header = $this->_t->getHeader();
foreach ($header as $th)
{
echo '
<th', (!empty($th['width']) ? ' width="' . $th['width'] . '"' : ''), (!empty($th['class']) ? ' class="' . $th['class'] . '"' : ''), ($th['colspan'] > 1 ? ' colspan="' . $th['colspan'] . '"' : ''), ' scope="', $th['scope'], '">',
$th['href'] ? '<a href="' . $th['href'] . '">' . $th['label'] . '</a>' : $th['label'];
if ($th['sort_mode'])
echo ' <i class="bi bi-caret-' . ($th['sort_mode'] === 'down' ? 'down' : 'up') . '-fill"></i>';
echo '</th>';
}
echo '
</tr>
</thead>
<tbody>';
// The body is what we came to see!
$body = $this->_t->getBody();
if (is_array($body))
{
foreach ($body as $tr)
{
echo '
<tr', (!empty($tr['class']) ? ' class="' . $tr['class'] . '"' : ''), '>';
foreach ($tr['cells'] as $td)
{
echo '
<td', (!empty($td['width']) ? ' width="' . $td['width'] . '"' : ''), '>';
if (!empty($td['class']))
echo '<span class="', $td['class'], '">', $td['value'], '</span>';
else
echo $td['value'];
echo '</td>';
}
echo '
</tr>';
}
}
// !!! Sum colspan!
else
echo '
<tr>
<td colspan="', count($header), '" class="fullwidth">', $body, '</td>
</tr>';
echo '
</tbody>
</table>';
if ($tableClass)
echo '
</div>';
// Showing an inline form?
if (!empty($pager) || isset($this->_t->form_below))
{
echo '
<div class="row clearfix justify-content-end">';
// Page index?
if (!empty($pager))
PageIndexWidget::paginate($pager);
// Form controls?
if (isset($this->_t->form_below))
$this->showForm($this->_t->form_below);
echo '
</div>';
}
if (!empty($title))
echo '
</div>';
}
protected function showForm($form)
{
echo '
<form action="', $form['action'], '" method="', $form['method'], '" class="', $form['class'], '">';
if (!empty($form['is_group']))
echo '
<div class="input-group">';
if (!empty($form['fields']))
{
foreach ($form['fields'] as $name => $field)
{
if ($field['type'] === 'select')
{
echo '
<select class="form-select" name="', $name, '"', (isset($field['onchange']) ? ' onchange="' . $field['onchange'] . '"' : ''), '>';
foreach ($field['values'] as $value => $caption)
{
if (!is_array($caption))
{
echo '
<option value="', $value, '"', $value === $field['selected'] ? ' selected' : '', '>', $caption, '</option>';
}
else
{
$label = $value;
$options = $caption;
echo '
<optgroup label="', $label, '">';
foreach ($options as $value => $caption)
{
echo '
<option value="', $value, '"', $value === $field['selected'] ? ' selected' : '', '>', $caption, '</option>';
}
echo '
</optgroup>';
}
}
echo '
</select>';
}
else
echo '
<input name="', $name, '" id="field_', $name, '" type="', $field['type'], '" placeholder="', $field['placeholder'], '" class="form-control', isset($field['class']) ? ' ' . $field['class'] : '', '"', isset($field['value']) ? ' value="' . htmlspecialchars($field['value']) . '"' : '', '>';
if (isset($field['html_after']))
echo $field['html_after'];
}
}
echo '
<input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '">';
if (!empty($form['buttons']))
foreach ($form['buttons'] as $name => $button)
{
echo '
<button class="btn ', isset($button['class']) ? $button['class'] : 'btn-primary', '" type="', $button['type'], '" name="', $name, '">', $button['caption'], '</button>';
if (isset($button['html_after']))
echo $button['html_after'];
}
if (!empty($form['is_group']))
echo '
</div>';
echo '
</form>';
}
}