\r\n\r\n"; // DATABASE $prefixData = 'survey_'; $database = pg_connect('dbname=web'); $databasetype = 1; // 1 = PostgreSQL, // 2 = MySQL (untested). // UNITS $units['Currency'] = array('GBP', 'EUR', 'US$'); $units['Length'] = array('kilometre', 'metre', 'centimetre', 'millimetre', 'mile', 'yard', 'foot', 'inch'); $units['Area'] = array('km^2', 'hectare', 'm^2', 'cm^2', 'mm^2', 'mile^2', 'acre', 'foot^2', 'inch^2'); $units['Volume'] = array('kilolitre', 'litre', 'millilitre', 'brpint', 'fluidounce', 'm^2', 'cm^2', 'mm^2'); $units['Weight'] = array('kilogram', 'gram', 'pound', 'ounce'); $units['Time'] = array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'); $units['Speed'] = array('km/hour', 'miles/hour', 'm/s'); $prefunit['Currency'] = 'GBP'; $prefunit['Length'] = 'metre'; $prefunit['Area'] = 'm^2'; $prefunit['Volume'] = 'litre'; $prefunit['Weight'] = 'kilogram'; $prefunit['Time'] = 'second'; $prefunit['Speed'] = 'm/s'; $unitspath = '/usr/bin/units'; /*************************************************** * draw_% functions ***************************************************/ function draw_Widget ($params=array(), $data='', $answers=array(), $ret=0) { global $errors; $w = $params[0]; if ($w=='') return NULL; if (function_exists("draw_{$w}")) { $retval = call_user_func("draw_{$w}", $params, $data, $answers, $ret); if ($ret) return $retval; print $retval; } else { $errors[] = "Did not understand widget {$w}"; if ($ret) return NULL; } } function draw_Heading ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($id, $txt) = explode('=', $data); $idstr = ''; if ($id) $idstr = ' id="' . $prefixIdent . $id . '"'; if ($params[0]=='Heading') $tag = 'h2'; else $tag = 'h3'; $retval = "<{$tag}{$idstr} class=\"{$prefixStyle}{$params[0]}\">" . htmlspecialchars($txt) . "\n"; if ($ret) return $retval; else echo $retval; } function draw_Subheading ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Heading ($params, $data, $answers, $ret); } function draw_Note ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $retval = "
$data
\n"; if ($ret) return $retval; else echo $retval; } function draw_BigNote ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Note ($params, $data, $answers, $ret); } function draw_Rule ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $retval = "
\n"; if ($ret) return $retval; else echo $retval; } function draw_Text ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; if ($params[0]=='Text' || $params[0]=='SmallText') if ($params[2]>0) $mlstr = ' maxlength="' . $params[2] . '"'; $brstr = '
'; if ($params[0]=='SmallText' || $params[0]=='Integer' || $params[0]=='Date' ) $brstr = ''; $retval = ''; $retval .= "
\n"; $retval .= " {$brstr}\n"; $retval .= " {$brstr}\n"; if ($params[0]=='Date') $retval .= " (Enter a date in an unambiguous format.)\n"; if ($params[0]=='Integer' && $params[1]>0 && $params[2]>0) $retval .= " (Enter a number between {$params[1]} and {$params[2]}.)\n"; elseif ($params[0]=='Integer') $retval .= " (Enter a number.)\n"; if ($params[0]=='Email') $retval .= " (Enter an e-mail address, including the @-sign.)\n"; if ($params[0]=='WWW') $retval .= " (Enter a website address, including the \"http://\".)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_SmallText ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Text ($params, $data, $answers, $ret); } function draw_Integer ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Text ($params, $data, $answers, $ret); } function draw_Date ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Text ($params, $data, $answers, $ret); } function draw_Email ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Text ($params, $data, $answers, $ret); } function draw_WWW ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_Text ($params, $data, $answers, $ret); } function draw_Units ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight, $units; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " {$brstr}\n"; $retval .= " \n"; $retval .= " \n"; $dimension = $params[1]; $n = 0; while ($u = array_shift($units[$dimension])) { $selstr = ''; if ($answers["{$code}_origunit"]==$u) $selstr = ' selected'; elseif (!isset($answers["{$code}_origunit"]) && $params[2]==$n) $selstr = ' selected'; $retval .= " " . htmlspecialchars($u) . "\n"; $n++; } $retval .= " \n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_BigText ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= "
\n"; $retval .= " \n"; if ($params[0]=='RichText') $retval .= "
(Accepts HTML input.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_RichText ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_BigText ($params, $data, $answers, $ret); } function draw_SingleRadio ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; if ($params[1] == 'Compact') $brstr = '     '; else $brstr = '
'; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "{$brstr}\n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; $selstr = ''; if ($answers[$code]==$c) $selstr = ' checked'; $retval .= " \n" . " {$brstr}\n"; } $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_Single ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_SingleRadio ($params, $data, $answers, $ret); } function draw_SingleDropdown ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $brstr = '
'; if ($params[1]=='Compact' && $params[0]!='SingleList') $brstr = ''; if ($params[0]=='SingleList') $liststr = ' size="6"'; $retval = ''; $retval .= "
\n"; $retval .= " {$brstr}\n"; $retval .= " \n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; $selstr = ''; if ($answers[$code]==$c) $selstr = ' selected'; $retval .= " " . htmlspecialchars($l) . "\n"; } $retval .= " \n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_SingleList ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_SingleDropdown ($params, $data, $answers, $ret); } function draw_YesNo ( $params=array(), $data='', $answers=array(), $ret=0) { $P = array('Single', 'Compact'); $D = $data . "\nyes=Yes\nno=No"; return draw_Single ($P, $D, $answers, $ret); } function draw_MultiCheckbox ($params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "
\n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; $selstr = ''; if ($answers["{$code}_{$c}"]=='on') $selstr = ' checked'; $retval .= " \n" . "
\n"; } if ($params[1]>0 || $params[2]>0) $retval .= " (Minimum $params[1], maximum $params[2] choices.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_Multi ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_MultiCheckbox ($params, $data, $answers, $ret); } function draw_GridMulti ($params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $textboxes = explode(',', $params[3]); foreach ($textboxes as $t) $txt[$t] = 1; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "
\n"; while ($d = array_shift($D)) { if (strtoupper($d)=='COLS') $axis = 'x'; elseif (strtoupper($d)=='ROWS') $axis = 'y'; else { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option on axis $axis must have a code."; if ($l=='') $errors[] = "draw: option on axis $axis have a label."; $carr[$axis][] = $c; $larr[$axis][] = $l; } } $xclass = 'x1'; $yclass = 'y1'; $retval .= " \n"; $retval .= " \n"; $retval .= " "; $retval .= ""; for ($i=0; isset($larr['x'][$i]); $i++) { $xclass = ($xclass=='x1')?'x2':'x1'; $retval .= ""; } $retval .= "\n"; $retval .= " \n"; $retval .= " \n"; for ($j=0; isset($larr['y'][$j]); $j++) { $xclass = 'x2'; $yclass = ($yclass=='y1')?'y2':'y1'; $retval .= " "; $retval .= ""; for ($i=0; isset($larr['x'][$i]); $i++) { $xclass = ($xclass=='x1')?'x2':'x1'; $retval .= ""; } $retval .= "\n"; } $retval .= " \n"; $retval .= "
 {$larr['x'][$i]}
{$larr['y'][$j]}"; $X = $carr['x'][$i]; $Y = $carr['y'][$j]; $Z = $code; if ($params[0]=='GridMulti' && $txt[$i]==1) { $val = $answers["{$Z}_{$Y}_{$X}"]; $retval .= ""; } else { if ($params[0]=='GridMulti') { $selstr = ''; if ($answers["{$Z}_{$Y}_{$X}"]=='on') $selstr = ' checked'; $retval .= ""; } elseif ($params[0]=='GridSingle') { $selstr = ''; if ($answers["{$Z}_{$Y}"]==$X) $selstr = ' checked'; $retval .= ""; } elseif ($params[0]=='GridSingleSingle') { $selstr = ''; if ($answers[$Z]=="{$Y}_{$X}") $selstr = ' checked'; $retval .= ""; } } $retval .= "
\n"; if ($params[0]=='GridMulti' && ($params[1]>0 || $params[2]>0)) $retval .= " (Minimum $params[1], maximum $params[2] choices.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function draw_GridSingle ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_GridMulti ($params, $data, $answers, $ret); } function draw_GridSingleSingle ( $params=array(), $data='', $answers=array(), $ret=0) { return draw_GridMulti ($params, $data, $answers, $ret); } /*************************************************** * rpt_% functions ***************************************************/ function rpt_Widget ($params=array(), $data='', $answers=array(), $ret=0) { global $errors; $w = $params[0]; if ($w=='') return NULL; if (function_exists("rpt_{$w}")) { $retval = call_user_func("rpt_{$w}", $params, $data, $answers, $ret); if ($ret) return $retval; print $retval; } else { $errors[] = "Did not understand widget {$w}"; if ($ret) return NULL; } } function rpt_Heading ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($id, $txt) = explode('=', $data); $idstr = ''; if ($id) $idstr = ' id="' . $prefixIdent . $id . '"'; if ($params[0]=='Heading') $tag = 'h2'; else $tag = 'h3'; $retval = "<{$tag}{$idstr} class=\"{$prefixStyle}{$params[0]}\">" . htmlspecialchars($txt) . "\n"; if ($ret) return $retval; else echo $retval; } function rpt_Subheading ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Heading ($params, $data, $answers, $ret); } function rpt_Note ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $retval = "
$data
\n"; if ($ret) return $retval; else echo $retval; } function rpt_BigNote ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Note ($params, $data, $answers, $ret); } function rpt_Rule ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $retval = "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_Text ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; if ($params[0]=='Text' || $params[0]=='SmallText') if ($params[2]>0) $mlstr = ' maxlength="' . $params[2] . '"'; $brstr = '
'; if ($params[0]=='SmallText' || $params[0]=='Integer' || $params[0]=='Date' ) $brstr = ''; $x = htmlspecialchars($answers[$code]); if ($params[0]=='Email') $x = "<{$answers[$code]}>"; if ($params[0]=='WWW') $x = "<URL:{$answers[$code]}>"; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "{$brstr}\n"; $retval .= " {$x}    {$brstr}\n"; if ($params[0]=='Date') $retval .= " (Enter a date in an unambiguous format.)\n"; if ($params[0]=='Integer' && $params[1]>0 && $params[2]>0) $retval .= " (Enter a number between {$params[1]} and {$params[2]}.)\n"; elseif ($params[0]=='Integer') $retval .= " (Enter a number.)\n"; if ($params[0]=='Email') $retval .= " (Enter an e-mail address, including the @-sign.)\n"; if ($params[0]=='WWW') $retval .= " (Enter a website address, including the \"http://\".)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_SmallText ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Text ($params, $data, $answers, $ret); } function rpt_Integer ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Text ($params, $data, $answers, $ret); } function rpt_Date ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Text ($params, $data, $answers, $ret); } function rpt_Email ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Text ($params, $data, $answers, $ret); } function rpt_WWW ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_Text ($params, $data, $answers, $ret); } function rpt_Units ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight, $units, $prefunit; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "\n"; $retval .= " ".htmlspecialchars($answers["{$code}_origval"]).' '.htmlspecialchars($answers["{$code}_origunit"])."\n"; $retval .= " (".htmlspecialchars($answers[$code]).' '.htmlspecialchars($prefunit[$params[1]]).")\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_BigText ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "
\n"; $retval .= " " . nl2br(htmlspecialchars($answers[$code])) . " \n"; if ($params[0]=='RichText') $retval .= "
(Accepts HTML input.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_RichText ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_BigText ($params, $data, $answers, $ret); } function rpt_SingleRadio ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; if ($params[1] == 'Compact') $brstr = '     '; else $brstr = '
'; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "{$brstr}\n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; $selstr = '○'; if ($answers[$code]==$c) $selstr = '◉'; $retval .= "{$selstr} " . htmlspecialchars($l) . "{$brstr}\n"; } $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_Single ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_SingleRadio ($params, $data, $answers, $ret); } function rpt_SingleDropdown ( $params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $brstr = '
'; if ($params[1]=='Compact' && $params[0]!='SingleList') $brstr = ''; if ($params[0]=='SingleList') $liststr = ' size="6"'; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "{$brstr}\n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; if ($answers[$code]==$c) $retval .= " " . htmlspecialchars($l) . "\n"; } $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_SingleList ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_SingleDropdown ($params, $data, $answers, $ret); } function rpt_YesNo ( $params=array(), $data='', $answers=array(), $ret=0) { $P = array('Single', 'Compact'); $D = $data . "\nyes=Yes\nno=No"; return rpt_Single ($P, $D, $answers, $ret); } function rpt_MultiCheckbox ($params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "
\n"; while ($d = array_shift($D)) { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option must have a code."; if ($l=='') $errors[] = "draw: option have a label."; if ($answers["{$code}_{$c}"]=='on') $selstr = '☑'; else $selstr = '☐'; $retval .= "{$selstr} " . htmlspecialchars($l) . "
\n"; } if ($params[1]>0 || $params[2]>0) $retval .= " (Minimum $params[1], maximum $params[2] choices.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_Multi ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_MultiCheckbox ($params, $data, $answers, $ret); } function rpt_GridMulti ($params=array(), $data='', $answers=array(), $ret=0) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); if ($highlight[$code]) $highlightStr = 'highlightfield '; if ($code=='') $errors[] = "draw: {$params[0]} widgets must have a code."; if ($label=='') $errors[] = "draw: {$params[0]} widgets must have a label."; $retval = ''; $retval .= "
\n"; $retval .= " " . htmlspecialchars($label) . "
\n"; while ($d = array_shift($D)) { if (strtoupper($d)=='COLS') $axis = 'x'; elseif (strtoupper($d)=='ROWS') $axis = 'y'; else { list($c, $l) = explode('=', $d); if ($c=='') $errors[] = "draw: option on axis $axis must have a code."; if ($l=='') $errors[] = "draw: option on axis $axis have a label."; $carr[$axis][] = $c; $larr[$axis][] = $l; } } $xclass = 'x1'; $yclass = 'y1'; $retval .= " \n"; $retval .= " \n"; $retval .= " "; $retval .= ""; for ($i=0; isset($larr['x'][$i]); $i++) { $xclass = ($xclass=='x1')?'x2':'x1'; $retval .= ""; } $retval .= "\n"; $retval .= " \n"; $retval .= " \n"; for ($j=0; isset($larr['y'][$j]); $j++) { $xclass = 'x2'; $yclass = ($yclass=='y1')?'y2':'y1'; $retval .= " "; $retval .= ""; for ($i=0; isset($larr['x'][$i]); $i++) { $xclass = ($xclass=='x1')?'x2':'x1'; $retval .= ""; } $retval .= "\n"; } $retval .= " \n"; $retval .= "
 {$larr['x'][$i]}
{$larr['y'][$j]}"; $X = $carr['x'][$i]; $Y = $carr['y'][$j]; $Z = $code; if ($params[0]=='GridMulti') { if ($answers["{$Z}_{$Y}_{$X}"]=='on') $selstr = '☑'; elseif ($answers["{$Z}_{$Y}_{$X}"]=='--') $selstr = '☐'; else $selstr = $answers["{$Z}_{$Y}_{$X}"]; } elseif ($params[0]=='GridSingle') { $selstr = '○'; if ($answers["{$Z}_{$Y}"]==$X) $selstr = '◉'; } elseif ($params[0]=='GridSingleSingle') { $selstr = '○'; if ($answers[$Z]=="{$Y}_{$X}") $selstr = '◉'; } $retval .= "{$selstr}
\n"; if ($params[0]=='GridMulti' && ($params[1]>0 || $params[2]>0)) $retval .= " (Minimum $params[1], maximum $params[2] choices.)\n"; $retval .= "
\n"; if ($ret) return $retval; else echo $retval; } function rpt_GridSingle ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_GridMulti ($params, $data, $answers, $ret); } function rpt_GridSingleSingle ( $params=array(), $data='', $answers=array(), $ret=0) { return rpt_GridMulti ($params, $data, $answers, $ret); } /*************************************************** * chk_% functions ***************************************************/ function chk_Widget ($SOURCE, $params=array(), $data='') { global $errors; $w = $params[0]; if ($w=='') return NULL; if (function_exists("chk_{$w}")) { $retval = call_user_func("chk_{$w}", $SOURCE, $params, $data); return $retval; } else { $errors[] = "Did not understand widget {$w}"; return array(); } } function chk_Heading ($SOURCE, $params=array(), $data='') { return array(); } function chk_Subheading ($SOURCE, $params=array(), $data='') { return array(); } function chk_Note ($SOURCE, $params=array(), $data='') { return array(); } function chk_BigNote ($SOURCE, $params=array(), $data='') { return array(); } function chk_Rule ($SOURCE, $params=array(), $data='') { return array(); } function chk_Text ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; if ($params[1]>0 && strlen($val)<$params[1]) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be at least {$params[1]} characters."; } elseif ($params[2]>0 && strlen($val)>$params[2]) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be at most {$params[2]} characters."; } elseif (isset($params[3]) && !preg_match($params[3], $val)) { $highlight[$code]=1; $errors[] = "\"{$val}\" is not a valid answer to \"{$label}\"."; } return array($code => $val); } function chk_SmallText ($SOURCE, $params=array(), $data='') { return chk_Text($SOURCE, $params, $data); } function chk_Integer ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; if (!preg_match('/^[0-9]$/', $val)) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be a number only."; } elseif ($params[1]>0 && $val<$params[1]) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be at least {$params[1]}."; } // Text, SmallText and BigText widgets can have a regular expression check. if ( ($params[0]=='Text'||$params[0]=='SmallText'||$params[0]=='BigText') && $params[2]>0 && $val>$params[2] ) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be at most {$params[2]}."; } return array($code => $val); } function chk_Date ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = date("d/m/Y H:i:s", strtotime($SOURCE["{$prefixRequest}{$code}"])); return array($code => $val); } function chk_Email ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; if (!preg_match('/\@.*\..*/', $val)) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be a valid e-mail address."; } return array($code => $val); } function chk_WWW ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; if (!preg_match('/\:/', $val)) { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be a valid URL."; } return array($code => $val); } function chk_Units ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; global $prefunit, $unitspath; list($code, $label) = explode('=', $data); $value = (float)$SOURCE["{$prefixRequest}{$code}_origval"]; $unit = $SOURCE["{$prefixRequest}{$code}_origunit"]; $dimension = $params[1]; $convto = $prefunit[$dimension]; $convert = `{$unitspath} '{$value} {$unit}' '{$convto}'`; list($convert, $dummy) = explode("\n", $convert); $convert = preg_replace('/^\s*\*\s*/', '', $convert); $retval["{$code}_origval"] = $value; $retval["{$code}_origunit"] = $unit; $retval[$code] = $convert; return $retval; } function chk_BigText ($SOURCE, $params=array(), $data='') { return chk_Text($SOURCE, $params, $data); } function chk_RichText ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; // TODO // Perhaps run HTML Tidy here? return array($code => $val); } function chk_SingleRadio ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; $lines = explode("\n", $data); $d = array_shift($lines); list($code, $label) = explode('=', $d); while ($d = array_shift($lines)) { list($c, $l) = explode('=', $d); $isAnOption[$c] = 1; } $val = $SOURCE["{$prefixRequest}{$code}"]; if (strlen($val)>0 && !isset($isAnOption[$val])) { $highlight[$code]=1; $errors[] = "Choice \"{$val}\" for \"{$label}\" is not an option."; } elseif (strlen($val)==0) { $highlight[$code]=1; $errors[] = "Must answer \"{$label}\"."; } else { return array($code => $val); } } function chk_Single ($SOURCE, $params=array(), $data='') { return chk_SingleRadio($SOURCE, $params, $data); } function chk_SingleDropdown ($SOURCE, $params=array(), $data='') { return chk_SingleRadio($SOURCE, $params, $data); } function chk_SingleList ($SOURCE, $params=array(), $data='') { return chk_SingleRadio($SOURCE, $params, $data); } function chk_YesNo ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; list($code, $label) = explode('=', $data); $val = $SOURCE["{$prefixRequest}{$code}"]; if ($val!='yes' && $val!='no') { $highlight[$code]=1; $errors[] = "Field \"{$label}\" must be YES or NO."; } return array($code => $val); } function chk_MultiCheckbox ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; $lines = explode("\n", $data); $d = array_shift($lines); list($code, $label) = explode('=', $d); $retval = array(); $n = 0; while ($d = array_shift($lines)) { list($c, $l) = explode('=', $d); $val = $SOURCE["{$prefixRequest}{$code}_{$c}"]; if ($val=='on') { $n++; $retval["{$code}_{$c}"] = 'on'; } elseif ($val=='' || !isset($val)) { $retval["{$code}_{$c}"] = '--'; } elseif ($params[0]=='GridMulti') { $retval["{$code}_{$c}"] = $val; } else { $retval["{$code}_{$c}"] = '--'; } } if ($params[1]>0 && $n<$params[1]) { $highlight[$code]=1; $errors[] = "Must choose at least {$params[1]} options for \"{$label}\"."; } elseif ($params[2]>0 && $n>$params[2]) { $highlight[$code]=1; $errors[] = "Must choose at most {$params[2]} options for \"{$label}\"."; } return $retval; } function chk_Multi ($SOURCE, $params=array(), $data='') { return chk_MultiCheckbox($SOURCE, $params, $data); } // This function cheats a little. It reduces the problem of solving the "chk" function // for a GridMulti widget into the problem of solving the "chk" function for a regular // Multi widget, using manipulation of $data. function chk_GridMulti ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); while ($d = array_shift($D)) { if (strtoupper($d)=='COLS') $axis = 'x'; elseif (strtoupper($d)=='ROWS') $axis = 'y'; else { list($c, $l) = explode('=', $d); $carr[$axis][] = $c; $larr[$axis][] = $l; } } $newdata = "{$code}={$label}"; for ($j=0; isset($carr['y'][$j]); $j++) for ($i=0; isset($carr['x'][$i]); $i++) $newdata .= "\n{$carr['y'][$j]}_{$carr['x'][$i]}=!"; return chk_Multi($SOURCE, $params, $newdata); } // This function cheats a little. It reduces the problem of solving the "chk" function // for a GridSingle widget into the problem of solving the "chk" function for a regular // Single widget once for each row, using manipulation of $data. function chk_GridSingle ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); while ($d = array_shift($D)) { if (strtoupper($d)=='COLS') $axis = 'x'; elseif (strtoupper($d)=='ROWS') $axis = 'y'; else { list($c, $l) = explode('=', $d); $carr[$axis][] = $c; $larr[$axis][] = $l; } } $retval = array(); for ($j=0; isset($carr['y'][$j]); $j++) { $ans = array(); $newdata = "{$code}_{$carr['y'][$j]}=$label ({$larr['y'][$j]})"; for ($i=0; isset($carr['x'][$i]); $i++) $newdata .= "\n{$carr['x'][$i]}={$larr['x'][$i]}"; $ans = chk_Single($SOURCE, $params, $newdata); $retval = array_merge($retval, $ans); } return $retval; } // This function cheats a little. It reduces the problem of solving the "chk" function // for a GridSingle widget into the problem of solving the "chk" function for a regular // Single widget, using manipulation of $data. function chk_GridSingleSingle ($SOURCE, $params=array(), $data='') { global $prefixRequest, $errors, $highlight; $D = explode("\n", $data); $d = array_shift($D); list($code, $label) = explode('=', $d); while ($d = array_shift($D)) { if (strtoupper($d)=='COLS') $axis = 'x'; elseif (strtoupper($d)=='ROWS') $axis = 'y'; else { list($c, $l) = explode('=', $d); $carr[$axis][] = $c; $larr[$axis][] = $l; } } $newdata = "{$code}={$label}"; for ($j=0; isset($carr['y'][$j]); $j++) for ($i=0; isset($carr['x'][$i]); $i++) $newdata .= "\n{$carr['y'][$j]}_{$carr['x'][$i]}={$larr['y'][$j]}/{$larr['x'][$i]}"; return chk_Single ($SOURCE, $params, $newdata); } function chk_Form ($SOURCE, $params=array(), $data='') { global $options; $lines = explode("\n", $data); $options['action'] = $_SERVER['SCRIPT_NAME']; $options['method'] = 'POST'; $options['submit'] = ' Submit '; $options['code'] = 'mainform'; $options['code'] = 'mainform'; $options['multiplesubmissions'] = '0'; $options['showsubmissions'] = '1'; while ($line = array_shift($lines)) { list($var, $val) = explode('=', $line); $options[$var] = $val; } } /*************************************************** * data_% functions ***************************************************/ // This function doesn't create the database tables itself: // it just returns a string that contains all the necessary // gubbins to create the tables on a PostgreSQL database. // Might come in handy! function data_CreateDatabase () { global $prefixData; $retval = ''; $retval .= "DROP TABLE {$prefixData}answers;\n"; $retval .= "DROP TABLE {$prefixData}submissions;\n"; $retval .= "\n"; $retval .= "CREATE TABLE {$prefixData}answers (\n"; $retval .= " submission integer,\n"; $retval .= " field varchar,\n"; $retval .= " value varchar\n"; $retval .= ") WITHOUT OIDS;\n"; $retval .= "\n"; $retval .= "CREATE TABLE {$prefixData}submissions (\n"; $retval .= " submissionid integer,\n"; $retval .= " surveyid varchar,\n"; $retval .= " username varchar,\n"; $retval .= " submitdate timestamp,\n"; $retval .= " ipaddr varchar,\n"; $retval .= " useragent varchar\n"; $retval .= ") WITHOUT OIDS;\n"; $retval .= "\n"; $retval .= "CREATE INDEX {$prefixData}answers_idx_submission ON {$prefixData}answers (submission);\n"; $retval .= "CREATE INDEX {$prefixData}answers_idx_field ON {$prefixData}answers (field);\n"; $retval .= "\n"; $retval .= "CREATE INDEX {$prefixData}submissions_idx_submissionid ON {$prefixData}submissions (submissionid);\n"; $retval .= "CREATE INDEX {$prefixData}submissions_idx_surveyid ON {$prefixData}submissions (surveyid);\n"; $retval .= "CREATE INDEX {$prefixData}submissions_idx_username ON {$prefixData}submissions (username);\n"; $retval .= "\n"; $retval .= "GRANT ALL ON {$prefixData}answers TO apache;\n"; $retval .= "GRANT ALL ON {$prefixData}submissions TO apache;\n"; return $retval; } function data_StoreSubmission ($surveyid, $username) { global $prefixData, $database, $databasetype; global $options, $errors; $ipaddr = $_SERVER['REMOTE_ADDR']; $useragent = stripslashes($_SERVER['HTTP_USER_AGENT']); $q = "SELECT submissionid FROM {$prefixData}submissions ORDER BY submissionid DESC LIMIT 1;"; if ($databasetype==1) { $rset = pg_query($database, $q); $arr = pg_fetch_array($rset); } elseif ($databasetype==2) { $rset = mysql_query($database, $q); $arr = mysql_fetch_array($rset); } else { $errors[] = 'No database type specified.'; return 0; } $submissionid = $arr[0] + 1; $q = sprintf ("INSERT INTO {$prefixData}submissions VALUES (%s, '%s', '%s', NOW(), '%s', '%s');", $submissionid, addslashes($surveyid), addslashes($username), $ipaddr, addslashes($useragent)); if ($databasetype==1) $rset = pg_query($database, $q); elseif ($databasetype==2) $rset = mysql_query($database, $q); return $submissionid; } function data_StoreAnswer ($submissionid, $field, $value) { global $prefixData, $database, $databasetype; $q = sprintf ("INSERT INTO {$prefixData}answers VALUES (%s, '%s', '%s');", $submissionid, addslashes($field), addslashes($value)); if ($databasetype==1) $rset = pg_query($database, $q); elseif ($databasetype==2) $rset = mysql_query($database, $q); } function data_FindSubmissions ($surveyid, $username) { global $prefixData, $database, $databasetype; $q = sprintf ("SELECT submissionid FROM {$prefixData}submissions WHERE surveyid='%s' AND username='%s';", addslashes($surveyid), addslashes($username)); $retval = array(); if ($databasetype==1) { $rset = pg_query($database, $q); while ($arr = pg_fetch_array($rset)) $retval[] = $arr[0]; } elseif ($databasetype==2) { $rset = mysql_query($database, $q); while ($arr = mysql_fetch_array($rset)) $retval[] = $arr[0]; } return $retval; } function data_DestroySubmission ($submissionid) { global $prefixData, $database, $databasetype; $q1 = "DELETE FROM {$prefixData}submissions WHERE submissionid={$submissionid};"; $q2 = "DELETE FROM {$prefixData}answers WHERE submission={$submissionid};"; if ($databasetype==1) { $rset = pg_query($database, $q1); $rset = pg_query($database, $q2); } elseif ($databasetype==2) { $rset = mysql_query($database, $q1); $rset = mysql_query($database, $q2); } } function data_RetrieveAnswers ($submissionid, $admin=0) { global $prefixData, $database, $databasetype; $whois = addslashes(form_Whois()); $where = ''; if ($admin==0) $where = " AND username='{$whois}'"; $q = "SELECT field,value " . "FROM {$prefixData}answers, {$prefixData}submissions " . "WHERE {$prefixData}answers.submission={$submissionid} " . "AND {$prefixData}submissions.submissionid={$submissionid}" . $where . ';'; $retval = array(); if ($databasetype==1) { $rset = pg_query($database, $q); while ($arr = pg_fetch_array($rset)) $retval[$arr[0]] = $arr[1]; } elseif ($databasetype==2) { $rset = mysql_query($database, $q); while ($arr = mysql_fetch_array($rset)) $retval[$arr[0]] = $arr[1]; } return $retval; } function data_AdminTable ($surveyid, $style=1) { global $prefixData, $database, $databasetype; $retval = array(); if ($style==1) { $q = sprintf ("SELECT submissionid, username, submitdate FROM {$prefixData}submissions WHERE surveyid='%s';", addslashes($surveyid)); if ($databasetype==1) { $rset = pg_query($database, $q); while ($arr = pg_fetch_array($rset)) $retval[] = array('submissionid'=>$arr[0], 'username'=>$arr[1], 'submitdate'=>$arr[2]); } elseif ($databasetype==2) { $rset = mysql_query($database, $q); while ($arr = mysql_fetch_array($rset)) $retval[] = array('submissionid'=>$arr[0], 'username'=>$arr[1], 'submitdate'=>$arr[2]); } } elseif ($style==2) { $q = sprintf ("SELECT submissionid, username, submitdate FROM {$prefixData}submissions WHERE surveyid='%s';", addslashes($surveyid)); if ($databasetype==1) { $rset = pg_query($database, $q); while ($arr = pg_fetch_array($rset)) { $q2 = sprintf("SELECT field, value FROM {$prefixData}answers WHERE submission=%s", htmlentities($arr[0])); $rset2 = pg_query($database, $q2); while ($arr2 = pg_fetch_array($rset2)) { $retval['fields'][$arr2[0]] = 1; $arr['answers'][$arr2[0]] = $arr2[1]; } $retval[] = array('submissionid'=>$arr[0], 'username'=>$arr[1], 'submitdate'=>$arr[2], 'answers'=>$arr['answers']); } } elseif ($databasetype==2) { $rset = mysql_query($database, $q); while ($arr = mysql_fetch_array($rset)) { $q2 = sprintf("SELECT field, value FROM {$prefixData}answers WHERE submission=%s", htmlentities($arr[0])); $rset2 = mysql_query($database, $q2); while ($arr2 = mysql_fetch_array($rset2)) { $retval['fields'][$arr2[0]] = 1; $arr['answers'][$arr2[0]] = $arr2[1]; } $retval[] = array('submissionid'=>$arr[0], 'username'=>$arr[1], 'submitdate'=>$arr[2], 'answers'=>$arr['answers']); } } } ksort ($retval['fields']); return $retval; } /*************************************************** * form_% functions - the big kahuna ***************************************************/ // You will probably need to customise this. String is treated // case-sensitively, so you will probably want to use strtolower here. function form_Whois () { session_start(); if ($_SESSION['number']) return strtolower($_SESSION['number']); else return $_SERVER['REMOTE_ADDR']; } // You will probably need to customise this. String is treated // case-sensitively, so you will probably want to use strtolower here. function form_WhoisAdmin () { session_start(); if (function_exists('testUser')) return testUser('role:STAFF'); else return FALSE; } // You will probably want to customise this too. function form_Prefill () { session_start; $retval['membnum'] = $_SESSION['number']; $retval['branchnum'] = $_SESSION['branch']; $retval['branchname'] = $_SESSION['branchn']; $retval['fullname'] = $_SESSION['name']; $retval['email'] = $_SESSION['email']; return $retval; } function form_Draw ($widgetList, $answers) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; global $options; $whois = form_Whois(); if (count($answers)==0) $answers = form_Prefill(); $widgets = preg_split("/\r?\n\r?\n/", $widgetList); // Need to process the first widget specially, as it will be the Form widget. $frm = array_shift($widgets); $lines = explode("\n", $frm); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); chk_Form(array(), $params, $data); // Now we have all the info we need to draw the rest of the form print "
\n"; $subs = data_FindSubmissions($options['code'], $whois); if (count($subs)>0 && $options['showsubmissions']) { print "

