source: web/Packages/Common/Table.php

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