pics/templates/TabularData.php

240 lines
5.6 KiB
PHP
Raw Permalink Normal View History

<?php
/*****************************************************************************
* TabularData.php
* Contains the template that displays tabular data.
*
* Kabuki CMS (C) 2013-2023, Aaron van Geffen
*****************************************************************************/
class TabularData extends SubTemplate
{
protected GenericTable $_t;
public function __construct(GenericTable $table)
{
$this->_t = $table;
}
protected function html_content()
{
$this->renderTitle();
foreach ($this->_subtemplates as $template)
$template->html_main();
// Showing an inline form?
$pager = $this->_t->getPageIndex();
if (!empty($pager) || isset($this->_t->form_above))
$this->renderPaginationForm($pager, $this->_t->form_above);
$tableClass = $this->_t->getTableClass();
if ($tableClass)
echo '
<div class="', $tableClass, '">';
// Build the table!
echo '
<table class="table table-striped table-condensed">';
$this->renderTableHead($this->_t->getHeader());
$this->renderTableBody($this->_t->getBody());
echo '
</table>';
if ($tableClass)
echo '
</div>';
// Showing an inline form?
if (!empty($pager) || isset($this->_t->form_below))
$this->renderPaginationForm($pager, $this->_t->form_below);
$title = $this->_t->getTitle();
if (!empty($title))
echo '
</div>';
}
protected function renderTitle()
{
$title = $this->_t->getTitle();
if (!empty($title))
{
$titleclass = $this->_t->getTitleClass();
echo '
<div class="generic-table', !empty($titleclass) ? ' ' . $titleclass : '', '">
<h1>', htmlspecialchars($title), '</h1>';
}
}
protected function renderPaginationForm($pager, $form, $class='row')
{
echo '
<div class="', $class, ' clearfix justify-content-end">';
// Page index?
if (!empty($pager))
PageIndexWidget::paginate($pager);
// Form controls?
if (isset($form))
$this->renderInlineForm($form);
echo '
</div>';
}
protected function renderTableHead(array $headers)
{
echo '
<thead>
<tr>';
foreach ($headers 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>';
}
protected function renderTableBody($body)
{
echo '
<tbody>';
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['class']) ? ' class="' . $td['class'] . '"' : ''),
(!empty($td['width']) ? ' width="' . $td['width'] . '"' : ''), '>',
$td['value'],
'</td>';
}
echo '
</tr>';
}
}
else
{
$header = $this->_t->getHeader();
echo '
<tr>
<td colspan="', count($header), '" class="fullwidth">', $body, '</td>
</tr>';
}
echo '
</tbody>';
}
protected function renderInlineForm($form)
{
if (!isset($form['is_embed']))
echo '
<form action="', $form['action'], '" method="', $form['method'], '" class="', $form['class'], '">';
else
echo '
<div 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, '"';
if (isset($button['onclick']))
echo ' onclick="', $button['onclick'], '"';
echo '>', $button['caption'], '</button>';
if (isset($button['html_after']))
echo $button['html_after'];
}
if (!empty($form['is_group']))
echo '
</div>';
if (!isset($form['is_embed']))
echo '
</form>';
else
echo '
</div>';
}
}