You have already submitted this form. Submission reference(s):

\n"; print "\n"; if ($options['multiplesubmissions']) print "

You may submit the form again if you like. Your previous submission will be kept.

\n"; else print "

If you submit the form again, your previous submission will be lost.

\n"; } if (isset($errors[0])) { print "

Errors:

\n"; print "\n"; $errors = array(); print "
\n"; print " \n"; print "
\n"; foreach ($widgets as $w) { $lines = explode("\n", $w); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); draw_Widget($params, $data, $answers); } print "
\n"; print " \n"; print "
\n"; if (isset($errors[0])) { print "

Errors:

\n"; print "\n"; print "
\n"; } function form_Report ($widgetList, $answers) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; global $options; $whois = form_Whois(); $widgets = preg_split("/\r?\n\r?\n/", $widgetList); // Need to process the first widget specially, as it will be the Form widget. $frm = array_shift($widgets); $lines = explode("\n", $frm); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); chk_Form(array(), $params, $data); // Now we have all the info we need to draw the rest of the form print "
\n"; foreach ($widgets as $w) { $lines = explode("\n", $w); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); rpt_Widget($params, $data, $answers); } print "
\n"; } function form_Chk ($widgetList) { global $prefixStyle, $prefixIdent, $prefixRequest, $errors, $highlight; global $options; $widgets = preg_split("/\r?\n\r?\n/", $widgetList); // Need to process the first widget specially, as it will be the Form widget. $frm = array_shift($widgets); $lines = explode("\n", $frm); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); chk_Form(array(), $params, $data); if ($options['method']=='GET') $REQUESTVAR = $_GET; else $REQUESTVAR = $_POST; // Don't want slashes added -- our database stuff can cope with single quotes! foreach ($REQUESTVAR as $k=>$v) $REQUESTVAR[$k] = stripslashes($v); $answers = array(); foreach ($widgets as $w) { $lines = explode("\n", $w); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); $A = chk_Widget($REQUESTVAR, $params, $data); $answers = array_merge($answers, $A); } if (isset($errors[0])) { form_Draw($widgetList, $answers); return FALSE; } else { return $answers; } } function form_Email ($answers) { global $options, $prefixSubj, $mailtoHeaders; if (preg_match('/\@/', $options['mailto'])) { $str = ''; ksort($answers); foreach ($answers as $k=>$v) { $str .= "*** {$k} = $v\n"; } $str = "--------\n{$str}--------\n"; mail($options['mailto'], $prefixSubj.$options['code'], $str, $mailtoHeaders); } return 0; } function form_Save ($answers) { global $options; $whois = form_Whois(); if (!$options['multiplesubmissions']) { $subs = data_FindSubmissions ($options['code'], $whois); foreach ($subs as $s) data_DestroySubmission($s); } $submissionid = data_StoreSubmission($options['code'], $whois); foreach ($answers as $k=>$v) { data_StoreAnswer($submissionid, $k, $v); } return $submissionid; } function form_AdminScreen ($widgetList, $style) { global $options, $prefixStyle; if ($style!=1 && $style!=2) return FALSE; if (!form_WhoisAdmin()) return FALSE; $widgets = explode("\n\n", $widgetList); $frm = array_shift($widgets); $lines = explode("\n", $frm); $firstline = array_shift($lines); $data = implode("\n", $lines); $params = preg_split("/\s+/", $firstline); chk_Form(array(), $params, $data); $X = data_AdminTable($options['code'], $style); if ($style==1) print "

