form_defs.php 22.2 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#
$footnotes = null;

# The idea behind this is simplify and make all of the form construction
# coherrent across the entire web interface. Steps to creating a form:
#
# 1) Create an array of attributes for the form.
#
#   $form = array('#id'	      => 'myform',	# Optional; default: myform
#	  	  '#action'   => 'foo.php',       # Required
#		  '#method'   => 'POST',          # Optional; default POST
#		  '#class'    => 'standard',      # Required; default standard
#		  '#caption'  => 'A Form',        # Optional; default ''
#		  '#enctype'  => '');             # Optional
#
# 2) Create an array of form fields:
#
#     $fields = array();
#     $fields['Name'] =
#          array('#type'       => 'textfield',	# Required
#		 '#label'      => 'Full Name',  # Required
#		 '#description'=> 'Your Name',  # Optional (extra text)
#                '#link'       => 'foo.html',   # Optional (link for label).
#		 '#size'       => 30,           # Optional
#		 '#maxlength'  => 30,           # Optional
#		 '#required'   => TRUE,         # Optional
49 50
#                '#prep'       => 'trim'        # Optional used by FormValidate
#                '#checkslot'  => 'default:text'# Optional used by FormValidate
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#                '#mouseover'  => 'Mouse Me',   # Optional string over label
#		 '#footnote'   => 'See ...',    # Optional footnote section.
#		 '#javascript' => "...");       # Optional
#
# 3) Call FormRender with the above, plus optional $errors and $values
#    arrays. Both of these are exactly as we use them now:
#
#	FormRender($form, $fields, $errors, $values);
#
#   which spits the html to the output stream.
#
# 4) The 'action' page will get $submit and $formfields as input.
#
# * See form_example.php for a good example usage.
#
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
# 5) A note about javascript. Many existing forms use the convention of
# naming them with name='myform' which is now deprecated. The new way of
# doing this is with id='myform' ... but the problem is that name='myform'
# created a global variable called myform that referenced the javascript
# object, while id='myform' does not. This is not so terrible, since the
# changes are simple.
#
# Change:
#	'#javascript' => "onchange=\"SetWikiName(myform);\"",
# to:
# 	'#javascript' => "onchange=\"SetWikiName('myform');\"",
#
# note that myform is in single quotes. In the function definition, change
# the prolog from:
#
#             function SetWikiName(theform) 
#             {
# to:
#             function SetWikiName(formname) 
#             {
#                 var theform = getObjbyName(formname);
#
#
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
function FormRenderTextField($name, $attrs)
{
    $type = ($attrs['#type'] == "password" ? "password" : "text");
	
    $html = "<input type=$type name=\"formfields[$name]\" ";
    if (isset($attrs['#value'])) {
	$html .= "value=\"" . $attrs['#value'] . "\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-text\" ";
    }
    if (isset($attrs['#size'])) {
	$html .= "size=" . $attrs['#size'] . " ";
    }
    if (isset($attrs['#maxlength'])) {
	$html .= "maxlength=" . $attrs['#maxlength'] . " ";
    }
    $html .= ">";
    
    return $html;
}

