source: trunk/Common/Form/Form.php@ 538

Last change on this file since 538 was 538, checked in by chronos, 12 years ago
  • Upraveno: Některé formulářové typy přesunuty do jednotlivých modulů.
  • Přidáno: Moduly Customer, Task a Stock.
File size: 12.1 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/../Database.php');
4include_once(dirname(__FILE__).'/Types/Type.php');
5
6class Form
7{
8 var $FormManager;
9 var $Definition;
10 var $Values;
11 var $ValuesFilter;
12 var $OnSubmit;
13 var $Database;
14
15 function __construct($FormManager)
16 {
17 $this->FormManager = &$FormManager;
18 $this->Database = $FormManager->Database;
19 $this->Definition = array();
20 $this->Values = array();
21 $this->ValuesFilter = array();
22 $this->OnSubmit = '';
23 }
24
25 function SetClass($Name)
26 {
27 $this->Definition = &$this->FormManager->Classes[$Name];
28 }
29
30 function ShowViewForm()
31 {
32 $Table = array(
33 //'Header' => array('Položka', 'Hodnota'),
34 'Rows' => array(),
35 );
36 foreach($this->Definition['Items'] as $Index => $Item)
37 if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
38 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
39 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
40 {
41 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
42 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
43 {
44 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
45 $this->FormManager->Type->RegisterType($Item['Type'], '', $this->FormManager->FormTypes[$Item['Type']]);
46 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
47 $UseType = 'OneToMany';
48 else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
49 $UseType = 'Enumeration';
50 } else $UseType = $Item['Type'];
51 $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnView',
52 array('Value' => $this->Values[$Index], 'Name' => $Index,
53 'Type' => $Item['Type'], 'Values' => $this->Values,
54 'Filter' => $this->ValuesFilter[$Index]));
55 if(array_key_exists('Suffix', $Item)) $Edit .= ' '.$Item['Suffix'];
56 if(!$this->FormManager->Type->IsHidden($UseType))
57 array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
58 }
59 $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
60 '</fieldset>';
61 return($Output);
62 }
63
64 function ShowEditForm()
65 {
66 if(!array_key_exists('SubmitText', $this->Definition)) $this->Definition['SubmitText'] = 'Uložit';
67 $Output = '<form enctype="multipart/form-data" class="Form" action="'.$this->OnSubmit.'" method="post">'.$this->ShowEditBlock().
68 '<div><input type="submit" value="'.$this->Definition['SubmitText'].'" /> '.
69 '<input type="button" value="Zrušit" onclick="location.href=\'?\'"/></div></form>';
70 return($Output);
71 }
72
73 function ShowEditBlock($Context = '')
74 {
75 $Hidden = '';
76 $IsHidden = false;
77 $Table = array(
78 //'Header' => array('Položka', 'Hodnota'),
79 'Rows' => array(),
80 );
81 if($Context != '') $Context = $Context.'-';
82 foreach($this->Definition['Items'] as $Index => $Item)
83 {
84 if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false;
85 if($Item['ReadOnly'] == false)
86 if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
87 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
88 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
89 {
90 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
91 $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index,
92 'Type' => $Item['Type'], 'Values' => $this->Values);
93 if(array_key_exists('Null', $Item)) $Parameters['Null'] = $Item['Null'];
94 else unset($Parameters['Null']);
95
96 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
97 {
98 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
99 $this->FormManager->Type->RegisterType($Item['Type'], '',
100 $this->FormManager->FormTypes[$Item['Type']]);
101 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
102 {
103 $UseType = 'OneToMany';
104 } else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
105 $UseType = 'Enumeration';
106 } else $UseType = $Item['Type'];
107 $Edit = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnEdit', $Parameters);
108 if(array_key_exists('Suffix', $Item)) $Edit .= $Item['Suffix'];
109
110 if(!$this->FormManager->Type->IsHidden($UseType))
111 array_push($Table['Rows'], array($Item['Caption'].':', $Edit));
112 else $Hidden .= $Edit;
113 }
114 }
115 $Output = '<fieldset><legend>'.$this->Definition['Title'].'</legend>'.Table($Table).
116 $Hidden.'</fieldset>';
117 return($Output);
118 }
119
120 function LoadValuesFromDatabase($Id)
121 {
122 foreach($this->Definition['Items'] as $Index => $Item)
123 if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
124 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
125 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
126 {
127 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
128 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
129 {
130 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
131 $this->FormManager->Type->RegisterType($Item['Type'], '',
132 $this->FormManager->FormTypes[$Item['Type']]);
133 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
134 $UseType = 'OneToMany';
135 else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
136 $UseType = 'Enumeration';
137 } else $UseType = $Item['Type'];
138 $Columns[] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnFilterNameQuery',
139 array('Name' => $Index, 'Type' => $Item['Type']));
140 }
141 $Columns = implode(',', $Columns);
142 $DbResult = $this->Database->query('SELECT '.$Columns.' FROM `'.$this->Definition['Table'].'` AS `T` WHERE `T`.`Id`='.$Id);
143 $DbRow = $DbResult->fetch_array();
144 foreach($this->Definition['Items'] as $Index => $Item)
145 if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
146 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
147 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
148 {
149 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
150 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
151 {
152 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
153 $this->FormManager->Type->RegisterType($Item['Type'], '',
154 $this->FormManager->FormTypes[$Item['Type']]);
155 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
156 $UseType = 'OneToMany';
157 else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
158 $UseType = 'Enumeration';
159 } else $UseType = $Item['Type'];
160 $this->Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoadDb',
161 array('Value' => $DbRow[$Index], 'Name' => $Index,
162 'Type' => $Item['Type'], 'Values' => $this->Values));
163 $this->ValuesFilter[$Index] = $DbRow[$Index.'_Filter'];
164 }
165 }
166
167 function SaveValuesToDatabase($Id)
168 {
169 $Values = array();
170 foreach($this->Definition['Items'] as $Index => $Item)
171 {
172 if(!array_key_exists('ReadOnly', $Item)) $Item['ReadOnly'] = false;
173 if($Item['ReadOnly'] == false)
174 if(!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
175 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
176 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne')))
177 {
178 if(!array_key_exists($Index, $this->Values) and isset($Item['Default'])) $this->Values[$Index] = $Item['Default'];
179 $Parameters = array('Value' => $this->Values[$Index], 'Name' => $Index,
180 'Type' => $Item['Type'], 'Values' => $this->Values);
181
182 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
183 {
184 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
185 $this->FormManager->Type->RegisterType($Item['Type'], '',
186 $this->FormManager->FormTypes[$Item['Type']]);
187 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
188 {
189 $UseType = 'OneToMany';
190 }
191 else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
192 $UseType = 'Enumeration';
193 } else $UseType = $Item['Type'];
194 $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnSaveDb', $Parameters);
195 if(($Item['Type'] == 'Password') and ($Values[$Index] == '')) unset($Values[$Index]);
196 }
197 }
198 if($Id == 0)
199 {
200 $Values['Id'] = $Id;
201 $DbResult = $this->Database->insert($this->Definition['Table'], $Values);
202 } else
203 $DbResult = $this->Database->update($this->Definition['Table'], 'Id='.$Id, $Values);
204 }
205
206 function LoadValuesFromForm()
207 {
208 $this->Values = $this->LoadValuesFromFormBlock();
209 }
210
211 function LoadValuesFromFormBlock($Context = '')
212 {
213 if($Context != '') $Context = $Context.'-';
214 $Values = array();
215 foreach($this->Definition['Items'] as $Index => $Item)
216 if((!array_key_exists($Item['Type'], $this->FormManager->FormTypes) or
217 (array_key_exists($Item['Type'], $this->FormManager->FormTypes) and
218 ($this->FormManager->FormTypes[$Item['Type']]['Type'] != 'ManyToOne'))) and
219 (!array_key_exists('ReadOnly', $Item) or
220 (array_key_exists('ReadOnly', $Item) and
221 ($Item['ReadOnly'] != true))))
222 {
223 //if(array_key_exists($Context.$Index, $_POST))
224 if(array_key_exists($Item['Type'], $this->FormManager->FormTypes))
225 {
226 if(!array_key_exists($Item['Type'], $this->FormManager->Type->TypeDefinitionList))
227 $this->FormManager->Type->RegisterType($Item['Type'], '',
228 $this->FormManager->FormTypes[$Item['Type']]);
229 if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Reference')
230 $UseType = 'OneToMany';
231 else if($this->FormManager->FormTypes[$Item['Type']]['Type'] == 'Enumeration')
232 $UseType = 'Enumeration';
233 } else $UseType = $Item['Type'];
234 $Values[$Index] = $this->FormManager->Type->ExecuteTypeEvent($UseType, 'OnLoad',
235 array('Name' => $Index, 'Type' => $Item['Type'], 'Values' => $this->Values));
236 }
237 return($Values);
238 }
239}
240
241function MakeLink($Target, $Title)
242{
243 return('<a href="'.$Target.'">'.$Title.'</a>');
244}
245
246function Table($Table)
247{
248 $Result = '<table class="BasicTable">';
249 if(array_key_exists('Header', $Table))
250 {
251 $Result .= '<tr>';
252 foreach($Table['Header'] as $Item)
253 $Result .= '<th>'.$Item.'</th>';
254 $Result .= '</tr>';
255 }
256 foreach($Table['Rows'] as $Row)
257 {
258 $Result .= '<tr>';
259 foreach($Row as $Index => $Item)
260 {
261 if($Index == 0) $Class = ' class="Header"'; else $Class = '';
262 $Result .= '<td'.$Class.' style="width: '.(floor(100 / count($Row))).'%">'.$Item.'</td>';
263 }
264 $Result .= '</tr>';
265 }
266 $Result .= '</table>';
267 return($Result);
268}
269
270class FormManager
271{
272 var $Classes;
273 var $FormTypes;
274 var $Database;
275 var $Type;
276 var $RootURL;
277
278 function __construct($Database)
279 {
280 $this->Database = &$Database;
281 $this->Classes = array();
282 $this->FormTypes = array();
283 $this->Type = new Type($this);
284 }
285
286 function RegisterClass($Name, $Class)
287 {
288 $this->Classes[$Name] = $Class;
289 }
290
291 function UnregisterClass($Name)
292 {
293 unset($this->Classes[$Name]);
294 }
295
296 function RegisterFormType($Name, $Class)
297 {
298 $this->FormTypes[$Name] = $Class;
299 }
300
301 function UnregisterFormType($Name)
302 {
303 unset($this->FormTypes[$Name]);
304 }
305}
306
307?>
Note: See TracBrowser for help on using the repository browser.