/[cvs]/nfo/php/libs/org.netfrag.patches/phphtmllib/widgets/EditDataItem.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.patches/phphtmllib/widgets/EditDataItem.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Fri Apr 4 23:58:02 2003 UTC (21 years, 5 months ago) by jonen
Branch: MAIN
Changes since 1.2: +19 -6 lines
+ saving of items sould now work with new 'GenericSource' structure

1 <?
2 /*
3 ## -----------------------------------------------------------------------------
4 ## $Id: EditDataItem.php,v 1.2 2003/04/04 00:43:22 jonen Exp $
5 ## -----------------------------------------------------------------------------
6 ## $Log: EditDataItem.php,v $
7 ## Revision 1.2 2003/04/04 00:43:22 jonen
8 ## + complete rework
9 ##
10 ## Revision 1.1 2003/03/27 01:18:06 jonen
11 ## + inital commit (was orginal 'DataItem.php')
12 ##
13 ## Revision 1.4 2003/02/27 17:33:14 cvsmax
14 ## + enabled saving of data objects at confirm action
15 ## + fixed bug with POST/GET vars at form confirm
16 ## o ToDo: fix all bugs related to un-wanted hidden form elements
17 ## caused by mixed/duplicated POST/GET vars!
18 ##
19 ## Revision 1.3 2003/02/26 21:37:53 cvsmax
20 ## + reworked to enable linking if item value is an array or special expression
21 ##
22 ## Revision 1.2 2003/02/25 03:46:59 cvsmax
23 ## + refactored complete to work now with phphtmllib FormProcessor/Content
24 ## o ToDo: field validation seems not to work...
25 ##
26 ## Revision 1.1 2003/02/22 17:27:31 cvsmax
27 ## + renamed from DataEdit.php
28 ## + added simple support for class 'FormElementsInterface' which is an interface
29 ## to class 'FormBuilder' by binarycloud
30 ## o ToDo: refactor code to use new (added 21.02.03!) 'FormProcess' classes by phphtmllib
31 ## + class 'DataItem' now extends class 'InfoTable'
32 ##
33 ## Revision 1.1 2003/02/21 03:19:35 cvsmax
34 ## + first init
35 ##
36 ##
37 ## -----------------------------------------------------------------------------
38 */
39
40 class EditDataItem extends StandardFormContent {
41
42 /**
43 * holds reference to source object.
44 */
45 var $_datasource = NULL;
46
47 /**
48 * container for arguments needed for e.g. setting the source object.
49 */
50 var $_options = array();
51
52 var $_confirm_msg = "Die Daten wurden erfolgreich gespeichert!";
53
54 function EditDataItem($title, $options = array()) {
55 $this->_options = $options;
56 // prefetch data
57 $this->data_prefetch();
58
59 $parent = get_parent_class($this);
60 $this->$parent($title, $PHP_SELF, 600);
61 }
62
63 function set_data_source(&$source) {
64 $this->_datasource = &$source;
65 }
66
67 function get_data_source() {
68 $initial_locator = php::mkComponent( 'DataSource::Locator', array( adapter_type => 'phpHtmlLib' ) );
69 $proxy = php::mkComponent('DataSource::Generic', $initial_locator, $this->_options['data_locator_meta']);
70 $source = $proxy->get_adapter();
71 $this->set_data_source( &$source );
72 }
73
74 function _check_datasource($function_name) {
75 if ( !is_object($this->_datasource) ) {
76 user_error("DataList::".$function_name."() - DataListSource object is not set");
77 exit;
78 }
79 }
80
81 function data_prefetch() {
82 $this->get_data_source();
83 $this->_check_datasource("data_prefetch");
84
85 $this->_datasource->do_query();
86 }
87
88 /**
89 * This method gets called EVERY time the object is
90 * created. It is used to build all of the
91 * FormElement objects used in this Form.
92 *
93 */
94 function form_init_elements() {
95 //we want an confirmation page for this form.
96 $this->set_confirm();
97
98 foreach($this->_datasource->_result as $key => $value) {
99 if($value) {
100 $validation = TRUE;
101 } else {
102 $validation = FALSE;
103 }
104 if(!$this->decode_item_expr($value, $this->_options) && !$this->decode_item_array($value, $this->_options)) {
105 $this->add_element( new FEText($key, $validation, "350px") );
106 }
107 }
108
109 }
110
111 /**
112 * This method is called only the first time the form
113 * page is hit. This enables u to query a DB and
114 * pre populate the FormElement objects with data.
115 *
116 */
117 function form_init_data() {
118 //Pull some valies from the DB
119 //and set the initial values of some of the
120 //form fields.
121
122 foreach($this->_datasource->_result as $key => $value) {
123 if(!$this->decode_item_expr($value, $this->_options) && !$this->decode_item_array($value, $this->_options)) {
124 $this->set_element_value($key, $value);
125 }
126 }
127
128 /* OLD !!
129 $post_vars = array_join_merge($_GET, $_POST);
130 foreach ($post_vars as $var => $val) {
131 $this->set_hidden_element_value($var, $val);
132 }
133 */
134
135 }
136
137
138 function form_content() {
139 $this->add_form_block($this->_caption, $this->_dataitem() );
140 }
141
142 function &_dataitem() {
143 $table = &html_table($this->_width,0,2);
144
145 foreach($this->_datasource->_result as $key => $value) {
146 // if item is match by expression we will replace it with an link object
147 if($this->_options['decode']) {
148 if($this->decode_item_expr($value)) {
149 $table->add_row($key, $this->decode_item_expr($value));
150 }
151 // if item is an Array we will replace it with an selection form object
152 elseif($this->decode_item_array($value) ) {
153 $table->add_row($key, $this->decode_item_array($value));
154 } else {
155 $table->add_row($this->element_label($key), $this->element_form($key));
156 }
157 } else {
158 $table->add_row($this->element_label($key), $this->element_form($key));
159 }
160 }
161
162 return $table;
163 }
164
165 /**
166 * This method gets called after the FormElement data has
167 * passed the validation. This enables you to validate the
168 * data against some backend mechanism, say a DB.
169 *
170 */
171 function form_backend_validation() {
172 //$this->add_error("Ass", "some bogus error happened");
173 //return FALSE;
174 //foreach($this->raw_data as $key => $value) {
175 // if($this->_elements[$key]->get_value()) {
176 // $this->add_error($key, "empty field!");
177 // return FALSE;
178 // } else {
179 // return TRUE;
180 // }
181 // }
182 $this->set_action("Confirm");
183 return TRUE;
184 }
185
186
187 /**
188 * This function is used to show an intermediary
189 * confirmation page. Use this function to
190 * show a confirmation of the data that was
191 * submitted by the user.
192 * This will get called after all of the
193 * form data was successfully validated.
194 * All of the form data will automatically
195 * be created as hidden form fields. All you
196 * have to do is show the data, and a confirm
197 * submit button.
198 *
199 * @return mixed - either raw html, or some
200 * container HTMLTag object.
201 */
202 function form_confirm( ) {
203 $title = "Form Confirmation / " . $this->_options['caption'];
204 $title .="( ".$this->_required_field_marker." ".$this->_required_field_text." )";
205 $table = new InfoTable($title, $this->_width);
206
207 $this->build_confirm_table( $table );
208
209 //now add the confirmation button
210 $td = new TDtag(array("colspan" => 2,
211 "class" => "contentnovertical",
212 "align" => "center"),
213 form_submit('ecdfe', "Confirm"),
214 _HTML_SPACE,
215 $this->add_action("Edit"));
216
217 if ($this->_cancel_action) {
218 $td->add(_HTML_SPACE, $this->add_cancel());
219 }
220
221 $table->add_row( $td );
222
223 return $table;
224 }
225
226
227
228
229 /**
230 * This method is called ONLY after ALL validation has
231 * passed. This is the method that allows you to
232 * do something with the data, say insert/update records
233 * in the DB.
234 */
235 function confirm_action() {
236
237 foreach($this->_datasource->_result as $label => $value_old) {
238 if(!$this->decode_item_expr($value_old) && !$this->decode_item_array($value_old)) {
239 $value_new = $this->_elements[$label]->get_value();
240 $data[$label] = $value_new;
241 }
242 }
243 // write new data to datasource
244 $this->_options['data_locator_meta']['action'] = "write";
245 $this->_options['data_locator_meta']['data'] = $data;
246 // TODO: 'data_prefetch()' will never return ANYTHING!
247 // Implement some error-handling there or somewhere.
248 $error = $this->data_prefetch();
249
250 //$error = $this->_datasource->set($item);
251 if(!$error) {
252 // fetch fresh data
253 $this->_options['data_locator_meta']['action'] = "read";
254 // unset previous initialed 'data' var
255 $this->_options['data_locator_meta']['data'] = NULL;
256 $this->data_prefetch();
257
258 $this->set_action_message($this->_confirm_msg);
259 return TRUE;
260 } else {
261 return FALSE;
262 }
263 }
264
265 /**
266 * This function is used to build the standard
267 * buttons for a form.
268 *
269 * @return ButtonPanel
270 */
271 function form_content_buttons() {
272 $div = new DIVtag( array("style" => "background-color: #eeeeee;".
273 "padding-top:5px;padding-bottom:5px",
274 "align"=>"center", "nowrap"),
275 form_submit('ecdfe', "Save"),
276 _HTML_SPACE,
277 //$this->add_cancel()
278 form_submit('ecdfc', "Cancel")
279 );
280 return $div;
281 }
282
283
284
285
286 function decode_item_array($item) {
287 $options = $this->_options['decode_args'];
288 if( is_array($item) ) {
289 //$cur_row_index = $this->_datasource->get_cur_data_index();
290 //$parent_guid = $this->_datasource->_data[$cur_row_index]['guid'];
291 // build list for selection form
292 if($options['form']) {
293 foreach($item as $key => $value) {
294 $tmp = split($options['seperator'], $value);
295 $ident = $tmp['1'];
296 $meta = $tmp['2'];
297 $list[$key] = $ident;
298 }
299 if(is_array($list) ) {
300 $container = container(
301 form_open( $item[0], $_SERVER["PHP_SELF"], "POST" ),
302 form_hidden("ecdm", $meta),
303 form_select("ecdid", $list),
304 form_submit("submit","view" )
305 );
306 foreach($this->_hidden_elements as $label => $value) {
307 $container->add(form_hidden($label, $value));
308 }
309 $container->add(form_close() );
310 $item = $container;
311 }
312 } else {
313 $container = container();
314 foreach($item as $key => $value) {
315 $tmp = split($options['seperator'], $value);
316 $ident = $tmp['1'];
317 $meta = $tmp['2'];
318 foreach($this->_hidden_elements as $label => $object) {
319 $value = $this->_hidden_elements[$label]->get_value();
320 $tmp_array[] = $label . "=" . $value;
321 }
322 $str_hidden = join("&", $tmp_array);
323 $container->add("->", html_a($_SERVER["PHP_SELF"] . "?ecdid=" . $ident . "&ecdm=" . $meta . "&" . $str_hidden, $key . " view"), html_br());
324 }
325 $item = $container;
326 }
327 return $item;
328 }
329 }
330
331
332 function decode_item_expr($item) {
333 $options = $this->_options['decode_args'];
334 if(substr($item, 0, 2) == "o_") {
335 $tmp = split($options['seperator'], $item);
336 $ident = $tmp['1'];
337 $meta = $tmp['2'];
338 foreach($this->_hidden_elements as $label => $object) {
339 $value = $this->_hidden_elements[$label]->get_value();
340 $tmp_array[] = $label . "=" . $value;
341 }
342 $str_hidden = join("&", $tmp_array);
343 $item = html_a($_SERVER["PHP_SELF"] . "?ecdid=" . $ident . "&ecdm=" . $meta . "&" . $str_hidden, "view");
344 return $item;
345 }
346 }
347
348
349 }
350
351
352 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed