Fix current_queue() list when there are no entries.
[adorno.git] / inc / DataEntry.php
blob2a38f1e9e869acd3a6df68a96355a09d370e55d7
1 <?php
3 /////////////////////////////////////////////////////////////
4 // C L A S S F O R D A T A E N T R Y T H I N G S //
5 /////////////////////////////////////////////////////////////
6 class EntryField
8 var $fname; // The original field name
9 var $ftype; // The type of entry field
10 var $current; // The current value
11 var $attributes; // An array of key value pairs
12 var $rendered; // Once it actually is...
14 /**
15 * Initialise an EntryField, used for data entry
17 * @param text $intype The type of field:
18 * select | lookup | date | checkbox | textarea
19 * (anything else is dealt with as "text")
21 * @param text $inname The name of the field.
23 * @param text $inextra An associative array of extra attributes to
24 * be applied to the field. Optional.
26 * @param text $inname The current value to use to initialise the
27 * field. Optional.
29 * @return object
31 * @author Andrew McMillan <andrew@catalyst.net.nz>
33 function EntryField( $intype, $inname, $inextra="", $current_value="" )
35 $this->ftype = $intype;
36 $this->fname = $inname;
37 $this->current = $current_value;
39 if ( isset($this->{"new_$intype"}) && function_exists($this->{"new_$intype"}) ) {
40 // Optionally call a function within this object called "new_<intype>" for setup
41 $this->{"new_$intype"}( $inextra );
43 else if ( is_array($inextra) ) {
44 $this->attributes = $inextra;
46 else {
49 $this->rendered = "";
51 return $this;
54 /**
55 * Render an EntryField into HTML
57 * @return text An HTML fragment for the data-entry field.
59 * @author Andrew McMillan <andrew@catalyst.net.nz>
61 function Render() {
62 $r = "<";
63 switch ( $this->ftype ) {
65 case "select":
66 $r .= "select name=\"$this->fname\"%%attributes%%>";
67 reset( $this->attributes );
68 while( list($k,$v) = each( $this->attributes ) ) {
69 if ( substr($k, 0, 1) != '_' ) continue;
70 if ( $k == '_help' ) continue;
71 $k = substr($k,1);
72 $r .= "<option value=\"".htmlentities($k)."\"";
73 if ( "$this->current" == "$k" ) $r .= " selected";
74 $r .= ">$v</option>\n" ;
76 $r .= "</select>\n";
77 break;
79 case "lookup":
80 $r .= "select name=\"$this->fname\"%%attributes%%>\n";
81 if ( isset($this->attributes["_all"]) )
82 $r .= sprintf("<option value=\"all\"".("all"==$this->current?" selected":"").">%s</option>\n", $this->attributes["_all"] );
83 if ( isset($this->attributes["_null"]) )
84 $r .= sprintf("<option value=\"\"".(""==$this->current?" selected":"").">%s</option>\n", $this->attributes["_null"] );
85 if ( isset($this->attributes["_zero"]) ) {
86 $r .= sprintf("<option value=\"0\"".(0==$this->current?" selected":"").">%s</option>\n", $this->attributes["_zero"] );
88 if ( isset($this->attributes["_sql"]) ) {
89 $qry = new PgQuery( $this->attributes["_sql"] );
91 else {
92 list( $tbl, $fld ) = explode("|", $this->attributes['_type'], 2);
93 $qry = new PgQuery( "SELECT lookup_code, lookup_desc FROM lookup_code WHERE source_table = ? AND source_field = ? ORDER BY lookup_seq, lookup_code", $tbl, $fld );
95 $r .= $qry->BuildOptionList( $this->current, "rndr:$this->fname" );
96 $r .= "</select>\n";
97 break;
99 case "date":
100 if ( !isset($this->attributes['size']) || $this->attributes['size'] == "" ) $size = " size=12";
101 $r .= "input type=\"text\" name=\"$this->fname\"$size value=\"".htmlentities($this->current)."\"%%attributes%%>\n";
102 break;
104 case "checkbox":
105 $checked = ( $this->current == 't' || $this->current == 'on' ? " checked" : "" );
106 $r .= "input type=\"checkbox\" name=\"$this->fname\"$checked%%attributes%%>\n";
107 break;
109 case "button":
110 $r .= "input type=\"button\" name=\"$this->fname\"%%attributes%%>\n";
111 break;
113 case "textarea":
114 $r .= "textarea name=\"$this->fname\"%%attributes%%>$this->current</textarea>\n";
115 break;
117 case "file":
118 if ( !isset($this->attributes['size']) || $this->attributes['size'] == "" ) $size = " size=25";
119 $r .= "input type=\"file\" name=\"$this->fname\"$size value=\"".htmlentities($this->current)."\"%%attributes%%>\n";
120 break;
122 default:
123 $r .= "input type=\"text\" name=\"$this->fname\" value=\"".htmlentities($this->current)."\"%%attributes%%>\n";
124 break;
127 // Now process the generic attributes
128 reset( $this->attributes );
129 $attribute_values = "";
130 while( list($k,$v) = each( $this->attributes ) ) {
131 if ( substr($k, 0, 1) == '_' ) continue;
132 $attribute_values .= " $k=\"".htmlentities($v)."\"";
134 $r = str_replace( '%%attributes%%', $attribute_values, $r );
136 $this->rendered = $r;
137 return $r;
140 function new_lookup( $inextra ) {
141 $this->attributes = $inextra;
145 class EntryForm
147 var $action; // The submit action for the form
148 var $record; // The record that the form is dealing with
149 var $editmode; // Whethere we are editing, or not
150 var $name; // The name of the form
151 var $class; // The CSS class of the form
152 var $break_line_format;
153 var $table_line_format;
155 function EntryForm( $action, &$record, $editmode=false )
157 $this->action = $action;
158 $this->record = &$record;
159 $this->editmode = $editmode;
160 $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
161 $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
163 return $this;
166 function NoHelp( ) {
167 $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
168 $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s</td></tr>'."\n";
171 function HelpInLine( ) {
172 $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
173 $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
176 function HelpInCell( ) {
177 $this->break_line_format = '<tr><th class="ph" colspan="3">%s</th></tr>'."\n";
178 $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s</td><td class="help">%s</td></tr>'."\n";
181 function SimpleForm( ) {
182 $this->break_line_format = '<hr>%s'."\n";
183 $this->table_line_format = '<span class="prompt">%s</span>&nbsp;<span class="entry">%s</span>'."\n";
186 function TrivialForm( ) {
187 $this->break_line_format = '%s'."\n";
188 $this->table_line_format = '<span class="prompt">%s</span><span class="entry">%s</span>'."\n";
191 function StartForm( $extra_attributes='' ) {
192 if ( !is_array($extra_attributes) && $extra_attributes != '' ) {
193 list( $k, $v ) = explode( '=', $extra_attributes );
194 $extra_attributes = array( $k => $v );
196 $extra_attributes['action'] = $this->action;
197 if ( !isset($extra_attributes['method']) ) $extra_attributes['method'] = 'post';
198 if ( !isset($extra_attributes['enctype']) ) $extra_attributes['enctype'] = 'multipart/form-data';
199 if ( !isset($extra_attributes['name']) ) $extra_attributes['name'] = 'form';
200 if ( !isset($extra_attributes['class']) ) $extra_attributes['class'] = 'formdata';
201 if ( !isset($extra_attributes['id']) ) $extra_attributes['id'] = $extra_attributes['name'];
203 // Now process the generic attributes
204 reset( $extra_attributes );
205 $attribute_values = "";
206 while( list($k,$v) = each( $extra_attributes ) ) {
207 $attribute_values .= " $k=\"".htmlentities($v)."\"";
209 return "<form$attribute_values>\n";
212 function EndForm( ) {
213 return "</form>\n";
216 //////////////////////////////////////////////////////
217 // A utility function for a heading line within a data entry table
218 //////////////////////////////////////////////////////
219 function BreakLine( $text = '' )
221 return sprintf( $this->break_line_format, $text);
224 //////////////////////////////////////////////////////
225 // A utility function for a hidden field within a data entry table
226 //////////////////////////////////////////////////////
227 function HiddenField($fname,$fvalue) {
228 return sprintf( '<input type="hidden" name="%s" value="%s" />%s', $fname, htmlentities($fvalue), "\n" );
231 //////////////////////////////////////////////////////
232 // A utility function for a submit button within a data entry table
233 //////////////////////////////////////////////////////
234 function SubmitButton( $fname, $fvalue )
236 return sprintf( $this->table_line_format, '&nbsp;',
237 sprintf('<input type="submit" name="%s" value="%s" class="submit" />',
238 $fname, $fvalue), "");
241 /////////////////////////////////////////////////////
242 // Internal function for parsing the type extra on a field.
243 /////////////////////////////////////////////////////
244 function _ParseTypeExtra( $ftype = '', $type_extra = '' ) {
245 if ( !is_array($type_extra) ) {
246 list( $k, $v ) = explode( '=', $type_extra );
247 $type_extra = array( $k => $v );
250 // Default the help to the title, or to blank
251 if ( !isset($type_extra['_help']) ) {
252 $type_extra['_help'] = "";
253 if ( isset($type_extra['title']) )
254 $type_extra['_help'] = $type_extra['title'];
257 // Default the style to fdate, ftext, fcheckbox etc.
258 if ( !isset($type_extra['class']) ) {
259 $type_extra['class'] = "f$ftype";
262 return $type_extra;
265 //////////////////////////////////////////////////////
266 // A utility function for a data entry line within a table
267 //////////////////////////////////////////////////////
268 function DataEntryField( $format, $ftype='', $fname='', $type_extra='' )
270 global $session;
272 if ( ($fname == '' || $ftype == '') ) {
273 // Displaying never-editable values
274 return $format;
276 elseif ( !$this->editmode ) {
277 // Displaying editable values when we are not editing
278 return sprintf($format, $this->record->{$fname} );
281 $currval = '';
282 // Get the default value, preferably from $_POST
283 if ( preg_match("/^(.+)\[(.+)\]$/", $fname, $parts) ) {
284 $p1 = $parts[1];
285 $p2 = $parts[2];
286 error_log( "DBG: fname=$fname, p1=$p1, p2=$p2, POSTVAL=" . $_POST[$p1][$p2] . ", record=".$this->record->{"$p1"}["$p2"] );
287 // fixme - This could be changed to handle more dimensions on submitted variable names
288 if ( isset($_POST[$p1]) ) {
289 if ( isset($_POST[$p1][$p2]) ) {
290 $currval = $_POST[$p1][$p2];
293 else if ( isset($this->record) && is_object($this->record)
294 && isset($this->record->{"$p1"}["$p2"])
296 $currval = $this->record->{"$p1"}["$p2"];
299 else {
300 if ( isset($_POST[$fname]) ) {
301 $currval = $_POST[$fname];
303 else if ( isset($this->record) && is_object($this->record) && isset($this->record->{"$fname"}) ) {
304 $currval = $this->record->{"$fname"};
307 if ( $ftype == "date" ) $currval = format_date($currval);
309 // Now build the entry field and render it
310 $field = new EntryField( $ftype, $fname, $this->_ParseTypeExtra($ftype,$type_extra), $currval );
311 return $field->Render();
315 //////////////////////////////////////////////////////
316 // A utility function for a data entry line within a table
317 //////////////////////////////////////////////////////
318 function DataEntryLine( $prompt, $format, $ftype='', $fname='', $type_extra='' )
320 $type_extra = $this->_ParseTypeExtra( $ftype, $type_extra );
321 return sprintf( $this->table_line_format, $prompt,
322 $this->DataEntryField( $format, $ftype, $fname, $type_extra ),
323 $type_extra['_help'] );
327 //////////////////////////////////////////////////////
328 // A utility function for a data entry line, where the
329 // prompt is a drop-down.
330 //////////////////////////////////////////////////////
331 function MultiEntryLine( $prompt_options, $prompt_name, $default_prompt, $format, $ftype='', $fname='', $type_extra='' )
333 global $session;
335 $prompt = "<select name=\"$prompt_name\">\n";
337 reset($prompt_options);
338 while( list($k,$v) = each($prompt_options) ) {
339 $selected = ( ( $k == $default_prompt ) ? ' selected="selected"' : '' );
340 $nextrow = "<option value=\"$k\"$selected>$v</option>\n";
341 if ( preg_match('/&/', $nextrow) ) $nextrow = preg_replace( '/&/', '&amp;', $nextrow);
342 $prompt .= $nextrow;
344 $prompt .= "</select>\n";
346 return $this->DataEntryLine( $prompt, $format, $ftype, $fname, $type_extra );