/[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.2 - (show annotations)
Thu Apr 3 23:47:38 2003 UTC (21 years, 4 months ago) by jonen
Branch: MAIN
Changes since 1.1: +4 -3 lines
+ moved process point of '_process_form()' to begin of function 'render()',
   so something could be done with the FormContent object before rendering
    (eg add hidden elements, etc.)

1 <?php
2 /**
3 * This file contains the FormProcessor class.
4 *
5 * $Id: FormProcessor.inc,v 1.1 2003/02/22 21:07:42 jonen 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 }
101
102 /**
103 * This method does the logic of
104 * doing the form processing
105 */
106 function _process_form() {
107 //let the form build the FormElement objects
108 //that will be used by the form
109 $this->_form_content->form_init_elements();
110
111 //first we need to
112 if (!$_POST[FORM_VISITED]) {
113 $this->_form_content->form_init_data();
114 }
115
116
117 //we only need to process the form
118 //if it has been visited. Otherwise
119 //it just gets rendered.
120 if ($_POST[FORM_VISITED] == 1) {
121 $this->_form_content->set_action($_POST[FORM_ACTION]);
122
123 //let see if this was a confirmation page.
124 if ( $_POST[FORM_CONFIRM] == 1 ) {
125 //looks like this was a submit on a
126 //confirmation page. we don't need
127 //to do form field validation.
128 $this->_confirmed = TRUE;
129 }
130
131 //now do the validation
132 if (!$this->_confirmed) {
133 //we haven't been confirmed, so we
134 //need to validate the form.
135 if ($this->can_validate()) {
136 //looks like we should do validation
137 $this->do_validation();
138 }
139 if (!$this->_has_errors) {
140 //no errors were found
141 if ($this->_form_content->has_confirm()) {
142 //the form content has a confirmation
143 //we need to process
144 $this->_has_errors = !$this->_pre_confirm();
145 } else {
146 //make sure we don't have any backend errors
147 $this->_has_errors = !$this->_form_content->form_backend_validation();
148 if (!$this->_has_errors) {
149 $this->_has_errors = !$this->_process_action();
150 }
151 }
152 }
153 } else {
154 //make sure we don't have any backend errors
155 $this->_has_errors = !$this->_form_content->form_backend_validation();
156 if (!$this->_has_errors) {
157 $this->_has_errors = !$this->_process_action();
158 }
159 }
160 }
161 }
162
163
164 /**
165 * This function is responsible for
166 * processing the form action
167 * after validation, and form confirmation
168 * happens.
169 *
170 */
171 function _process_action() {
172 //There were no validation errors.
173 return $this->_form_content->form_action();
174 }
175
176 /**
177 * This method calls the FormContent
178 * to let it do any data munging before the
179 * confirmation page is rendered
180 */
181 function _pre_confirm() {
182 return $this->_form_content->pre_confirm();
183 }
184
185
186 /**
187 * This method walks the FormContent's visible elements
188 * and calls the validation function for the element
189 *
190 */
191 function do_validation() {
192 $keys = array_keys( $this->_form_content->_elements );
193 foreach( $keys as $key ) {
194 $valid = $this->_form_content->_elements[$key]->_do_validation();
195 if (!$valid) {
196 $this->_has_errors = TRUE;
197 }
198 }
199 }
200
201
202 /**
203 * This method is called to render the form's html
204 *
205 */
206 function render($indent_level=0, $output_debug=0) {
207 //now process the form
208 $this->_process_form();
209
210 if ($this->_has_errors) {
211 //we need to render the form errors
212 //and then the form
213 return $this->render_error($indent_level, $output_debug);
214 } else {
215 //there are no errors!
216 if ($_POST[FORM_VISITED] == 1) {
217 //looks like the form has been processed?
218 if ($this->_form_content->has_confirm() && !$this->_confirmed) {
219 return $this->render_confirm($indent_level, $output_debug);
220 } else {
221 //Looks like the action worked
222 $success = $this->_form_content->form_success($this->_message);
223
224 if ($this->_form_success_render) {
225 return $this->render_form($indent_level, $output_debug,
226 $success);
227 } else {
228 if (method_exists($success,"render")) {
229 //looks like this is an object.
230 //we'll assume it has a render function.
231 return $success->render($indent_level, $output_debug);
232 } else {
233 //since its not an object,
234 //lets just display it.
235 return $success;
236 }
237 }
238 }
239 } else {
240 return $this->render_form($indent_level, $output_debug);
241 }
242 }
243 }
244
245
246 /**
247 * This renders the form
248 *
249 * @param the FormContent->form() object
250 * @param int - $indent_level
251 * @param int - $output_debug
252 * @param object - the form errors object.
253 * @return raw html
254 */
255 function render_form( $indent_level, $output_debug, $obj=NULL ) {
256
257 //build the $this->_form object.
258 $this->_build_form_tag();
259
260 if ($obj) {
261 $this->_form->add_reference( $obj );
262 }
263
264 $this->_form->add_reference( $this->_form_content->form() );
265
266 //add the FormContent's hidden declared
267 //hidden form fields.
268 $this->_add_hidden_fields();
269
270 //Ok lets add our hidden vars
271 $this->__hidden_fields();
272
273 return $this->_form->render($indent_level, $output_debug);
274 }
275
276 /**
277 * This function renders the confirmation
278 * page. This page sits in between the
279 * front end form, and the action handler.
280 * This only gets called after a form
281 * and its data has been successfully
282 * validated.
283 *
284 * @param int - $indent_level
285 * @param int - $output_debug
286 *
287 * @return string - the raw html
288 */
289 function render_confirm( $indent_level, $output_debug ) {
290 //build the $this->_form object.
291 $this->_build_form_tag();
292
293 //add the confirm object/html
294 $confirm = &$this->_form_content->form_confirm( $this->data );
295 $this->_form->add_reference( $confirm );
296
297 //ok add all of the submitted data as hidden form fields
298 $this->_add_confirm_data();
299
300 //Ok lets add our hidden vars
301 $this->__hidden_fields();
302
303 return $this->_form->render($indent_level, $output_debug);
304 }
305
306
307 /**
308 * This renders the error table
309 * and then the form with the fields
310 *
311 * @param array - the form field vlues.
312 * @param array - array of errors.
313 * @param int - $indent_level
314 * @param int - $output_debug
315 *
316 * @return raw html
317 */
318 function render_error( $indent_level, $output_debug) {
319 $wrapper_div = new DIVtag;
320
321 //Ok first lets build the error table
322 $errors = &$this->_form_content->form_errors();
323 if ($errors != NULL) $wrapper_div->add( $errors, html_br() );
324
325 return $this->render_form( $indent_level, $output_debug, $wrapper_div);
326 }
327
328
329
330
331 //***********************************************//
332 //* utility functions for this class *//
333 //***********************************************//
334
335
336 /**
337 * This method lets us turn on/off the
338 * ability to do validation for the form
339 *
340 * @return BOOLEAN
341 */
342 function can_validate() {
343 return TRUE;
344 }
345
346
347 /**
348 * This function turns on the ability to
349 * render the form after the success
350 * of the action. Normally this feature
351 * is off
352 *
353 */
354 function set_render_form_after_success() {
355 $this->_form_success_render = TRUE;
356 }
357
358
359 /**
360 * this function builds the FORMtag object
361 * and its attributes.
362 *
363 * @return FORMtag object.
364 */
365 function _build_form_tag() {
366 $form_attrs = array("method" => "POST",
367 "action" => $this->_form_action,
368 "name" => $this->_form_name,
369 "style" => "margin: 0px 0px 0px 0px;");
370 if ($this->_enctype != NULL)
371 $form_attrs["enctype"] = $this->_enctype;
372 if ($this->_target != NULL)
373 $form_attrs["target"] = $this->_target;
374 if ($this->_onsubmit != NULL)
375 $form_attrs["onSubmit"] = $this->_onsubmit;
376
377 $this->_form = new FORMtag( $form_attrs );
378 }
379
380 /**
381 * This adds all of the submitted data as
382 * hidden form fields
383 *
384 */
385 function _add_confirm_data() {
386 $keys = array_keys( $this->_form_content->_elements );
387 foreach( $keys as $key ) {
388 $element_name = $this->_form_content->_elements[$key]->get_element_name();
389 $values = $this->_form_content->_elements[$key]->get_value();
390 if (is_array($values)) {
391 foreach($values as $value) {
392 $this->_form->add( form_hidden($element_name, $value) );
393 }
394 } else {
395 $this->_form->add( form_hidden($element_name, $values) );
396
397 }
398 }
399
400 $keys = array_keys( $this->_form_content->_hidden_elements );
401 foreach( $keys as $key ) {
402 $this->_form->add( $this->_form_content->_hidden_elements[$key]->get_element() );
403 }
404 }
405
406
407 /**
408 * This function adds the form content's
409 * hidden form fields to the
410 * form automatically
411 *
412 */
413 function _add_hidden_fields() {
414 //Lets add the form's hidden vars it wants
415 foreach ($this->_form_content->_hidden_elements as $element) {
416 $this->_form->add( $element->get_element() );
417 }
418 }
419
420
421 /**
422 * This method adds the processor specific
423 * hidden fields.
424 *
425 */
426 function __hidden_fields() {
427 $this->_form->add( form_hidden(FORM_ACTION),
428 form_hidden(FORM_VISITED,1) );
429
430 if ($this->_form_content->has_confirm() && !$this->_confirmed) {
431 if (!$_POST[FORM_VISITED] || $this->_has_errors) {
432 $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
433 } else {
434 $this->_form->add( form_hidden(FORM_CONFIRM, 1 ) );
435 }
436 } else if ($this->_form_content->has_confirm() && !$this->_confirmed) {
437 //reset this so they can submit again
438 $this->_form->add( form_hidden(FORM_CONFIRM, 0 ) );
439 }
440 }
441 }
442 ?>

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