Go to expanded view.

"; else print "

Go to collapsed view.

"; print "\n"; print " \n"; print " "; print ""; if ($style==2) foreach ($X['fields'] as $f=>$v) printf ("", htmlentities($f)); print " \n"; print " \n"; print " \n"; for ($i=0; isset($X[$i]); $i++) { $S = $X[$i]; print " "; printf ("", $S['submissionid'], $S['submissionid'], htmlentities($S['username']), htmlentities(substr($S['submitdate'], 0, 16)) ); if ($style==2) foreach ($X['fields'] as $f=>$v) printf ("", htmlentities( $S['answers'][$f]) ); print "\n"; } print " \n"; print "
Sub#User#Date%s
%s%s%s%s
\n"; // yes, this next line does do something important! print "

 

\n"; // it makes it easier to select the table data with a mouse-drag. } // Handles three modes: drawing forms, showing reports and handling // form submissions function form_Main ($widgetList) { $answers = FALSE; if ($_REQUEST['submitted']) { $answers = form_Chk($widgetList); } elseif ($_REQUEST['viewsubmission']) { $answers = data_RetrieveAnswers($_REQUEST['viewsubmission'], 0); form_Report($widgetList, $answers); $answers = FALSE; } elseif ($_REQUEST['adminsubmission'] && form_WhoisAdmin()) { $answers = data_RetrieveAnswers($_REQUEST['adminsubmission'], 1); form_Report($widgetList, $answers); $answers = FALSE; } elseif ($_REQUEST['adminscreen']) { form_AdminScreen($widgetList, $_REQUEST['adminscreen']); $answers = FALSE; } else { form_Draw($widgetList); } // Form submissions... if ($answers===FALSE) return FALSE; form_Email($answers); $submissionid = form_Save($answers); if ($submissionid==0) { print "

Sorry, there was a problem saving your "; print "answers. Please try again later.

\n"; } else { print "

Your answers have been saved with submission reference number "; print ""; print "{$submissionid}.

\n"; } } ?>