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