NAME

Tk::DBI::Form

SYNOPSIS

my $mw = MainWindow->new;
my $tkdbi = $mw->DBIForm(
	-dbh   		=> $dbh,
	-table  	=> 'Inventory',
	-editId		=> 'yes',
	-readonly => {
		changed_by => 'xpix',
		created => 'NOW',
		...
		},
	-required => {
		name => 1,
		state => 1,
		owner => 1,
		...
		},
	-test_cb => {
		type_id => sub{
			my ($save, $name) = @_;
			if($save->{type_id} and $save->{type_id} !~ /^\d+$/) {
				$dbh->do(sprintf("INSERT INTO Type (name) VALUES ('%s')", $save->{type_id}));
				$save->{type_id} = $dbh->{'mysql_insertid'};				
			}
			return undef; # Alles ok!
		},
		...
	},
	-link => {
		type_id => {
			table 	=> 'Type',
			display	=> 'name',
			id	=> 'id',
		},
		...
	},
	-validate_cb => {
		serial_no => sub {
			my ($entry, $save, $input) = @_;
			$save->{id} = 0 unless(defined $save->{id});
			$entry->configure( 
				-bg => ( exists $SERIAL->{$input} ? 'red' : 'green' ),
				-fg => ( exists $SERIAL->{$input} ? 'white' : 'black' ),
				 );
			return 1 ;
		},
		...
	},
	-images => {
		id 	  => $pics{F1},
		parent_id => $pics{F2},
		...
	},
	-events => {
		'<KeyRelease-F1>' => sub { 
				$DBIFORM->{entrys}->{id}->focus; 
		},
		...
	}, 
	-addButtons => {
		Logs => {
			-type => ['update'],
			-callback => sub{
				my ($save, $name) = @_;
				&launch_browser_log($save->{id});				
			}, 
		},
		...,
	},
	-alternateTypes => {
		filename => {
			type => 'file',
			directory => $DOCUPATH,
		},
		...
	},

	-debug => 1,
);

my $ok = $tkdbi->editRecord($row->{id});

NAME

Tk::DBI::Form - Megawidget to offering edit, delete or insert a record.

DESCRIPTION

Tk::DBI::Form is a Megawidget offering edit, delete or insert operations for table records.

OPTIONS

-dbh

The database handle to get the information from the Database.

-table

Name of the table you intend to modify records from.

-debug => 1

Switch the debug output to the standart console on.

-edit_id => 1

This allows to edit the ID-Number on the form, this is normaly a unique and autoincrement Field for each column.

-update => [qw(id col1 col2 ...)]

List of fields that are granted update priviliges on. Only these fields are visible on the Update Form

-insert => [qw(id col1 col2 ...)]

List of fields that are granted insert priviliges on. Only these fields are displayed on the Insert Form.

This is a special Feature for fields located in a different table than given in -table. Often data from further tables is used, this data usually has an id number and a description. The id number from this table is mostly in the table to edit as id number. Here you can display the Description for this id and the user can change this choice. I.e.:

  -link => {
	parent_id => {
		table 	=> 'Inventory',
		display	=> 'name',
		where	=> 'WHERE type_id = 1', 
		id	=> 'id',
	},
	type_id => {
		table 	=> 'Type',
		display	=> 'name',
		id	=> 'id',
	},
  }

Ok, here we have two linktables. This will display a Listwidget, thes have the column 'name' to display in this Listbox. But the form write the id in the original column.

-required => { col1 => 1, col2 = 1, ...}

Here you can mark the fields where an entry is mandatory on the Form, is case no entry will be provided, the form will raise an error MessageBox displaying 'col1 is a required field!'.

  -required => {
	changed_by => 1,
	deadline => 1,
	Server => 1,
  } 

-readonly => { col1 => 'text', col2 = number, ...}

This option will set the columns as read only. The values are displayed but the user cannot change the data

  -readonly => {
	changed_by => $USER,
	deadline => 'NOW',
	Server => $HOST,
  } 

-default => { col1 => 'text', col2 = number, ...}

This option sets the default values for the listed fields that will be displayed on the form. I.e.:

  -default => {
	changed_by => $USER,
	deadline => 'NOW',
	Server => $HOST,
  } 

-images => { col1 => ImageObj, col2 = ImageObj, ...}

This option sets the Image Object for an icon that will be displayed next to the input or widget.

-alternateTypes => { col1 => ImageObj, col2 = ImageObj, ...}

