view release on metacpan or search on metacpan
Changelog.ini view on Meta::CPAN
[V 1.14]
Date=2009-11-13T13:20:00
Comments= <<EOT
- Run dos2unix
- Rename Changes.txt to CHANGES
EOT
[V 1.13]
Date=2006-03-22T10:57:00
Comments= <<EOT
- In subs popup_menu and radio_group, use a case-insensitive compare when trying to set the default menu item.
I need this in one project because the menu values and the default come from different databases
EOT
[V 1.12]
Date=2005-12-12T13:18:00
Comments= <<EOT
- In sub popup_menu(), check the caller has set a default value before trying to use the default.
This stops an 'uninitialized' warning being emitted
- Add more patterns to MANIFEST.SKIP
EOT
[V 1.11]
Date=2005-11-09T15:38:00
Comments= <<EOT
- Add a new parameter to new() and set(): options.
This takes a hash ref as its value, and allows you to specify any list of options
you wish to add to the HTML of the 'select'. Eg:
- Remove POD heads 'Required Modules' and 'Changes'.
1.15 Wed Feb 10 14:01:48 2010
- MANIFEST.SKIP updated to exclude MYMETA.yml. Add MYMETA.yml.
1.14 Fri Nov 13 13:20:00 2009
- Run dos2unix
- Rename Changes.txt to CHANGES
1.13 Wed Mar 22 10:57:00 2006
- In subs popup_menu and radio_group, use a case-insensitive compare when trying to set the default menu item.
I need this in one project because the menu values and the default come from different databases
1.12 Mon Dec 12 13:18:00 2005
- In sub popup_menu(), check the caller has set a default value before trying to use the default.
This stops an 'uninitialized' warning being emitted
- Add more patterns to MANIFEST.SKIP
1.11 Wed Nov 9 15:38:00 2005
- Add a new parameter to new() and set(): options.
This takes a hash ref as its value, and allows you to specify any list of options
you wish to add to the HTML of the 'select'. Eg:
With new(name => 'a_name', ...), the 'select' would look like:
<select id = "a_name" name = "a_name">
With new(name => 'a_name', options => {tabindex => 2, force => 1}, ...),
Changelog.ini
Changes
examples/bootstrap-menus.pl
examples/test-complex-popup-radio.cgi
examples/test-simple-popup-radio.cgi
lib/DBIx/HTML/PopupRadio.pm
LICENSE
Makefile.PL
MANIFEST This list of files
MANIFEST.SKIP
README
t/00.versions.t
t/00.versions.tx
t/test.t
xt/author/pod.t
{
"abstract" : "Convert db data to HTML popup menu or radio group",
"author" : [
"Ron Savage (ron@savage.net.au)"
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 7.58, CPAN::Meta::Converter version 2.150010",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
---
abstract: 'Convert db data to HTML popup menu or radio group'
author:
- 'Ron Savage (ron@savage.net.au)'
build_requires:
ExtUtils::MakeMaker: '0'
Test::More: '1.001014'
Test::Pod: '1.48'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.58, CPAN::Meta::Converter version 2.150010'
Makefile.PL view on Meta::CPAN
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
my(%params) =
(
($] ge '5.005') ?
(
AUTHOR => 'Ron Savage (ron@savage.net.au)',
ABSTRACT => 'Convert db data to HTML popup menu or radio group',
) : (),
clean =>
{
FILES => 'blib/* Makefile MANIFEST DBIx-HTML-PopupRadio-*'
},
dist =>
{
COMPRESS => 'gzip',
SUFFIX => 'gz'
},
examples/test-complex-popup-radio.cgi view on Meta::CPAN
#!/usr/bin/perl
#
# Name:
# test-complex-popup-radio.cgi.
#
# Purpose:
# Test DBIx::HTML::PopupRadio.
#
# Author:
# Ron Savage <ron@savage.net.au>
# http://savage.net.au/index.html
use strict;
use warnings;
examples/test-complex-popup-radio.cgi view on Meta::CPAN
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use DBI;
use DBIx::HTML::PopupRadio;
use Error qw/ :try /;
# -----------------------------------------------
delete @ENV{'BASH_ENV', 'CDPATH', 'ENV', 'IFS', 'SHELL'}; # For security.
my(%popup_data) =
(
one =>
{ comment => 'Demonstrate returning an id when the user selects a name',
default => '', # Default menu item number.
menu => '', # Menu in HTML returned from menu object.
name => 'campus_1', # CGI name of menu.
object => '', # Menu object returned from DBIx::HTML::PopupRadio.
order => 1, # Sort order of menus down the page.
previous => '', # Previous user selection for this menu.
prompt => '', # Prompt at top of menu.
examples/test-complex-popup-radio.cgi view on Meta::CPAN
name => 'radio_2',
object => '',
order => 2,
previous => '',
sql => 'select unit_id, unit_code from unit order by unit_id',
},
);
my($caption) = 'Test DBIx::HTML::PopupRadio';
my($q) = CGI -> new();
$popup_data{$_}{'previous'} = $q -> param($popup_data{$_}{'name'}) || '' for keys %popup_data;
$radio_data{$_}{'previous'} = $q -> param($radio_data{$_}{'name'}) || '' for keys %radio_data;
my(@html);
try
{
my($dbh) = DBI -> connect
(
'DBI:mysql:test:127.0.0.1',
'root',
'pass',
{
AutoCommit => 1,
HandleError => sub {Error::Simple -> record($_[0]); 0},
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
}
);
for my $key (sort{$popup_data{$a}{'order'} <=> $popup_data{$b}{'order'} } keys %popup_data)
{
$popup_data{$key}{'object'} = DBIx::HTML::PopupRadio -> new(dbh => $dbh, name => $popup_data{$key}{'name'}, sql => $popup_data{$key}{'sql'});
$popup_data{$key}{'object'} -> set(default => $popup_data{$key}{'default'});
$popup_data{$key}{'menu'} = $popup_data{$key}{'object'} -> popup_menu(prompt => $popup_data{$key}{'prompt'});
push(@html, $q -> th('Comment') . $q -> td($popup_data{$key}{'comment'}) );
push(@html, $q -> th("Previous $popup_data{$key}{'name'}") . $q -> td($popup_data{$key}{'previous'} . ' => ' . $popup_data{$key}{'object'} -> param($popup_data{$key}{'previous'}) ) );
push(@html, $q -> th('Default') . $q -> td($popup_data{$key}{'default'}) );
push(@html, $q -> th('Prompt') . $q -> td($popup_data{$key}{'prompt'}) );
push(@html, $q -> th('SQL') . $q -> td($popup_data{$key}{'sql'}) );
push(@html, $q -> th('Campus') . $q -> td($popup_data{$key}{'menu'}) );
push(@html, $q -> th(' ') . $q -> td(' ') );
}
for my $key (sort{$radio_data{$a}{'order'} <=> $radio_data{$b}{'order'} } keys %radio_data)
{
$radio_data{$key}{'object'} = DBIx::HTML::PopupRadio -> new(dbh => $dbh, name => $radio_data{$key}{'name'}, sql => $radio_data{$key}{'sql'});
$radio_data{$key}{'object'} -> set(linebreak => $radio_data{$key}{'linebreak'});
$radio_data{$key}{'menu'} = $radio_data{$key}{'object'} -> radio_group(default => $radio_data{$key}{'default'});
examples/test-simple-popup-radio.cgi view on Meta::CPAN
#!/usr/bin/perl
#
# Name:
# test-simple-popup-radio.cgi.
#
# Purpose:
# Test DBIx::HTML::PopupRadio.
#
# Author:
# Ron Savage <ron@savage.net.au>
# http://savage.net.au/index.html
use strict;
use warnings;
examples/test-simple-popup-radio.cgi view on Meta::CPAN
'pass',
{
AutoCommit => 1,
HandleError => sub {Error::Simple -> record($_[0]); 0},
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
}
);
my($popup_object) = DBIx::HTML::PopupRadio -> new(dbh => $dbh, sql => "select campus_id, campus_name from campus order by campus_name");
# Or, use this code instead to trigger the msg: SQL returned no rows.
#my($popup_object) = DBIx::HTML::PopupRadio -> new(dbh => $dbh, sql => "select campus_id, campus_name from campus where campus_name = 'x' order by campus_name");
$popup_object -> set(default => 'Melbourne');
my($popup_menu) = $popup_object -> popup_menu();
$popup_menu = 'SQL returned no rows' if ($popup_object -> size() == 0);
push(@html, $q -> th('Previous selection') . $q -> td($previous . ' => ' . $popup_object -> param($previous) ) );
push(@html, $q -> th('Please choose a campus') . $q -> td($popup_menu) );
push(@html, $q -> th(' ') . $q -> td(' ') );
push(@html, $q -> th({colspan => 2}, $q -> submit({name => $caption, class => 'submit'}) ) );
}
catch Error::Simple with
{
my($error) = 'Error::Simple: ' . $_[0] -> text();
chomp($error);
push(@html, $q -> th('Error') . $q -> td($error) );
};
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
our $VERSION = '1.17';
# -----------------------------------------------
# Encapsulated class data.
{
my(%_attr_data) =
( # Alphabetical order.
_dbh => '',
_default => '', # For popup_menu or radio_group.
_javascript => '',
_linebreak => 0, # For radio_group.
_name => 'dbix_menu',
_options => {},
_prompt => '', # For popup_menu.
_sql => '',
);
sub _default_for
{
my($self, $attr_name) = @_;
$_attr_data{$attr_name};
}
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
sub param
{
my($self, $id) = @_;
$id ? $$self{'_data'}{$id}{'value'} : '';
} # End of param.
# -----------------------------------------------
sub popup_menu
{
my($self, %arg) = @_;
# Give the user one last chance to set some parameters.
$self -> set(%arg);
$self -> _validate_options();
$self -> _read_data() if (! $$self{'_data'});
my(@html, $s);
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
$s .= qq| selected="selected"| if (defined($$self{'_default'}) && (lc $$self{'_default'} eq lc $$self{'_data'}{$_}{'value'}) );
$s .= qq|>$Entitize{$$self{'_data'}{$_}{'value'} }</option>|;
push @html, $s;
}
push @html, '</select>', '';
join "\n", @html;
} # End of popup_menu.
# -----------------------------------------------
sub radio_group
{
my($self, %arg) = @_;
# Give the user one last chance to set some parameters.
$self -> set(%arg);
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
} # End of size.
# -----------------------------------------------
1;
__END__
=head1 NAME
C<DBIx::HTML::PopupRadio> - Convert sql into a popup menu or radio group.
=head1 Synopsis
use DBIx::HTML::PopupRadio;
my($popup_object) = DBIx::HTML::PopupRadio -> new
(
dbh => $dbh,
sql => 'select campus_id, campus_name from campus order by campus_name',
);
$popup_object -> set(default => '1');
my($popup_menu) = $popup_object -> popup_menu();
my($radio_group) = $popup_object -> radio_group();
print $popup_menu;
=head1 Description
This module takes a db handle and an SQL statement, and builds a hash.
Then you ask for that hash in HTML, as a popup menu or as a radio group.
The reading of the db table is delayed until you actually call one of the
methods 'popup_menu' or 'radio_group'. Even then, it is delayed until any
parameters passed in to these 2 methods are processed.
After a call to one of these 2 methods, you can call the 'size' method if
you need to check how many rows were returned by the SQL you used.
Neither the module CGI.pm, nor any of that kidney, are used by this module.
We simply output pure HTML.
=head1 Distributions
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
See http://savage.net.au/Perl-modules/html/installing-a-module.html for
help on unpacking and installing each type of distro.
=head1 Usage
You create an object of the class by calling the constructor, 'new'.
You then call 'set', if you wish, to set any options.
Now call 'popup_menu' or 'radio_group' to get the HTML.
Lastly, display the HTML as part of a form.
The method names 'popup_menu' and 'radio_group' (and 'param') were chosen to be
reminiscent of methods with the same names in the CGI.pm module. But let
me repeat, my module does not use CGI.
=head1 Options
Here, in alphabetical order, are the options accepted by the constructor,
together with their default values.
=over 4
=item dbh => ''
Pass in an open database handle.
This option is mandatory, in the call to new, set, popup_menu or radio_group.
Ie By the time you call one of the latter 2 methods, dbh must be set.
=item default => ''
Pass in the string (from SQL column 2) which is to be the default item on the popup
menu or radio group. You supply here the visible menu item, not the value associated with
that menu item.
Starting with V 1.13, a case-insensitive match is used to compare the value provided here with the
values read in from the database.
If default is not given a value, the first menu item becomes the default.
See the discussion of the sql option for details about the menu items.
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
{
a_form.field02.value =
a_form.field01.options[a_form.field01.selectedIndex].text;
a_form.field03.value =
a_form.field01.options[a_form.field01.selectedIndex].value;
}
</script>
You can see what is happening: The menu item, both visible text and corresponding value returned
to your CGI script, are being copied from the popup menu to 2 other fields in the form.
Obviously you would replace the body of this function with code of your own choosing.
Q: Since this is an onChange handler, the 2 other fields will not be initialized until
the default menu selection is changed. So, how do you initialize them before the user selects a
new menu item?
A: By outputting the following Javascript further down the form, after the function, menu and
other 2 fields have been defined:
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
=item linebreak => 0
Pass in 1 if you want each radio group item on a separate line, ie separated
by <br />s.
This option is not mandatory.
=item name => 'dbix_menu'
Pass in the name of the form item to use for the popup menu or radio group.
This option is not mandatory, since it has a default value. It could be unset
and then reset, but must have a value by the time you call popup_menu or
radio_group.
The value of this parameter is what you will pass into a CGI object when you
call its param() method to retrieve the user selection.
Hence you would do something like:
my($name) = 'fancy_menu';
my($popup_object) = DBIx::HTML::PopupRadio -> new(name => $name, ...);
my($q) = CGI -> new();
my($id) = $q -> param($name) || '';
=item options => {}
This is a hash ref of options you wish to set on the HTML 'select' itself.
Eg: new(name => 'a_name', options => {tabindex => 2, force => 1}, ...).
With new(name => 'a_name', ...), the select would look like:
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
With new(name => 'a_name', options => {tabindex => 2, force => 1}, ...), the select would look like:
<select id = "a_name" name = "a_name" tabindex = "2" force = "1">
This option is not mandatory.
=item prompt => ''
Alternately, this is possible: prompt => {'x' => 'y'}.
Pass in a prompt to use as the first entry in the popup menu.
The string, in the first case, can contain a single quote but not a double quote. Eg:
prompt => "O'Connor".
The key, in the second case, can contain a single quote but not a double quote. Eg:
prompt => {"'aitch" => 'Haitch = "h"'}.
Hence we can say new(prompt => 'Choose one') or new(prompt => {'0' => 'Choose one'}).
In the first case, the string 'Choose one' is both a visible menu item and the value
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
Alternately, an anonymous hash or hash reference can be used, so multiple pairs
may be passed and they will appear at the top of the list sorted in the order of the
values. Example new(prompt => {'no change' => 'no change', 'remove' => 'remove'});
This latter mechanism allows you to put multiple selections at the top of the menu.
This option is not mandatory.
=item sql => ''
Pass in the SQL used to select the popup menu or radio group items.
This option is mandatory, in the call to new, set, popup_menu or radio_group.
Ie By the time you call one of the latter 2 methods, sql must be set.
The SQL must select 2 columns. The first will be used as the value returned by
a CGI object, for example, when you call its param() method. The second value
will be used as the visible selection offered to the user on the menu.
Of course, the 2 columns selected could be the same:
$obj -> set(sql => 'select campus_name, campus_name from campus');
lib/DBIx/HTML/PopupRadio.pm view on Meta::CPAN
The question remains: After you do something like this:
my($q) = CGI -> new();
my($id) = $q -> param('dbxi_menu') || '';
how do you convert the value, eg campus_id, back into the visible menu item, eg
campus_name.
Simple: You call the param method of the DBIx::HTML::PopupRadio class:
my($name) = $popup_object -> param($id);
param returns the empty string if the value of $id is unknown.
=back
=head1 Methods
=over 4
=item new(%arg): The constructor
See the previous section for details of the parameters.
=item param($id): Returns visible menu item corresponding to menu value
Call this to convert the value returned to the CGI script when the user
selected a menu item, into the visible menu item selected by the user.
In other words, convert the first column of the SQL into the second column.
=item popup_menu(%arg): Return the HTML for a popup menu
popup_menu(%arg) takes the same parameters as new().
=item radio_group(%arg): Return the HTML for a radio group
radio_group(%arg) takes the same parameters as new().
=item set(%arg): Set options thus: some_option => $arg{'some_option'}
Call this to set any option after calling new().
set(%arg) takes the same parameters as new().
=item size(): Return the number of rows returned by your SQL
Call this after calling 'popup_menu' or 'radio_group'.
It will tell you whether or not your menu is empty.
=back
=head1 Sample Code
See examples/*.cgi for complete programs, both simple and complex.
You will need to run examples/bootstrap-menus.pl to load the 'test'