3 * Classes to handle entry and viewing of field-based data.
6 * @subpackage DataEntry
7 * @author Andrew McMillan <andrew@catalyst.net.nz>
8 * @copyright Catalyst IT Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
11 require_once("AWLUtilities.php");
14 * Individual fields used for data entry / viewing.
16 * This object is not really intended to be used directly. The more normal
17 * interface is to instantiate an {@link EntryForm} and then issue calls
18 * to {@link DataEntryLine()} and other {@link EntryForm} methods.
20 * Understanding the operation of this class (and possibly auditing the source
21 * code, particularly {@link EntryField::Render}) will however convey valuable
22 * understanding of some of the more
25 * @todo This class doesn't really provide a huge amount of utility between construct
26 * and render, but there must be good things possible there. Perhaps one EntryField
27 * is created and used repeatedly as a template (e.g.). That might be useful to
28 * support... Why is this a Class anyway? Maybe we should have just done half a
29 * dozen functions (one per major field type) and just used those... Maybe we should
30 * build a base class for this and extend it to make EntryField in a better way.
32 * EntryField is only useful at present if you desperately want to use it's simple
33 * field interface, but want to intimately control the layout (or parts of the layout),
34 * otherwise you should be using {@link EntryForm} as the main class.
44 * The name of the field
50 * The type of entry field
66 * An array of key value pairs
72 * Once it actually is...
79 * Initialise an EntryField, used for data entry.
81 * The following types of fields are possible:
83 * <li>select - Will display a select list of the keys/values in $attributes where the
84 * key starts with an underscore. The key will have the '_' removed before being used
85 * as the key in the list. All the $attributes with keys not beginning with '_' will
86 * be used in the normal manner as HTML attributes within the <select ...> tag.</li>
87 * <li>lookup - Will display a select list of values from the database.
88 * If $attributes defines a '_sql' attibute then that will be used to make
89 * the list, otherwise the database values will be from the 'codes' table
90 * as in "SELECT code_id, code_value FROM codes WHERE code_type = '_type' ORDER BY code_seq, code_id"
91 * using the value of $attributes['_type'] as the code_type.</li>
92 * <li>date - Will be a text field, expecting a date value which might be
93 * javascript validated at some point in the future.</li>
94 * <li>checkbox - Will display a checkbox for an on-off value.</li>
95 * <li>textarea - Will display an HTML textarea.</li>
96 * <li>file - Will display a file browse / enter field.</li>
97 * <li>button - Will display a button field.</li>
98 * <li>password - Password entry. This will display entered data as asterisks.</li>
101 * The $attributes array is useful to set specific HTML attributes within the HTML tag
102 * used for the entry field however $attribute keys named starting with an underscore ('_')
103 * affect the field operation rather than the HTML. For the 'select' field type, these are
104 * simply used as the keys / values for the selection (with the '_' removed), but other
105 * cases are more complex:
107 * <li>_help - While this will be ignored by the EntryField::Render() method the _help
108 * should be assigned (or will be assigned the same value as the 'title' attribute) and
109 * will (depending on the data-entry line format in force) be displayed as help for the
110 * field by the EntryForm::DataEntryLine() method.</li>
111 * <li>_sql - When used in a 'lookup' field this controls the SQL to return keys/values
112 * for the list. The actual SQL should return two columns, the first will be used for
113 * the key and the second for the displayed value.</li>
114 * <li>_type - When used in a 'lookup' field this defines the codes type used.</li>
115 * <li>_null - When used in a 'lookup' field this will control the description for an
116 * option using a '' key value which will precede the list of values from the database.</li>
117 * <li>_zero - When used in a 'lookup' field this will control the description for an
118 * option using a '0' key value which will precede the list of values from the database.</li>
119 * <li>_label - When used in a 'radio' or 'checkbox' field this will wrap the field
120 * with an HTML label tag as <label ...><input field...>$attributes['_label']</label></li>
124 * @param text $intype The type of field:
125 * select | lookup | date | checkbox | textarea | file | button | password
126 * (anything else is dealt with as "text")
128 * @param text $inname The name of the field.
130 * @param text $attributes An associative array of extra attributes to be applied
131 * to the field. Optional, but generally important. Some $attribute keys have
132 * special meaning, while others are simply added as HTML attributes to the field.
134 * @param text $current_value The current value to use to initialise the
137 function EntryField( $intype, $inname, $attributes="", $current_value="" )
139 $this->ftype
= $intype;
140 $this->fname
= $inname;
141 $this->current
= $current_value;
143 if ( isset($this->{"new_$intype"}) && function_exists($this->{"new_$intype"}) ) {
144 // Optionally call a function within this object called "new_<intype>" for setup
145 $this->{"new_$intype"}( $attributes );
147 else if ( is_array($attributes) ) {
148 $this->attributes
= $attributes;
153 $this->rendered
= "";
157 * Render an EntryField into HTML
158 * @see EntryField::EntryField(), EntryForm::DataEntryLine()
160 * @return text An HTML fragment for the data-entry field.
166 dbg_error_log( "EntryField", ":Render: Name: %s, Type: %s, Current: %s", $this->fname
, $this->ftype
, $this->current
);
167 switch ( $this->ftype
) {
170 $r .= "select name=\"$this->fname\"%%attributes%%>";
171 reset( $this->attributes
);
172 while( list($k,$v) = each( $this->attributes
) ) {
173 if ( substr($k, 0, 1) != '_' ) continue;
174 if ( $k == '_help' ) continue;
176 $r .= "<option value=\"".htmlspecialchars($k)."\"";
177 if ( "$this->current" == "$k" ) $r .= " selected";
178 $r .= ">$v</option>" ;
184 $r .= "select name=\"$this->fname\"%%attributes%%>";
185 reset( $this->attributes
);
186 while( list($k,$v) = each( $this->attributes
) ) {
187 if ( substr($k, 0, 1) != '_' ) continue;
189 if ( $k == 'help' ||
$k == "sql" ||
$k == "type" ) continue;
190 if ( $k == "null" ) $k = "";
191 if ( $k == "zero" ) $k = "0";
192 $r .= "<option value=\"".htmlspecialchars($k)."\"";
193 if ( "$this->current" == "$k" ) $r .= " selected";
194 $r .= ">$v</option>" ;
196 if ( isset($this->attributes
["_sql"]) ) {
197 $qry = new PgQuery( $this->attributes
["_sql"] );
200 $qry = new PgQuery( "SELECT code_id, code_value FROM codes WHERE code_type = ? ORDER BY code_seq, code_id", $this->attributes
['_type'] );
202 $r .= $qry->BuildOptionList( $this->current
, "rndr:$this->fname" );
209 if ( !isset($this->attributes
['size']) ||
$this->attributes
['size'] == "" ) $size = " size=" . ($this->ftype
== 'date' ?
"12" : "18");
210 $r .= "input type=\"text\" name=\"$this->fname\"$size value=\"".$session->FormattedDate(htmlspecialchars($this->current
))."\"%%attributes%%>";
214 // We send a hidden field with a false value, which will be overridden by the real
215 // field with a true value (if true) or not overridden (if false).
216 $r .= "input type=\"hidden\" name=\"$this->fname\" value=\"off\"><";
219 if ( $this->current
== 't' ||
intval($this->current
) == 1 ||
$this->current
== 'on'
220 ||
(isset($this->attributes
['value']) && $this->current
== $this->attributes
['value'] ) )
221 $checked = " checked";
222 $id = "id_$this->fname" . ( $this->ftype
== "radio" ?
"_".$this->attributes
['value'] : "");
223 if ( isset($this->attributes
['_label']) ) {
224 $r .= "label for=\"$id\"";
225 if ( isset($this->attributes
['class']) )
226 $r .= ' class="'. $this->attributes
['class'] . '"';
229 $r .= "input type=\"$this->ftype\" name=\"$this->fname\" id=\"$id\"$checked%%attributes%%>";
230 if ( isset($this->attributes
['_label']) ) {
231 $r .= " " . $this->attributes
['_label'];
237 $r .= "input type=\"button\" name=\"$this->fname\"%%attributes%%>";
241 $r .= "input type=\"submit\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current
)."\"%%attributes%%>";
245 $r .= "textarea name=\"$this->fname\"%%attributes%%>$this->current</textarea>";
249 if ( !isset($this->attributes
['size']) ||
$this->attributes
['size'] == "" ) $size = " size=25";
250 $r .= "input type=\"file\" name=\"$this->fname\"$size value=\"".htmlspecialchars($this->current
)."\"%%attributes%%>";
254 $r .= "input type=\"password\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current
)."\"%%attributes%%>";
258 $r .= "input type=\"text\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current
)."\"%%attributes%%>";
262 // Now process the generic attributes
263 reset( $this->attributes
);
264 $attribute_values = "";
265 while( list($k,$v) = each( $this->attributes
) ) {
266 if ( $k == '_readonly' ) $attribute_values .= " readonly";
267 else if ( $k == '_disabled' ) $attribute_values .= " disabled";
268 if ( substr($k, 0, 1) == '_' ) continue;
269 $attribute_values .= " $k=\"".htmlspecialchars($v)."\"";
271 $r = str_replace( '%%attributes%%', $attribute_values, $r );
273 $this->rendered
= $r;
278 * Function called indirectly when a new EntryField of type 'lookup' is created.
279 * @param array $attributes The attributes array that was passed in to the new EntryField()
282 function new_lookup( $attributes ) {
283 $this->attributes
= $attributes;
289 * A class to handle displaying a form on the page (for editing) or a structured
290 * layout of non-editable content (for viewing), with a simple switch to flip from
291 * view mode to edit mode.
301 * The submit action for the form
307 * The record that the form is dealing with
313 * Whether we are editing, or not
319 * The name of the form
325 * The CSS class of the form
331 * Format string for lines that are breaks in the data entry field groupings
334 var $break_line_format;
337 * Format string for normal data entry field lines.
340 var $table_line_format;
343 * Format string that has been temporarily saved so we can restore it later
346 var $saved_line_format;
350 * Initialise a new data-entry form.
351 * @param string $action The action when the form is submitted.
352 * @param objectref $record A reference to the database object we are displaying / editing.
353 * @param boolean $editmode Whether we are editing.
355 function EntryForm( $action, &$record, $editing=false )
357 $this->action
= $action;
358 $this->record
= &$record;
359 $this->EditMode
= $editing;
360 $this->break_line_format
= '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
361 $this->table_line_format
= '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
365 * Initialise some more of the forms fields, possibly with a prefix
366 * @param objectref $record A reference to the database object we are displaying / editing.
367 * @param string $prefix A prefix to prepend to the field name.
369 function PopulateForm( &$record, $prefix="" )
371 foreach( $record AS $k => $v ) {
372 $this->record
->{"$prefix$k"} = $v;
377 * Set the line format to have no help display
380 $this->break_line_format
= '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
381 $this->table_line_format
= '<tr><th class="prompt">%s</th><td class="entry">%s</td></tr>'."\n";
385 * Set the line format to have help displayed in the same cell as the entry field.
387 function HelpInLine( ) {
388 $this->break_line_format
= '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
389 $this->table_line_format
= '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
393 * Set the line format to have help displayed in it's own separate cell
395 function HelpInCell( ) {
396 $this->break_line_format
= '<tr><th class="ph" colspan="3">%s</th></tr>'."\n";
397 $this->table_line_format
= '<tr><th class="prompt">%s</th><td class="entry">%s</td><td class="help">%s</td></tr>'."\n";
401 * Set the line format to an extremely simple CSS based prompt / field layout.
403 function SimpleForm( $new_format = '<span class="prompt">%s:</span> <span class="entry">%s</span>' ) {
404 $this->break_line_format
= '%s'."\n";
405 $this->table_line_format
= $new_format."\n";
409 * Set the line format to a temporary one that we can revert from.
410 * @param string $new_format The (optional) new format we will temporarily use.
412 function TempLineFormat( $new_format = '<span class="prompt">%s:</span> <span class="entry">%s</span>' ) {
413 $this->saved_line_format
= $this->table_line_format
;
414 $this->table_line_format
= $new_format ."\n";
418 * Revert the line format to what was in place before the last TempLineFormat call.
420 function RevertLineFormat( ) {
421 if ( isset($this->saved_line_format
) ) {
422 $this->table_line_format
= $this->saved_line_format
;
427 * Start the actual HTML form. Return the fragment to do this.
428 * @param array $extra_attributes Extra key/value pairs for the FORM tag.
429 * @return string The HTML fragment for the start of the form.
431 function StartForm( $extra_attributes='' ) {
432 if ( !is_array($extra_attributes) && $extra_attributes != '' ) {
433 list( $k, $v ) = explode( '=', $extra_attributes );
434 $extra_attributes = array( $k => $v );
436 $extra_attributes['action'] = $this->action
;
437 if ( !isset($extra_attributes['method']) ) $extra_attributes['method'] = 'post';
438 if ( !isset($extra_attributes['enctype']) ) $extra_attributes['enctype'] = 'multipart/form-data';
439 if ( !isset($extra_attributes['name']) ) $extra_attributes['name'] = 'form';
440 if ( !isset($extra_attributes['class']) ) $extra_attributes['class'] = 'formdata';
441 if ( !isset($extra_attributes['id']) ) $extra_attributes['id'] = $extra_attributes['name'];
443 // Now process the generic attributes
444 reset( $extra_attributes );
445 $attribute_values = "";
446 while( list($k,$v) = each( $extra_attributes ) ) {
447 $attribute_values .= " $k=\"".htmlspecialchars($v)."\"";
449 return "<form$attribute_values>\n";
453 * Return the HTML fragment to end the form.
454 * @return string The HTML fragment to end the form.
456 function EndForm( ) {
461 * A utility function for a heading line within a data entry table
462 * @return string The HTML fragment to end the form.
464 function BreakLine( $text = '' )
466 return sprintf( $this->break_line_format
, translate($text));
470 * A utility function for a hidden field within a data entry table
472 * @param string $fname The name of the field.
473 * @param string $fvalue The value of the field.
474 * @return string The HTML fragment for the hidden field.
476 function HiddenField($fname,$fvalue) {
477 return sprintf( '<input type="hidden" name="%s" value="%s" />%s', $fname, htmlspecialchars($fvalue), "\n" );
481 * Internal function for parsing the type extra on a field.
483 * If the '_help' attribute is not set it will be assigned the value of
484 * the 'title' attribute, if there is one.
486 * If the 'class' attribute is not set it will be assigned to 'flookup',
487 * 'fselect', etc, according to the field type.
489 * @return string The parsed type extra.
491 function _ParseAttributes( $ftype = '', $attributes = '' ) {
493 if ( !is_array($attributes) ) {
494 if ( strpos( $attributes, '=' ) === false ) {
495 $attributes = array();
498 list( $k, $v ) = explode( '=', $attributes );
499 $attributes = array( $k => $v );
503 // Default the help to the title, or to blank
504 if ( !isset($attributes['_help']) ) {
505 $attributes['_help'] = "";
506 if ( isset($attributes['title']) )
507 $attributes['_help'] = $attributes['title'];
510 // Default the style to fdate, ftext, fcheckbox etc.
511 if ( !isset($attributes['class']) ) {
512 $attributes['class'] = "f$ftype";
519 * A utility function for a data entry line within a table
520 * @return string The HTML fragment to display the data entry field
522 function DataEntryField( $format, $ftype='', $base_fname='', $attributes='', $prefix='' )
526 if ( ($base_fname == '' ||
$ftype == '') ) {
527 // Displaying never-editable values
530 $fname = $prefix . $base_fname;
532 dbg_error_log( "DataEntry", ":DataEntryField: fmt='%s', fname='%s', fvalue='%s'", $format, $fname, (isset($this->record
->{$fname})?
$this->record
->{$fname}:'value not set') );
533 if ( !$this->EditMode
) {
534 // Displaying editable values when we are not editing
535 // If it is a date, then format it according to the current user's date format type
536 if ($ftype == "date" ||
$ftype == "timestamp")
537 return sprintf($format, $session->FormattedDate($this->record
->{$fname}) );
538 dbg_error_log( "DataEntry", ":DataEntryField: fmt='%s', fname='%s', fvalue='%s'", $format, $fname, (isset($this->record
->{$fname})?
$this->record
->{$fname}:'value not set') );
539 return sprintf($format, $this->record
->{$fname} );
543 // Get the default value, preferably from $_POST
544 if ( preg_match("/^(.+)\[(.+)\]$/", $fname, $parts) ) {
547 @dbg_error_log
( "DataEntry", ":DataEntryField: fname=%s, p1=%s, p2=%s, POSTVAL=%s, \$this->record->{'%s'}['%s']=%s",
548 $fname, $p1, $p2, $_POST[$p1][$p2], $p1, $p2, $this->record
->{"$p1"}["$p2"] );
549 // FIXME - This could be changed to handle more dimensions on submitted variable names
550 if ( isset($_POST[$p1]) ) {
551 if ( isset($_POST[$p1][$p2]) ) {
552 $currval = $_POST[$p1][$p2];
555 else if ( isset($this->record
) && is_object($this->record
)
556 && isset($this->record
->{"$p1"}["$p2"])
558 $currval = $this->record
->{"$p1"}["$p2"];
562 if ( isset($_POST[$fname]) ) {
563 $currval = $_POST[$fname];
565 else if ( isset($this->record
) && is_object($this->record
) && isset($this->record
->{"$base_fname"}) ) {
566 $currval = $this->record
->{"$base_fname"};
568 else if ( isset($this->record
) && is_object($this->record
) && isset($this->record
->{"$fname"}) ) {
569 $currval = $this->record
->{"$fname"};
572 if ( $ftype == "date" ) $currval = $session->FormattedDate($currval);
573 else if ( $ftype == "timestamp" ) $currval = $session->FormattedDate($currval, $ftype);
575 // Now build the entry field and render it
576 $field = new EntryField( $ftype, $fname, $this->_ParseAttributes($ftype,$attributes), $currval );
577 return $field->Render();
582 * A utility function for a submit button within a data entry table
583 * @return string The HTML fragment to display a submit button for the form.
585 function SubmitButton( $fname, $fvalue, $attributes = '' )
587 $field = new EntryField( 'submit', $fname, $this->_ParseAttributes('submit', $attributes), $fvalue );
588 return $field->Render();
592 * A utility function for a data entry line within a table
593 * @return string The HTML fragment to display the prompt and field.
595 function DataEntryLine( $prompt, $field_format, $ftype='', $fname='', $attributes='', $prefix = '' )
597 $attributes = $this->_ParseAttributes( $ftype, $attributes );
598 return sprintf( $this->table_line_format
, $prompt,
599 $this->DataEntryField( $field_format, $ftype, $fname, $attributes, $prefix ),
600 $attributes['_help'] );
605 * A utility function for a data entry line, where the prompt is a drop-down.
606 * @return string The HTML fragment for the drop-down prompt and associated entry field.
608 function MultiEntryLine( $prompt_options, $prompt_name, $default_prompt, $format, $ftype='', $fname='', $attributes='', $prefix )
611 $prompt = "<select name=\"$prompt_name\">";
613 reset($prompt_options);
614 while( list($k,$v) = each($prompt_options) ) {
615 $selected = ( ( $k == $default_prompt ) ?
' selected="selected"' : '' );
616 $nextrow = "<option value=\"$k\"$selected>$v</option>";
617 if ( preg_match('/&/', $nextrow) ) $nextrow = preg_replace( '/&/', '&', $nextrow);
620 $prompt .= "</select>";
622 return $this->DataEntryLine( $prompt, $format, $ftype, $fname, $attributes, $prefix );