1 | <?php
|
---|
2 |
|
---|
3 | class Control
|
---|
4 | {
|
---|
5 | public string $Name;
|
---|
6 |
|
---|
7 | function Show(): string
|
---|
8 | {
|
---|
9 | return '';
|
---|
10 | }
|
---|
11 | }
|
---|
12 |
|
---|
13 | class 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 |
|
---|
34 | class 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 |
|
---|
49 | class 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 |
|
---|
81 | class TableColumn
|
---|
82 | {
|
---|
83 | public string $Name;
|
---|
84 | public string $Title;
|
---|
85 | }
|
---|
86 |
|
---|
87 | class 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 | private array $OrderDirSQL;
|
---|
100 |
|
---|
101 | function __construct()
|
---|
102 | {
|
---|
103 | global $System;
|
---|
104 |
|
---|
105 | $this->Columns = array();
|
---|
106 | $this->Table = new TableMemory();
|
---|
107 | $this->OrderDirSQL = array('ASC', 'DESC');
|
---|
108 | $this->OrderArrowImage = array($System->Link('/images/sort_asc.png'),
|
---|
109 | $System->Link('/images/sort_desc.png'));
|
---|
110 | $this->DefaultOrder = 0;
|
---|
111 | $this->Style = 'BaseTable';
|
---|
112 | }
|
---|
113 |
|
---|
114 | function SetColumns($Columns)
|
---|
115 | {
|
---|
116 | $this->Columns = array();
|
---|
117 | foreach ($Columns as $Column)
|
---|
118 | {
|
---|
119 | $NewCol = new TableColumn();
|
---|
120 | $NewCol->Name = $Column['Name'];
|
---|
121 | $NewCol->Title = $Column['Title'];
|
---|
122 | $this->Columns[] = $NewCol;
|
---|
123 | }
|
---|
124 | if (count($this->Columns) > 0)
|
---|
125 | $this->DefaultColumn = $this->Columns[0]->Name;
|
---|
126 | else $this->DefaultColumn = '';
|
---|
127 | }
|
---|
128 |
|
---|
129 | function Show(): string
|
---|
130 | {
|
---|
131 | $Output = '<table class="'.$this->Style.'">';
|
---|
132 | $Output .= $this->GetOrderHeader();
|
---|
133 | $this->Table->BeginRead();
|
---|
134 | for ($Y = 0; $Y < $this->Table->RowsCount(); $Y++)
|
---|
135 | {
|
---|
136 | $Output .= '<tr>';
|
---|
137 |
|
---|
138 | for ($X = 0; $X < count($this->Columns); $X++)
|
---|
139 | {
|
---|
140 | $Cell = $this->Table->GetCell($Y, $X);
|
---|
141 | if ($Cell == '') $Output .= '<td> </td>';
|
---|
142 | else $Output .= '<td>'.$Cell.'</td>';
|
---|
143 | }
|
---|
144 | $Output .= '</tr>';
|
---|
145 | }
|
---|
146 | $this->Table->EndRead();
|
---|
147 | $Output .= '</table>';
|
---|
148 | return $Output;
|
---|
149 | }
|
---|
150 |
|
---|
151 | function GetOrderHeader(): string
|
---|
152 | {
|
---|
153 | if (array_key_exists('OrderCol', $_GET)) $_SESSION['OrderCol'] = $_GET['OrderCol'];
|
---|
154 | if (array_key_exists('OrderDir', $_GET)) $_SESSION['OrderDir'] = $_GET['OrderDir'];
|
---|
155 | if (!array_key_exists('OrderCol', $_SESSION)) $_SESSION['OrderCol'] = $this->DefaultColumn;
|
---|
156 | if (!array_key_exists('OrderDir', $_SESSION)) $_SESSION['OrderDir'] = $this->DefaultOrder;
|
---|
157 |
|
---|
158 | // Check OrderCol
|
---|
159 | $Found = false;
|
---|
160 | foreach ($this->Columns as $Column)
|
---|
161 | {
|
---|
162 | if ($Column->Name == $_SESSION['OrderCol'])
|
---|
163 | {
|
---|
164 | $Found = true;
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | if ($Found == false)
|
---|
169 | {
|
---|
170 | $_SESSION['OrderCol'] = $this->DefaultColumn;
|
---|
171 | $_SESSION['OrderDir'] = $this->DefaultOrder;
|
---|
172 | }
|
---|
173 | // Check OrderDir
|
---|
174 | if (($_SESSION['OrderDir'] != 0) and ($_SESSION['OrderDir'] != 1))
|
---|
175 | $_SESSION['OrderDir'] = 0;
|
---|
176 |
|
---|
177 | $Result = '';
|
---|
178 | $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);
|
---|
179 | foreach ($this->Columns as $Index => $Column)
|
---|
180 | {
|
---|
181 | $QueryItems['OrderCol'] = $Column->Name;
|
---|
182 | $QueryItems['OrderDir'] = 1 - $_SESSION['OrderDir'];
|
---|
183 | if ($Column->Name == $_SESSION['OrderCol'])
|
---|
184 | $ArrowImage = '<img style="vertical-align: middle; border: 0px;" src="'.$this->OrderArrowImage[$_SESSION['OrderDir']].'" alt="order arrow">';
|
---|
185 | else $ArrowImage = '';
|
---|
186 | if ($Column->Name == '') $Result .= '<th>'.$Column->Title.'</th>';
|
---|
187 | else $Result .= '<th><a href="?'.SetQueryStringArray($QueryItems).'">'.$Column->Title.$ArrowImage.'</a></th>';
|
---|
188 | }
|
---|
189 | $this->OrderSQL = ' ORDER BY `'.$_SESSION['OrderCol'].'` '.$this->OrderDirSQL[$_SESSION['OrderDir']];
|
---|
190 | $this->OrderColumn = $_SESSION['OrderCol'];
|
---|
191 | $this->OrderDirection = $_SESSION['OrderDir'];
|
---|
192 |
|
---|
193 | return '<tr>'.$Result.'</tr>';
|
---|
194 | }
|
---|
195 | }
|
---|