1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/../Database.php');
|
---|
4 | include_once(dirname(__FILE__).'/Types/Type.php');
|
---|
5 |
|
---|
6 | class Form
|
---|
7 | {
|
---|
8 | var $FormManager;
|
---|
9 | var $Definition;
|
---|
10 | var $Values;
|
---|
11 | var $ValuesValidate;
|
---|
12 | var $ValuesFilter;
|
---|
13 | var $OnSubmit;
|
---|
14 | var $Database;
|
---|
15 |
|
---|
16 | function __construct($FormManager)
|
---|
17 | {
|
---|
18 | $this->FormManager = &$FormManager;
|
---|
19 | $this->Database = $FormManager->Database;
|
---|
20 | $this->Definition = array();
|
---|
21 | $this->Values = array();
|
---|
22 | $this->ValuesFilter = array();
|
---|
23 | $this->ValuesValidate = array();
|
---|
24 | $this->OnSubmit = '';
|
---|
25 | }
|
---|
26 |
|
---|
27 | function SetClass($Name)
|
---|
28 | {
|
---|
29 | $this->Definition = &$this->FormManager->Classes[$Name];
|
---|
30 | }
|
---|
31 |
|
---|
32 | function ShowViewForm()
|
---|
33 | {
|
---|
34 | $Table = array(
|
---|
35 | //'Header' => array('Položka', 'Hodnota'),
|
---|
36 | 'Rows' => array(),
|
---|
37 | );
|
---|
38 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
39 | if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
40 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
41 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
42 | {
|
---|
43 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
44 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
45 | {
|
---|
46 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
47 | $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]);
|
---|
48 | if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
49 | $UseType = 'OneToMany';
|
---|
50 | else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
51 | $UseType = 'Enumeration';
|
---|
52 | } else $UseType = $Item['Type'];
|
---|
53 | $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnView',
|
---|
54 | array('Value' => $this->Values[$Index], 'Name' => $Index,
|
---|
55 | 'Type' => $Item['Type'], 'Values' => $this->Values,
|
---|
56 | 'Filter' => $this->ValuesFilter[$Index]));
|
---|
57 | if(array_key_exists('Suffix', $Item)) $Edit .= ' '.$Item['Suffix'];
|
---|
58 | if(!$this->FormManager->Type->IsHidden($UseType))
|
---|
59 | array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
|
---|
60 | }
|
---|
61 | $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
|
---|
62 | '</fieldset>';
|
---|
63 | return($Output);
|
---|
64 | }
|
---|
65 |
|
---|
66 | function ShowEditForm()
|
---|
67 | {
|
---|
68 | if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit';
|
---|
69 | $Output = '<form enctype="multipart/form-data" class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().
|
---|
70 | '<div><input type="submit" value="'.$this->Definition['SubmitText'].'" /> '.
|
---|
71 | '<input type="button" value="Zrušit" onclick="location.href=\'?\'"/></div></form>';
|
---|
72 | return($Output);
|
---|
73 | }
|
---|
74 |
|
---|
75 | function ShowEditBlock($Context = '')
|
---|
76 | {
|
---|
77 | $Hidden = '';
|
---|
78 | $IsHidden = false;
|
---|
79 | $Table = array(
|
---|
80 | //'Header' => array('Položka', 'Hodnota'),
|
---|
81 | 'Rows' => array(),
|
---|
82 | );
|
---|
83 | if($Context != '') $Context = $Context.'-';
|
---|
84 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
85 | {
|
---|
86 | if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false;
|
---|
87 | if($Item['ReadOnly'] == false)
|
---|
88 | if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
89 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
90 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
91 | {
|
---|
92 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
93 | $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index,
|
---|
94 | 'Type' => $Item['Type'], 'Values' => $this->Values);
|
---|
95 | if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
|
---|
96 | else unset($Parameters['Null']);
|
---|
97 |
|
---|
98 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
99 | {
|
---|
100 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
101 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
102 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
103 | if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
104 | {
|
---|
105 | $UseType = 'OneToMany';
|
---|
106 | } else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
107 | $UseType = 'Enumeration';
|
---|
108 | } else $UseType = $Item['Type'];
|
---|
109 | $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnEdit', $Parameters);
|
---|
110 | if(array_key_exists('Suffix', $Item)) $Edit .= $Item['Suffix'];
|
---|
111 |
|
---|
112 | $Caption = $Item['Caption'].':';
|
---|
113 | if(array_key_exists($Index, $this->ValuesValidate) and
|
---|
114 | $this->ValuesValidate[$Index]) {
|
---|
115 | $Format = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'GetValidationFormat', array());
|
---|
116 | if($Format != '') $Caption .= '<br/><small>'.$Format.'</small>';
|
---|
117 | $Caption = '<span style="color:red;">'.$Caption.'</span>';
|
---|
118 | }
|
---|
119 | if(!$this->FormManager->Type->IsHidden($UseType))
|
---|
120 | array_push($Table['Rows'], array($Caption, $Edit));
|
---|
121 | else $Hidden .= $Edit;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
|
---|
125 | $Hidden.'</fieldset>';
|
---|
126 | return($Output);
|
---|
127 | }
|
---|
128 |
|
---|
129 | function LoadValuesFromDatabase($Id)
|
---|
130 | {
|
---|
131 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
132 | if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
133 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
134 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
135 | {
|
---|
136 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
137 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
138 | {
|
---|
139 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
140 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
141 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
142 | if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
143 | $UseType = 'OneToMany';
|
---|
144 | else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
145 | $UseType = 'Enumeration';
|
---|
146 | } else $UseType = $Item['Type'];
|
---|
147 | if(!array_key_exists('SQL', $Item)) $Item['SQL'] = '';
|
---|
148 | else $Item['SQL'] = str_replace('#Id', $Id, $Item['SQL']);
|
---|
149 | $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery',
|
---|
150 | array('Name' => $Index, 'Type' => $Item['Type'], 'SQL' => $Item['SQL']));
|
---|
151 | }
|
---|
152 | $Columns = implode(',', $Columns);
|
---|
153 | $DbResult = $this->Database->query('SELECT '.$Columns.' FROM `'.$this->Definition['Table'].'` AS `T` WHERE `T`.`Id`='.$Id);
|
---|
154 | $DbRow = $DbResult->fetch_array();
|
---|
155 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
156 | if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
157 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
158 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
159 | {
|
---|
160 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
161 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
162 | {
|
---|
163 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
164 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
165 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
166 | if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
167 | $UseType = 'OneToMany';
|
---|
168 | else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
169 | $UseType = 'Enumeration';
|
---|
170 | } else $UseType = $Item['Type'];
|
---|
171 | $this->Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoadDb',
|
---|
172 | array('Value' => $DbRow[$Index], 'Name' => $Index,
|
---|
173 | 'Type' => $Item['Type'], 'Values' => $this->Values));
|
---|
174 | $this->ValuesFilter[$Index] = $DbRow[$Index.'_Filter'];
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | function SaveValuesToDatabase($Id)
|
---|
179 | {
|
---|
180 | $Values = array();
|
---|
181 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
182 | {
|
---|
183 | if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false;
|
---|
184 | if($Item['ReadOnly'] == false)
|
---|
185 | if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
186 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
187 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
|
---|
188 | {
|
---|
189 | if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
|
---|
190 | $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index,
|
---|
191 | 'Type' => $Item['Type'], 'Values' => $this->Values);
|
---|
192 |
|
---|
193 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
194 | {
|
---|
195 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
196 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
197 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
198 | if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
|
---|
199 | {
|
---|
200 | $UseType = 'OneToMany';
|
---|
201 | }
|
---|
202 | else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
|
---|
203 | $UseType = 'Enumeration';
|
---|
204 | } else $UseType = $Item['Type'];
|
---|
205 | $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnSaveDb', $Parameters);
|
---|
206 | if(($Item['Type'] == 'Password') and ($Values[$Index] == '')) unset($Values[$Index]);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | if($Id == 0)
|
---|
210 | {
|
---|
211 | $Values['Id'] = $Id;
|
---|
212 | $DbResult = $this->Database->insert($this->Definition['Table'], $Values);
|
---|
213 | } else
|
---|
214 | $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values);
|
---|
215 | }
|
---|
216 |
|
---|
217 | function LoadValuesFromForm()
|
---|
218 | {
|
---|
219 | $this->Values = $this->LoadValuesFromFormBlock();
|
---|
220 | }
|
---|
221 |
|
---|
222 | function LoadValuesFromFormBlock($Context = '')
|
---|
223 | {
|
---|
224 | if($Context != '') $Context = $Context.'-';
|
---|
225 | $Values = array();
|
---|
226 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
227 | if((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
228 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
229 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and
|
---|
230 | (!array_key_exists('ReadOnly', $Item) or
|
---|
231 | (array_key_exists('ReadOnly', $Item) and
|
---|
232 | ($Item['ReadOnly'] != true))))
|
---|
233 | {
|
---|
234 | //if(array_key_exists($Context.$Index, $_POST))
|
---|
235 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
236 | {
|
---|
237 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
238 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
239 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
240 | $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type'];
|
---|
241 | if($CustomType == 'Reference')
|
---|
242 | $UseType = 'OneToMany';
|
---|
243 | else if($CustomType == 'Enumeration')
|
---|
244 | $UseType = 'Enumeration';
|
---|
245 | } else $UseType = $Item['Type'];
|
---|
246 | $Parameters = array('Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values);
|
---|
247 | if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
|
---|
248 | else unset($Parameters['Null']);
|
---|
249 | $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoad',
|
---|
250 | $Parameters);
|
---|
251 | }
|
---|
252 | return($Values);
|
---|
253 | }
|
---|
254 |
|
---|
255 | function Validate()
|
---|
256 | {
|
---|
257 | $Valid = true;
|
---|
258 | foreach($this->Definition['Items'] as $Index => $Item)
|
---|
259 | if((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
|
---|
260 | (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
|
---|
261 | ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and
|
---|
262 | (!array_key_exists('ReadOnly', $Item) or
|
---|
263 | (array_key_exists('ReadOnly', $Item) and
|
---|
264 | ($Item['ReadOnly'] != true))))
|
---|
265 | {
|
---|
266 | //if(array_key_exists($Context.$Index, $_POST))
|
---|
267 | if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
|
---|
268 | {
|
---|
269 | if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
|
---|
270 | $this->FormManager->Type->RegisterType($Item['Type'], '',
|
---|
271 | $this->FormManager->FormTypes[$Item['Type']]);
|
---|
272 | $CustomType = $this->FormManager->FormTypes[$Item['Type']]['Type'];
|
---|
273 | if($CustomType == 'Reference')
|
---|
274 | $UseType = 'OneToMany';
|
---|
275 | else if($CustomType == 'Enumeration')
|
---|
276 | $UseType = 'Enumeration';
|
---|
277 | } else $UseType = $Item['Type'];
|
---|
278 |
|
---|
279 | $Parameters = array('Value' => $this->Values[$Index]);
|
---|
280 | if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
|
---|
281 | else $Parameters['Null'] = false;
|
---|
282 | if(!$this->FormManager->Type->ExecuteTypeEvent($UseType, 'Validate',
|
---|
283 | $Parameters)) {
|
---|
284 | $this->ValuesValidate[$Index] = true;
|
---|
285 | $Valid = false;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | if($Valid == false) throw new Exception('not validated');
|
---|
289 | return($Valid);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | function MakeLink($Target, $Title)
|
---|
295 | {
|
---|
296 | return('<a href="'.$Target.'">'.$Title.'</a>');
|
---|
297 | }
|
---|
298 |
|
---|
299 | function Table($Table)
|
---|
300 | {
|
---|
301 | $Result = '<table class="BasicTable">';
|
---|
302 | if(array_key_exists('Header', $Table))
|
---|
303 | {
|
---|
304 | $Result .= '<tr>';
|
---|
305 | foreach($Table['Header'] as $Item)
|
---|
306 | $Result .= '<th>'.$Item.'</th>';
|
---|
307 | $Result .= '</tr>';
|
---|
308 | }
|
---|
309 | foreach($Table['Rows'] as $Row)
|
---|
310 | {
|
---|
311 | $Result .= '<tr>';
|
---|
312 | foreach($Row as $Index => $Item)
|
---|
313 | {
|
---|
314 | if($Index == 0) $Class = ' class="Header"'; else $Class = '';
|
---|
315 | $Result .= '<td'.$Class.' style="width: '.(floor(100 / count($Row))).'%">'.$Item.'</td>';
|
---|
316 | }
|
---|
317 | $Result .= '</tr>';
|
---|
318 | }
|
---|
319 | $Result .= '</table>';
|
---|
320 | return($Result);
|
---|
321 | }
|
---|
322 |
|
---|
323 | class FormManager
|
---|
324 | {
|
---|
325 | var $Classes;
|
---|
326 | var $FormTypes;
|
---|
327 | var $Database;
|
---|
328 | var $Type;
|
---|
329 | var $RootURL;
|
---|
330 | var $ShowRelation;
|
---|
331 |
|
---|
332 | function __construct($Database)
|
---|
333 | {
|
---|
334 | $this->Database = &$Database;
|
---|
335 | $this->Classes = array();
|
---|
336 | $this->FormTypes = array();
|
---|
337 | $this->Type = new Type($this);
|
---|
338 | $this->ShowRelation = false;
|
---|
339 | }
|
---|
340 |
|
---|
341 | function RegisterClass($Name, $Class)
|
---|
342 | {
|
---|
343 | $this->Classes[$Name] = $Class;
|
---|
344 | }
|
---|
345 |
|
---|
346 | function UnregisterClass($Name)
|
---|
347 | {
|
---|
348 | unset($this->Classes[$Name]);
|
---|
349 | }
|
---|
350 |
|
---|
351 | function RegisterFormType($Name, $Class)
|
---|
352 | {
|
---|
353 | $this->FormTypes[$Name] = $Class;
|
---|
354 | }
|
---|
355 |
|
---|
356 | function UnregisterFormType($Name)
|
---|
357 | {
|
---|
358 | unset($this->FormTypes[$Name]);
|
---|
359 | }
|
---|
360 | }
|
---|