Cells[$Y][$X];
}
function RowsCount(): int
{
return count($this->Cells);
}
}
class TableSQL extends Table
{
public string $Query;
public Database $Database;
public array $Cells;
function GetCell($Y, $X): string
{
return $this->Cells[$Y][$X];
}
function BeginRead()
{
$this->Cells = array();
$DbResult = $this->Database->query($this->Query);
while ($DbRow = $DbResult->fetch_row())
{
$this->Cells[] = $DbRow;
}
}
function EndRead()
{
$this->Cells = array();
}
function RowsCount(): int
{
return count($this->Cells);
}
}
class TableColumn
{
public string $Name;
public string $Title;
}
class VisualTable extends Control
{
public array $Cells;
public array $Columns;
public string $OrderSQL;
public string $OrderColumn;
public int $OrderDirection;
public array $OrderArrowImage;
public string $DefaultColumn;
public int $DefaultOrder;
public TableMemory $Table;
public string $Style;
private array $OrderDirSQL;
function __construct(System $System)
{
$this->Columns = array();
$this->Table = new TableMemory();
$this->OrderDirSQL = array('ASC', 'DESC');
$this->OrderArrowImage = array(Core::Cast($System)->Link('/images/sort_asc.png'),
$System->Link('/images/sort_desc.png'));
$this->DefaultOrder = 0;
$this->Style = 'BaseTable';
}
function SetColumns($Columns)
{
$this->Columns = array();
foreach ($Columns as $Column)
{
$NewCol = new TableColumn();
$NewCol->Name = $Column['Name'];
$NewCol->Title = $Column['Title'];
$this->Columns[] = $NewCol;
}
if (count($this->Columns) > 0)
$this->DefaultColumn = $this->Columns[0]->Name;
else $this->DefaultColumn = '';
}
function Show(): string
{
$Output = '
';
$Output .= $this->GetOrderHeader();
$this->Table->BeginRead();
for ($Y = 0; $Y < $this->Table->RowsCount(); $Y++)
{
$Output .= '';
for ($X = 0; $X < count($this->Columns); $X++)
{
$Cell = $this->Table->GetCell($Y, $X);
if ($Cell == '') $Output .= ' | ';
else $Output .= ''.$Cell.' | ';
}
$Output .= '
';
}
$this->Table->EndRead();
$Output .= '
';
return $Output;
}
function GetOrderHeader(): string
{
if (array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
if (array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
if (!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $this->DefaultColumn;
if (!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $this->DefaultOrder;
// Check OrderCol
$Found = false;
foreach ($this->Columns as $Column)
{
if ($Column->Name == $_SESSION['OrderCol'])
{
$Found = true;
break;
}
}
if ($Found == false)
{
$_SESSION['OrderCol'] = $this->DefaultColumn;
$_SESSION['OrderDir'] = $this->DefaultOrder;
}
// Check OrderDir
if (($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1))
$_SESSION['OrderDir'] = 0;
$Result = '';
$QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
foreach ($this->Columns as $Index => $Column)
{
$QueryItems['OrderCol'] = $Column->Name;
$QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
if ($Column->Name == $_SESSION['OrderCol'])
$ArrowImage = '';
else $ArrowImage = '';
if ($Column->Name == '') $Result .= ''.$Column->Title.' | ';
else $Result .= ''.$Column->Title.$ArrowImage.' | ';
}
$this->OrderSQL = ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$this->OrderDirSQL[$_SESSION['OrderDir']];
$this->OrderColumn = $_SESSION['OrderCol'];
$this->OrderDirection = $_SESSION['OrderDir'];
return ''.$Result.'
';
}
}