source: trunk/Packages/Common/Table.php

Last change on this file was 888, checked in by chronos, 2 years ago
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File size: 4.4 KB
Line 
1<?php
2
3class Control
4{
5 public string $Name;
6
7 function Show(): string
8 {
9 return '';
10 }
11}
12
13class Table
14{
15 function GetCell($Y, $X): string
16 {
17 return '';
18 }
19
20 function BeginRead()
21 {
22 }
23
24 function EndRead()
25 {
26 }
27
28 function RowsCount(): int
29 {
30 return 0;
31 }
32}
33
34class TableMemory extends Table
35{
36 public array $Cells;
37
38 function GetCell($Y, $X): string
39 {
40 return $this->Cells[$Y][$X];
41 }
42
43 function RowsCount(): int
44 {
45 return count($this->Cells);
46 }
47}
48
49class TableSQL extends Table
50{
51 public string $Query;
52 public Database $Database;
53 public array $Cells;
54
55 function GetCell($Y, $X): string
56 {
57 return $this->Cells[$Y][$X];
58 }
59
60 function BeginRead()
61 {
62 $this->Cells = array();
63 $DbResult = $this->Database->query($this->Query);
64 while ($DbRow = $DbResult->fetch_row())
65 {
66 $this->Cells[] = $DbRow;
67 }
68 }
69
70 function EndRead()
71 {
72 $this->Cells = array();
73 }
74
75 function RowsCount(): int
76 {
77 return count($this->Cells);
78 }
79}
80
81class TableColumn
82{
83 public string $Name;
84 public string $Title;
85}
86
87class VisualTable extends Control
88{
89 public array $Cells;
90 public array $Columns;
91 public string $OrderSQL;
92 public string $OrderColumn;
93 public int $OrderDirection;
94 public array $OrderArrowImage;
95 public string $DefaultColumn;
96 public int $DefaultOrder;
97 public TableMemory $Table;
98 public string $Style;
99
100 function __construct()
101 {
102 global $System;
103
104 $this->Columns = array();
105 $this->Table = new TableMemory();
106 $this->OrderDirSQL = array('ASC', 'DESC');
107 $this->OrderArrowImage = array($System->Link('/images/sort_asc.png'),
108 $System->Link('/images/sort_desc.png'));
109 $this->DefaultOrder = 0;
110 $this->Style = 'BaseTable';
111 }
112
113 function SetColumns($Columns)
114 {
115 $this->Columns = array();
116 foreach ($Columns as $Column)
117 {
118 $NewCol = new TableColumn();
119 $NewCol->Name = $Column['Name'];
120 $NewCol->Title = $Column['Title'];
121 $this->Columns[] = $NewCol;
122 }
123 if (count($this->Columns) > 0)
124 $this->DefaultColumn = $this->Columns[0]->Name;
125 else $this->DefaultColumn = '';
126 }
127
128 function Show(): string
129 {
130 $Output = '<table class="'.$this->Style.'">';
131 $Output .= $this->GetOrderHeader();
132 $this->Table->BeginRead();
133 for ($Y = 0; $Y < $this->Table->RowsCount(); $Y++)
134 {
135 $Output .= '<tr>';
136
137 for ($X = 0; $X < count($this->Columns); $X++)
138 {
139 $Cell = $this->Table->GetCell($Y, $X);
140 if ($Cell == '') $Output .= '<td>&nbsp;</td>';
141 else $Output .= '<td>'.$Cell.'</td>';
142 }
143 $Output .= '</tr>';
144 }
145 $this->Table->EndRead();
146 $Output .= '</table>';
147 return $Output;
148 }
149
150 function GetOrderHeader(): string
151 {
152 if (array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
153 if (array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
154 if (!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $this->DefaultColumn;
155 if (!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $this->DefaultOrder;
156
157 // Check OrderCol
158 $Found = false;
159 foreach ($this->Columns as $Column)
160 {
161 if ($Column->Name == $_SESSION['OrderCol'])
162 {
163 $Found = true;
164 break;
165 }
166 }
167 if ($Found == false)
168 {
169 $_SESSION['OrderCol'] = $this->DefaultColumn;
170 $_SESSION['OrderDir'] = $this->DefaultOrder;
171 }
172 // Check OrderDir
173 if (($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1))
174 $_SESSION['OrderDir'] = 0;
175
176 $Result = '';
177 $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
178 foreach ($this->Columns as $Index => $Column)
179 {
180 $QueryItems['OrderCol'] = $Column->Name;
181 $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
182 if ($Column->Name == $_SESSION['OrderCol'])
183 $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$this->OrderArrowImage[$_SESSION['OrderDir']].'" alt="order arrow">';
184 else $ArrowImage = '';
185 if ($Column->Name == '') $Result .= '<th>'.$Column->Title.'</th>';
186 else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column->Title.$ArrowImage.'</a></th>';
187 }
188 $this->OrderSQL = ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$this->OrderDirSQL[$_SESSION['OrderDir']];
189 $this->OrderColumn = $_SESSION['OrderCol'];
190 $this->OrderDirection = $_SESSION['OrderDir'];
191
192 return '<tr>'.$Result.'</tr>';
193 }
194}
Note: See TracBrowser for help on using the repository browser.