/[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.2 - (show annotations)
Fri Apr 4 00:43:22 2003 UTC (21 years, 5 months ago) by jonen
Branch: MAIN
Changes since 1.1: +93 -31 lines
+ complete rework

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

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