Back to index

awl  0.53
DataEntry.php
Go to the documentation of this file.
00001 <?php
00011 require_once("AWLUtilities.php");
00012 
00038 class EntryField
00039 {
00047   var $fname;
00048 
00053   var $ftype;
00063   var $current;
00064 
00069   var $attributes;
00070 
00075   var $rendered;
00137   function EntryField( $intype, $inname, $attributes="", $current_value="" )
00138   {
00139     $this->ftype = $intype;
00140     $this->fname = $inname;
00141     $this->current = $current_value;
00142 
00143     if ( isset($this->{"new_$intype"}) && function_exists($this->{"new_$intype"}) ) {
00144       // Optionally call a function within this object called "new_<intype>" for setup
00145       $this->{"new_$intype"}( $attributes );
00146     }
00147     else if ( is_array($attributes) ) {
00148       $this->attributes = $attributes;
00149     }
00150     else {
00151     }
00152 
00153     $this->rendered = "";
00154   }
00155 
00162   function Render() {
00163     global $session;
00164 
00165     $r = "<";
00166     dbg_error_log( "EntryField", ":Render: Name: %s, Type: %s, Current: %s", $this->fname, $this->ftype, $this->current );
00167     $size = "";
00168     switch ( $this->ftype ) {
00169 
00170       case "select":
00171         $r .= "select name=\"$this->fname\"%%attributes%%>";
00172         reset( $this->attributes );
00173         while( list($k,$v) = each( $this->attributes ) ) {
00174           if ( substr($k, 0, 1) != '_' ) continue;
00175           if ( $k == '_help' ) continue;
00176           $k = substr($k,1);
00177           $r .= "<option value=\"".htmlspecialchars($k)."\"";
00178           if ( "$this->current" == "$k" ) $r .= " selected";
00179           $r .= ">$v</option>" ;
00180         }
00181         $r .= "</select>";
00182         break;
00183 
00184       case "lookup":
00185         $r .= "select name=\"$this->fname\"%%attributes%%>";
00186         reset( $this->attributes );
00187         while( list($k,$v) = each( $this->attributes ) ) {
00188           if ( substr($k, 0, 1) != '_' ) continue;
00189           $k = substr($k,1);
00190           if ( $k == 'help' || $k == "sql" || $k == "type" ) continue;
00191           if ( $k == "null" ) $k = "";
00192           if ( $k == "zero" ) $k = "0";
00193           $r .= "<option value=\"".htmlspecialchars($k)."\"";
00194           if ( "$this->current" == "$k" ) $r .= " selected";
00195           $r .= ">$v</option>" ;
00196         }
00197         if ( isset($this->attributes["_sql"]) ) {
00198           $qry = new PgQuery( $this->attributes["_sql"] );
00199         }
00200         else {
00201           $qry = new PgQuery( "SELECT code_id, code_value FROM codes WHERE code_type = ? ORDER BY code_seq, code_id", $this->attributes['_type'] );
00202         }
00203         $r .= EntryField::BuildOptionList( $qry, $this->current, "rndr:$this->fname", array('translate'=>1) );
00204         $r .= "</select>";
00205         break;
00206 
00207       case "date":
00208       case "timestamp":
00209         $size = '';
00210         if ( !isset($this->attributes['size']) || $this->attributes['size'] == "" ) $size = " size=" . ($this->ftype == 'date' ? "12" : "18");
00211         $r .= "input type=\"text\" name=\"$this->fname\"$size value=\"".$session->FormattedDate(htmlspecialchars($this->current))."\"%%attributes%%>";
00212         break;
00213 
00214       case "checkbox":
00215         // We send a hidden field with a false value, which will be overridden by the real
00216         // field with a true value (if true) or not overridden (if false).
00217         $r .= "input type=\"hidden\" name=\"$this->fname\" value=\"off\"><";
00218       case "radio":
00219         $checked = "";
00220         if ( $this->current === true || $this->current == 't' || intval($this->current) == 1 || $this->current == 'on'
00221               || (isset($this->attributes['value']) && $this->current == $this->attributes['value'] ) )
00222           $checked = " checked";
00223         $id = "id_$this->fname" . ( $this->ftype == "radio" ? "_".$this->attributes['value'] : "");
00224         if ( isset($this->attributes['_label']) ) {
00225           $r .= "label for=\"$id\"";
00226           if ( isset($this->attributes['class']) )
00227             $r .= ' class="'. $this->attributes['class'] . '"';
00228           $r .= "><";
00229         }
00230         $r .= "input type=\"$this->ftype\" name=\"$this->fname\" id=\"$id\"$checked%%attributes%%>";
00231         if ( isset($this->attributes['_label']) ) {
00232           $r .= " " . $this->attributes['_label'];
00233           $r .= "</label>";
00234         }
00235         break;
00236 
00237       case "button":
00238         $r .= "input type=\"button\" name=\"$this->fname\"%%attributes%%>";
00239         break;
00240 
00241       case "submit":
00242         $r .= "input type=\"submit\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current)."\"%%attributes%%>";
00243         break;
00244 
00245       case "textarea":
00246         $r .= "textarea name=\"$this->fname\"%%attributes%%>$this->current</textarea>";
00247         break;
00248 
00249       case "file":
00250         if ( !isset($this->attributes['size']) || $this->attributes['size'] == "" ) $size = " size=25";
00251         $r .= "input type=\"file\" name=\"$this->fname\"$size value=\"".htmlspecialchars($this->current)."\"%%attributes%%>";
00252         break;
00253 
00254       case "password":
00255         $r .= "input type=\"password\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current)."\"%%attributes%%>";
00256         break;
00257 
00258       default:
00259         $r .= "input type=\"text\" name=\"$this->fname\" value=\"".htmlspecialchars($this->current)."\"%%attributes%%>";
00260         break;
00261     }
00262 
00263     // Now process the generic attributes
00264     reset( $this->attributes );
00265     $attribute_values = "";
00266     while( list($k,$v) = each( $this->attributes ) ) {
00267       if ( $k == '_readonly' ) $attribute_values .= " readonly";
00268       else if ( $k == '_disabled' ) $attribute_values .= " disabled";
00269       if ( substr($k, 0, 1) == '_' ) continue;
00270       $attribute_values .= " $k=\"".htmlspecialchars($v)."\"";
00271     }
00272     $r = str_replace( '%%attributes%%', $attribute_values, $r );
00273 
00274     $this->rendered = $r;
00275     return $r;
00276   }
00277 
00283   function new_lookup( $attributes ) {
00284     $this->attributes = $attributes;
00285   }
00286 
00294   static function BuildOptionList( $qry, $current = '', $location = 'options', $parameters = false ) {
00295     global $debuggroups;
00296     $result = '';
00297     $translate = false;
00298 
00299     if ( isset($maxwidth) ) unset($maxwidth);
00300     if ( is_array($parameters) ) {
00301       if ( isset($parameters['maxwidth']) ) $maxwidth = max(4,intval($parameters['maxwidth']));
00302       if ( isset($parameters['translate']) ) $translate = true;
00303     }
00304 
00305     // The query may not have already been executed
00306     if ( $qry->rows() > 0 || $qry->Exec($location) ) {
00307       while( $row = $qry->Fetch(true) )
00308       {
00309         if (is_array($current)) {
00310           $selected = ( ( in_array($row[0],$current,true) || in_array($row[1],$current,true)) ? ' selected="selected"' : '' );
00311         }
00312         else {
00313           $selected = ( ( "$row[0]" == "$current" || "$row[1]" == "$current" ) ? ' selected="selected"' : '' );
00314         }
00315         $display_value = $row[1];
00316         if ( isset($translate) ) $display_value = translate( $display_value );
00317         if ( isset($maxwidth) ) $display_value = substr( $display_value, 0, $maxwidth);
00318         $nextrow = "<option value=\"".htmlspecialchars($row[0])."\"$selected>".htmlspecialchars($display_value)."</option>";
00319         $result .= $nextrow;
00320       }
00321     }
00322     return $result;
00323    }
00324   
00325 }
00326 
00334 class EntryForm
00335 {
00343   var $action;
00344 
00349   var $record;
00350 
00355   var $EditMode;
00356 
00361   var $name;
00362 
00367   var $class;
00368 
00373   var $break_line_format;
00374 
00379   var $table_line_format;
00380 
00385   var $saved_line_format;
00394   function EntryForm( $action, &$record, $editing=false )
00395   {
00396     $this->action   = $action;
00397     $this->record   = &$record;
00398     $this->EditMode = $editing;
00399     $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
00400     $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
00401   }
00402 
00408   function PopulateForm( &$record, $prefix="" )
00409   {
00410     foreach( $record AS $k => $v ) {
00411       $this->record->{"$prefix$k"} = $v;
00412     }
00413   }
00414 
00418   function NoHelp( ) {
00419     $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
00420     $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s</td></tr>'."\n";
00421   }
00422 
00426   function HelpInLine( ) {
00427     $this->break_line_format = '<tr><th class="ph" colspan="2">%s</th></tr>'."\n";
00428     $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s<span class="help">%s</span></td></tr>'."\n";
00429   }
00430 
00434   function HelpInCell( ) {
00435     $this->break_line_format = '<tr><th class="ph" colspan="3">%s</th></tr>'."\n";
00436     $this->table_line_format = '<tr><th class="prompt">%s</th><td class="entry">%s</td><td class="help">%s</td></tr>'."\n";
00437   }
00438 
00442   function SimpleForm( $new_format = '<span class="prompt">%s:</span>&nbsp;<span class="entry">%s</span>' ) {
00443     $this->break_line_format = '%s'."\n";
00444     $this->table_line_format = $new_format."\n";
00445   }
00446 
00451   function TempLineFormat( $new_format = '<span class="prompt">%s:</span>&nbsp;<span class="entry">%s</span>' ) {
00452     $this->saved_line_format = $this->table_line_format;
00453     $this->table_line_format = $new_format ."\n";
00454   }
00455 
00459   function RevertLineFormat( ) {
00460     if ( isset($this->saved_line_format) ) {
00461       $this->table_line_format = $this->saved_line_format;
00462     }
00463   }
00464 
00470   function StartForm( $extra_attributes='' ) {
00471     if ( !is_array($extra_attributes) && $extra_attributes != '' ) {
00472       list( $k, $v ) = explode( '=', $extra_attributes );
00473       $extra_attributes = array( $k => $v );
00474     }
00475     $extra_attributes['action']  = $this->action;
00476     if ( !isset($extra_attributes['method']) )  $extra_attributes['method']  = 'post';
00477     if ( strtolower($extra_attributes['method']) != 'get' )
00478       if ( !isset($extra_attributes['enctype']) ) $extra_attributes['enctype'] = 'multipart/form-data';
00479     if ( !isset($extra_attributes['name']) )    $extra_attributes['name']    = 'form';
00480     if ( !isset($extra_attributes['class']) )   $extra_attributes['class']   = 'formdata';
00481     if ( !isset($extra_attributes['id']) )      $extra_attributes['id']      = $extra_attributes['name'];
00482 
00483     // Now process the generic attributes
00484     reset( $extra_attributes );
00485     $attribute_values = "";
00486     while( list($k,$v) = each( $extra_attributes ) ) {
00487       $attribute_values .= " $k=\"".htmlspecialchars($v)."\"";
00488     }
00489     return "<form$attribute_values>\n";
00490   }
00491 
00496   function EndForm( ) {
00497     return "</form>\n";
00498   }
00499 
00504   function BreakLine( $text = '' )
00505   {
00506     return sprintf( $this->break_line_format, translate($text));
00507   }
00508 
00516   function HiddenField($fname,$fvaluei,$fid = null) {
00517     return sprintf( '<input type="hidden" name="%s" value="%s" %s/>%s', $fname,
00518                                htmlspecialchars($fvalue), (isset($id) ? 'id="$id" ' : ''), "\n" );
00519   }
00520 
00532   function _ParseAttributes( $ftype = '', $attributes = '' )  {
00533 
00534     if ( !is_array($attributes) ) {
00535       if ( strpos( $attributes, '=' ) === false ) {
00536         $attributes = array();
00537       }
00538       else {
00539         list( $k, $v ) = explode( '=', $attributes );
00540         $attributes = array( $k => $v );
00541       }
00542     }
00543 
00544     // Default the help to the title, or to blank
00545     if ( !isset($attributes['_help']) ) {
00546       $attributes['_help'] = "";
00547       if ( isset($attributes['title']) )
00548         $attributes['_help'] = $attributes['title'];
00549     }
00550 
00551     // Default the style to fdate, ftext, fcheckbox etc.
00552     if ( !isset($attributes['class']) ) {
00553       $attributes['class'] = "f$ftype";
00554     }
00555 
00556     return $attributes;
00557   }
00558 
00563   function DataEntryField( $format, $ftype='', $base_fname='', $attributes='', $prefix='' )
00564   {
00565     global $session;
00566 
00567     if ( ($base_fname == '' || $ftype == '') ) {
00568       // Displaying never-editable values
00569       return $format;
00570     }
00571     $fname = $prefix . $base_fname;
00572 
00573     dbg_error_log( "DataEntry", ":DataEntryField: fmt='%s', fname='%s', fvalue='%s'", $format, $fname, (isset($this->record->{$fname})?$this->record->{$fname}:'value not set') );
00574     if ( !$this->EditMode ) {
00576       if ( !isset($this->record->{$fname}) && substr($fname,0,4) == 'xxxx' && isset($this->record->{substr($fname,4)}) )
00577         $fname = substr($fname,4);
00578       if ( !isset($this->record->{$fname}) ) return '';
00580       if ($ftype == "date" || $ftype == "timestamp")
00581         return sprintf($format, $session->FormattedDate($this->record->{$fname}) );
00582       dbg_error_log( "DataEntry", ":DataEntryField: fmt='%s', fname='%s', fvalue='%s'", $format, $fname, (isset($this->record->{$fname})?$this->record->{$fname}:'value not set') );
00583       return sprintf($format, $this->record->{$fname} );
00584     }
00585 
00586     $currval = '';
00587     // Get the default value, preferably from $_POST
00588     if ( preg_match("/^(.+)\[(.+)\]$/", $fname, $parts) ) {
00589       $p1 = $parts[1];
00590       $p2 = $parts[2];
00591       @dbg_error_log( "DataEntry", ":DataEntryField: fname=%s, p1=%s, p2=%s, POSTVAL=%s, \$this->record->{'%s'}['%s']=%s",
00592                                                   $fname, $p1, $p2, $_POST[$p1][$p2], $p1, $p2, $this->record->{"$p1"}["$p2"] );
00593       // @todo This could be changed to handle more dimensions on submitted variable names
00594       if ( isset($_POST[$p1]) ) {
00595         if ( isset($_POST[$p1][$p2]) ) {
00596           $currval = $_POST[$p1][$p2];
00597         }
00598       }
00599       else if ( isset($this->record) && is_object($this->record)
00600                 && isset($this->record->{"$p1"}["$p2"])
00601               ) {
00602         $currval = $this->record->{"$p1"}["$p2"];
00603       }
00604     }
00605     else {
00606       if ( isset($_POST[$fname]) ) {
00607         $currval = $_POST[$fname];
00608       }
00609       else if ( isset($this->record) && is_object($this->record) && isset($this->record->{"$base_fname"}) ) {
00610         $currval = $this->record->{"$base_fname"};
00611       }
00612       else if ( isset($this->record) && is_object($this->record) && isset($this->record->{"$fname"}) ) {
00613         $currval = $this->record->{"$fname"};
00614       }
00615     }
00616     if ( $ftype == "date" ) $currval = $session->FormattedDate($currval);
00617     else if ( $ftype == "timestamp" ) $currval = $session->FormattedDate($currval, $ftype);
00618 
00619     // Now build the entry field and render it
00620     $field = new EntryField( $ftype, $fname, $this->_ParseAttributes($ftype,$attributes), $currval );
00621     return $field->Render();
00622   }
00623 
00624 
00629   function SubmitButton( $fname, $fvalue, $attributes = '' )
00630   {
00631     $field = new EntryField( 'submit', $fname, $this->_ParseAttributes('submit', $attributes), $fvalue );
00632     return $field->Render();
00633   }
00634 
00639   function DataEntryLine( $prompt, $field_format, $ftype='', $fname='', $attributes='', $prefix = '' )
00640   {
00641     $attributes = $this->_ParseAttributes( $ftype, $attributes );
00642     return sprintf( $this->table_line_format, $prompt,
00643                 $this->DataEntryField( $field_format, $ftype, $fname, $attributes, $prefix ),
00644                 $attributes['_help'] );
00645   }
00646 
00647 
00652   function MultiEntryLine( $prompt_options, $prompt_name, $default_prompt, $format, $ftype='', $fname='', $attributes='', $prefix )
00653   {
00654 
00655     $prompt = "<select name=\"$prompt_name\">";
00656 
00657     reset($prompt_options);
00658     while( list($k,$v) = each($prompt_options) ) {
00659       $selected = ( ( $k == $default_prompt ) ? ' selected="selected"' : '' );
00660       $nextrow = "<option value=\"$k\"$selected>$v</option>";
00661       if ( preg_match('/&/', $nextrow) ) $nextrow = preg_replace( '/&/', '&amp;', $nextrow);
00662       $prompt .= $nextrow;
00663     }
00664     $prompt .= "</select>";
00665 
00666     return $this->DataEntryLine( $prompt, $format, $ftype, $fname, $attributes, $prefix );
00667   }
00668 
00669 }
00670