1 | <?php
|
---|
2 |
|
---|
3 | include_once('form_classes.php');
|
---|
4 | include_once('Database.php');
|
---|
5 |
|
---|
6 | class Form
|
---|
7 | {
|
---|
8 | var $Definition = array();
|
---|
9 | var $Values = array();
|
---|
10 | var $OnSubmit = '';
|
---|
11 |
|
---|
12 | function __construct($ClassName)
|
---|
13 | {
|
---|
14 | global $FormClasses;
|
---|
15 |
|
---|
16 | $this->Definition = &$FormClasses[$ClassName];
|
---|
17 | }
|
---|
18 |
|
---|
19 | function ShowViewForm()
|
---|
20 | {
|
---|
21 | global $Database, $FormTypes, $System;
|
---|
22 |
|
---|
23 | $Table = array(
|
---|
24 | //'Header' => array('Položka', 'Hodnota'),
|
---|
25 | 'Rows' => array(),
|
---|
26 | );
|
---|
27 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
28 | if(!array_key_exists($Item['Type'], $FormTypes) or
|
---|
29 | (array_key_exists($Item['Type'], $FormTypes) and ($FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
30 | {
|
---|
31 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
32 | if(array_key_exists($Item['Type'], $FormTypes))
|
---|
33 | {
|
---|
34 | if(!array_key_exists($Item['Type'], $System->Type->TypeDefinitionList))
|
---|
35 | $System->Type->RegisterType($Item['Type'], '',
|
---|
36 | $FormTypes[$Item['Type']]);
|
---|
37 | if($FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
38 | $Edit = ''.$System->Type->ExecuteTypeEvent('OneToMany', 'OnView',
|
---|
39 | array('Value' => $this->Values[$Index], 'Name' => $Index,
|
---|
40 | 'Type' => $Item['Type'])).'';
|
---|
41 | else if($FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
42 | $Edit = ''.$System->Type->ExecuteTypeEvent('Enumeration', 'OnView',
|
---|
43 | array('Value' => $this->Values[$Index], 'Name' => $Index,
|
---|
44 | 'Type' => $Item['Type'])).'';
|
---|
45 | } else $Edit = ''.$System->Type->ExecuteTypeEvent($Item['Type'], 'OnView',
|
---|
46 | array('Value' => $this->Values[$Index], 'Name' => $Index)).'';
|
---|
47 | array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
|
---|
48 | }
|
---|
49 | $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
|
---|
50 | '</fieldset>';
|
---|
51 | return($Output);
|
---|
52 | }
|
---|
53 |
|
---|
54 | function ShowEditForm()
|
---|
55 | {
|
---|
56 | if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit';
|
---|
57 | $Output = '<form class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().
|
---|
58 | '<div><input type="submit" value="'.$this->Definition['SubmitText'].'" /> '.
|
---|
59 | '<input type="button" value="Zrušit" onclick="location.href=\'?\'"/></div></form>';
|
---|
60 | return($Output);
|
---|
61 | }
|
---|
62 |
|
---|
63 | function ShowEditBlock($Context = '')
|
---|
64 | {
|
---|
65 | global $Database, $FormTypes;
|
---|
66 |
|
---|
67 | $Table = array(
|
---|
68 | //'Header' => array('Položka', 'Hodnota'),
|
---|
69 | 'Rows' => array(),
|
---|
70 | );
|
---|
71 | if($Context != '') $Context = $Context.'-';
|
---|
72 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
73 | if(!array_key_exists($Item['Type'], $FormTypes) or
|
---|
74 | (array_key_exists($Item['Type'], $FormTypes) and ($FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
75 | {
|
---|
76 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
77 | switch($Item['Type'])
|
---|
78 | {
|
---|
79 | case 'Boolean':
|
---|
80 | if($this->Values[$Index] == 0) $Checked = ''; else $Checked = ' CHECKED';
|
---|
81 | $Edit = '<input type="checkbox" name="'.$Context.$Index.'"'.$Checked.' />';
|
---|
82 | break;
|
---|
83 | case 'String':
|
---|
84 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
85 | break;
|
---|
86 | case 'Text':
|
---|
87 | $Edit = '<textarea style="width: 98%; height: 200px;" name="'.$Context.$Index.'">'.$this->Values[$Index].'</textarea>';
|
---|
88 | break;
|
---|
89 | case 'Password':
|
---|
90 | $Edit = '<input style="width: 98%;" type="password" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
91 | break;
|
---|
92 | case 'Integer':
|
---|
93 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
94 | break;
|
---|
95 | case 'Float':
|
---|
96 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
97 | break;
|
---|
98 | case 'Time':
|
---|
99 | if($this->Values[$Index] == 'Now') $this->Values[$Index] = date('G:i:s');
|
---|
100 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
101 | break;
|
---|
102 | case 'Date':
|
---|
103 | if($this->Values[$Index] == 'Now') $this->Values[$Index] = date('j.n.Y');
|
---|
104 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
105 | break;
|
---|
106 | case 'DateTime':
|
---|
107 | if($this->Values[$Index] == 'Now') $this->Values[$Index] = date('j.n.Y G:i:s');
|
---|
108 | $Edit = '<input style="width: 98%;" type="text" name="'.$Context.$Index.'" value="'.$this->Values[$Index].'" />';
|
---|
109 | break;
|
---|
110 | case 'Array':
|
---|
111 | $Form = new Form($Item['ItemClass']);
|
---|
112 | $Edit = '<script type="text/javascript" id="syndication">'.
|
---|
113 | "var Count = 0;".
|
---|
114 | "function AddItem() {".
|
---|
115 | "Count = Count + 1;".
|
---|
116 | "var newcontent = document.createElement('div');".
|
---|
117 | "newcontent.id = '".$Context.$Item['ItemClass']."-' + Count;".
|
---|
118 | "newcontent.innerHTML = '<input type=\"hidden\" name=\"".$Context.$Item['ItemClass']."-' + Count + '\">".$Form->ShowEditBlock($Context.$Item['ItemClass']."-' + Count + '")."';".
|
---|
119 | "var scr = document.getElementById('syndication');".
|
---|
120 | "scr.parentNode.insertBefore(newcontent, scr); }".
|
---|
121 | '</script>';
|
---|
122 | $Edit .= '<form><input type="button" onclick="AddItem();" value="Přidat položku" /></form>';
|
---|
123 | break;
|
---|
124 | default:
|
---|
125 | if(array_key_exists($Item['Type'], $FormTypes))
|
---|
126 | {
|
---|
127 | // Custom types
|
---|
128 | switch($FormTypes[$Item['Type']]['Type'])
|
---|
129 | {
|
---|
130 | case 'Enumeration':
|
---|
131 | $Edit = '<select style="width: 100%;" name="'.$Context.$Index.'">';
|
---|
132 | foreach($FormTypes[$Item['Type']]['States'] as $StateIndex => $StateName)
|
---|
133 | {
|
---|
134 | if($this->Values[$Index] == $StateIndex) $Selected = 'selected="selected"';
|
---|
135 | else $Selected = '';
|
---|
136 | $Edit .= '<option value="'.$StateIndex.'"'.$Selected.'>'.$StateName.'</option>';
|
---|
137 | }
|
---|
138 | $Edit .= '</select>';
|
---|
139 | break;
|
---|
140 | case 'Reference':
|
---|
141 | $Edit = '<select style="width: 100%;" name="'.$Context.$Index.'">';
|
---|
142 | $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');
|
---|
143 | while($Row = $DbResult->fetch_assoc())
|
---|
144 | {
|
---|
145 | if($Row['Id'] == $this->Values[$Index]) $Selected = ' selected="selected"';
|
---|
146 | else $Selected = '';
|
---|
147 | $Edit .= '<option value="'.$Row['Id'].'"'.$Selected.'>'.$Row['Id'].': '.$Row['Name'].'</option>';
|
---|
148 | }
|
---|
149 | $Edit .= '</select>';
|
---|
150 | break;
|
---|
151 | default:
|
---|
152 | $Edit = 'Neznámý typ';
|
---|
153 | }
|
---|
154 | } else $Edit = 'Neznámý typ';
|
---|
155 | }
|
---|
156 | array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
|
---|
157 | }
|
---|
158 | $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
|
---|
159 | '</fieldset>';
|
---|
160 | return($Output);
|
---|
161 | }
|
---|
162 |
|
---|
163 | function LoadValuesFromDatabase($Id)
|
---|
164 | {
|
---|
165 | global $Database, $FormTypes;
|
---|
166 |
|
---|
167 | $DbResult = $Database->query('SELECT T.* FROM '.$this->Definition['Table'].' AS T WHERE T.Id='.$Id);
|
---|
168 | $DbRow = $DbResult->fetch_array();
|
---|
169 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
170 | if(!array_key_exists($Item['Type'], $FormTypes) or
|
---|
171 | (array_key_exists($Item['Type'], $FormTypes) and ($FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
172 | {
|
---|
173 | $this->Values[$Index] = $DbRow[$Index];
|
---|
174 | switch($Item['Type'])
|
---|
175 | {
|
---|
176 | case 'Password':
|
---|
177 | if($Item['Type'] == 'Password') $this->Values[$Index] = ''; // Dont show password
|
---|
178 | break;
|
---|
179 | case 'Time':
|
---|
180 | $this->Values[$Index] == MysqlDateTimeToTime($this->Values[$Index]);
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | function SaveValuesToDatabase($Id)
|
---|
187 | {
|
---|
188 | global $Database;
|
---|
189 |
|
---|
190 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
191 | {
|
---|
192 | switch($Item['Type'])
|
---|
193 | {
|
---|
194 | case 'Password':
|
---|
195 | if($this->Values[$Index] == '') unset($this->Values[$Index]); // Do not modify empty passwords
|
---|
196 | else $this->Values[$Index] = sha1($this->Values[$Index]);
|
---|
197 | break;
|
---|
198 | case 'Time':
|
---|
199 | $this->Values[$Index] = TimeToMysqlDateTime($this->Values[$Index]);
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | if($Id == 0)
|
---|
204 | {
|
---|
205 | $this->Values['Id'] = $Id;
|
---|
206 | $DbResult = $Database->insert($this->Definition['Table'], $this->Values);
|
---|
207 | } else
|
---|
208 | $DbResult = $Database->update($this->Definition['Table'], 'Id='.$Id, $this->Values);
|
---|
209 | //echo($Database->LastQuery);
|
---|
210 | }
|
---|
211 |
|
---|
212 | function LoadValuesFromForm()
|
---|
213 | {
|
---|
214 | $this->Values = $this->LoadValuesFromFormBlock();
|
---|
215 | }
|
---|
216 |
|
---|
217 | function LoadValuesFromFormBlock($Context = '')
|
---|
218 | {
|
---|
219 | global $FormTypes;
|
---|
220 |
|
---|
221 | if($Context != '') $Context = $Context.'-';
|
---|
222 | $Values = array();
|
---|
223 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
224 | {
|
---|
225 | if(array_key_exists($Context.$Index, $_POST))
|
---|
226 | switch($Item['Type'])
|
---|
227 | {
|
---|
228 | case 'Boolean':
|
---|
229 | if(array_key_exists($Context.$Index, $_POST)) $Values[$Index] = 1;
|
---|
230 | else $Values[$Index] = 0;
|
---|
231 | break;
|
---|
232 | case 'String':
|
---|
233 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
234 | break;
|
---|
235 | case 'Text':
|
---|
236 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
237 | break;
|
---|
238 | case 'Password':
|
---|
239 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
240 | break;
|
---|
241 | case 'Integer':
|
---|
242 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
243 | break;
|
---|
244 | case 'Float':
|
---|
245 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
246 | break;
|
---|
247 | case 'Time':
|
---|
248 | $Values[$Index] = explode('.', $_POST[$Context.$Index]);
|
---|
249 | $Values[$Index] = mktime(0, 0, 0, $Values[$Index][1], $Values[$Index][0], $Values[$Index][2]);
|
---|
250 | break;
|
---|
251 | case 'Array':
|
---|
252 | $I = 1;
|
---|
253 | //echo('Expect: '.$Context.$Item['ItemClass'].'-'.$I.'<br />');
|
---|
254 | while(isset($_POST[$Context.$Item['ItemClass'].'-'.$I]))
|
---|
255 | {
|
---|
256 | $Form = new Form($Item['ItemClass']);
|
---|
257 | $Values[$Index][] = $Form->LoadValuesFromFormBlock($Context.$Item['ItemClass'].'-'.$I);
|
---|
258 | $I++;
|
---|
259 | }
|
---|
260 | break;
|
---|
261 | default:
|
---|
262 | if(array_key_exists($Item['Type'], $FormTypes))
|
---|
263 | {
|
---|
264 | // Custom types
|
---|
265 | switch($FormTypes[$Item['Type']]['Type'])
|
---|
266 | {
|
---|
267 | case 'Enumeration':
|
---|
268 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
269 | break;
|
---|
270 | case 'Reference':
|
---|
271 | $Values[$Index] = $_POST[$Context.$Index];
|
---|
272 | break;
|
---|
273 | default:
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 | return($Values);
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | function MakeLink($Target, $Title)
|
---|
283 | {
|
---|
284 | return('<a href="'.$Target.'">'.$Title.'</a>');
|
---|
285 | }
|
---|
286 |
|
---|
287 | function Table($Table)
|
---|
288 | {
|
---|
289 | $Result = '<table class="BasicTable">';
|
---|
290 | if(array_key_exists('Header', $Table))
|
---|
291 | {
|
---|
292 | $Result .= '<tr>';
|
---|
293 | foreach($Table['Header'] as $Item)
|
---|
294 | $Result .= '<th>'.$Item.'</th>';
|
---|
295 | $Result .= '</tr>';
|
---|
296 | }
|
---|
297 | foreach($Table['Rows'] as $Row)
|
---|
298 | {
|
---|
299 | $Result .= '<tr>';
|
---|
300 | foreach($Row as $Index => $Item)
|
---|
301 | {
|
---|
302 | if($Index == 0) $Class = ' class="Header"'; else $Class = '';
|
---|
303 | $Result .= '<td'.$Class.' style="width: '.(floor(100 / count($Row))).'%">'.$Item.'</td>';
|
---|
304 | }
|
---|
305 | $Result .= '</tr>';
|
---|
306 | }
|
---|
307 | $Result .= '</table>';
|
---|
308 | return($Result);
|
---|
309 | }
|
---|
310 |
|
---|
311 | function ShowEditTable($ClassName, $Values)
|
---|
312 | {
|
---|
313 | }
|
---|
314 |
|
---|
315 | ?>
|
---|