source: trunk/forms.php@ 176

Last change on this file since 176 was 148, checked in by george, 16 years ago
  • Upraveno: Přepracován systém generování zobrazení výstupu. Pro nový systím přepsáno mnoho stránek.
File size: 7.9 KB
Line 
1<?php
2
3include_once('form_classes.php');
4include_once('database.php');
5
6class Form
7{
8 var $Definition = array();
9 var $Values = array();
10 var $OnSubmit = '';
11
12 function Form($ClassName)
13 {
14 global $FormClasses;
15
16 $this->Definition = &$FormClasses[$ClassName];
17 }
18
19 function ShowEditForm()
20 {
21 $Output = '<form action="'.$this->OnSubmit.'" method="post"><center>'.$this->ShowEditBlock().'<input type="submit" value="Uložit"></center></form>';
22 return($Output);
23 }
24
25 function ShowEditBlock($Context = '')
26 {
27 global $Database, $FormTypes;
28
29 $Table = array(
30 //'Header' => array('Položka', 'Hodnota'),
31 'Rows' => array(),
32 );
33 if($Context != '') $Context = $Context.'-';
34 foreach($this->Definition['Items'] as $Index => $Item)
35 {
36 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
37 switch($Item['Type'])
38 {
39 case 'Boolean':
40 if($this->Values[$Index] == 0) $Checked = ''; else $Checked = ' CHECKED';
41 $Edit = '<input type="checkbox" name="'.$Context.$Index.'"'.$Checked.'>';
42 break;
43 case 'String':
44 $Edit = '<input type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'">';
45 break;
46 case 'Password':
47 $Edit = '<input type="password" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'">';
48 break;
49 case 'Integer':
50 $Edit = '<input type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'">';
51 break;
52 case 'Float':
53 $Edit = '<input type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'">';
54 break;
55 case 'Time':
56 if($this->Values[$Index] == 'Now') $this->Values[$Index] = date('j.n.Y');
57 $Edit = '<input type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'">';
58 break;
59 case 'Array':
60 $Form = new Form($Item['ItemClass']);
61 $Edit = '<script type="text/javascript" id="syndication">'.
62 "var Count = 0;".
63 "function AddItem() {".
64 "Count = Count + 1;".
65 "var newcontent = document.createElement('div');".
66 "newcontent.id = '".$Context.$Item['ItemClass']."-' + Count;".
67 "newcontent.innerHTML = '<input type=\"hidden\" name=\"".$Context.$Item['ItemClass']."-' + Count + '\">".$Form->ShowEditBlock($Context.$Item['ItemClass']."-' + Count + '")."';".
68 "var scr = document.getElementById('syndication');".
69 "scr.parentNode.insertBefore(newcontent, scr); }".
70 '</script>';
71 $Edit .= '<form><input type="button" onclick="AddItem();" value="Přidat položku"></form>';
72 break;
73 default:
74 if(array_key_exists($Item['Type'], $FormTypes))
75 {
76 // Custom types
77 switch($FormTypes[$Item['Type']]['Type'])
78 {
79 case 'Enumeration':
80 $Edit = '<select name="'.$Context.$Index.'">';
81 foreach($FormTypes[$Item['Type']]['States'] as $StateIndex => $StateName)
82 $Edit .= '<option value="'.$StateIndex.'">'.$StateName.'</option>';
83 $Edit .= '</select>';
84 break;
85 case 'Reference':
86 $Edit = '<select name="'.$Context.$Index.'">';
87 $DbResult = $Database->select($FormTypes[$Item['Type']]['Table'], $FormTypes[$Item['Type']]['Id'].' as Id, '.$FormTypes[$Item['Type']]['Name'].' as Name', $FormTypes[$Item['Type']]['Filter'].' ORDER BY Name');
88 while($Row = $DbResult->fetch_array())
89 $Edit .= '<option value="'.$Row['Id'].'">'.$Row['Name'].'</option>';
90 $Edit .= '</select>';
91 break;
92 default:
93 $Edit = 'Neznámý typ';
94 }
95 } else $Edit = 'Neznámý typ';
96 }
97 array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
98 }
99 $Output = '<fieldset style="width: 500px;"><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
100 '</fieldset>';
101 return($Output);
102 }
103
104 function LoadValuesFromDatabase($Id)
105 {
106 global $Database;
107
108 $DbResult = $Database->query('SELECT * FROM '.$this->Definition['Table'].' WHERE Id='.$Id);
109 $DbRow = $DbResult->fetch_array();
110 foreach($this->Definition['Items'] as $Index => $Item)
111 {
112 $this->Values[$Index] = $DbRow[$Index];
113 switch($Item['Type'])
114 {
115 case 'Password':
116 if($Item['Type'] == 'Password') $this->Values[$Index] = ''; // Dont show password
117 break;
118 case 'Time':
119 $this->Values[$Index] == MysqlDateTimeToTime($this->Values[$Index]);
120 break;
121 }
122 }
123 }
124
125 function SaveValuesToDatabase($Id)
126 {
127 global $Database;
128
129 foreach($this->Definition['Items'] as $Index => $Item)
130 {
131 switch($Item['Type'])
132 {
133 case 'Password':
134 if($this->Values[$Index] == '') unset($this->Values[$Index]); // Do not modify empty passwords
135 else $this->Values[$Index] = sha1($this->Values[$Index]);
136 break;
137 case 'Time':
138 $this->Values[$Index] = TimeToMysqlDateTime($this->Values[$Index]);
139 break;
140 }
141 }
142 $this->Values['Id'] = $Id;
143 $DbResult = $Database->replace($this->Definition['Table'], $this->Values);
144 //echo($Database->LastQuery);
145 }
146
147 function LoadValuesFromForm()
148 {
149 $this->Values = $this->LoadValuesFromFormBlock();
150 }
151
152 function LoadValuesFromFormBlock($Context = '')
153 {
154 global $FormTypes;
155
156 if($Context != '') $Context = $Context.'-';
157 $Values = array();
158 foreach($this->Definition['Items'] as $Index => $Item)
159 {
160 switch($Item['Type'])
161 {
162 case 'Boolean':
163 if(array_key_exists($Context.$Index, $_POST)) $Values[$Index] = 1;
164 else $Values[$Index] = 0;
165 break;
166 case 'String':
167 $Values[$Index] = $_POST[$Context.$Index];
168 break;
169 case 'Password':
170 $Values[$Index] = $_POST[$Context.$Index];
171 break;
172 case 'Integer':
173 $Values[$Index] = $_POST[$Context.$Index];
174 break;
175 case 'Float':
176 $Values[$Index] = $_POST[$Context.$Index];
177 break;
178 case 'Time':
179 $Values[$Index] = explode('.', $_POST[$Context.$Index]);
180 $Values[$Index] = mktime(0, 0, 0, $Values[$Index][1], $Values[$Index][0], $Values[$Index][2]);
181 break;
182 case 'Array':
183 $I = 1;
184 //echo('Expect: '.$Context.$Item['ItemClass'].'-'.$I.'<br>');
185 while(isset($_POST[$Context.$Item['ItemClass'].'-'.$I]))
186 {
187 $Form = new Form($Item['ItemClass']);
188 $Values[$Index][] = $Form->LoadValuesFromFormBlock($Context.$Item['ItemClass'].'-'.$I);
189 $I++;
190 }
191 break;
192 default:
193 if(array_key_exists($Item['Type'], $FormTypes))
194 {
195 // Custom types
196 switch($FormTypes[$Item['Type']]['Type'])
197 {
198 case 'Enumeration':
199 $Values[$Index] = $_POST[$Context.$Index];
200 break;
201 case 'Reference':
202 $Values[$Index] = $_POST[$Context.$Index];
203 break;
204 default:
205 }
206 }
207 }
208 }
209 return($Values);
210 }
211}
212
213function MakeLink($Target, $Title)
214{
215 return('<a href="'.$Target.'">'.$Title.'</a>');
216}
217
218function Table($Table)
219{
220 $Result = '<table class="BasicTable">';
221 $Result .= '<tr>';
222 if(array_key_exists('Header', $Table))
223 {
224 foreach($Table['Header'] as $Item)
225 $Result .= '<th>'.$Item.'</th>';
226 $Result .= '</tr>';
227 }
228 foreach($Table['Rows'] as $Row)
229 {
230 $Result .= '<tr>';
231 foreach($Row as $Index => $Item)
232 {
233 if($Index == 0) $Class = ' class="Header"'; else $Class = '';
234 $Result .= '<td'.$Class.'>'.$Item.'</td>';
235 }
236 $Result .= '</tr>';
237 }
238 $Result .= '</table>';
239 return($Result);
240}
241
242function ShowEditTable($ClassName, $Values)
243{
244}
245
246?>
Note: See TracBrowser for help on using the repository browser.