File Coverage

lib/CGI/Session/Submitted.pm
Criterion Covered Total %
statement 37 62 59.6
branch 5 18 27.7
condition 0 7 0.0
subroutine 7 10 70.0
pod 4 4 100.0
total 53 101 52.4


line stmt bran cond sub pod time code
1             package CGI::Session::Submitted;
2 1     1   24237 use strict;
  1         4  
  1         49  
3 1     1   8 use Carp;
  1         3  
  1         237  
4 1     1   6 use base qw(CGI::Session);
  1         7  
  1         1260  
5 1     1   17609 use CGI;
  1         21670  
  1         9  
6             #use Smart::Comments '###'; # we'll use this for debug
7 1     1   71 use warnings;
  1         2  
  1         683  
8             #this is to get version num according to cvs
9             our $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)/g;
10              
11             sub run {
12 1     1 1 125756 my $self = shift;
13 1         4 $self->{preset} = shift;
14 1         3 $self->{args} = shift;
15            
16 1 50       6 (ref $self->{preset}) eq 'HASH' or croak('arg must be hash ref');
17 1         2 my $preset = $self->{preset};
18            
19             ### param_presets from Submitted.pm
20 1 50       6 if ($self->is_new){
21             ### is new , setting defaults
22 1         15 $self->param( $preset );
23             ### ok, assuring cookie
24 1 50       13 $self->_assure_cookie unless $self->{args}->{nocookie}; # will redirect and exit.
25             # just to make a point
26             }
27              
28             # make sure all of the params we want *are* there
29              
30 1         1 for ( keys %{$preset} ){
  1         3  
31 2 50       45 unless ( $self->param($_) ){
32 2         24 $self->param($_ => $preset->{$_});
33             }
34             }
35              
36             ### saving params from cgi
37            
38 1         31 $self->save_param($self->query, [keys %{$preset}] ); # save these params if they any of them came in
  1         20  
39              
40             ### loading params from session in to cgi
41              
42 1         42 $self->load_param($self->query, [keys %{$preset}] ); # load all these query params from session storage to cgi object
  1         14  
43              
44 1         162 return $self->query;
45             }
46              
47             sub session_to_tmpl {
48 0     0 1 0 my $self= shift;
49 0 0       0 my $tmpl = shift; $tmpl or croak('is this an HTML::Template object?');
  0         0  
50              
51 0         0 for ($self->get_presets){
52 0         0 my $key = $_;
53 0         0 my $val = $self->param($key);
54 0   0     0 $val ||=0;
55 0         0 $tmpl->param($key => $val);
56             }
57 0         0 return $tmpl;
58             }
59              
60             sub query_to_tmpl {
61 0     0 1 0 my $self = shift;
62 0 0       0 my $tmpl = shift; $tmpl or croak('is this an HTML::Template object?');
  0         0  
63              
64 0         0 for ($self->query->param){
65 0         0 my $key = $_;
66 0         0 my $val = $self->query->param($key);
67 0   0     0 $val ||=0;
68 0         0 $tmpl->param($key => $val);
69             }
70            
71 0         0 return $tmpl;
72             }
73              
74             sub get_presets {
75 1     1 1 627 my $self = shift;
76 1 50       6 (ref $self->{preset}) eq 'HASH' or croak('arg must be hash ref');
77 1         2 my @p = sort keys %{$self->{preset}};
  1         6  
78 1         8 return \@p;
79             }
80              
81             sub _assure_cookie {
82 0     0     my $self = shift;
83 0 0         $self->is_new or return 1;
84            
85 0           my $redirect = shift;
86 0 0 0       $redirect ||= $ENV{SCRIPT_NAME}; $redirect or croak('CGI::Session::Submitted::_assure_coooie() error: missing $ENV{SCRIPT_NAME} - where to redirect to?');
  0            
87            
88 0           print $self->query->redirect(
89             -uri => $redirect,
90             -cookie =>
91             $self->query->cookie(
92             -name => $self->name,
93             -value => $self->id,
94             )
95             );
96 0           exit(0);
97             }
98              
99              
100             1;
101             __END__