| 1 | package User::Config::UI::HTMLFormHandler; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | our $VERSION = '0.01_01'; | ||||||||||||||||||||||||
| 7 | $VERSION = eval $VERSION; # see L<perlmodstyle> | ||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||
| 9 | 1 | ▶ | use User::Config::UI::HTMLFormHandler::Model; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | 1 | ▶ | use Moose; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 11 | extends 'HTML::FormHandler'; | ||||||||||||||||||||||||
| 12 | with 'User::Config::UI'; | ||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | User::Config::UI::HTMLFormHandler - generate a userinterface for | ||||||||||||||||||||||||
| 17 | L<User::Config> using a web-form. | ||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||
| 21 | use User::Config; | ||||||||||||||||||||||||
| 22 | use CGI qw/:cgi-lib/; | ||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | my $uc = User::Config->instance; | ||||||||||||||||||||||||
| 25 | my $ui = $uc->ui("HTMLFormHandler"); | ||||||||||||||||||||||||
| 26 | my $params = Vars; | ||||||||||||||||||||||||
| 27 | my $form->generate($params); | ||||||||||||||||||||||||
| 28 | ... | ||||||||||||||||||||||||
| 29 | print $form->render; | ||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||
| 33 | Using this module, a form is generated containing all visible options | ||||||||||||||||||||||||
| 34 | for L<User::Config>. The C<generate>-method will also process the parameters, | ||||||||||||||||||||||||
| 35 | if any are given. Either way a fully functional C<HTML::FormHandler>-object is | ||||||||||||||||||||||||
| 36 | returned. | ||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||
| 40 | # perform common operations on a form element | ||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _form_entry_general { | ||||||||||||||||||||||
|
Subroutine
_form_entry_general: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 42 | 0 | my ($opt, $form) = @_; | |||||||||||||||||||||||
| 43 | 0 | ▶ | $form->{validate} = $opt->{validate} if $opt->{validate}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 44 | 0 | return $form; | |||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | # generate a selection; maybe with a user-selectable entry. | ||||||||||||||||||||||||
| 48 | 0 | ▶ | sub _get_form_selection { | ||||||||||||||||||||||
|
Subroutine
_get_form_selection: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||
| 49 | 0 | my ($opt, $name) = @_; | |||||||||||||||||||||||
| 50 | 0 | my @presets; | |||||||||||||||||||||||
| 51 | my $withtext; | ||||||||||||||||||||||||
| 52 | 0 | for(keys(%{$opt->{presets}})) { | |||||||||||||||||||||||
| 53 | 0 | push(@presets, { label => $_, value => $opt->{presets}->{$_}}); | |||||||||||||||||||||||
| 54 | 0 | ▶ | if(not defined($opt->{presets}->{$_})) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 55 | 0 | $withtext = 1; | |||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||
| 58 | 0 | my $ret = { | |||||||||||||||||||||||
| 59 | type => 'Select', | ||||||||||||||||||||||||
| 60 | multiple => 1, | ||||||||||||||||||||||||
| 61 | options => \@presets, | ||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||
| 63 | 0 | ▶ | return _form_entry_general($opt, $ret) unless $withtext; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 64 | 0 | $ret->{javascript} = "onchange=\"if(document.getElementById('$name.select').value == '' ) { document.getElementById('$name.text').display('block'); } else { document.getElementById('$name.text').display('none') }\""; | |||||||||||||||||||||||
| 65 | 0 | $ret->{id} = $name.".select"; | |||||||||||||||||||||||
| 66 | 0 | return _form_entry_general($opt, { type => 'Compound', }), | |||||||||||||||||||||||
| 67 | $name."._hfh_select" => $ret, | ||||||||||||||||||||||||
| 68 | $name."._hfh_text" => { | ||||||||||||||||||||||||
| 69 | type => 'Text', | ||||||||||||||||||||||||
| 70 | id => $name.".text", | ||||||||||||||||||||||||
| 71 | javascript => "display=\"none\"", | ||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||
| 75 | # generate a form-entry with a given range | ||||||||||||||||||||||||
| 76 | 0 | ▶ | sub _get_form_range { | ||||||||||||||||||||||
|
Subroutine
_get_form_range: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 77 | 0 | my ($opt, $name) = @_; | |||||||||||||||||||||||
| 78 | return _form_entry_general $opt, { | ||||||||||||||||||||||||
| 79 | type => ($opt->{ui_type} || 'Integer'), | ||||||||||||||||||||||||
| 80 | range_start => $opt->{range}->[0], | ||||||||||||||||||||||||
| 81 | 0 | ▶ | range_end => $opt->{range}->[-1], | ||||||||||||||||||||||
|
Condition0%0/2
$opt->{'ui_type'} || 'Integer'
MC/DC0%0/1
$opt->{'ui_type'} || 'Integer'
$opt->{'ui_type'}
Truth table0%0/2
A
A= $opt->{'ui_type'}
| |||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||
| 85 | # generate a form-entry from a corresponding option | ||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _get_form_entry { | ||||||||||||||||||||||
|
Subroutine
_get_form_entry: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||
| 87 | 0 | my ($opt, $name) = @_; | |||||||||||||||||||||||
| 88 | 0 | ▶ | return _get_form_selection($opt, $name) if $opt->{presets}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 89 | 0 | ▶ | return _get_form_range($opt, $name) if $opt->{range}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 90 | 0 | my $form; | |||||||||||||||||||||||
| 91 | 0 | ▶ | $form->{type} = $opt->{ui_type} || $opt->{isa} || 'Text'; | ||||||||||||||||||||||
|
Condition0%0/5
$opt->{'ui_type'} || $opt->{'isa'} || 'Text'
$opt->{"ui_type"} or $opt->{"isa"}
MC/DC0%0/2
$opt->{'ui_type'} || $opt->{'isa'} || 'Text'
$opt->{"ui_type"}
$opt->{"isa"}
Truth table0%0/3
A or B
A= $opt->{"ui_type"}B= $opt->{"isa"}
| |||||||||||||||||||||||||
| 92 | 0 | ▶ | $form->{validate} = $opt->{validate} if $opt->{validate}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 93 | 0 | return _form_entry_general $opt, $form; | |||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | # generate a list of form-entries from a list of options | ||||||||||||||||||||||||
| 97 | 0 | ▶ | sub _opt2form { | ||||||||||||||||||||||
|
Subroutine
_opt2form: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||
| 98 | 0 | my ($opts, $prefix) = @_; | |||||||||||||||||||||||
| 99 | 0 | my $pprefix = $prefix; | |||||||||||||||||||||||
| 100 | 0 | ▶ | $pprefix .= "." if $pprefix; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 101 | 0 | my @ret; | |||||||||||||||||||||||
| 102 | 0 | for(keys %{$opts}) { | |||||||||||||||||||||||
| 103 | 0 | my $subprefix = $pprefix.$_; | |||||||||||||||||||||||
| 104 | 0 | my $topt = $opts->{$_}; | |||||||||||||||||||||||
| 105 | 0 | ▶ | if($topt->{is_option}) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 106 | 0 | push(@ret, $subprefix => _get_form_entry($topt, $subprefix)); | |||||||||||||||||||||||
| 107 | } elsif($prefix) { | ||||||||||||||||||||||||
| 108 | 0 | push(@ret, $prefix => { type => 'Compound', }, _opt2form($topt, $subprefix)); | |||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||
| 110 | 0 | push(@ret, _opt2form($topt, $subprefix)); | |||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||
| 113 | 0 | return @ret; | |||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||
| 116 | =head2 ATTRIBUTES | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | =head3 context | ||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | This will set the context L<User::Config> is working in. By default, this will | ||||||||||||||||||||||||
| 121 | be the global context. | ||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||
| 125 | has 'context' => ( | ||||||||||||||||||||||||
| 126 | default => User::Config->instance->context, | ||||||||||||||||||||||||
| 127 | is => "rw", | ||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||
| 130 | =head3 form_options | ||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||
| 132 | This attribute will contain all additional parameters used while generating | ||||||||||||||||||||||||
| 133 | the L<HTML::FormHandler>-object. | ||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||
| 137 | has 'form_options' => ( | ||||||||||||||||||||||||
| 138 | default => sub { {} }, | ||||||||||||||||||||||||
| 139 | is => "rw" | ||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||
| 142 | =head3 params | ||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||
| 144 | the CGI-parameters given | ||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||
| 148 | has 'params' => is => "rw"; | ||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||
| 150 | =head2 METHODS | ||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||
| 152 | =head3 C<<$self->generate()>> | ||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||
| 154 | generates a new L<HTML::FormHandler>-object. Optionally a hash-ref containing | ||||||||||||||||||||||||
| 155 | the actual CGI-parameters can be submitted. This will replace the parameters | ||||||||||||||||||||||||
| 156 | set within the current instance. | ||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||
| 160 | 0 | ▶ | sub generate { | ||||||||||||||||||||||
|
Subroutine
generate: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||
| 161 | 0 | my ($self, $params) = @_; | |||||||||||||||||||||||
| 162 | 0 | my $init = $self->form_options; | |||||||||||||||||||||||
| 163 | 0 | $init->{field_list} = [ _opt2form($self->get_options, "") ]; | |||||||||||||||||||||||
| 164 | 0 | my $ret = HTML::FormHandler->new(%{$init}); | |||||||||||||||||||||||
| 165 | 0 | $ret->{item_class} = $self->context; | |||||||||||||||||||||||
| 166 | 0 | ▶ | $params = $self->params unless $params; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 167 | 0 | ▶ | $ret->process($params) if $params; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 168 | 0 | return $ret; | |||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||
| 173 | Benjamin Tietz E<lt>benjamin@micronet24.deE<gt> | ||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||
| 175 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||
| 177 | Copyright (C) 2011 by Benjamin Tietz | ||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||
| 179 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||
| 180 | it under the same terms as Perl itself, either Perl version 5.10.1 or, | ||||||||||||||||||||||||
| 181 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||
| 185 | 1 | ▶ | no Moose; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 186 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||
| 187 | 1; | ||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||