ZofCMS/Plugin/StartPage.pm

filter: Statementstmt 18.8% 17 / 90 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 31.2% 5 / 16 Podpod 0.0% 0 / 7 totaltotal 12.3% 22 / 178 CC 29 SCAR 40.4
CC
29
Cov
12%
_do
156.0
save_settings
42.0
get_redirect_page
30.0
CRAP 56.8
« ZofCMS/Plugin/FeatureSuggestionBox.pm ↑ Summary ZofCMS/Plugin/PreferentialOrder.pm »
1 package App::ZofCMS::Plugin::StartPage;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001010'; # VERSION
7  
8 1 ▶use base 'App::ZofCMS::Plugin::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub _key { 'plug_start_page' }
Subroutine _key: not called CC=1 SCAR=6.9
13 0 ▶sub _defaults {
Subroutine _defaults: not called CC=1 SCAR=6.9
14 return (
15 dsn => "DBI:mysql:database=test;host=localhost",
16 user => '',
17 pass => undef,
18 opt => { RaiseError => 1, AutoCommit => 1 },
19 table => 'users',
20 login_col => 'login',
21 page_col => 'start_page',
22 0 ▶ login => sub { $_[0]->{d}{user}{login} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 # pages => [],
24 0 label_text => 'Start page:',
25 default_page => undef,
26 no_redirect => undef,
27 submit_button => q|<input type="submit" class="input_submit"|
28 . q| value="Save">|,
29 );
30 }
31 0 ▶sub _do {
Subroutine _do: not called CC=12 SCAR=50.5
32 0 my ( $self, $conf, $t, $q, $config ) = @_;
33  
34 0 for ( qw/pages login/ ) {
35 $conf->{$_} = $conf->{$_}->( $t, $q, $config )
36 0 ▶ if ref $conf->{$_} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf->{$_} eq "CODE" 0 0 0
37 }
38  
39 return
40 unless defined $conf->{pages}
41 0 and @{ $conf->{pages} }
42 0 ▶ and defined $conf->{login};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"pages"} and @{$conf->{"pages"};} and defined $conf->{"login"} 0 0 0
Condition0%0/6
defined $conf->{"pages"} and @{$conf->{"pages"};} and defined $conf->{"login"}
!ll&&!rl&&r
000
defined $conf->{"pages"} and @{$conf->{"pages"};}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $conf->{"pages"} and @{$conf->{"pages"};} and defined $conf->{"login"}
defined $conf->{"pages"} @{$conf->{"pages"};} defined $conf->{"login"}
Truth table0%0/4
A and B and C
A= defined $conf->{"pages"}B= @{$conf->{"pages"};}C= defined $conf->{"login"}
ABCresult
0XX0
10X0
1100
1111
43  
44 0 ▶ @{ $conf->{pages} } % 2
Branch: 1 branch
BranchTrueFalseTotal
@{$conf->{"pages"};} % 2 and die "`pages` must contain even number of elements" 0 0 0
45 and die '`pages` must contain even number of elements';
46  
47 0 $self->{CONF} = $conf;
48 0 $self->{Q} = $q;
49 0 ▶ $self->{Q_PAGE} = ( $q->{dir} || '' ) . ( $q->{page} || '' );
Condition0%0/4
$q->{'dir'} || ''
l!l
00
$q->{'page'} || ''
l!l
00
MC/DC0%0/1
$q->{'dir'} || ''
$q->{'dir'}
MC/DC0%0/1
$q->{'page'} || ''
$q->{'page'}
Truth table0%0/2
A
A= $q->{'dir'}
Aresult
11
00
Truth table0%0/2
A
A= $q->{'page'}
Aresult
11
00
50  
51 0 my $page = $self->get_redirect_page;
52  
53 0 ▶ unless ( $conf->{no_redirect} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"no_redirect"}) 0 0 0
54 0 ▶ if ( has_value( $page ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_value($page)) { } 0 0 0
55 0 print $config->cgi->redirect( $page );
56 0 exit;
57 }
58 else {
59 0 return;
60 }
61 }
62  
63 0 $self->prepare_pages;
64 $self->save_settings
65 0 ▶ if $q->{plugsp_save_settings};
Branch: 1 branch
BranchTrueFalseTotal
if $q->{"plugsp_save_settings"} 0 0 0
66  
67 0 $t->{t}{plug_start_page_form} = $self->make_form;
68 }
69  
70 0 ▶sub get_redirect_page {
Subroutine get_redirect_page: not called CC=5 SCAR=34.0
Pod undocumented
71 0 my $self = shift;
72 0 my $q = $self->{Q};
73 0 my $conf = $self->{CONF};
74 0 my $dbh = $self->dbh;
75  
76 my $page = ($dbh->selectall_arrayref(
77 'SELECT `' . $conf->{page_col} . '` FROM `' .
78 $conf->{table} . '` WHERE `' .
79 $conf->{login_col} . '` = ?',
80 { Slice => {} },
81 $conf->{login},
82 0 ▶ ) || [])->[0] || {};
Condition0%0/4
+($dbh->selectall_arrayref('SELECT `' . $conf->{'page_col'} . '` FROM `' . $conf->{'table'} . '` WHERE `' . $conf->{'login_col'} . '` = ?', {'Slice', {}}, $conf->{'login'}) || [])->[0] || {}
l!l
00
$dbh->selectall_arrayref('SELECT `' . $conf->{'page_col'} . '` FROM `' . $conf->{'table'} . '` WHERE `' . $conf->{'login_col'} . '` = ?', {'Slice', {}}, $conf->{'login'}) || []
l!l
00
MC/DC0%0/1
+($dbh->selectall_arrayref('SELECT `' . $conf->{'page_col'} . '` FROM `' . $conf->{'table'} . '` WHERE `' . $conf->{'login_col'} . '` = ?', {'Slice', {}}, $conf->{'login'}) || [])->[0] || {}
$dbh->selectall_arrayref('SELECT `' . $conf->{'page_col'} . '` FROM `' . $conf->{'table'} . '` WHERE `' . $conf->{'login_col'} . '` = ?', {'Slice', {}}, $conf->{'login'})
Truth table0%0/2
A
A= $dbh->selectall_arrayref('SELECT `' . $conf->{'page_col'} . '` FROM `' . $conf->{'table'} . '` WHERE `' . $conf->{'login_col'} . '` = ?', {'Slice', {}}, $conf->{'login'})
Aresult
11
00
83  
84 0 ▶ if ( has_value( $page->{ $conf->{page_col} } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (has_value($$page{$conf->{'page_col'}})) { } 0 0 0
elsif (has_value($conf->{'default_page'})) { } 0 0 0
85 0 $page = $page->{ $conf->{page_col} };
86 }
87 elsif ( has_value( $conf->{default_page} ) ) {
88 0 $page = $conf->{default_page};
89 }
90 else {
91 0 undef $page;
92 }
93  
94 0 $conf->{redirect_page} = $page;
95 0 return $page;
96 }
97  
98 0 ▶sub save_settings {
Subroutine save_settings: not called CC=6 SCAR=37.4
Pod undocumented
99 0 my $self = shift;
100 0 my $q = $self->{Q};
101 0 my $conf = $self->{CONF};
102  
103 return
104 unless has_value( $q->{plugsp_page} )
105 0 ▶ and $conf->{valid_pages}{ $q->{plugsp_page} };
Branch: 1 branch
BranchTrueFalseTotal
unless has_value($q->{"plugsp_page"}) and $conf->{"valid_pages"}{$q->{"plugsp_page"}} 0 0 0
Condition0%0/3
has_value($q->{"plugsp_page"}) and $conf->{"valid_pages"}{$q->{"plugsp_page"}}
!ll&&!rl&&r
000
MC/DC0%0/2
has_value($q->{"plugsp_page"}) and $conf->{"valid_pages"}{$q->{"plugsp_page"}}
has_value($q->{"plugsp_page"}) $conf->{"valid_pages"}{$q->{"plugsp_page"}}
Truth table0%0/3
A and B
A= has_value($q->{"plugsp_page"})B= $conf->{"valid_pages"}{$q->{"plugsp_page"}}
ABresult
0X0
100
111
106  
107 0 ▶ for ( @{ $conf->{pages} || [] } ) {
Condition0%0/2
$conf->{'pages'} || []
l!l
00
MC/DC0%0/1
$conf->{'pages'} || []
$conf->{'pages'}
Truth table0%0/2
A
A= $conf->{'pages'}
Aresult
11
00
108 0 ▶ $_->{is_selected} = $_->{page} eq $q->{plugsp_page} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'page'} eq $q->{'plugsp_page'} ? : 0 0 0
109 }
110  
111 0 my $dbh = $self->dbh;
112 $dbh->do(
113 'UPDATE `' . $conf->{table} . '` SET `' . $conf->{page_col}
114 . '` = ? WHERE `' . $conf->{login_col} . '` = ?',
115 undef,
116 $q->{plugsp_page},
117 $conf->{login},
118 0 );
119  
120 0 $conf->{save_settings_ok} = 1;
121 }
122  
123 0 ▶sub prepare_pages {
Subroutine prepare_pages: not called CC=5 SCAR=34.0
Pod undocumented
124 0 my $self = shift;
125 0 my $q = $self->{Q};
126 0 my $conf = $self->{CONF};
127  
128 0 my %valid_pages;
129 my @proper_pages;
130 0 my $key;
131 0 my $is_redirect_page = 0;
132 my $redir_page = has_value( $conf->{redirect_page} )
133 0 ▶ ? $conf->{redirect_page} : $conf->{pages}[0];
Branch: 1 branch
BranchTrueFalseTotal
has_value($conf->{'redirect_page'}) ? : 0 0 0
134  
135 0 for ( 0 .. $#{ $conf->{pages} } ) {
136 0 ▶ if ( $_ % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ % 2 == 0) { } 0 0 0
137 0 $key = $conf->{pages}[ $_ ];
138 0 $valid_pages{ $key } = 1;
139 0 ▶ $is_redirect_page = $redir_page eq $key ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$redir_page eq $key ? : 0 0 0
140 }
141 else {
142 push @proper_pages, {
143 page => $key,
144 0 name => $conf->{pages}[ $_ ],
145 is_selected => $is_redirect_page,
146 };
147 }
148 }
149  
150 0 $conf->{pages} = \@proper_pages;
151 0 $conf->{valid_pages} = \%valid_pages;
152 }
153  
154 0 ▶sub make_form {
Subroutine make_form: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my $self = shift;
156 0 my $conf = $self->{CONF};
157  
158 0 my $template = HTML::Template->new_scalar_ref(
159 \ form_html_template(),
160 die_on_bad_params => 0,
161 );
162  
163 $template->param(
164 ( $conf->{save_settings_ok} ? ( save_success => 1 ) : () ),
165 page => $self->{Q_PAGE},
166 pages => $conf->{pages},
167 submit_button => $conf->{submit_button},
168 label_text => $conf->{label_text},
169 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$conf->{'save_settings_ok'} ? : 0 0 0
170  
171 0 return $template->output;
172 }
173  
174  
175 0 ▶sub dbh {
Subroutine dbh: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my $self = shift;
177  
178 return $self->{DBH}
179 0 ▶ if $self->{DBH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DBH"} 0 0 0
180  
181 $self->{DBH} = DBI->connect_cached(
182 0 @{ $self->{CONF} }{ qw/dsn user pass opt/ },
183 );
184  
185 0 return $self->{DBH};
186 }
187  
188 0 ▶sub has_value {
Subroutine has_value: not called CC=3 SCAR=24.8
Pod undocumented
189 0 my $v = shift;
190  
191 0 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and length $v 0 0 0
Condition0%0/3
defined $v and length $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table0%0/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
192 if defined $v and length $v;
193  
194 0 return 0;
195 }
196  
197 0 ▶sub form_html_template {
Subroutine form_html_template: not called CC=1 SCAR=6.9
Pod undocumented
198 0 return <<'END_HTML';
199 <tmpl_if name='save_success'>
200 <p class="success-message">Successfully saved</p>
201 </tmpl_if>
202 <form action="" method="POST" id="plug_start_page_form">
203 <div>
204 <input type="hidden" name="page"
205 value="<tmpl_var escape='html' name='page'>">
206 <input type="hidden" name="plugsp_save_settings" value="1">
207  
208 <label for="plugsp_page"><tmpl_var escape='html'
209 name='label_text'></label
210 ><select id="plugsp_page" name="plugsp_page"
211 ><tmpl_loop name='pages'>
212 <option value="<tmpl_var escape='html' name='page'>"
213 <tmpl_if name='is_selected'>selected</tmpl_if>
214 ><tmpl_var escape='html' name='name'></option>
215 </tmpl_loop>
216  
217 <tmpl_var name="submit_button">
218 </div>
219 </form>
220 END_HTML
221 }
222  
223 1 1;
224 __END__
« ZofCMS/Plugin/FeatureSuggestionBox.pm ↑ Summary ZofCMS/Plugin/PreferentialOrder.pm »