Commit a35e40cb authored by Leigh Stoller's avatar Leigh Stoller

All new and improved form definition support. All forms will be implemented

declaritively, and then rendered by the support code in form_defs.php. The
best thing to do is look at newosid.php with this checkin, but there is also
some good code in form_example.php.
parent 2187fa3b
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
# All rights reserved.
#
$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
# '#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.
#
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]\" ";
if (isset($attrs['#return_value'])) {
$html .= "value=\"" . $attrs['#return_value'] . "\" ";
if (isset($attrs['#value']) &&
$attrs['#value'] == $attrs['#return_value']) {
$html .= "checked ";
}
}
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;
}
function FormRenderSelect($name, $attrs)
{
$html = "<select name=\"formfields[$name]\" ";
if (isset($attrs['#class'])) {
$html .= "class=\"" . $attrs['#class'] . "\" ";
}
else {
$html .= "class=\"form-select\" ";
}
if (isset($attrs['#javascript'])) {
$html .= $attrs['#javascript'] . " ";
}
$html .= ">\n";
$html .= "<option value=''>Please Select &nbsp</option>\n";
if (isset($attrs['#options'])) {
while (list ($selectvalue, $selectlabel) = each ($attrs['#options'])) {
$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;
}
function FormRenderSubmit($name, $attrs)
{
$html = "";
$html .= "<td align=center colspan=2>";
$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'] . " ";
}
$html .= "></td>";
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;
}
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'];
$field_html .= "<input type=hidden name=$name value=\"$value\">\n";
break;
case "submit":
$field_html = FormRenderSubmit($name, $attributes);
break;
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;
}
return $field_html;
}
function FormRenderElements($fields, $submitted)
{
global $footnotes;
$html = "";
while (list ($name, $attributes) = each ($fields)) {
$field_html = FormRenderElement($name, $attributes, $submitted);
if ($field_html) {
if ($attributes['#type'] == "hidden") {
$html .= $field_html;
continue;
}
$cols = "";
if ($attributes['#type'] == "table")
$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>";
if ($attributes['#type'] != "submit") {
$html .= "<td $mouseover $cols>";
# 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>";
}
else {
$html .= "</td>";
$html .= "<td>$field_html</td>";
}
}
else
$html .= "$field_html";
$html .= "</tr>\n";
}
}
return $html;
}
function FormRenderMouseOver($string)
{
$string = ereg_replace("\n", "<br>", $string);
$string = ereg_replace("\r", "", $string);
$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";
}
$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)) {
$html .= "<div align=left><h4><ol>\n";
foreach ($footnotes as $i => $note) {
$html .= "<li>$note\n";
}
$html .= "</ol></h4></div>\n";
}
echo "$html\n";
}
function FormValidate($form, &$errors, $fields, $submitted)
{
while (list ($name, $attributes) = each ($fields)) {
# Check for required fields not filled out
if (isset($attributes['#required']) && $attributes['#required'] &&
!(isset($submitted[$name]) && $submitted[$name] != "")) {
$errors[$attributes['#label']] = "Missing required value";
}
else if (isset($attributes['#checkslot']) &&
isset($submitted[$name]) && $submitted[$name] != "") {
# Check slot
if (preg_match("/^([-\w]+):([-\w]+)$/",
$attributes['#checkslot'], $matches)) {
if (!TBcheck_dbslot($submitted[$name],
$matches[1], $matches[2],
TBDB_CHECKDBSLOT_WARN|
TBDB_CHECKDBSLOT_ERROR)) {
$errors[$attributes['#label']] = TBFieldErrorString();
}
}
else {
TBERROR("Could not parse " . $attributes['#checkslot'], 1);
}
}
}
}
?>
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include("form_defs.php");
PAGEHEADER("Silly Forms example");
#
# Only known and logged in users.
#
$this_user = CheckLoginOrDie();
$uid = $this_user->uid();
$optargs = OptionalPageArguments("submit", PAGEARG_STRING,
"formfields", PAGEARG_ARRAY);
if (!isset($formfields)) {
$formfields = null;
}
# The form attributes:
$form = array('#id' => 'form1',
'#caption' => 'My Form',
'#action' => 'form_example.php');
# A set of form fields.
$fields = array();
# Text field.
$fields['slot1'] = array('#type' => 'textfield',
'#label' => 'Slot1',
'#value' => 'Hello World',
'#size' => 60,
'#maxlength' => 64,
'#description' => 'Alphanumeric, No Blanks'
);
# Password
$fields['slot2'] = array('#type' => 'password',
'#label' => 'Password',
'#required' => TRUE,
'#footnote' => 'This is a footnote',
'#size' => 8);
# File Upload. You must set '#entype' in the form array.
$fields['myfile'] = array('#type' => 'file',
'#label' => 'Your File',
'#size' => 30,
'#description' => 'An NS File'
);
# A plain checkbox
$fields['slot3'] = array('#type' => 'checkbox',
'#return_value'=> "Yep",
'#label' => 'Check this Box',
);
# A list of checkboxes
$fields['slot4'] = array('#type' => 'checkboxes',
'#label' => 'Check some of these',
'#boxes' =>
array('box1' => array('#return_value' => "Yep",
'#label' => 'L1'
),
'box2' => array('#return_value' => "Yep",
'#label' => 'L2'
),
'box3' => array('#return_value' => "Yep",
'#label' => 'L3'
),
),
);
# A radio checklist.
$fields['slot5'] = array('#type' => 'radio',
'#label' => 'Check one of these',
'#radios' =>
array('Rad1' => array('#label' => 'R1',
'#return_value' => "R1v",
),
'Rad2' => array('#label' => 'R2',
'#return_value' => "R2v",
),
'Rad3' => array('#label' => 'R3',
'#return_value' => "R3v",
),
),
);
# A selection list.
$fields['slot6'] = array('#type' => 'select',
'#label' => 'A selection of items',
'#default_value' => 'B',
'#options' => array('A' => 'Pick A',
'B' => 'Pick B',
'C' => 'Pick C'),
'#description'=> 'Bla Bla Bla'
);
# A hidden Field.
$fields['slot7'] = array('#type' => 'hidden',
'#value' => "69",
);
# A simple subsection (rendered as an inner table).
$fields['address'] = array('#type' => 'table',
'#label' => 'Fill in your addresss',
'#fields' =>
array('part1' => array('#type' => 'textfield',
'#label' => 'Street',
'#size' => 60,
),
'part2' => array('#type' => 'textfield',
'#label' => 'Town',
'#value' => 'Corvallis',
'#size' => 30,
),
),
);
# The submit button.
$fields['submit'] = array('#type' => 'submit',
'#value' => "Submit This Form",
);
echo "<center>";
FormRender($form, null, $fields, $formfields);
echo "</center>";
#
# Standard Testbed Footer
#
PAGEFOOTER();
?>
......@@ -7,6 +7,7 @@
include("defs.php3");
include_once("osinfo_defs.php");
include("osiddefs.php3");
include("form_defs.php");
#
# Standard Testbed Header
......@@ -36,284 +37,191 @@ if (! count($projlist)) {
USERERROR("You do not appear to be a member of any Projects in which ".
"you have permission to create new OS Descriptors!", 1);
}
$projselection = array();
while (list($project) = each($projlist)) {
$projselection[$project] = $project;
}
#
# Spit the form out using the array of data.
#
function SPITFORM($formfields, $errors)
{
global $this_user, $projlist, $isadmin;
global $osid_opmodes, $osid_oslist, $osid_featurelist;
global $TBDB_OSID_OSNAMELEN, $TBDB_OSID_OSNAMELEN;
global $TBDB_OSID_VERSLEN, $TBDB_OSID_VERSLEN, $TBBASE;
if ($errors) {
echo "<table class=nogrid
align=center border=0 cellpadding=6 cellspacing=0>
<tr>
<th align=center colspan=2>
<font size=+1 color=red>
&nbsp;Oops, please fix the following errors!&nbsp;
</font>
</td>
</tr>\n";
while (list ($name, $message) = each ($errors)) {
echo "<tr>
<td align=right>
<font color=red>$name:&nbsp;</font></td>
<td align=left>
<font color=red>$message</font></td>
</tr>\n";
}
echo "</table><br>\n";
}
echo "<br>
<table align=center border=1>
<tr>
<td align=center colspan=2>
<em>(Fields marked with * are required)</em>
</td>
</tr>
<form action='newosid.php3' method=post name=idform>\n";
#
# Select Project
#
echo "<tr>
<td>*Select Project:</td>
<td><select name=\"formfields[pid]\">
<option value=''>Please Select &nbsp</option>\n";
while (list($project) = each($projlist)) {
$selected = "";
# Define the form.
#
$form = array('#id' => 'myform',
'#action' => "newosid.php3");
$fields = array();
if ($formfields["pid"] == $project)
$selected = "selected";
echo " <option $selected value='$project'>$project </option>\n";
}
echo " </select>";
echo " </td>
</tr>\n";
#
# Project Name:
#
$fields['pid'] = array('#type' => 'select',
'#label' => 'Project',
'#options' => $projselection,
'#checkslot' => 'os_info:pid',
'#required' => TRUE);
#
# OS Name
#
$fields['osname'] = array('#type' => 'textfield',
'#label' => 'Descriptor Name',
'#description'=> 'alphanumeric, no spaces',
'#size' => $TBDB_OSID_OSNAMELEN,
'#maxlength' => $TBDB_OSID_OSNAMELEN,
'#checkslot' => 'os_info:osname',
'#required' => TRUE);
#
# OS Name
#
echo "<tr>
<td>*Descriptor Name (no blanks):</td>
<td><input type=text
name=\"formfields[osname]\"
value=\"" . $formfields["osname"] . "\"
size=$TBDB_OSID_OSNAMELEN
maxlength=$TBDB_OSID_OSNAMELEN>
</td>
</tr>\n";
#
# Description
#
$fields['description'] =
array('#type' => 'textfield',
'#label' => 'Description',
'#description'=> 'a short pithy sentence',
'#size' => 50,
'#checkslot' => 'os_info:description',
'#required' => TRUE);
#
# Description
#
echo "<tr>
<td>*Description:<br>
(a short pithy sentence)</td>
<td class=left>
<input type=text
name=\"formfields[description]\"
value=\"" . $formfields["description"] . "\"
size=50>
</td>
</tr>\n";
#
# Select an OS
#
$OSselection = array();
while (list ($os, $userokay) = each($osid_oslist)) {
if (!$userokay && !$isadmin)
continue;
$OSselection[$os] = $os;
}
$fields['OS'] = array('#type' => 'select',
'#label' => 'Select OS',
'#options' => $OSselection,
'#checkslot' => 'os_info:OS',
'#required' => TRUE);
#
# Select an OS
#
echo "<tr>
<td>*Select OS:</td>
<td><select name=\"formfields[OS]\">\n";
#