function FormRenderCheckBox($name, $attrs)
{
    $html = "<input type=checkbox name=\"formfields[$name]\" ";
120 121 122 123 124
    $return_value = isset($attrs['#return_value']) ? $attrs['#return_value'] : 1;
    $html .= "value=\"" . $return_value . "\" ";
    if (isset($attrs['#value']) &&
	$attrs['#value'] == $return_value) {
	$html .= "checked ";
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-checkbox\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
    $html .= ">";
    
    return $html;
}

function FormRenderRadio($name, $attrs)
{
    $html = "";
    
    while (list ($subname, $subattrs) = each ($attrs['#radios'])) {
	$html .= "<input type=radio name=\"formfields[$name]\" ";
	if (isset($attrs['#class'])) {
	    $html .= "class=\"" . $attrs['#class'] . "\" ";
	}
	else {
	    $html .= "class=\"form-radio\" ";
	}
	if (isset($subattrs['#return_value'])) {
	    $html .= "value=\"" . $subattrs['#return_value'] . "\" ";
	    if (isset($attrs['#value']) &&
		$attrs['#value'] == $subattrs['#return_value']) {
		$html .= "checked ";
	    }
	}
	if (isset($attrs['#javascript'])) {
	    $html .= $attrs['#javascript'] . " ";
	}
	$html .= ">";
	if (isset($subattrs['#label']))
	    $html .= $subattrs['#label'] . " &nbsp; ";
	$html .= "\n";
    }
    return $html;
}

170

171 172
function FormRenderSelect($name, $attrs)
{
173 174 175 176 177 178 179 180
    $html = "<select ";

    if (array_key_exists('#name', $attrs)) {
	$html .= " name='" . $attrs['#name'] . "' ";
    }
    else {
	$html .= " name=\"formfields[$name]\" ";
    }
181 182 183 184 185 186 187 188 189
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-select\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
190 191 192
    if (array_key_exists('#id', $attrs)) {
	$html .= " id='" . $attrs['#id'] . "' ";
    }
193
    $html .= ">\n";
194

195 196 197 198 199 200 201
    if (!@$attrs['#no_default']) {
	if (isset($attrs['#default']))
	    $default = $attrs['#default'];
	else
	    $default = "Please Select";
	$html .= "<option value=''>$default &nbsp</option>\n";
    }
202 203

    if (isset($attrs['#options'])) {
204 205 206 207 208
        $options = $attrs['#options'];
        if (isset($attrs['#value']) && $attrs['#value'] != ''  
            && !isset($options[$attrs['#value']]))
  	    $options[$attrs['#value']] = $attrs['#value'];
	foreach ($options as $selectvalue => $selectlabel) {
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	    $selected = "";	    
	    if (isset($attrs['#value']) && $attrs['#value'] == $selectvalue) {
		$selected = "selected";
	    }
	    $html .= "<option $selected value='$selectvalue'>";
	    $html .= "$selectlabel </option>\n";
	}
    }
    $html .= "</select>\n";
    return $html;
}

function FormRenderFile($name, $attrs)
{
    $html = "<input type=file name=\"$name\" ";
    if (isset($attrs['#value'])) {
	$html .= "value=\"" . $attrs['#value'] . "\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-file\" ";
    }
    if (isset($attrs['#size'])) {
	$html .= "size=" . $attrs['#size'] . " ";
    }
    $html .= ">";
    
    return $html;
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
function FormRenderTextArea($name, $attrs)
{
    $html = "<textarea name=\"formfields[$name]\" ";
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-text\" ";
    }
    if (isset($attrs['#cols'])) {
	$html .= "cols=" . $attrs['#cols'] . " ";
    }
    if (isset($attrs['#rows'])) {
	$html .= "rows=" . $attrs['#rows'] . " ";
    }
    $html .= ">";
    if (isset($attrs['#value'])) {
	$html .= htmlspecialchars($attrs['#value']);
    }
    $html .= "</textarea>\n";
    return $html;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
function FormRenderSubmit($name, $attrs)
{
    $html = "";

    $html .= "<input type=submit name=\"$name\" ";
    if (isset($attrs['#value'])) {
	$html .= "value=\"" . $attrs['#value'] . "\" ";
    }
    else {
	$html .= "value=Submit";
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-submit\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    $html .= ">";
    
    return $html;
}

function FormRenderImage($name, $attrs)
{
    $html = "";
    $img  = $attrs['#image'];

    $html .= "<input type=image name=\"formfields[$name]\" ";
    if (isset($attrs['#value'])) {
	$html .= "value=\"" . $attrs['#value'] . "\" ";
    }
    else {
	$html .= "value=Submit ";
    }
    if (isset($attrs['#class'])) {
	$html .= "class=\"" . $attrs['#class'] . "\" ";
    }
    else {
	$html .= "class=\"form-image\" ";
    }
    if (isset($attrs['#javascript'])) {
	$html .= $attrs['#javascript'] . " ";
    }
    $html .= "src=$img ";
    $html .= ">";
318 319 320 321 322 323 324 325 326 327 328 329 330
    
    return $html;
}

# Render a subsection (table) of a form.
function FormRenderTable($name, $attributes, $submitted)
{
    $html = "<table align=center border=1>\n";
    $html .= FormRenderElements($attributes['#fields'], $submitted);
    $html .= "</table>\n";
    return $html;
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
# Render a list of elements together.
function FormRenderList($name, $attributes, $submitted)
{
    $html = "";
    
    while (list ($subname, $subattrs) = each ($attributes['#elements'])) {
	if ($submitted && array_key_exists($subname, $submitted)) {
	    $subattrs['#value'] = $submitted[$subname];
	}
	$html .= FormRenderElement($subname, $subattrs, $submitted);
	if (isset($subattrs['#label']))
	    $html .= $subattrs['#label'] . " &nbsp; ";
	$html .= " &nbsp; ";
    }
    return $html;
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
# Render a list of elements together, vertically
function FormRenderVList($name, $attributes, $submitted)
{
    $html = "";
    
    while (list ($subname, $subattrs) = each ($attributes['#elements'])) {
	if ($submitted && array_key_exists($subname, $submitted)) {
	    $subattrs['#value'] = $submitted[$subname];
	}
	if (isset($subattrs['#label']))
	    $html .= $subattrs['#label'] . ": ";
	$html .= FormRenderElement($subname, $subattrs, $submitted);
	$html .= "<br>";
    }
    return $html;
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
function FormRenderElement($name, $attributes, $submitted)
{
    $field_html = null;

    #
    # The value that was submitted overrides the value in the attributes.
    # For most things, we just munge the attributes field.
    #
    if ($submitted && array_key_exists($name, $submitted)) {
	$attributes['#value'] = $submitted[$name];
    }
    
    switch ($attributes['#type']) {
    case "textfield":
    case "password":
	$field_html = FormRenderTextField($name, $attributes);
	break;
    case "hidden":
	$value = $attributes['#value'];
384 385 386
	$field_html .=
	    "<input type=hidden name=\"formfields[$name]\" ".
	    "value=\"$value\">\n";
387 388 389 390
	break;
    case "submit":
	$field_html = FormRenderSubmit($name, $attributes);
	break;
391 392 393
    case "image":
	$field_html = FormRenderImage($name, $attributes);
	break;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    case "checkbox":
	$field_html = FormRenderCheckBox($name, $attributes);
	break;
    case "radio":
	$field_html = FormRenderRadio($name, $attributes);
	break;
    case "file":
	$field_html = FormRenderFile($name, $attributes);
	break;
    case "checkboxes":
	while (list ($subname, $subattrs) = each ($attributes['#boxes'])) {
	    if ($submitted && array_key_exists($subname, $submitted)) {
		$subattrs['#value'] = $submitted[$subname];
	    }
	    $field_html .= FormRenderCheckBox($subname, $subattrs);
	    if (isset($subattrs['#label']))
		$field_html .= $subattrs['#label'] . " &nbsp; ";
	}
	break;
    case "select":
	$field_html = FormRenderSelect($name, $attributes);
	break;
    case "table":
	$field_html = FormRenderTable($name, $attributes, $submitted);
	break;
419 420 421
    case "list":
	$field_html = FormRenderList($name, $attributes, $submitted);
	break;
422 423 424 425 426 427 428 429 430
    case "vlist":
	$field_html = FormRenderVList($name, $attributes, $submitted);
	break;
    case "textarea":
	$field_html = FormRenderTextArea($name, $attributes, $submitted);
	break;
    case "display":
	$field_html = isset($submitted[$name]) ? $submitted[$name] : $attributes['#value'];
	break;
431 432 433 434 435 436 437 438 439
    }
    return $field_html;
}

function FormRenderElements($fields, $submitted)
{
    global $footnotes;
    $html = "";
    
440
    foreach ($fields as $name => $attributes) {
441 442 443 444 445 446 447 448
	$field_html = FormRenderElement($name, $attributes, $submitted);

	if ($field_html) {
	    if ($attributes['#type'] == "hidden") {
		$html .= $field_html;
		continue;
	    }
	    $cols  = "";
449
	    if ($attributes['#type'] == "table" || $attributes['#type'] == "textarea")
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		$cols = "colspan=2";

	    $mouseover = "";
	    if (isset($attributes['#mouseover'])) {
		$mouseover = FormRenderMouseOver($attributes['#mouseover']);
	    }

	    # Record footnote for later.
	    $footnote = "";
	    if (isset($attributes['#footnote'])) {
		$thisnote = 0;
		
		#
		# Slight complication; we want to be able to specify the
		# same footnote multiple times, but show it only once.
		#
		foreach ($footnotes as $i => $note) {
		    if ($note == $attributes['#footnote']) {
			$thisnote = $i + 1;
			break;
		    }
		}
		if (! $thisnote) {
		    $thisnote = count($footnotes) + 1;
		    $footnotes[] = $attributes['#footnote'];
		}
		$footnote = "[<b>$thisnote</b>]";
	    }
		
	    $html .= "<tr>";
480
	    if ($attributes['#type'] != "submit" &&
481
                isset($attributes['#label']) &&
482
		!isset($attributes['#colspan'])) {
483
		$html .= "<td align=left $mouseover $cols>";
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

		# Required fields mark with *
		if (isset($attributes['#required']) &&
		    $attributes['#required'])
		    $html .= "*";

		$label = $attributes['#label'];
		if (isset($attributes['#link']))
		    $label = "<a href=\"" . $attributes['#link'] . "\"" .
			"target=_blank>$label</a>";
		$html .= $label . $footnote . ": ";
		if (isset($attributes['#description']))
		    $html .= "<br><font size=-2>(" .
			$attributes['#description'] . ")";
		if ($attributes['#type'] == "table") {
		    $html .= "$field_html</td>";
500 501 502
		} else if ($attributes['#type'] == "textarea") {
	 	    $html .= "<br>$field_html</td>";
                } else {
503
		    $html .= "</td>";
504
		    $html .= "<td align=left>$field_html</td>";
505 506
		}
	    }
507 508
	    else {
		$html .= "<td align=center colspan=2>";
509
		$html .= "$field_html";
510 511
		$html .= "</td>\n";
	    }
512 513 514 515 516 517 518 519
	    $html .= "</tr>\n";
	}
    }
    return $html;
}

function FormRenderMouseOver($string)
{
520 521
    $string = str_replace("\n", "<br>", $string);
    $string = str_replace("\r", "", $string);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    $string = htmlentities($string);
    $string = preg_replace("/\'/", "\&\#039;", $string);

    return "onmouseover=\"return escape('$string')\"";
}

function FormRender($attributes, $errors, $fields, $submitted = null)
{
    global $footnotes;
    
    $html = "";
    $footnotes = array();

    $action = $attributes['#action'];
    $id = "myform";
    if (array_key_exists('#id', $attributes))
	$id = $attributes['#id'];
    $caption = null;
    if (array_key_exists('#caption', $attributes))
	$caption = $attributes['#caption'];
    $method = "POST";
    if (array_key_exists('#method', $attributes))
	$method = $attributes['#method'];
    $enctype = "";
    if (array_key_exists('#enctype', $attributes))
	$enctype = "enctype=" . $attributes['#enctype'];
    $class = "standard";
    if (array_key_exists('#class', $attributes))
	$class = $attributes['#class'];

    if ($errors) {
	$html .= "<table class=nogrid border=0 cellpadding=6 cellspacing=0>";
	$html .= "<tr><th align=center colspan=2>";
	$html .= "<font size=+1 color=red>";
	$html .= "&nbsp;Oops, please fix the following errors!&nbsp;";
	$html .= "</font></td></tr>\n";
	
	while (list ($name, $message) = each ($errors)) {
	    $html .= "<tr><td align=right>";
	    $html .= "<font color=red>$name:&nbsp;</font></td>";
	    $html .= "<td align=left><font color=red>$message</font></td>";
	    $html .= "</tr>\n";
	}
	$html .= "</table><br><br>\n";
566
	reset($errors);
567
    }
568
    $html .= "<div align=center>\n";
569 570 571 572 573 574 575 576 577 578 579 580 581
    $html .= "<table align=center border=1>\n";
    if ($caption)
	$html .= "<caption>$caption</caption>\n";
    $html .= "<tr><td align=center colspan=2>";
    $html .= "<em>(Fields marked with * are required)</em></td></tr>";

    $html .= "<form action='$action' $enctype method=$method id=$id ";
    $html .= "class='$class'>";
    $html .= FormRenderElements($fields, $submitted);
    $html .= "</form>";
    $html .= "</table>\n";

    if (count($footnotes)) {
582 583
	$html .= "<div style=\"width:90%; margin:auto; text-align: left\">\n";
	$html .= "<font size=-1><ol>\n";
584
	foreach ($footnotes as $i => $note) {
585
	    $html .= "<li>$note</li>\n";
586
	}
587
	$html .= "</ol></font></div>\n";
588
    }
589
    $html .= "</div>\n";
590 591 592 593
    
    echo "$html\n";
}

594 595 596 597
#
# FormValidate and helper functions
#

598 599 600 601 602 603 604 605 606 607
function CombineLabels($parent, $attributes) {
    $res = '';
    $label = @$attributes['#label'];
    $res .= $parent;
    if ($parent != '' && $label != '') $res .= ', ';
    $res .= $label;
    return $res;
}

function FormValidateElement($name, &$errors, $attributes, &$submitted, $parent_label)
608
{
609
    $error_label = CombineLabels($parent_label, $attributes);
610
    # Check for required fields not filled out
611 612 613 614
    if (isset($attributes['#prep']) && 
	isset($submitted[$name]) && $submitted[$name] != "") {
        $submitted[$name] = $attributes['#prep']($submitted[$name]);
    }
615 616
    if (isset($attributes['#required']) && $attributes['#required'] &&
	!(isset($submitted[$name]) && $submitted[$name] != "")) {
617
	$errors[$error_label] = "Missing required value";
618
    }
619 620
    elseif (isset($attributes['#checkslot']) &&
	    isset($submitted[$name]) && $submitted[$name] != "") {
621 622 623 624
	$check = $attributes['#checkslot'];
	
	if (function_exists($check)) {
	    $check($name, $errors, $attributes, $submitted[$name]);
625
	}
626 627 628 629 630 631
	elseif (preg_match("/^([-\w]+):([-\w]+)$/", $check, $matches)) {
	    #
	    # What if not required and not set?
	    #
	    if (!isset($submitted[$name]) || $submitted[$name] == "") {
		$submitted[$name] = "";
632
	    }
633 634 635 636
	    if (!TBcheck_dbslot($submitted[$name],
				$matches[1], $matches[2],
				TBDB_CHECKDBSLOT_WARN|
				TBDB_CHECKDBSLOT_ERROR)) {
637
		$errors[$error_label] = TBFieldErrorString();
638 639 640 641 642
	    }
	}
	elseif (substr($check, 0, 1) == "/") {
	    # Regular expression.
	    if (!preg_match($check, $submitted[$name])) {
643
		$errors[$error_label] = "Illegal characters";
644 645 646 647 648 649 650 651
	    }
	}
	else {
	    TBERROR("Could not parse checkslot: $check", 1);
	}
    }
}

652 653
function FormValidateFileUpload($name, &$errors, $attributes)
{
654
    $error_label = CombineLabels($parent_label, $attributes);
655 656 657
    # Check for required fields not filled out
    if (isset($attributes['#required']) && $attributes['#required'] &&
	!(isset($_FILES[$name]['name']) && $_FILES[$name]['size'] != 0)) {
658
	$errors[$error_label] = "Missing required value";
659 660 661 662 663 664 665 666 667 668 669 670 671
    }
    elseif (isset($attributes['#checkslot'])) {
	$check = $attributes['#checkslot'];
	
	if (function_exists($check)) {
	    $check($name, $errors, $attributes, null);
	}
	else {
	    TBERROR("Could not parse checkslot: $check", 1);
	}
    }
}

672
function FormValidate($form, &$errors, $fields, &$submitted, $parent_label = '') 
673
{
674
    foreach ($fields as $name => $attributes) {
675 676 677 678 679 680 681 682
	switch ($attributes['#type']) {
	case "textfield":
	case "password":
	case "hidden":
	case "submit":
	case "checkbox":
	case "radio":
	case "select":
683 684
        case "textarea":
	    FormValidateElement($name, $errors, $attributes, $submitted, $parent_label);
685 686 687
	    break;
	case "checkboxes":
	    while (list ($subname, $subattrs) = each ($attributes['#boxes'])) {
688 689
		FormValidateElement($subname, $errors, $subattrs, $submitted,
		                    CombineLabels($parent_label, $attributes));
690 691 692
	    }
	    break;
	case "table":
693 694
	    FormValidate($form, $errors, $attributes['#fields'], $submitted,
                         CombineLabels($parent_label, $attributes));
695 696
	    break;
	case "list":
697
        case "vlist":
698 699
	    while (list ($subname, $subattrs) =
		   each ($attributes['#elements'])) {
700 701
		FormValidateElement($subname, $errors, $subattrs, $submitted,
                                    CombineLabels($parent_label, $attributes));
702
	    }
703
	    break;
704
	case "file":
705
	    FormValidateFileUpload($name, $errors, $attributes, $parent_label);
706
	    break;
707 708
        case "display":
            break;
709 710 711
	default:
	    $errors[$name] = "Invalid slot type: " . $attributes['#type'];
	    break;
712 713 714
	}
    }
}
715

716 717 718 719
#
# FormTextDump and helper functions
#

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
function FormTextDumpElement($name, $attributes, $values, $label_width, $parent_label)
{
    $res = '';
    if (!isset($values[$name]) || $values[$name] == '' || @$attributes['#nodump'])
        return $res;
    $label = CombineLabels($parent_label, $attributes);
    $type = $attributes['#type'];
    $value = $values[$name];
    if ($type == 'checkbox')
        $value = $values[$name] ? 'true' : 'false';
    if ($type == 'select' && isset($attributes['#options'][$value]))
        $value = $attributes['#options'][$value];
    if ($type == 'textarea') {
      $res .= "$label: \n";
      $res .= $value;
    } else {
      $res .= sprintf("%-${label_width}s %s\n", "$label:", $value);
    }
    return $res;
}

function FormTextDump($form, $fields, $values, $label_width = 20, $parent_label = '') 
{
    $res = '';
744
    foreach ($fields as $name => $attributes) {
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	switch ($attributes['#type']) {
	case "hidden":
	case "textfield":
	case "password":
	case "submit":
	case "checkbox":
	case "radio":
	case "select":
	case "textarea":
        case "display":
	    $res .= FormTextDumpElement($name, $attributes, $values, 
                                        $label_width, $parent_label);
	    break;
	case "checkboxes":
	    while (list ($subname, $subattrs) = each ($attributes['#boxes'])) {
		FormTextDumpElement($subname, $subattrs, $values, $label_width,
		                    CombineLabels($parent_label, $attributes));
	    }
	    break;
	case "table":
	    $res .= FormTextDump($form, $attributes['#fields'], $values, $label_width,
                                 CombineLabels($parent_label, $attributes));
	    break;
	case "list":
	case "vlist":
	    while (list ($subname, $subattrs) =
		   each ($attributes['#elements'])) {
		$res .= FormTextDumpElement($subname, $subattrs, $values, $label_width,
                                            CombineLabels($parent_label, $attributes));
	    }
	    break;
	case "file":
	    # Skip for now
	    break;
	default:
	    user_error("Invalid slot type \"". $attributes['#type'] ."\" in FormTextDump",
                       E_USER_NOTICE);
	    break;
	}
    }
    return $res;
}
787
?>