Merge branch 'master' of ssh://karora@repo.or.cz/srv/git/adorno
[adorno.git] / inc / FormHandler.php
blob02099412633f87ff6814769c037ba95b415c2fd0
1 <?php
2 class Field
4 var $type;
5 var $name;
6 var $prompt;
7 var $seq;
8 var $x;
9 var $y;
10 var $select;
11 var $default;
12 var $current;
14 function Field( $type, $name, $prompt, $seq = 50, $default = "", $current = "", $inextra = "" ) {
15 $this->type = $type;
16 $this->seq = $seq;
17 $this->name = $name;
18 $this->prompt = $prompt;
19 $this->default = $default;
20 $this->current = $current;
22 if ( isset($this->{"new_$type"}) && function_exists($this->{"new_$type"}) ) {
23 $this->{"new_$type"}( $inextra );
25 else if ( is_array($inextra) ) {
26 $this->attributes = $inextra;
28 else {
31 return $this;
35 function new_lookup( $inextra ) {
36 $this->attributes = $inextra;
39 function BuildHTML( $stub = "fh_" ) {
40 global $sysname;
41 error_log( "$sysname DBG: Building HTML for field $this->name" );
42 if ( substr($this->type, 0,4) == "btn_" ) {
43 $type = substr($this->type, 4);
44 $h = "<tr><td class=\"fprompt\">&nbsp;</td><td class=\"fdata\"><input type=\"$type\" name=\"$stub$this->name\" value=\"".$this->prompt."\"></td></tr>\n";
46 else {
47 $h = "<tr><th class=fprompt>$this->prompt</th>";
48 $r = "<";
49 switch ( $this->type ) {
50 case "select":
51 $r .= "select name=\"$this->name\"%%attributes%%>";
52 reset( $this->attributes );
53 while( list($k,$v) = each( $this->attributes ) ) {
54 if ( substr($k, 0, 1) != '_' ) continue;
55 $k = substr($k,1);
56 $r .= "<option value=\"".htmlentities($k)."\"";
57 if ( "$this->current" == "$k" ) $r .= " selected";
58 $r .= ">$v</option>\n" ;
60 $r .= "</select>\n";
61 break;
63 case "lookup":
64 $r .= "select name=\"$this->name\"%%attributes%%>";
65 if ( isset($this->attributes["_sql"]) ) {
66 $qry = new PgQuery( $this->attributes["_sql"] );
68 else {
69 $qry = new PgQuery( "SELECT lookup_id, lookup_description FROM lookups WHERE lookup_type = ? ORDER BY lookup_type, lookup_seq, lookup_id", $this->attributes['_type'] );
71 error_log( "$sysname DBG: lookup $this->name via: $qry->querystring" );
72 $r .= $qry->BuildOptionList( $this->current, "rndr:$this->name" );
73 $r .= "</select>\n";
74 break;
76 case "multiselect":
77 $r .= "select multiple name=\"$this->name" . "[]\"%%attributes%%>";
78 if ( isset($this->attributes["_sql"]) ) {
79 $qry = new PgQuery( $this->attributes["_sql"] );
81 else {
82 $qry = new PgQuery( "SELECT lookup_id, lookup_description FROM lookups WHERE lookup_type = ? ORDER BY lookup_type, lookup_seq, lookup_id", $this->attributes['_type'] );
84 error_log( "$sysname DBG: lookup $this->name via: $qry->querystring" );
85 $r .= $qry->BuildOptionList( $this->current, "rndr:$this->name" );
86 $r .= "</select>\n";
87 break;
89 case "radio":
90 $r = "";
91 $fmt .= "<label><input type=\"radio-set\" name=\"$this->name\" value=\"%s\"%s%%attributes%%>&nbsp;%s&nbsp; </label>";
92 if ( isset($this->attributes["_values"]) ) {
93 foreach( $this->attributes["_values"] as $k => $v ) {
94 $selected = ($this->current == $k ? " selected" : "" );
95 $r .= sprintf( $fmt, $k, $selected, $v );
98 else {
99 if ( isset($this->attributes["_sql"]) ) {
100 $qry = new PgQuery( $this->attributes["_sql"] );
102 else {
103 $qry = new PgQuery( "SELECT lookup_id, lookup_description FROM lookups WHERE lookup_type = ? ORDER BY lookup_type, lookup_seq, lookup_id", $this->attributes['_type'] );
105 error_log( "$sysname DBG: lookup $this->name via: $qry->querystring" );
106 $r .= $qry->BuildRadioSet( $fmt, $this->current, "rndr:$this->name" );
108 break;
110 case "date":
111 if ( !isset($this->attributes['size']) || $this->attributes['size'] == "" ) $size = " size=12";
112 $r .= "input type=\"text\" name=\"$this->name\"$size value=\"".htmlentities($this->current)."\"%%attributes%%>\n";
113 break;
115 case "textarea":
116 $r .= "textarea type=\"$this->type\" name=\"$this->name\" %%attributes%%>$this->current</textarea>\n";
117 break;
119 default:
120 $r .= "input type=\"$this->type\" name=\"$this->name\" value=\"".htmlentities($this->current)."\"%%attributes%%>\n";
121 break;
124 // Now process the generic attributes
125 if ( is_array($this->attributes) || is_object($this->attributes) ) {
126 reset( $this->attributes );
127 $attribute_values = "";
128 while( list($k,$v) = each( $this->attributes ) ) {
129 if ( substr($k, 0, 1) == '_' ) continue;
130 $attribute_values .= " $k=\"".htmlentities($v)."\"";
132 $r = str_replace( '%%attributes%%', $attribute_values, $r );
135 $h .= "<td class=fdata>$r</td></tr>\n";
138 return $h;
143 class Form
145 var $phase = 'display';
147 var $action = "";
148 var $target = "";
149 var $method = "";
150 var $record = "";
152 var $fields;
154 function Form( $name = "fh", $rec = false ) {
155 $this->name = $name;
156 $this->record = $rec;
157 $this->fields = array();
160 function AddField( $type, $name, $prompt, $seq = 50, $default = "", $extra = "" ) {
161 if ( is_object($this->record) ) {
162 $current = $this->record->{$name};
164 else {
165 $current = "";
167 if ( isset($this->fields[$name]) ) {
168 $this->fields[$name]->type = $type;
169 $this->fields[$name]->seq = $seq;
170 $this->fields[$name]->name = $name;
171 $this->fields[$name]->prompt = $prompt;
172 $this->fields[$name]->default = $default;
173 $this->fields[$name]->current = $current;
174 $this->fields[$name]->attributes = $extra;
176 else {
177 $this->fields[$name] = new Field( $type, $name, $prompt, $seq, $default, $current, $extra );
181 function AddButton( $type, $name, $label, $seq = 50 ) {
182 $type = "btn_$type";
183 $name = "btn_$name";
184 $prompt = $label;
185 $this->fields[$name] = new Field( $type, $name, $prompt, $seq );
188 function BuildHTML( ) {
189 global $sysname;
190 $fst = sprintf( "<form action=\"%s\" target=\"%s\" method=\"%s\">\n", $this->action, $this->target, $this->method);
192 $fst .= "<table>";
193 while( list($k,$v) = each($this->fields) ) {
194 $fst .= $v->BuildHTML();
196 $fst .= "</table></form>";
197 return $fst;