Here you can set a alternativeType to display. I.E.:

  -alternateTypes => {
	filename => {
		type => 'file',
		directory => $DOCUPATH,
	},
	password => {
		type => 'password',
	},
  },
file

This parameter results in displaying an entry and a button, the user can click on this button and a Fileselector will pop up on the form to select the right file and path.

password

This will display an entry with hidden letters as stars on the form.

-events => { Event => sub{}, Event => sub{}, ...}

This option lets you add your personal events. I.E.:

  -events => {
	'<KeyRelease-F1>' => sub { 
			$DBIFORM->{entrys}->{id}->focus; 
   },

-validate_cb => { col1 => sub{}, col2 => sub{}, ...}

Here you can add a callback to test the input from the user in realtime. The parameter for the subroutine is the entry, save hash with data from the Form and the input from the User. I.E.:

  serial_no => sub {
	my ($entry, $save, $input) = @_;
	$save->{id} = 0 unless(defined $save->{id});
	$entry->configure( 
		-bg => ( exists $SERIAL->{$input} ? 'red' : 'green' ),
		-fg => ( exists $SERIAL->{$input} ? 'white' : 'black' ),
		 );
	return 1 ;
  },

This changes the foreground and background color of the entry if the serial number exists in the table. The subroutine can return a undef value, then the widget will igrnore this Userinput. I.e.:

  only_numbers => sub {
	my ($entry, $save, $input) = @_;
	return undef unless($input =~ /[^0-9]+/);
	return 1 ;
  },

-test_cb => { col1 => sub{}, col2 => sub{}, ...}

Here you can add a callback to test the user input AFTER submission of the form. The parameter for the subroutine is the save hash and the name of the field. I.E.:

  -test_cb => {
	id => sub{
		my ($save, $name) = @_;
		if($DBIFORM->type() eq 'insert' and $save->{id}) {
			my $answer = qsure($top,sprintf('You will REPLACE row <%s>?', $save->{id}));
			return 'NOMESSAGE' unless($answer);  # Back without message
		}
		return undef; # All OK ...
	},
	parent_id => sub{
		my ($save, $name) = @_;
		my $pid = sprintf('%010d', $save->{parent_id});
		unless(exists $INV->{$pid}) {
			my $msg = sprintf('Parent ID %s not exists', $pid);
			return $msg;
		}
		return undef; # All OK!
	},
  }

The first example will pop up a MessageBox if the User makes an Insert with an id number (replace). The second example will reformat the parent_id Number to 0000000012. If the parent_id does not exist in the Hash, an Errormessage (MessageBox) with the returned the returned text. 'NOMESSAGE' as return doesnt pop up a MessageBox. Return undef, all ok.

-cancel_cb => sub{ }

Here you can add a callback when the User activates the Cancel Button.

-addButtons => { ButtonName => {-type => ['update', 'insert'], -callback => sub{} }

Here you can add a Button to the FormBox. The -type option will only display the button in the following state (insert, update or delete). The callback has one parameter. The save hash. I.e.:

-addButtons => {
	Logs => { 
		-type => ['update'],
		-callback => sub{
			my ($save, $name) = @_;
			&launch_browser_log($save->{id});				
		}, 
	},
},

The example will display a logbrowser when the user click on the Button 'Logs'.

METHODS

editRecord(id);

This will display the update form with the following id number for an update.

newRecord([id]);

This will display the insert form with the following id number for a Replace operation.

  my $datahash = $DBH->selectall_hashref(select * from table where id = 12);
  delete $datahash->{id};
  $DBIFORM->newRecord(
	{
		default => $datahash,
	},	
  );

Here you see a trick to copy a column, also display a insert form with the values from column 12.

deleRecord(id);

This will display the delete form with the following id number for a delete operation.

Table_is_Change(last_time, 'tablename');

This returns true if the table was modified the last_time (seconds at epoche).

ADVERTISED WIDGETS

The Widgets in the form are advertised with 'wi_namecolumn'.

CHANGES

$Log: Form.pm,v $ Revision 1.6 2003/04/29 16:34:46 xpix * add Doku tag Changes

AUTHOR

xpix@netzwert.ag

Copyright (C) 2003 , Frank (xpix) Herrmann. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

KEYWORDS

Tk::JBrowseEntry, Tk::XDialogBox, Tk::NumEntry, Tk::Date, Tk::LabFrame, Tk::FBox