Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library\View\Sky;
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use TechFry\Library\TAmp;
class Table extends Bootstrap
{
public $class_options = array('table_class', 'striped', 'hover', 'bordered', 'small');
public $headers = [];
public $sortable_headers = [];
public function display()
{
$table_class = 'table ' . $this->get_class();
$output = '<table class="' . $table_class . '">';
$show_headings = $this->config['show_headings'] ?? '';
if ($show_headings)
{
// Normal headers: $this->config['theads'] or $this->data['theads']
$this->headers = isset($this->config['theads']) && $this->config['theads'] ? str_getcsv($this->config['theads']) : $this->headers;
if (empty($this->headers))
{
$this->headers = isset($this->data['theads']) && $this->data['theads'] ? $this->data['theads'] : $this->headers;
}
unset($this->data['theads']);
if (!empty($this->headers))
{
$output .= $this->get_headers();
}
// Sortable headers
if (!empty($this->sortable_headers))
{
$output .= $this->get_sortable_headers();
}
}
$output .= '<tbody>';
foreach ($this->data as $item)
{
$output .= '<tr>';
if (isset($item['title']) && isset($item['url']))
{
$output .= '<td><a href="' . $item['url'] . '">' . $item['title'] . '</a></td>';
unset($item['title']);
unset($item['url']);
}
foreach ($item as $cell)
{
$output .= '<td>' . $cell . '</td>';
}
$output .= '</tr>';
}
$output .= '</tbody>';
$output .= '</table>';
return $output;
}
public function add_header($header)
{
$this->headers[] = $header;
}
public function add_sortable_header($header)
{
$this->sortable_headers[] = $header;
}
public function get_headers()
{
$output = '<thead>';
$output .= '<tr>';
foreach ($this->headers as $thead)
{
$output .= '<th>' . $thead . '</th>';
}
$output .= '</tr>';
$output .= '</thead>';
return $output;
}
public function get_sortable_headers()
{
$output = '<thead>';
$output .= '<tr>';
foreach ($this->sortable_headers as $thead)
{
$output .= '<th>' . $this->sortable($thead) . '</th>';
}
$output .= '</tr>';
$output .= '</thead>';
return $output;
}
// Sortable Headers (libraries/src/HTML/helpers/Grid.php)
public function sortable($thead = array())
{
// If $thead is array, then sortable.
if (!is_array($thead))
{
return $thead;
}
$title = $thead[0];
$order = $thead[1];
$direction = isset($thead[2]) && $thead[2] ? $thead[2] : 'asc';
$selected = isset($thead[3]) && $thead[3] ? $thead[3] : '';
$task = isset($thead[4]) && $thead[4] ? $thead[4] : null;
$form = isset($thead[5]) && $thead[5] ? $thead[5] : null;
$amp = new TAmp();
$is_amp = $amp->is_amp();
if ($is_amp)
{
return Text::_($title);
}
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip');
$direction = strtolower($direction);
$icon = ['chevron-up', 'chevron-down'];
$index = (int) ($direction === 'desc');
$newDirection = 'asc';
if ($order != $selected)
{
$direction = $newDirection;
}
else
{
$direction = $direction === 'desc' ? 'asc' : 'desc';
}
if ($form)
{
$form = ', document.getElementById(\'' . $form . '\')';
}
$html = '<a href="#" onclick="Joomla.tableOrdering(\'' . $order . '\',\'' . $direction . '\',\'' . $task . '\'' . $form . ');return false;"'
. ' class="hasTooltip" title="' . htmlspecialchars(Text::_('JGLOBAL_CLICK_TO_SORT_THIS_COLUMN')) . '" data-bs-placement="top">';
if (isset($title['0']) && $title['0'] === '<')
{
$html .= $title;
}
else
{
$html .= Text::_($title);
}
if ($order == $selected)
{
$html .= ' <span class="fa-solid fa-' . $icon[$index] . '"></span>';
}
$html .= '</a>';
return $html;
}
}