source: trunk/Global.php

Last change on this file was 9, checked in by chronos, 19 months ago
  • Fixed: Modules initialization.
  • Property svn:executable set to *
File size: 3.2 KB
Line 
1<?php
2
3function HumanDate(?string $Time): string
4{
5 return date('j.n.Y', $Time);
6}
7
8function GetMicrotime(): float
9{
10 list($Usec, $Sec) = explode(' ', microtime());
11 return (float)$Usec + (float)$Sec;
12}
13
14function MakeLink(string $Target, string $Title): string
15{
16 return '<a href="'.$Target.'">'.$Title.'</a>';
17}
18
19function Table(array $Table): string
20{
21 $Result = '<table class="BasicTable">';
22 if (array_key_exists('Header', $Table))
23 {
24 $Result .= '<tr>';
25 foreach ($Table['Header'] as $Item)
26 $Result .= '<th>'.$Item.'</th>';
27 $Result .= '</tr>';
28 }
29 foreach ($Table['Rows'] as $Row)
30 {
31 $Result .= '<tr>';
32 foreach ($Row as $Index => $Item)
33 {
34 if ($Index == 0) $Class = ' class="Header"'; else $Class = '';
35 $Result .= '<td'.$Class.' style="width: '.(floor(100 / count($Row))).'%">'.$Item.'</td>';
36 }
37 $Result .= '</tr>';
38 }
39 $Result .= '</table>';
40 return $Result;
41}
42
43function ShowEditTable(string $ClassName, array $Values): string
44{
45 global $Classes, $Types;
46 $Class = $Classes[$ClassName];
47 $Table = array(
48 'Header' => array('Položka', 'Hodnota'),
49 'Rows' => array(),
50 );
51
52 foreach ($Class as $Index => $Item)
53 {
54 if (!array_key_exists($Index, $Values)) $Values[$Index] = $Item['Default'];
55 switch ($Item['Type'])
56 {
57 case 'Boolean':
58 if ($Values[$Index] == 0) $Checked = ''; else $Checked = ' checked="yes"';
59 $Edit = '<input type="checkbox" name="'.$Index.'"'.$Checked.'>';
60 break;
61 case 'String':
62 $Edit = '<input type="text" name="'.$Index.'" value="'.$Values[$Index].'">';
63 break;
64 case 'Integer':
65 $Edit = '<input type="text" name="'.$Index.'" value="'.$Values[$Index].'">';
66 break;
67 default:
68 $Edit = 'Neznámý typ';
69 }
70 array_push($Table['Rows'], array($Item['Caption'], $Edit));
71 }
72 $Output = '<h3>Tabulka '.$ClassName.'</h3>'.Table($Table).MakeLink('?Operation=Add2', 'Přidat');
73 return $Output;
74}
75
76function ProcessURL(): array
77{
78 if (array_key_exists('REDIRECT_QUERY_STRING', $_SERVER))
79 $PathString = $_SERVER['REDIRECT_QUERY_STRING'];
80 else $PathString = '';
81 if (substr($PathString, -1, 1) == '/') $PathString = substr($PathString, 0, -1);
82 $PathItems = explode('/', $PathString);
83 if (array_key_exists('REQUEST_URI', $_SERVER) and (strpos($_SERVER['REQUEST_URI'], '?') !== false))
84 $_SERVER['QUERY_STRING'] = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?') + 1);
85 else $_SERVER['QUERY_STRING'] = '';
86 parse_str($_SERVER['QUERY_STRING'], $_GET);
87 return $PathItems;
88}
89
90function GetQueryStringArray(string $QueryString): array
91{
92 $Result = array();
93 $Parts = explode('&', $QueryString);
94 foreach ($Parts as $Part)
95 {
96 if ($Part != '')
97 {
98 if (!strpos($Part, '=')) $Part .= '=';
99 $Item = explode('=', $Part);
100 $Result[$Item[0]] = $Item[1];
101 }
102 }
103 return $Result;
104}
105
106function SetQueryStringArray(array $QueryStringArray): string
107{
108 $Parts = array();
109 foreach ($QueryStringArray as $Index => $Item)
110 {
111 $Parts[] = $Index.'='.$Item;
112 }
113 return implode('&amp;', $Parts);
114}
115
116function GetRemoteAddress(): string
117{
118 if (array_key_exists('REMOTE_ADDR', $_SERVER)) $IP = $_SERVER['REMOTE_ADDR'];
119 else $IP = '0.0.0.0';
120 return $IP;
121}
Note: See TracBrowser for help on using the repository browser.