/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/form/FormProcessor.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sat Feb 22 21:07:42 2003 UTC (21 years, 6 months ago) by jonen
Branch: MAIN
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

1 <?php
2 /**
3 * This file contains the FormProcessor class.
4 *
5 * $Id: FormProcessor.inc,v 1.20 2003/02/19 00:37:45 hemna Exp $
6 *
7 * @author Walter A. Boring IV <waboring@buildabetterweb.com>
8 * @author Suren Markossian <suren@cbestwhat?>
9 * @package phpHtmlLib
10 * @subpackage FormProcessing
11 *
12 * @copyright LGPL - See LICENCE
13 *
14 */
15
16 define(FORM_ACTION, "_form_action");
17 define(FORM_VISITED, "_form_visited");
18 define(FORM_CONFIRM, "_form_confirm");
19
20 /**
21 * This is the main engine for the processing
22 * of Forms. It builds the form tag, and calls
23 * the appropriate FormContent methods to build
24 * the FormElement's and validation, as well as
25 * backend processing to do the action after the
26 * data has been validated.
27 *
28 * @package phpHtmlLib
29 * @subpackage FormProcessing
30 */
31 class FormProcessor extends Container {
32
33 /**
34 * This holds the name of the form
35 * for js that needs it
36 */
37 var $_form_name="forms[0]";
38
39
40 /**
41 * The action for the form tag
42 */
43 var $_form_action = "";
44
45 /**
46 * The form's enctype attribute.
47 * ie <form enctype="multipart/form_data">
48 *
49 */
50 var $_form_enctype = NULL;
51
52 /**
53 * This holds the FormContent Object
54 * that knows how to render the
55 * form.
56 */
57 var $_form_content = NULL;
58
59
60 /**
61 * This flag lets us know there
62 * were errors during processing or
63 * validating the form.
64 */
65 var $_has_errors = FALSE;
66
67 /**
68 * Flag to let us know the form
69 * has been confirmed.
70 */
71 var $_confirmed = FALSE;
72
73 /**
74 * Flag to let us know if we should
75 * render the form after it was
76 * successfully processed
77 */
78 var $_form_success_render = TRUE;
79
80
81 /**
82 * The constructor for the FormProcessor
83 *
84 * @param FormContent object
85 * @param string the form name
86 */
87 function FormProcessor(&$form_content, $form_name="forms[0]",
88 $form_action=NULL) {
89
90 $this->_form_name = $form_name;
91 $this->_form_content = &$form_content;
92 $this->_form_content->set_form_name($form_name);
93
94 if ($form_action != NULL) {
95 $this->_form_action = $form_action;
96 } else {
97 $this->_form_action = $_SERVER["PHP_SELF"];
98 }
99
100 //now process the form
101 $this->_process_form();
102 }
103
104 /**
105 * This method does the logic of
106 * doing the form processing
107 */
108 function _process_form() {
109 //let the form build the FormElement objects
110 //that will be used by the form
111 $this->_form_content->form_init_elements();
112
113 //first we need to
114 if (!$_POST[FORM_VISITED]) {
115 $this->_form_content->form_init_data();
116 }
117
118
119 //we only need to process the form
120 //if it has been visited. Otherwise
121 //it just gets rendered.
122 if ($_POST[FORM_VISITED] == 1) {
123 $this->_form_content->set_action($_POST[FORM_ACTION]);
124
125 //let see if this was a confirmation page.
126 if ( $_POST[FORM_CONFIRM] == 1 ) {
127 //looks like this was a submit on a
128 //confirmation page. we don't need
129 //to do form field validation.
130 $this->_confirmed = TRUE;
131 }
132
133 //now do the validation
134 if (!$this->_confirmed) {
135 //we haven't been confirmed, so we
136 //need to validate the form.
137 if ($this->can_validate()) {
138 //looks like we should do validation
139 $this->do_validation();
140 }
141 if (!$this->_has_errors) {
142 //no errors were found
143 if ($this->_form_content->has_confirm()) {
144 //the form content has a confirmation
145 //we need to process
146 $this->_has_errors = !$this->_pre_confirm();
147 } else {
148 //make sure we don't have any backend errors
149 $this->_has_errors = !$this->_form_content->form_backend_validation();
150 if (!$this->_has_errors) {
151 $this->_has_errors = !$this->_process_action();
152 }
153 }
154 }
155 } else {
156 //make sure we don't have any backend errors
157 $this->_has_errors = !$this->_form_content->form_backend_validation();
158 if (!$this->_has_errors) {
159 $this->_has_errors = !$this->_process_action();
160 }
161 }
162 }
163 }
164
165
166 /**
167 * This function is responsible for
168 * processing the form action
169 * after validation, and form confirmation
170 * happens.
171 *
172 */
173 function _process_action() {
174 //There were no validation errors.
175 return $this->_form_content->form_action();
176 }
177
178 /**
179 * This method calls the FormContent
180 * to let it do any data munging before the
181 * confirmation page is rendered
182 */
183 function _pre_confirm() {
184 return $this->_form_content->pre_confirm();
185 }
186
187
188 /**
189 * This method walks the FormContent's visible elements
190 * and calls the validation function for the element
191 *
192 */
193 function do_validation() {
194 $keys = array_keys( $this->_form_content->_elements );
195 foreach( $keys as $key ) {
196 $valid = $this->_form_content->_elements[$key]->_do_validation();
197 if (!$valid) {
198 $this->_has_errors = TRUE;
199 }
200 }
201 }
202
203
204 /**
205 * This method is called to render the form's html
206 *
207 */
208 function render($indent_level=0, $output_debug=0) {
209 if ($this->_has_errors) {
210 //we need to render the form errors
211 //and then the form
212 return $this->render_error($indent_level, $output_debug);
213 } else {
214 //there are no errors!
215 if ($_POST[FORM_VISITED] == 1) {
216 //looks like the form has been processed?
217 if ($this->_form_content->has_confirm() && !$this->_confirmed) {
218 return $this->render_confirm($indent_level, $output_debug);
219 } else {
220 //Looks like the action worked
221 $success = $this->_form_content->form_success($this->_message);
222
223 if ($this->_form_success_render) {
224 return $this->render_form($indent_level, $output_debug,
225 $success);
226 } else {
227 if (method_exists($success,"render")) {
228 //looks like this is an object.
229 //we'll assume it has a render function.
230 return $success->render($indent_level, $output_debug);
231 } else {
232 //since its not an object,
233 //lets just display it.
234 return $success;
235 }
236 }
237 }
238 } else {
239 return $this->render_form($indent_level, $output_debug);
240 }
241 }
242 }
243
244
245 /**
246 * This renders the form
247 *
248 * @param the FormContent->form() object
249 * @param int - $indent_level
250 * @param int - $output_debug
251 * @param object - the form errors object.
252 * @return raw html
253 */
254 function render_form( $indent_level, $output_debug, $obj=NULL ) {
255
256 //build the $this->_form object.
257 $this->_build_form_tag();
258
259 if ($obj) {
260 $this->_form->add_reference( $obj );
261 }
262
263 $this->_form->add_reference( $this->_form_content->form() );
264
265 //add the FormContent's hidden declared
266 //hidden form fields.
267 $this->_add_hidden_fields();
268
269 //Ok lets add our hidden vars
270 $this->__hidden_fields();
271
272 return $this->_form->render($indent_level, $output_debug);
273 }
274
275 /**
276 * This function renders the confirmation
277 * page. This page sits in between the
278 * front end form, and the action handler.
279 * This only gets called after a form
280 * and its data has been successfully
281 * validated.
282 *
283 * @param int - $indent_level
284 * @param int - $output_debug
285 *
286 * @return string - the raw html
287 */
288 function render_confirm( $indent_level, $output_debug ) {
289 //build the $this->_form object.
290 $this->_build_form_tag();
291
292 //add the confirm object/html
293 $confirm = &$this->_form_content->form_confirm( $this->data );
294 $this->_form->add_reference( $confirm );
295
296 //ok add all of the submitted data as hidden form fields
297 $this->_add_confirm_data();
298
299 //Ok lets add our hidden vars
300 $this->__hidden_fields();
301
302 return $this->_form->render($indent_level, $output_debug);
303 }
304
305
306 /**
307 * This renders the error table
308 * and then the form with the fields
309 *
310 * @param array - the form field vlues.
311 * @param array - array of errors.
312 * @param int - $indent_level
313 * @param int - $output_debug
314 *
315 * @return raw html
316 */
317 function render_error( $indent_level, $output_debug) {
318 $wrapper_div = new DIVtag;
319
320 //Ok first lets build the error table
321 $errors = &$this->_form_content->form_errors();
322 if ($errors != NULL) $wrapper_div->add( $errors, html_br() );
323
324 return $this->render_form( $indent_level, $output_debug, $wrapper_div);
325 }
326
327
328
329
330 //***********************************************//
331 //* utility functions for this class *//
332 //***********************************************//
333
334
335 /**
336 * This method lets us turn on/off the
337 * ability to do validation for the form
338 *
339 * @return BOOLEAN
340 */
341 function can_validate() {
342 return TRUE;
343 }
344
345
346 /**
347 * This function turns on the ability to
348 * render the form after the success
349 * of the action. Normally this feature
350 * is off
351 *
352 */
353 function set_render_form_after_success() {
354 $this->_form_success_render = TRUE;
355 }
356
357
358 /**
359 * this function builds the FORMtag object
360 * and its attributes.
361 *
362 * @return FORMtag object.
363 */
364 function _build_form_tag() {
365 $form_attrs = array("method" => "POST",
366 "action" => $this->_form_action,
367 "name" => $this->_form_name,
368 "style" => "margin: 0px 0px 0px 0px;");
369 if ($this->_enctype != NULL)
370 $form_attrs["enctype"] = $this->_enctype;
371 if ($this->_target != NULL)
372 $form_attrs["target"] = $this->_target;
373 if ($this->_onsubmit != NULL)
374 $form_attrs["onSubmit"] = $this->_onsubmit;
375
376 $this->_form = new FORMtag( $form_attrs );
377 }
378
379 /**
380 * This adds all of the submitted data as
381 * hidden form fields
382 *
383 */
384 function _add_confirm_data() {
385 $keys = array_keys( $this->_form_content->_elements );
386 foreach( $keys as $key ) {
387 $element_name = $this->_form_content->_elements[$key]->get_element_name();
388 $values = $this->_form_content->_elements[$key]->get_value();
389 if (is_array($values)) {
390 foreach($values as $value) {
391 $this->_form->add( form_hidden($element_name, $value) );
392 }
393 } else {
394 $this->_form->add( form_hidden($element_name, $values) );
395
396 }
397 }
398
399 $keys = array_keys( $this->_form_content->_hidden_elements );
400 foreach( $keys as $key ) {
401 $this->_form->add( $this->_form_content->_hidden_elements[$key]->get_element() );
402 }
403 }
404
405
406 /**
407 * This function adds the form content's
408 * hidden form fields to the
409 * form automatically
410 *
411 */
412 function _add_hidden_fields() {
413 //Lets add the form's hidden vars it wants
414 foreach ($this->_form_content->_hidden_elements as $element) {
415 $this->_form->add( $element->get_element() );
416 }
417 }
418
419
420 /**
421 * This method adds the processor specific
422 * hidden fields.
423 *
424 */
425 function __hidden_fields() {
426 $this->_form->add( form_hidden(FORM_ACTION),
427 form_hidden(FORM_VISITED,1) );
428
429 if ($this->_form_content->has_confirm() && !$this->_confirmed) {
430 if (!$_POST[FORM_VISITED] || $this->_has_errors) {
431 $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
432 } else {
433 $this->_form->add( form_hidden(FORM_CONFIRM, 1 ) );
434 }
435 } else if ($this->_form_content->has_confirm() && !$this->_confirmed) {
436 //reset this so they can submit again
437 $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
438 }
439 }
440 }
441 ?>

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