source: trunk/Common/VCL/General.php

Last change on this file was 929, checked in by chronos, 3 years ago
  • Modified: Removed commended out print_r and echo commands used only for debugging.
File size: 6.4 KB
Line 
1<?php
2
3function ReadSessionVar(string $Name, bool $Persistent = true): string
4{
5 $Result = '';
6 if ($Persistent)
7 if (array_key_exists($Name, $_SESSION)) $Result = $_SESSION[$Name];
8 if (array_key_exists($Name, $_GET))
9 {
10 $Result = $_GET[$Name];
11 if ($Persistent) $_SESSION[$Name] = $Result;
12 }
13 return $Result;
14}
15
16class Element
17{
18 public string $Id;
19 public bool $Enabled;
20 public bool $Visible;
21
22 function __construct()
23 {
24 $this->Visible = true;
25 $this->Enabled = true;
26 }
27
28 function Show(): string
29 {
30 $Output = '';
31 //$Output .= '#'.$this->Id;
32 return $Output;
33 }
34
35 function Prepare(): void
36 {
37 }
38}
39
40class Layout extends Element
41{
42 public array $Items;
43
44 function Show(): string
45 {
46 if ($this->Visible)
47 {
48 $Output = parent::Show();
49 foreach ($this->Items as $Item)
50 $Output .= $Item->Show();
51 return $Output;
52 }
53 }
54
55 function Prepare(): void
56 {
57 foreach ($this->Items as $Item)
58 $Item->Prepare();
59 }
60}
61
62class Button extends Element
63{
64 public string $Caption;
65
66 function Show(): string
67 {
68 if ($this->Visible)
69 {
70 return parent::Show().'<button>'.$this->Caption.'</button>';
71 }
72 }
73}
74
75class Edit extends Element
76{
77 public string $Text;
78
79 function Show(): string
80 {
81 return parent::Show().'<input type="text" name="'.$this->Id.'" value="'.$this->Text.'"/>';
82 }
83
84 function Prepare(): void
85 {
86 $this->Text = ReadSessionVar($this->Id.'_Text');
87 }
88}
89
90class PageSelect extends Element
91{
92 public int $Count;
93 public int $Position;
94 public int $PageSize;
95
96 function __construct()
97 {
98 parent::__construct();
99 $this->PageSize = 10;
100 $this->Position = 0;
101 $this->Count = 0;
102 }
103
104 function Show(): string
105 {
106 if (array_key_exists($this->Id.'_Page', $_GET))
107 $this->Position = $_GET[$this->Id.'_Page'];
108 if ($this->Position >= $this->Count) $this->Position = $this->Count / $this->PageSize - 1;
109 $Output = '';
110 for ($I = 0; $I < $this->Count / $this->PageSize; $I++)
111 {
112 $Text = '<a href="?'.$this->Id.'_Page='.$I.'">'.$I.'</a> ';
113 if ($I == $this->Position) $Text = '<strong>'.$Text.'</strong>';
114 $Output .= $Text;
115 }
116 $Output .= '<br/>';
117 return $Output;
118 }
119
120 function Prepare(): void
121 {
122 $this->Position = ReadSessionVar($this->Id.'_Page');
123 }
124}
125
126class ListViewColumn extends Element
127{
128 public string $Name;
129
130 function Show(): string
131 {
132 return $this->Name;
133 }
134}
135
136class ListView extends Element
137{
138 public array $Columns;
139 public array $Rows;
140 public $OnColumnClick;
141 public $OnColumnDraw;
142
143 function __construct()
144 {
145 parent::__construct();
146 $this->Rows = array();
147 $this->Columns = array();
148 }
149
150 function Show(): string
151 {
152 $Output = '<table class="WideTable" style="font-size: small;><tr>';
153 foreach ($this->Columns as $Column)
154 {
155 if ($this->OnColumnDraw != '') $Text = call_user_func($this->OnColumnDraw, $Column);
156 else $Text = $Column->Show();
157 $Output .= '<th>'.$Text.'</th>';
158 }
159 $Output .= '</tr>';
160 foreach ($this->Rows as $Row)
161 {
162 $Output .= '<tr>';
163 foreach ($Row as $Value)
164 $Output .= '<td>'.$Value.'</td>';
165 $Output .= '</tr>';
166 }
167 $Output .= '</table>';
168 return $Output;
169 }
170}
171
172class PageListView extends ListView
173{
174 public PageSelect $PageSelect;
175 public int $SortOrder;
176 public string $SortColumn;
177 public array $OrderArrowImage;
178
179 function __construct()
180 {
181 parent::__construct();
182 $this->PageSelect = new PageSelect();
183 $this->OrderArrowImage = array('sort_asc.png', 'sort_desc.png');
184 $this->SortOrder = 0;
185 $this->SortColumn = '';
186 }
187
188 function ColumnClick(ListViewColumn $Column): string
189 {
190 return '?'.$this->Id.'_SortColumn='.$Column->Id.'&amp;'.$this->Id.'_SortOrder='.(1 - $this->SortOrder);
191 }
192
193 function ColumnDraw(ListViewColumn $Column): string
194 {
195 global $System;
196
197 $Output = $Column->Show();
198
199 if ($Column->Name == $this->SortColumn)
200 {
201 $Output .= '<img style="vertical-align: middle; border: 0px;" src="'.
202 $System->Link('/images/'.$this->OrderArrowImage[$this->SortOrder]).'" alt="order arrow">';
203 }
204 if ($this->OnColumnClick != '')
205 $Output = '<a href="'.call_user_func($this->OnColumnClick, $Column).'">'.$Output.'</a>';
206 return $Output;
207 }
208
209 function Show(): string
210 {
211 $this->PageSelect->Count = count($this->Rows);
212 $this->OnColumnClick = array($this, 'ColumnClick');
213 $this->OnColumnDraw = array($this, 'ColumnDraw');
214 $Output = $this->PageSelect->Show();
215 $Output .= parent::Show();
216 $Output .= $this->PageSelect->Show();
217 return $Output;
218 }
219
220 function Prepare(): void
221 {
222 $this->PageSelect->Id = $this->Id.'PageSelect';
223 $this->PageSelect->Prepare();
224 $this->SortColumn = ReadSessionVar($this->Id.'_SortColumn');
225 $this->SortOrder = ReadSessionVar($this->Id.'_SortOrder');
226 }
227}
228
229class TableLayout extends Element
230{
231 public array $Rows;
232 public string $Span;
233
234 function Show(): string
235 {
236 $Output = '<table>';
237 foreach ($this->Rows as $RowNum => $Row)
238 {
239 $Output .= '<tr>';
240 foreach ($Row as $ColNum => $Value)
241 {
242 if ($this->Span[$RowNum][$ColNum] != 1) $Span = ' colspan="'.$this->Span[$RowNum][$ColNum].'"';
243 else $Span = '';
244 $Output .= '<td'.$Span.'>'.$Value->Show().'</td>';
245 }
246 $Output .= '</tr>';
247 }
248 $Output .= '</table>';
249 return $Output;
250 }
251
252 function Prepare(): void
253 {
254 foreach ($this->Rows as $RowNum => $Row)
255 {
256 foreach ($Row as $ColNum => $Value)
257 {
258 $Value->Prepare();
259 }
260 }
261 }
262}
263
264class Label extends Element
265{
266 public string $Caption;
267 public $OnExecute;
268
269 function Show(): string
270 {
271 $Output = $this->Caption;
272 if (method_exists($this->OnExecute[0], $this->OnExecute[1]))
273 {
274 $Link = 'O='.$this->Id.'&amp;M=Execute';
275 $Output = '<a href="?'.$Link.'">'.$Output.'</a>';
276 };
277 return $Output;
278 }
279
280 function Prepare(): void
281 {
282 $Object = ReadSessionVar('O', false);
283 $Method = ReadSessionVar('M', false);
284 if (($Object == $this->Id) and ($Method == 'Execute'))
285 call_user_func($this->OnExecute, $this);
286 }
287}
288
289class Page2
290{
291 public array $Items;
292
293 function Show(): string
294 {
295 $Output = '<!DOCTYPE html><html><head></head><body>';
296 foreach ($this->Items as $Item)
297 $Output .= $Item->Show();
298 $Output .= '</body></html>';
299 return $Output;
300 }
301
302 function Prepare(): void
303 {
304 foreach ($this->Items as $Item)
305 $Item->Prepare();
306 }
307}
Note: See TracBrowser for help on using the repository browser.