1 |
<? |
2 |
/* |
3 |
## ----------------------------------------------------------------------------- |
4 |
## $Id: EditDataItem.php,v 1.6 2003/04/11 00:48:03 joko Exp $ |
5 |
## ----------------------------------------------------------------------------- |
6 |
## $Log: EditDataItem.php,v $ |
7 |
## |
8 |
## |
9 |
## ----------------------------------------------------------------------------- |
10 |
*/ |
11 |
|
12 |
class CreateDataItem extends StandardFormContent { |
13 |
|
14 |
/** |
15 |
* holds reference to source object. |
16 |
*/ |
17 |
var $_datasource = NULL; |
18 |
|
19 |
/** |
20 |
* container for arguments needed for e.g. setting the source object. |
21 |
*/ |
22 |
var $_options = array(); |
23 |
|
24 |
var $_confirm_msg = "Die Daten wurden erfolgreich gespeichert!"; |
25 |
|
26 |
function CreateDataItem($title, $options = array()) { |
27 |
$this->_options = $options; |
28 |
// prefetch data |
29 |
$this->data_prefetch(); |
30 |
|
31 |
// doesn't work if EditDataItem gets inherited |
32 |
//$parent = get_parent_class($this); |
33 |
//$this->$parent($title, $PHP_SELF, 600); |
34 |
|
35 |
$this->StandardFormContent($title, $PHP_SELF, 600); |
36 |
|
37 |
} |
38 |
|
39 |
function set_data_source(&$source) { |
40 |
$this->_datasource = &$source; |
41 |
} |
42 |
|
43 |
function get_data_source() { |
44 |
$initial_locator = php::mkComponent( 'DataSource::Locator', array( adapter_type => 'phpHtmlLib' ) ); |
45 |
$proxy = php::mkComponent('DataSource::Generic', $initial_locator, $this->_options['data_locator_meta']); |
46 |
$source = $proxy->get_adapter(); |
47 |
$this->set_data_source( &$source ); |
48 |
} |
49 |
|
50 |
function _check_datasource($function_name) { |
51 |
if ( !is_object($this->_datasource) ) { |
52 |
user_error("DataList::".$function_name."() - DataListSource object is not set"); |
53 |
exit; |
54 |
} |
55 |
} |
56 |
|
57 |
function data_prefetch() { |
58 |
$this->get_data_source(); |
59 |
$this->_check_datasource("data_prefetch"); |
60 |
|
61 |
$this->_datasource->do_query(); |
62 |
} |
63 |
|
64 |
/** |
65 |
* This method gets called EVERY time the object is |
66 |
* created. It is used to build all of the |
67 |
* FormElement objects used in this Form. |
68 |
* |
69 |
*/ |
70 |
function form_init_elements() { |
71 |
//we want an confirmation page for this form. |
72 |
$this->set_confirm(); |
73 |
|
74 |
$utils = php::mkComponent('WebExplorer::utils'); |
75 |
$hidden = $this->get_hidden_elements(); |
76 |
$options = $this->_options['decode_args']; |
77 |
|
78 |
//print "Result: " . Dumper($this->_datasource->_result); |
79 |
//return; |
80 |
|
81 |
if(php::is_hash($this->_datasource->_result)) { |
82 |
foreach($this->_datasource->_result as $key => $value) { |
83 |
if($value) { |
84 |
$validation = TRUE; |
85 |
} else { |
86 |
$validation = FALSE; |
87 |
} |
88 |
if(!is_array($value)) { |
89 |
if(!$utils->decode_item_expr($value, $hidden, $options) && !$utils->decode_item_array($value, $hidden, $options)) { |
90 |
$this->add_element( new FEText($key, $validation, "350px") ); |
91 |
} |
92 |
} else { |
93 |
$cnt = count($value); |
94 |
foreach($value as $item) { |
95 |
$list[$item] = $item; |
96 |
} |
97 |
$this->add_element( new FEListBox( $key, TRUE, "250px", $cnt*15 . "pt", $list ) ); |
98 |
} |
99 |
} |
100 |
} else { |
101 |
//$label = "list_box"; |
102 |
$label = $this->_options[data_locator_meta][ident]; |
103 |
$data = $this->_datasource->_result; |
104 |
$cnt = count($data); |
105 |
foreach($data as $value) { |
106 |
$data_list[$value] = $value; |
107 |
} |
108 |
$listbox = new FEListBox( $label, TRUE, "250px", $cnt*15 . "pt", $data_list ); |
109 |
$this->add_element( $listbox ); |
110 |
} |
111 |
|
112 |
} |
113 |
|
114 |
/** |
115 |
* This method is called only the first time the form |
116 |
* page is hit. This enables u to query a DB and |
117 |
* pre populate the FormElement objects with data. |
118 |
* |
119 |
*/ |
120 |
function form_init_data() { |
121 |
//Pull some valies from the DB |
122 |
//and set the initial values of some of the |
123 |
//form fields. |
124 |
|
125 |
$utils = php::mkComponent('WebExplorer::utils'); |
126 |
$hidden = $this->get_hidden_elements(); |
127 |
$options = $this->_options['decode_args']; |
128 |
|
129 |
if(php::is_hash($this->_datasource->_result)) { |
130 |
if(!$this->_is_new_hash_elem()) { |
131 |
foreach($this->_datasource->_result as $key => $value) { |
132 |
if(!$utils->decode_item_expr($value, $hidden, $options) && !$utils->decode_item_array($value, $hidden, $options)) { |
133 |
$this->set_element_value($key, $value); |
134 |
} |
135 |
} |
136 |
} else { |
137 |
$value = $this->_datasource->_result['class']; |
138 |
$this->set_element_value('class', $value); |
139 |
} |
140 |
} else { |
141 |
// nothing to do here for a ListBox, cause element hash is given to constructor at 'form_init_elements()' |
142 |
} |
143 |
|
144 |
/* OLD !! |
145 |
$post_vars = array_join_merge($_GET, $_POST); |
146 |
foreach ($post_vars as $var => $val) { |
147 |
$this->set_hidden_element_value($var, $val); |
148 |
} |
149 |
*/ |
150 |
|
151 |
} |
152 |
|
153 |
|
154 |
function form_content() { |
155 |
$this->add_form_block($this->_caption, $this->_dataitem() ); |
156 |
} |
157 |
|
158 |
function &_dataitem() { |
159 |
$table = &html_table($this->_width,0,2); |
160 |
|
161 |
if(php::is_hash($this->_datasource->_result)) { |
162 |
//if(!$this->_is_new_hash_elem()) { |
163 |
foreach($this->_datasource->_result as $key => $value) { |
164 |
// if item is match by expression we will replace it with an link object |
165 |
if($this->_options['decode']) { |
166 |
$utils = php::mkComponent('WebExplorer::utils'); |
167 |
$hidden = $this->get_hidden_elements(); |
168 |
$options = $this->_options['decode_args']; |
169 |
$options[label] = $key; |
170 |
$options[parent_guid] = $this->_options['parent']['guid']; |
171 |
$options[parent_class] = $this->_options['parent']['class']; |
172 |
if($utils->decode_item_expr($value, $hidden, $options)) { |
173 |
$table->add_row($key, $value); |
174 |
} |
175 |
// if item is an Array we will replace it with an selection form object |
176 |
elseif($utils->decode_item_array($value, $hidden, $options) ) { |
177 |
$table->add_row($key, $value); |
178 |
} else { |
179 |
$table->add_row($this->element_label($key), $this->element_form($key)); |
180 |
} |
181 |
} else { |
182 |
$table->add_row($this->element_label($key), $this->element_form($key)); |
183 |
} |
184 |
} |
185 |
//} else { |
186 |
// $label = $this->_options[data_locator_meta][ident] . " new key:"; |
187 |
//$table->add_row($this->element_label($label), $this->element_form('hash_elem'), $this->element_form($value)); |
188 |
// $table->add_row($label, $this->element_form('hash_key')); |
189 |
//} |
190 |
} else { |
191 |
$label = $this->_options[data_locator_meta][ident]; |
192 |
$table->add_row($this->element_label($label), $this->element_form($label)); |
193 |
} |
194 |
|
195 |
return $table; |
196 |
} |
197 |
|
198 |
/** |
199 |
* This method gets called after the FormElement data has |
200 |
* passed the validation. This enables you to validate the |
201 |
* data against some backend mechanism, say a DB. |
202 |
* |
203 |
*/ |
204 |
function form_backend_validation() { |
205 |
//$this->add_error("Ass", "some bogus error happened"); |
206 |
//return FALSE; |
207 |
//foreach($this->raw_data as $key => $value) { |
208 |
// if($this->_elements[$key]->get_value()) { |
209 |
// $this->add_error($key, "empty field!"); |
210 |
// return FALSE; |
211 |
// } else { |
212 |
// return TRUE; |
213 |
// } |
214 |
// } |
215 |
$this->set_action("Confirm"); |
216 |
return TRUE; |
217 |
} |
218 |
|
219 |
|
220 |
/** |
221 |
* This function is used to show an intermediary |
222 |
* confirmation page. Use this function to |
223 |
* show a confirmation of the data that was |
224 |
* submitted by the user. |
225 |
* This will get called after all of the |
226 |
* form data was successfully validated. |
227 |
* All of the form data will automatically |
228 |
* be created as hidden form fields. All you |
229 |
* have to do is show the data, and a confirm |
230 |
* submit button. |
231 |
* |
232 |
* @return mixed - either raw html, or some |
233 |
* container HTMLTag object. |
234 |
*/ |
235 |
function form_confirm( ) { |
236 |
$title = "Form Confirmation / " . $this->_options['caption']; |
237 |
$title .="( ".$this->_required_field_marker." ".$this->_required_field_text." )"; |
238 |
$table = new InfoTable($title, $this->_width); |
239 |
|
240 |
$this->build_confirm_table( $table ); |
241 |
|
242 |
if(php::is_hash($this->_datasource->_result)) { |
243 |
if(!$this->_is_new_hash_elem()) { |
244 |
$button_confirm = array( 'value' => "ecdfe", 'label' => "Confirm" ); |
245 |
} else { |
246 |
$button_confirm = array( 'value' => "ecdfcr", 'label' => "Confirm"); |
247 |
} |
248 |
} else { |
249 |
$button_confirm = array( 'value' => "ecdfcr", 'label' => "Confirm"); |
250 |
} |
251 |
//now add the confirmation button |
252 |
$td = new TDtag(array("colspan" => 2, |
253 |
"class" => "contentnovertical", |
254 |
"align" => "center"), |
255 |
form_submit($button_confirm[value], $button_confirm[label]), |
256 |
_HTML_SPACE, |
257 |
$this->add_action("Edit")); |
258 |
|
259 |
if ($this->_cancel_action) { |
260 |
$td->add(_HTML_SPACE, $this->add_cancel()); |
261 |
} |
262 |
|
263 |
$table->add_row( $td ); |
264 |
|
265 |
return $table; |
266 |
} |
267 |
|
268 |
|
269 |
|
270 |
|
271 |
/** |
272 |
* This method is called ONLY after ALL validation has |
273 |
* passed. This is the method that allows you to |
274 |
* do something with the data, say insert/update records |
275 |
* in the DB. |
276 |
*/ |
277 |
function confirm_action() { |
278 |
|
279 |
$utils = php::mkComponent('WebExplorer::utils'); |
280 |
$hidden = $this->get_hidden_elements(); |
281 |
$options = $this->_options['decode_args']; |
282 |
|
283 |
if(php::is_hash($this->_datasource->_result)) { |
284 |
if(!$this->_is_new_hash_elem()) { |
285 |
foreach($this->_datasource->_result as $label => $value_old) { |
286 |
if(!$utils->decode_item_expr($value_old, $hidden, $options) && !$utils->decode_item_array($value_old, $hidden, $options)) { |
287 |
$value_new = $this->_elements[$label]->get_value(); |
288 |
$data[$label] = $value_new; |
289 |
} |
290 |
} |
291 |
// set query values for datasource |
292 |
$this->_options['data_locator_meta']['action'] = "write"; |
293 |
$this->_options['data_locator_meta']['data'] = $data; |
294 |
|
295 |
// TODO: 'data_prefetch()' will never return ANYTHING! |
296 |
// Implement some error-handling there or somewhere. |
297 |
$error = $this->data_prefetch(); |
298 |
|
299 |
//$error = $this->_datasource->set($item); |
300 |
if(!$error) { |
301 |
// fetch fresh data |
302 |
$this->_options['data_locator_meta']['action'] = "read"; |
303 |
// unset previous initialed 'data' var |
304 |
$this->_options['data_locator_meta']['data'] = NULL; |
305 |
$this->data_prefetch(); |
306 |
|
307 |
$this->set_action_message($this->_confirm_msg); |
308 |
return TRUE; |
309 |
} else { |
310 |
return FALSE; |
311 |
} |
312 |
} else { |
313 |
$label = $this->_options[data_locator_meta][ident]; |
314 |
$value = $this->_elements['hash_key']->get_value(); |
315 |
$class = $this->_elements['class']->get_value(); |
316 |
$this->_options['data_locator_meta']['hash_key'] = $value; |
317 |
$this->_options['data_locator_meta']['nodename'] = $class; |
318 |
$error = $this->data_prefetch(); |
319 |
if(!$error) { |
320 |
// set 'caption' to new created item |
321 |
$this->_form_title = $value; |
322 |
// set query values for datasource |
323 |
$this->_options['data_locator_meta']['action'] = "read"; |
324 |
$this->_options['data_locator_meta']['ident'] = $this->_datasource->_result[guid]; |
325 |
$this->_options['data_locator_meta']['nodename'] = $value; |
326 |
$this->_options['data_locator_meta']['parent'] = NULL; |
327 |
// fetch data |
328 |
$this->data_prefetch(); |
329 |
// init data elements: cause data result is now diffrent (the new created data entry) |
330 |
//, we have to initialise elements new |
331 |
$this->form_init_elements(); |
332 |
$this->form_init_data(); |
333 |
|
334 |
$this->set_action_message($this->_confirm_msg); |
335 |
return TRUE; |
336 |
} else { |
337 |
return FALSE; |
338 |
} |
339 |
} |
340 |
} else { |
341 |
$label = $this->_options[data_locator_meta][ident]; |
342 |
$value = $this->_elements[$label]->get_value(); |
343 |
// set query values for datasource |
344 |
//$this->_options['data_locator_meta']['action'] = "create"; |
345 |
$this->_options['data_locator_meta']['nodename'] = $value; |
346 |
|
347 |
// TODO: 'data_prefetch()' will never return ANYTHING! |
348 |
// Implement some error-handling there or somewhere. |
349 |
$error = $this->data_prefetch(); |
350 |
|
351 |
//$error = $this->_datasource->set($item); |
352 |
if(!$error) { |
353 |
// set 'caption' to new created item |
354 |
$this->_form_title = $value; |
355 |
// set query values for datasource |
356 |
$this->_options['data_locator_meta']['action'] = "read"; |
357 |
$this->_options['data_locator_meta']['ident'] = $this->_datasource->_result[guid]; |
358 |
$this->_options['data_locator_meta']['nodename'] = $value; |
359 |
$this->_options['data_locator_meta']['parent'] = NULL; |
360 |
// fetch data |
361 |
$this->data_prefetch(); |
362 |
// init data elements: cause data result is now diffrent (the new created data entry) |
363 |
//, we have to initialise elements new |
364 |
$this->form_init_elements(); |
365 |
$this->form_init_data(); |
366 |
|
367 |
$this->set_action_message($this->_confirm_msg); |
368 |
return TRUE; |
369 |
} else { |
370 |
return FALSE; |
371 |
} |
372 |
} |
373 |
|
374 |
} |
375 |
|
376 |
/** |
377 |
* This function is used to build the standard |
378 |
* buttons for a form. |
379 |
* |
380 |
* @return ButtonPanel |
381 |
*/ |
382 |
function form_content_buttons() { |
383 |
if(php::is_hash($this->_datasource->_result)) { |
384 |
if(!$this->_is_new_hash_elem()) { |
385 |
$button_save = array( 'value' => "ecdfe", 'label' => "Save" ); |
386 |
} else { |
387 |
$button_save = array( 'value' => "ecdfcr", 'label' => "Create"); |
388 |
} |
389 |
} else { |
390 |
$button_save = array( 'value' => "ecdfcr", 'label' => "Create"); |
391 |
} |
392 |
$div = new DIVtag( array("style" => "background-color: #eeeeee;". |
393 |
"padding-top:5px;padding-bottom:5px", |
394 |
"align"=>"center", "nowrap"), |
395 |
form_submit($button_save[value], $button_save[label]), |
396 |
_HTML_SPACE, |
397 |
//$this->add_cancel() |
398 |
form_submit('ecdfc', "Cancel") |
399 |
); |
400 |
return $div; |
401 |
} |
402 |
|
403 |
|
404 |
function get_hidden_elements() { |
405 |
foreach($this->_hidden_elements as $label => $object) { |
406 |
$value = $this->_hidden_elements[$label]->get_value(); |
407 |
$tmp_array[$label] = $value; |
408 |
} |
409 |
return $tmp_array; |
410 |
} |
411 |
|
412 |
|
413 |
function _is_new_hash_elem() { |
414 |
foreach($this->_datasource->_result as $key => $value) { |
415 |
if($key == "hash_key") { return TRUE; } |
416 |
else { return FALSE; } |
417 |
} |
418 |
} |
419 |
|
420 |
} |
421 |
|
422 |
|
423 |
?> |