source: trunk/View.php

Last change on this file was 64, checked in by chronos, 2 years ago
  • Added: MG Dance import.
File size: 6.8 KB
Line 
1<?php
2
3define('PAGE_NOT_FOUND', 'Stránka nenalezena');
4
5class BaseView extends View
6{
7 public int $TimeStart;
8 public bool $FormatHTML = false;
9 public bool $ShowRuntimeInfo = false;
10 public bool $ShowNavigation = false;
11 public bool $BasicHTML = false;
12 public string $ParentClass = '';
13 public string $Title;
14 public string $Description;
15 public string $Encoding;
16 public string $Style;
17
18 function __construct($System)
19 {
20 parent::__construct($System);
21
22 $this->FormatHTML = false;
23 $this->ShowRuntimeInfo = false;
24 $this->Encoding = 'utf-8';
25 $this->Style = 'default';
26
27 // TODO: Move to external code
28 if (isset($this->System->Config['Web']['FormatHTML']))
29 $this->FormatHTML = $this->System->Config['Web']['FormatHTML'];
30 if (isset($this->System->Config['Web']['ShowRuntimeInfo']))
31 $this->ShowRuntimeInfo = $this->System->Config['Web']['ShowRuntimeInfo'];
32 if (isset($this->System->Config['Web']['Charset']))
33 $this->Encoding = $this->System->Config['Web']['Charset'];
34 if (isset($this->System->Config['Web']['Style']))
35 $this->Style = $this->System->Config['Web']['Style'];
36 }
37
38 function SystemMessage(string $Title, string $Text): string
39 {
40 return '<table align="center"><tr><td><div class="SystemMessage"><h3>'.$Title.'</h3><div>'.$Text.'</div></div></td></tr></table>';
41 //ShowFooter();
42 //die();
43 }
44
45 function ShowNavigation(Page $Page): string
46 {
47 if (array_key_exists('REQUEST_URI', $_SERVER))
48 $ScriptName = $_SERVER['REQUEST_URI'];
49 else $ScriptName = '';
50 while (strpos($ScriptName, '//') !== false)
51 $ScriptName = str_replace('//', '/', $ScriptName);
52 if (strpos($ScriptName, '?') !== false)
53 $ScriptName = substr($ScriptName, 0, strrpos($ScriptName, '?'));
54 $ScriptName = substr($ScriptName, strlen($this->System->Link('')));
55 if (substr($ScriptName, -1, 1) == '/') $ScriptName = substr($ScriptName, 0, -1);
56
57 $Output = '';
58 while ($Page)
59 {
60 $Output = ' &gt; <a href="'.$this->System->Link($ScriptName).'/">'.$Page->Title.'</a>'.$Output;
61
62 if (class_exists($Page->ParentClass))
63 {
64 $PageClass = $Page->ParentClass;
65 $Page = new $PageClass($this->System);
66 $ScriptName = substr($ScriptName, 0, strrpos($ScriptName, '/'));
67 } else $Page = null;
68 }
69 $Output = substr($Output, 6);
70 return $Output;
71 }
72
73 function ShowHeader(Page $Page): string
74 {
75 $Path = $Page->Title;
76
77 $Navigation = $this->ShowNavigation($Page);
78
79 $BodyParam = '';
80 if (isset($Page->Load)) $BodyParam .= ' onload="'.$Page->Load.'"';
81 if (isset($Page->Unload)) $BodyParam .= ' onunload="'.$Page->Unload.'"';
82 $Output = '<?xml version="1.0" encoding="'.$this->Encoding.'"?>'."\n".
83 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.
84 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">'.
85 '<head>'.
86 '<link rel="stylesheet" href="'.$this->System->Link('/style/').$this->Style.'/style.css" type="text/css" media="all" />';
87 $Output .= '<link rel="alternate" title="Taneční seznamka" href="'.
88 $this->System->Link('/seznamka/rss/').'" type="application/rss+xml" />';
89 $Output .= '<meta http-equiv="content-type" content="application/xhtml+xml; charset='.$this->Encoding.'" />'.
90 '<meta name="viewport" content="width=device-width, initial-scale=1">'.
91 '<script type="text/javascript" src="'.$this->System->Link('/style/').$this->Style.'/jquery.js"></script>'.
92 '<title>'.$Path.' - '.$this->System->Config['Web']['Title'].'</title>';
93
94 // Show page headers
95 $Bar = '';
96 foreach ($this->System->PageHeaders as $Item)
97 $Output .= call_user_func($Item);
98
99 $Output .= '</head><body'.$BodyParam.'>';
100 $Output .= Core::Cast($this->System)->ShowMenu();
101 if ($this->BasicHTML == false)
102 {
103 //$Output .= '<div class="MainTitle">'.$Title.'</div>';
104 if ($this->ShowNavigation)
105 $Output .= '<div class="MainTitle"><span class="MenuItem"><strong>Navigace :: </strong> '.$Navigation.'</span><div class="MenuItem2">';
106 $Bar = '';
107 foreach ($this->System->Bars['Top'] as $BarItem)
108 $Bar .= call_user_func($BarItem);
109 if (trim($Bar) != '') $Output .= $Bar;
110 else $Output .= '&nbsp;';
111 $Output .= '</div></div>';
112 }
113 return $Output;
114 }
115
116 function ShowFooter(): string
117 {
118 global $ScriptTimeStart, $Revision, $ReleaseTime;
119
120 $Time = round(GetMicrotime() - $ScriptTimeStart, 2);
121 $Output = '';
122 if ($this->BasicHTML == false)
123 {
124 $Output .= '<div class="Footer">'.
125 '<i>';
126 $Output .= ' Projekt <a href="https://app.zdechov.net/tanec/">Tanec</a> | ';
127 $Output .= ' Správce: <a href="mailto:'.$this->System->Config['Web']['AdminEmail'].'">'.$this->System->Config['Web']['AdminEmail'].'</a> ';
128
129 //$Output .= ' Verze: '.$Revision.' ('.HumanDate($ReleaseTime).') ';
130 if ($this->ShowRuntimeInfo == true)
131 {
132 $Output .= ' Doba generování: '.$Time.' s / '.ini_get('max_execution_time').' s '.
133 ' Použitá paměť: '.HumanSize(memory_get_peak_usage(FALSE)).' / '.ini_get('memory_limit').'B ';
134 }
135 $Output .= '</i></div>';
136 }
137 $Output .= '</body></html>';
138 return $Output;
139 }
140
141 function GetOutput(Page $Page): string
142 {
143 $Page->OnSystemMessage = array($this->System->BaseView, 'SystemMessage');
144
145 $Output = $Page->Show();
146 if ($Page->RawPage == false)
147 {
148 $Output = $this->ShowHeader($Page).$Output.$this->ShowFooter();
149 if ($this->FormatHTML == true) $Output = $this->FormatOutput($Output);
150 }
151 return $Output;
152 }
153
154 function NewPage(string $ClassName): Page
155 {
156 $Page = new $ClassName();
157 $Page->System = $this->System;
158 $Page->Database = $this->Database;
159 $Page->FormatHTML = $this->FormatHTML;
160 return $Page;
161 }
162
163 // XML formating function
164 function FormatOutput(string $s): string
165 {
166 $out = '';
167 $nn = 0;
168 $n = 0;
169 while ($s != '')
170 {
171 $start = strpos($s, '<');
172 $end = strpos($s, '>');
173 if ($start != 0)
174 {
175 $end = $start - 1;
176 $start = 0;
177 }
178 $line = trim(substr($s, $start, $end + 1));
179 if (strlen($line) > 0)
180 if ($line[0] == '<')
181 {
182 if ($s[$start + 1] == '/')
183 {
184 $n = $n - 2;
185 $nn = $n;
186 } else
187 {
188 if (strpos($line, ' ')) $cmd = substr($line, 1, strpos($line, ' ') - 1);
189 else $cmd = substr($line, 1, strlen($line) - 2);
190 //echo('['.$cmd.']');
191 if (strpos($s, '</'.$cmd.'>')) $n = $n + 2;
192 }
193 }// else $line = '['.$line.']';
194 //if ($line != '') echo(htmlspecialchars(str_repeat(' ',$nn).$line."\n"));
195 if ($line != '') $out .= (str_repeat(' ', $nn).$line."\n");
196 $s = substr($s, $end + 1, strlen($s));
197 $nn = $n;
198 }
199 return $out;
200 }
201}
Note: See TracBrowser for help on using the repository browser.