blib/lib/ClearPress/view.pm

filter: Statementstmt 84.6% 374 / 442 Branchbran 66.6% 120 / 180 Conditioncond 64.9% 89 / 137 MC/DCmcdc 42.3% 33 / 78 Subroutinesub 88.5% 54 / 61 Podpod 100% 34 / 34 totaltotal 75.5% 704 / 932 CC 151 SCAR 27.7
CC
151
Cov
77%
process_template
42.9
__ANON__
42.0
new
27.7
CRAP 16.0
« blib/lib/ClearPress/driver/mysql.pm ↑ Summary blib/lib/ClearPress/authdecor.pm »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 # Created: 2007-03-28
6 #
7 package ClearPress::view;
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Template::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use HTML::Entities qw(encode_entities_numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use XML::Simple qw(XMLin);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use ClearPress::Localize;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 our $VERSION = q[2026.07.25];
27  
28 7 our $DEBUG_OUTPUT = 0;
29 7 our $DEBUG_L10N = 0;
30 7 our $TEMPLATE_CACHE = {};
31 7 our $LEXICON_CACHE = {};
32 7 our $TRAP_REDIR_OVERFLOW = 0; # set to non-zero value to cut-off at that many bytes
33  
34 7 Readonly::Scalar our $DEFAULT_PROTO_PORTS => {
35 http => 80,
36 https => 443,
37 };
38  
39 7 __PACKAGE__->mk_accessors(qw(util model action aspect content_type entity_name autoescape charset decorator headers));
40  
41 83 ▶sub new { ## no critic (Complexity)
Subroutine new: called CC=26 SCAR=33.2
Pod documented
42 83 my ($class, $self) = @_;
43 83 ▶ $self ||= {};
Condition100%2/2
$self ||= {}
l!l
803
MC/DC100%1/1
$self ||= {}
$self
Truth table100%2/2
A
A= $self
Aresult
11
00
44 83 bless $self, $class;
45  
46 83 my $util = $self->util;
47 83 ▶ my $username = $util ? $util->username : q[];
Branch: 1 branch
BranchTrueFalseTotal
$util ? : 79 4 83
48 83 $self->{requestor_username} = $username;
49 83 ▶ $self->{logged_in} = $username?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$username ? : 1 82 83
50 83 $self->{warnings} = [];
51 83 $self->{output_buffer} = [];
52 83 $self->{output_finished} = 0;
53 83 $self->{autoescape} = 1;
54  
55 83 ▶ my $aspect = $self->aspect || q[];
Condition100%2/2
$self->aspect || ''
l!l
5528
MC/DC100%1/1
$self->aspect || ''
$self->aspect
Truth table100%2/2
A
A= $self->aspect
Aresult
11
00
56  
57 83 ▶ $self->{content_type} ||= ($aspect =~ /(?:rss|atom|ajax|xml)$/smx)?'text/xml':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /(?:rss|atom|ajax|xml)$/msx ? : 18 65 83
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /(?:rss|atom|ajax|xml)$/msx ? "text/xml" : ""
l!l&&r!l&&!r
01865
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /(?:rss|atom|ajax|xml)$/msx ? "text/xml" : ""
$self->{"content_type"} $aspect =~ /(?:rss|atom|ajax|xml)$/msx ? "text/xml" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /(?:rss|atom|ajax|xml)$/msx ? "text/xml" : ""
ABresult
1X1
011
000
58 83 ▶ $self->{content_type} ||= ($aspect =~ /(?:js|json)$/smx)?'application/json':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /(?:js|json)$/msx ? : 6 59 65
Condition100%3/3
$self->{"content_type"} ||= $aspect =~ /(?:js|json)$/msx ? "application/json" : ""
l!l&&r!l&&!r
18659
MC/DC100%2/2
$self->{"content_type"} ||= $aspect =~ /(?:js|json)$/msx ? "application/json" : ""
$self->{"content_type"} $aspect =~ /(?:js|json)$/msx ? "application/json" : ""
Truth table100%3/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /(?:js|json)$/msx ? "application/json" : ""
ABresult
1X1
011
000
59 83 ▶ $self->{content_type} ||= ($aspect =~ /_svg$/smx)?'image/svg+xml':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_svg$/msx ? : 0 59 59
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /_svg$/msx ? "image/svg+xml" : ""
l!l&&r!l&&!r
24059
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /_svg$/msx ? "image/svg+xml" : ""
$self->{"content_type"} $aspect =~ /_svg$/msx ? "image/svg+xml" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_svg$/msx ? "image/svg+xml" : ""
ABresult
1X1
011
000
60 83 ▶ $self->{content_type} ||= ($aspect =~ /_svgz$/smx)?'image/svg+xml':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_svgz$/msx ? : 0 59 59
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /_svgz$/msx ? "image/svg+xml" : ""
l!l&&r!l&&!r
24059
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /_svgz$/msx ? "image/svg+xml" : ""
$self->{"content_type"} $aspect =~ /_svgz$/msx ? "image/svg+xml" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_svgz$/msx ? "image/svg+xml" : ""
ABresult
1X1
011
000
61 83 ▶ $self->{content_type} ||= ($aspect =~ /_png$/smx)?'image/png':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_png$/msx ? : 2 57 59
Condition100%3/3
$self->{"content_type"} ||= $aspect =~ /_png$/msx ? "image/png" : ""
l!l&&r!l&&!r
24257
MC/DC100%2/2
$self->{"content_type"} ||= $aspect =~ /_png$/msx ? "image/png" : ""
$self->{"content_type"} $aspect =~ /_png$/msx ? "image/png" : ""
Truth table100%3/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_png$/msx ? "image/png" : ""
ABresult
1X1
011
000
62 83 ▶ $self->{content_type} ||= ($aspect =~ /_jpg$/smx)?'image/jpeg':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_jpg$/msx ? : 2 55 57
Condition100%3/3
$self->{"content_type"} ||= $aspect =~ /_jpg$/msx ? "image/jpeg" : ""
l!l&&r!l&&!r
26255
MC/DC100%2/2
$self->{"content_type"} ||= $aspect =~ /_jpg$/msx ? "image/jpeg" : ""
$self->{"content_type"} $aspect =~ /_jpg$/msx ? "image/jpeg" : ""
Truth table100%3/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_jpg$/msx ? "image/jpeg" : ""
ABresult
1X1
011
000
63 83 ▶ $self->{content_type} ||= ($aspect =~ /_txt$/smx)?'text/plain':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_txt$/msx ? : 0 55 55
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /_txt$/msx ? "text/plain" : ""
l!l&&r!l&&!r
28055
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /_txt$/msx ? "text/plain" : ""
$self->{"content_type"} $aspect =~ /_txt$/msx ? "text/plain" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_txt$/msx ? "text/plain" : ""
ABresult
1X1
011
000
64 83 ▶ $self->{content_type} ||= ($aspect =~ /_csv$/smx)?'text/csv':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_csv$/msx ? : 0 55 55
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /_csv$/msx ? "text/csv" : ""
l!l&&r!l&&!r
28055
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /_csv$/msx ? "text/csv" : ""
$self->{"content_type"} $aspect =~ /_csv$/msx ? "text/csv" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_csv$/msx ? "text/csv" : ""
ABresult
1X1
011
000
65 83 ▶ $self->{content_type} ||= ($aspect =~ /_xls$/smx)?'application/vnd.ms-excel':q[];
Branch: 1 branch
BranchTrueFalseTotal
$aspect =~ /_xls$/msx ? : 0 55 55
Condition66%2/3
$self->{"content_type"} ||= $aspect =~ /_xls$/msx ? "application/vnd.ms-excel" : ""
l!l&&r!l&&!r
28055
MC/DC50%1/2
$self->{"content_type"} ||= $aspect =~ /_xls$/msx ? "application/vnd.ms-excel" : ""
$self->{"content_type"} $aspect =~ /_xls$/msx ? "application/vnd.ms-excel" : ""
Truth table66%2/3
A ||= B
A= $self->{"content_type"}B= $aspect =~ /_xls$/msx ? "application/vnd.ms-excel" : ""
ABresult
1X1
011
000
66  
67 83 $self->setup_filters;
68  
69 83 $self->init;
70  
71 83 ClearPress::Localize->init($self->locales);
72  
73 83 ▶ $self->{content_type} ||= 'text/html';
Condition100%2/2
$self->{"content_type"} ||= "text/html"
l!l
2855
MC/DC100%1/1
$self->{"content_type"} ||= "text/html"
$self->{"content_type"}
Truth table100%2/2
A
A= $self->{"content_type"}
Aresult
11
00
74  
75 83 ▶ $self->{charset} ||= 'UTF-8';
Condition100%2/2
$self->{"charset"} ||= "UTF-8"
l!l
182
MC/DC100%1/1
$self->{"charset"} ||= "UTF-8"
$self->{"charset"}
Truth table100%2/2
A
A= $self->{"charset"}
Aresult
11
00
76 83 ▶ $self->{headers} ||= HTTP::Headers->new;
Condition66%2/3
$self->{"headers"} ||= "HTTP::Headers"->new
l!l&&r!l&&!r
34490
MC/DC0%0/2
$self->{"headers"} ||= "HTTP::Headers"->new
$self->{"headers"} "HTTP::Headers"->new
Truth table66%2/3
A ||= B
A= $self->{"headers"}B= "HTTP::Headers"->new
ABresult
1X1
011
000
77  
78 83 return $self;
79 }
80  
81 83 ▶sub setup_filters {
Subroutine setup_filters: called CC=1 SCAR=3.5
Pod documented
82 83 my $self = shift;
83 0 ▶ $self->add_tt_filter('js_string', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
84 0 my $string = shift;
85 0 ▶ if(!defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 0 0
86 0 $string = q[];
87 }
88 0 $string =~ s/\r/\\r/smxg;
89 0 $string =~ s/\n/\\n/smxg;
90 0 $string =~ s/"/\\"/smxg;
91 # $string =~ s/'/\\'/smxg;
92 0 return $string;
93 83 });
94  
95 5 ▶ $self->add_tt_filter('xml_entity', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 5 my $string = shift;
97 5 ▶ if(!defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 1 4 5
98 1 $string = q[];
99 }
100 5 return encode_entities_numeric($string),
101 83 });
102  
103 0 ▶ $self->add_tt_filter('base64', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 my $string = shift;
105 0 ▶ if(!defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 0 0
106 0 $string = q[];
107 }
108 0 return encode_base64($string),
109 83 });
110  
111 83 my $util = $self->util;
112  
113 0 ▶ $self->add_tt_filter('loc', [sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114  
115 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
116 0 my ($string) = shift;
117  
118 #########
119 # Cache lexicons for
120 # speed. However, loading on-demand
121 # won't generally use shared memory
122 #
123 0 my $lang = ClearPress::Localize->lang;
124 0 ▶ if($lang && !$LEXICON_CACHE->{$lang}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and not $ClearPress::view::LEXICON_CACHE->{$lang}) 0 0 0
Condition0%0/3
$lang and not $ClearPress::view::LEXICON_CACHE->{$lang}
!ll&&!rl&&r
000
MC/DC0%0/2
$lang and not $ClearPress::view::LEXICON_CACHE->{$lang}
$lang not $ClearPress::view::LEXICON_CACHE->{$lang}
Truth table0%0/3
A and not(B)
A= $langB= not $ClearPress::view::LEXICON_CACHE->{$lang}
ABresult
0X0
100
111
125 0 $LEXICON_CACHE->{$lang} = ClearPress::Localize->localizer;
126 }
127  
128 0 my $loc = $string;
129 eval {
130 0 $loc = $util->{localizers}->{$lang}->maketext($string);
131 0 1;
132 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $loc = $util->{"localizers"}{$lang}->maketext($string); 1 } } or do { $ClearPress::view::DEBUG_L10N and carp("Could not localize $string to $lang"); '???' } 0 0 0
133 0 ▶ $DEBUG_L10N && carp qq[Could not localize $string to $lang];
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_L10N and carp("Could not localize $string to $lang") 0 0 0
134 0 1;
135 };
136  
137 0 ▶ return $loc || $string;
Condition0%0/3
$loc || $string
l!l&&r!l&&!r
000
MC/DC0%0/2
$loc || $string
$loc $string
Truth table0%0/3
A || B
A= $locB= $string
ABresult
1X1
011
000
138 0 };
139 83 }, 1]);
140  
141 83 return 1;
142 }
143  
144 83 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
145 83 return 1;
146 }
147  
148 83 ▶sub locales {
Subroutine locales: called CC=2 SCAR=6.9
Pod documented
149 83 my $self = shift;
150 83 my $util = $self->util;
151 return {
152 83 ▶ $util ? (q[*] => [Gettext => sprintf q[%s/po/*.po], $util->data_path] ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$util ? : 79 4 83
153 q[en] => ['Auto'],
154 };
155 }
156  
157 1 ▶sub add_warning {
Subroutine add_warning: called CC=1 SCAR=0.0
Pod documented
158 1 my ($self, $warning) = @_;
159 1 push @{$self->{warnings}}, $warning;
160 1 return 1;
161 }
162  
163 12 ▶sub warnings {
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
164 12 my $self = shift;
165 12 return $self->{warnings};
166 }
167  
168 2 ▶sub _accessor { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _accessor: called CC=2 SCAR=6.9
169 2 my ($self, $field, $val) = @_;
170 2 carp q[_accessor is deprecated. Use __PACKAGE__->mk_accessors(...) instead];
171 2 ▶ if(defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1 1 2
172 1 $self->{$field} = $val;
173 }
174 2 return $self->{$field};
175 }
176  
177 26 ▶sub authorised {
Subroutine authorised: called CC=9 SCAR=22.0
Pod documented
178 26 my $self = shift;
179 26 ▶ my $action = $self->action || q[];
Condition50%1/2
$self->action || ''
l!l
260
MC/DC0%0/1
$self->action || ''
$self->action
Truth table50%1/2
A
A= $self->action
Aresult
11
00
180 26 ▶ my $aspect = $self->aspect || q[];
Condition100%2/2
$self->aspect || ''
l!l
197
MC/DC100%1/1
$self->aspect || ''
$self->aspect
Truth table100%2/2
A
A= $self->aspect
Aresult
11
00
181 26 my $util = $self->util;
182 26 my $requestor = $util->requestor;
183  
184 26 ▶ if(!$requestor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($requestor) 17 9 26
185 #########
186 # If there's no requestor user object then authorisation isn't supported
187 #
188 17 return 1;
189 }
190  
191 9 ▶ if($action =~ /^list/smx ||
Branch: 1 branch
BranchTrueFalseTotal
if ($action =~ /^list/msx or $action eq 'read' and not $aspect =~ /^(?:add|edit|delete|update|create)/msx) { } 2 7 9
Condition100%6/6
$action =~ /^list/msx or $action eq "read" and not $aspect =~ /^(?:add|edit|delete|update|create)/msx
l!l&&r!l&&!r
117
$action eq "read" and not $aspect =~ /^(?:add|edit|delete|update|create)/msx
!ll&&!rl&&r
611
MC/DC100%3/3
$action =~ /^list/msx or $action eq "read" and not $aspect =~ /^(?:add|edit|delete|update|create)/msx
$action =~ /^list/msx $action eq "read" not $aspect =~ /^(?:add|edit|delete|update|create)/msx
Truth table100%4/4
A or B and not(C)
A= $action =~ /^list/msxB= $action eq "read"C= not $aspect =~ /^(?:add|edit|delete|update|create)/msx
ABCresult
1XX1
0111
00X0
0100
192 ($action eq 'read' &&
193 $aspect !~ /^(?:add|edit|delete|update|create)/smx)) {
194 #########
195 # by default assume public read access for 'read' actions
196 #
197 2 return 1;
198  
199 } else {
200 #########
201 # by default allow only 'admin' group for non-read aspects (add, edit, create, update, delete)
202 #
203 7 ▶ if($requestor->can('is_member_of') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($requestor->can("is_member_of") and $requestor->is_member_of("admin")) 3 4 7
Condition66%2/3
$requestor->can("is_member_of") and $requestor->is_member_of("admin")
!ll&&!rl&&r
043
MC/DC50%1/2
$requestor->can("is_member_of") and $requestor->is_member_of("admin")
$requestor->can("is_member_of") $requestor->is_member_of("admin")
Truth table66%2/3
A and B
A= $requestor->can("is_member_of")B= $requestor->is_member_of("admin")
ABresult
0X0
100
111
204 $requestor->is_member_of('admin')) {
205 3 return 1;
206 }
207 }
208  
209 4 return;
210 }
211  
212 23 ▶sub template_name {
Subroutine template_name: called CC=7 SCAR=19.5
Pod documented
213 23 my ($self, @args) = @_;
214  
215 23 ▶ if(scalar @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 1 22 23
216 1 $self->{template_override} = $args[0];
217 }
218  
219 23 ▶ if(exists $self->{template_override}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"template_override"}) 2 21 23
220 2 return $self->{template_override};
221 }
222  
223 21 my $name = $self->entity_name;
224 21 ▶ if(!$name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 6 15 21
225 6 ($name) = (ref $self) =~ /view::(.*)$/smx;
226 }
227 21 ▶ $name ||= 'view';
Condition100%2/2
$name ||= "view"
l!l
156
MC/DC100%1/1
$name ||= "view"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
228 21 my $method = $self->method_name;
229  
230 21 $name =~ s/:+/_/smxg;
231 21 ▶ if(!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 1 20 21
232 1 return $name;
233 }
234  
235 20 my $util = $self->util;
236 20 my $tmp = "${name}/$method";
237 20 my $path = sprintf q[%s/templates], $util->data_path;
238  
239 #########
240 # I do not like this stat. I'd prefer a global mode switch in config.
241 #
242 20 ▶ if(-e "$path/$tmp.tt2") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$path/$tmp.tt2") 1 19 20
243 1 return $tmp;
244 }
245  
246 19 return "${name}_$method";
247 }
248  
249 39 ▶sub method_name {
Subroutine method_name: called CC=6 SCAR=17.9
Pod documented
250 39 my $self = shift;
251 39 my $aspect = $self->aspect;
252 39 my $action = $self->action;
253 39 ▶ my $method = $aspect || $action;
Condition100%3/3
$aspect || $action
l!l&&r!l&&!r
3171
MC/DC100%2/2
$aspect || $action
$aspect $action
Truth table100%3/3
A || B
A= $aspectB= $action
ABresult
1X1
011
000
254 39 my $model = $self->model;
255 39 my $pk = $model->primary_key;
256  
257 39 ▶ if(!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 1 38 39
258 1 return q[];
259 }
260  
261 38 ▶ if($pk &&
Branch: 1 branch
BranchTrueFalseTotal
if ($pk and $method eq "read" and not $model->$pk) 1 37 38
Condition83%5/6
$pk and $method eq "read" and not $model->$pk
!ll&&!rl&&r
3701
$pk and $method eq "read"
!ll&&!rl&&r
3341
MC/DC66%2/3
$pk and $method eq "read" and not $model->$pk
$pk $method eq "read" not $model->$pk
Truth table75%3/4
A and B and not(C)
A= $pkB= $method eq "read"C= not $model->$pk
ABCresult
0XX0
10X0
1100
1111
262 $method eq 'read' &&
263 !$model->$pk()) {
264 1 $method = 'list';
265 }
266  
267 38 $method =~ s/__/_/smxg;
268  
269 38 return $method;
270 }
271  
272 32 ▶sub streamed_aspects {
Subroutine streamed_aspects: called CC=1 SCAR=0.0
Pod documented
273 32 return [];
274 }
275  
276 34 ▶sub streamed {
Subroutine streamed: called CC=3 SCAR=11.0
Pod documented
277 34 my $self = shift;
278 34 my $aspect = $self->aspect;
279  
280 34 for my $str_aspect (@{$self->streamed_aspects}) {
281 2 ▶ if($aspect eq $str_aspect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aspect eq $str_aspect) 2 0 2
282 2 return 1;
283 }
284 }
285 32 return;
286 }
287  
288 18 ▶sub render {
Subroutine render: called CC=15 SCAR=32.3
Pod documented
289 18 my $self = shift;
290 18 my $util = $self->util;
291 18 ▶ my $aspect = $self->aspect || q[];
Condition100%2/2
$self->aspect || ''
l!l
162
MC/DC100%1/1
$self->aspect || ''
$self->aspect
Truth table100%2/2
A
A= $self->aspect
Aresult
11
00
292 18 my $action = $self->action;
293  
294 18 ▶ if(!$util) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($util) 0 18 18
295 0 croak q[No util object available];
296 }
297  
298 18 my $requestor = $util->requestor;
299  
300 18 ▶ if(!$self->authorised) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->authorised) 0 18 18
301 #########
302 # set http forbidden response code
303 #
304 0 $self->headers->header('Status', HTTP_FORBIDDEN);
305  
306 0 ▶ if(!$requestor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($requestor) 0 0 0
307 0 croak q[Authorisation unavailable for this view.];
308 }
309  
310 0 my $username = $requestor->username;
311 0 ▶ if(!$username) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username) 0 0 0
312 0 croak q[You are not authorised for this view. You need to log in.];
313 }
314 0 croak qq[You ($username) are not authorised for this view];
315 }
316  
317 #########
318 # Figure out and call the appropriate action if available
319 #
320 18 my $method = $self->method_name;
321 18 ▶ if($method !~ /^(?:add|edit|create|read|update|delete|list|options|head)/smx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^(?:add|edit|create|read|update|delete|list|options|head)/msx) 0 18 18
322 0 croak qq[Illegal method: $method];
323 }
324  
325 18 ▶ if($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 18 0 18
326 18 ▶ if($action eq 'options' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "options" or $aspect =~ /_(?:jpg|png|gif|svg|svgz)/msx) 0 18 18
Condition33%1/3
$action eq "options" or $aspect =~ /_(?:jpg|png|gif|svg|svgz)/msx
l!l&&r!l&&!r
0018
MC/DC0%0/2
$action eq "options" or $aspect =~ /_(?:jpg|png|gif|svg|svgz)/msx
$action eq "options" $aspect =~ /_(?:jpg|png|gif|svg|svgz)/msx
Truth table33%1/3
A or B
A= $action eq "options"B= $aspect =~ /_(?:jpg|png|gif|svg|svgz)/msx
ABresult
1X1
011
000
327 $aspect =~ /_(?:jpg|png|gif|svg|svgz)/smx) {
328 0 return $self->$method();
329 }
330  
331 #########
332 # handle streamed methods
333 #
334 18 my $streamed = $self->streamed;
335  
336 18 ▶ if($streamed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($streamed) 1 17 18
337 1 $self->output_flush;
338 }
339  
340 18 $self->$method();
341  
342 18 ▶ if($streamed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($streamed) 1 17 18
343 1 $self->output_end;
344 1 return q[];
345 }
346  
347 } else {
348 0 croak qq[Unsupported method: $method];
349 }
350  
351 17 my $model = $self->model;
352 17 my $actions = my $warnings = q[];
353  
354 17 ▶ if($self->decor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->decor) 9 8 17
355 9 $actions = $self->actions;
356 eval {
357 9 $self->process_template('warnings.tt2', {
358 warnings => $self->warnings,
359 }, \$warnings);
360  
361 9 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->process_template("warnings.tt2", {"warnings", $self->warnings}, \$warnings) } } or do { carp("Warning: $ClearPress::view::EVAL_ERROR") } 9 0 9
362 #########
363 # non-fatal warning - usually warnings.tt2 missing
364 #
365 0 carp "Warning: $EVAL_ERROR";
366 };
367 }
368  
369 #########
370 # handle block (non-streamed) methods
371 #
372 17 my $tmpl = $self->template_name;
373 17 my $cfg = $util->config;
374 17 my $content = q[];
375  
376 17 $self->process_template("$tmpl.tt2", {}, \$content);
377  
378 15 ▶ return $warnings . $actions . $content || q[No data];
Condition50%1/2
$warnings . $actions . $content || 'No data'
l!l
150
MC/DC0%0/1
$warnings . $actions . $content || 'No data'
$warnings . $actions . $content
Truth table50%1/2
A
A= $warnings . $actions . $content
Aresult
11
00
379 }
380  
381 39 ▶sub process_template { ## no critic (Complexity)
Subroutine process_template: called CC=25 SCAR=37.6
Pod documented
382 39 my ($self, $template, $extra_params, $where_to_ref) = @_;
383 39 my $util = $self->util;
384 39 my $cfg = $util->config;
385 39 my ($entity) = (ref $self) =~ /([^:]+)$/smx;
386 39 ▶ $entity ||= q[];
Condition50%1/2
$entity ||= ""
l!l
390
MC/DC0%0/1
$entity ||= ""
$entity
Truth table50%1/2
A
A= $entity
Aresult
11
00
387 39 ▶ my $script_name = $ENV{SCRIPT_NAME} || q[];
Condition50%1/2
$ENV{'SCRIPT_NAME'} || ''
l!l
039
MC/DC0%0/1
$ENV{'SCRIPT_NAME'} || ''
$ENV{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $ENV{'SCRIPT_NAME'}
Aresult
11
00
388 39 my ($xfh, $xfp) = ($ENV{HTTP_X_FORWARDED_HOST}, $ENV{HTTP_X_FORWARDED_PORT});
389 39 ▶ my $http_host = ($xfh ? $xfh : $ENV{HTTP_HOST}) || q[localhost];
Branch: 1 branch
BranchTrueFalseTotal
$xfh ? : 0 39 39
Condition100%2/2
($xfh ? $xfh : $ENV{'HTTP_HOST'}) || 'localhost'
l!l
2712
MC/DC100%1/1
($xfh ? $xfh : $ENV{'HTTP_HOST'}) || 'localhost'
($xfh ? $xfh : $ENV{'HTTP_HOST'})
Truth table100%2/2
A
A= ($xfh ? $xfh : $ENV{'HTTP_HOST'})
Aresult
11
00
390 39 ▶ my $http_port = ($xfh ? $xfp : $ENV{HTTP_PORT}) || q[];
Branch: 1 branch
BranchTrueFalseTotal
$xfh ? : 0 39 39
Condition50%1/2
($xfh ? $xfp : $ENV{'HTTP_PORT'}) || ''
l!l
039
MC/DC0%0/1
($xfh ? $xfp : $ENV{'HTTP_PORT'}) || ''
($xfh ? $xfp : $ENV{'HTTP_PORT'})
Truth table50%1/2
A
A= ($xfh ? $xfp : $ENV{'HTTP_PORT'})
Aresult
11
00
391 39 ▶ my $http_proto = $ENV{HTTP_X_FORWARDED_PROTO} || $ENV{HTTPS}?q[https]:q[http];
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HTTP_X_FORWARDED_PROTO'} || $ENV{'HTTPS'} ? : 0 39 39
Condition33%1/3
$ENV{"HTTP_X_FORWARDED_PROTO"} or $ENV{"HTTPS"}
l!l&&r!l&&!r
0039
MC/DC0%0/2
$ENV{"HTTP_X_FORWARDED_PROTO"} or $ENV{"HTTPS"}
$ENV{"HTTP_X_FORWARDED_PROTO"} $ENV{"HTTPS"}
Truth table33%1/3
A or B
A= $ENV{"HTTP_X_FORWARDED_PROTO"}B= $ENV{"HTTPS"}
ABresult
1X1
011
000
392  
393 #########
394 # if default port for protocol, skip addition of the port
395 #
396 39 for my $default_proto (keys %{$DEFAULT_PROTO_PORTS}) {
397 78 ▶ if(($default_proto eq lc $http_proto) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($default_proto eq lc $http_proto and $http_port eq $ClearPress::view::DEFAULT_PROTO_PORTS->{$default_proto}) 0 78 78
Condition66%2/3
$default_proto eq lc $http_proto and $http_port eq $ClearPress::view::DEFAULT_PROTO_PORTS->{$default_proto}
!ll&&!rl&&r
39390
MC/DC0%0/2
$default_proto eq lc $http_proto and $http_port eq $ClearPress::view::DEFAULT_PROTO_PORTS->{$default_proto}
$default_proto eq lc $http_proto $http_port eq $ClearPress::view::DEFAULT_PROTO_PORTS->{$default_proto}
Truth table66%2/3
A and B
A= $default_proto eq lc $http_protoB= $http_port eq $ClearPress::view::DEFAULT_PROTO_PORTS->{$default_proto}
ABresult
0X0
100
111
398 $http_port eq $DEFAULT_PROTO_PORTS->{$default_proto}) {
399 0 $http_port = q[];
400 0 last;
401 }
402 }
403  
404 39 ▶ my $href = sprintf q[%s://%s%s%s%s],
Branch: 2 branches
BranchTrueFalseTotal
$http_port ? : 0 39 39
$script_name eq '/' ? : 0 39 39
405 $http_proto,
406 $http_host,
407 $http_port?":$http_port":q[],
408 $script_name,
409 ($script_name eq q[/])?q[]:q[/];
410  
411 my $cfg_globals = {
412 (map {
413 0 $_ => $cfg->val('globals',$_)
414 } $cfg->Parameters('globals'))
415 };
416  
417 my $params = {
418 requestor => $util->requestor || undef,
419 model => $self->model,
420 view => $self,
421 entity => $entity,
422 SCRIPT_NAME => $script_name,
423 HTTP_HOST => $http_host,
424 HTTP_PORT => $http_port,
425 HTTPS => $http_proto,
426 SCRIPT_HREF => $href,
427 ENTITY_HREF => "$href$entity",
428 now => (strftime '%Y-%m-%dT%H:%M:%S', localtime),
429 39 %{$cfg_globals},
430 39 ▶ %{$extra_params||{}},
Condition75%3/4
$util->requestor || undef
l!l
831
$extra_params || {}
l!l
390
MC/DC100%1/1
$util->requestor || undef
$util->requestor
MC/DC0%0/1
$extra_params || {}
$extra_params
Truth table100%2/2
A
A= $util->requestor
Aresult
11
00
Truth table50%1/2
A
A= $extra_params
Aresult
11
00
431 };
432  
433  
434 my $appname = $util->config->val('application', 'name') ||
435 $util->config->val('application', 'namespace') ||
436 39 ▶ $ENV{SCRIPT_NAME};
Condition33%2/6
$util->config->val('application', 'name') || $util->config->val('application', 'namespace') || $ENV{'SCRIPT_NAME'}
l!l&&r!l&&!r
3900
$util->config->val("application", "name") or $util->config->val("application", "namespace")
l!l&&r!l&&!r
3900
MC/DC0%0/3
$util->config->val('application', 'name') || $util->config->val('application', 'namespace') || $ENV{'SCRIPT_NAME'}
$util->config->val("application", "name") $util->config->val("application", "namespace") $ENV{'SCRIPT_NAME'}
Truth table25%1/4
A or B || C
A= $util->config->val("application", "name")B= $util->config->val("application", "namespace")C= $ENV{'SCRIPT_NAME'}
ABCresult
1XX1
01X1
0011
0000
437  
438 39 ▶ $TEMPLATE_CACHE->{$appname} ||= {};
Condition100%2/2
$ClearPress::view::TEMPLATE_CACHE->{$appname} ||= {}
l!l
345
MC/DC100%1/1
$ClearPress::view::TEMPLATE_CACHE->{$appname} ||= {}
$ClearPress::view::TEMPLATE_CACHE->{$appname}
Truth table100%2/2
A
A= $ClearPress::view::TEMPLATE_CACHE->{$appname}
Aresult
11
00
439 39 my $template_cache = $TEMPLATE_CACHE->{$appname};
440  
441 39 ▶ if(!$template_cache->{$template}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template_cache->{$template}) 25 14 39
442 25 my $path = sprintf q[%s/templates], $util->data_path;
443 25 ▶ open my $fh, q[<], "$path/$template" or croak qq[Error opening $template];
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$path/$template" or croak("Error opening $template") 23 2 25
444 23 local $RS = undef;
445 23 $template_cache->{$template} = <$fh>;
446 23 ▶ close $fh or croak qq[Error closing $template];
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing $template") 23 0 23
447 }
448  
449 37 $template = \$template_cache->{$template};
450  
451 37 ▶ if($where_to_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where_to_ref) { } 37 0 37
452 37 ▶ $self->tt->process($template, $params, $where_to_ref, {binmode => ':utf8'}) or croak $self->tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process($template, $params, $where_to_ref, {"binmode", ":utf8"}) or croak($self->tt->error) 37 0 37
453  
454 } else {
455 0 ▶ $self->tt->process($template, $params, $where_to_ref, {binmode => ':utf8'}) or croak $self->tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process($template, $params, $where_to_ref, {"binmode", ":utf8"}) or croak($self->tt->error) 0 0 0
456 }
457  
458 37 return 1;
459 }
460  
461 14 ▶sub _populate_from_cgi {
Subroutine _populate_from_cgi: called CC=19 SCAR=31.2
462 14 my $self = shift;
463 14 my $util = $self->util;
464 14 my $model = $self->model;
465 14 my $cgi = $util->cgi;
466  
467 #########
468 # Populate model object with parameters posted into CGI
469 # by default (in controller.pm) model will only have util & its primary_key.
470 #
471 14 $model->read;
472  
473 14 my $pk = $model->primary_key;
474  
475 14 my @fields = $model->fields;
476 14 ▶ if($pk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pk) 14 0 14
477 #########
478 # don't leave primary key in field list
479 #
480 14 @fields = grep { $_ ne $pk } @fields;
481 }
482  
483 my $params = {
484 map { ## no critic (ProhibitComplexMappings)
485 14 my $p = $cgi->param($_);
486 17 utf8::decode($p);
487 17 $_ => $p;
488 } $cgi->param
489 };
490  
491 #########
492 # parse new-style POST payload
493 # todo: look at PUTDATA as well
494 #
495 14 my ($method) = $ENV{REQUEST_METHOD} =~ /(PUT|POST)/smix;
496 14 ▶ $method ||= q[];
Condition100%2/2
$method ||= ""
l!l
77
MC/DC100%1/1
$method ||= ""
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
497 14 ▶ my $PDATA = sprintf q[%sDATA], (uc $method || 'POST');
Condition100%2/2
uc $method || 'POST'
l!l
77
MC/DC100%1/1
uc $method || 'POST'
uc $method
Truth table100%2/2
A
A= uc $method
Aresult
11
00
498 14 my $reqdata = $cgi->param($PDATA);
499  
500 14 ▶ if($reqdata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reqdata) 6 8 14
501 6 utf8::decode($reqdata);
502 eval {
503 6 my $json = JSON->new->utf8;
504 eval {
505 6 $params = $json->decode($reqdata);
506 5 1;
507  
508 6 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $params = $json->decode($reqdata); 1 } } or do { $params = XMLin($reqdata) } 5 1 6
509 1 $params = XMLin($reqdata);
510 };
511  
512 6 for my $k (keys %{$params}) {
513 10 ▶ if(ref $params->{$k} &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}) 0 10 10
Condition33%2/6
ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}
!ll&&!rl&&r
1000
ref $params->{$k} and ref $params->{$k} eq "HASH"
!ll&&!rl&&r
1000
MC/DC0%0/3
ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}
ref $params->{$k} ref $params->{$k} eq "HASH" not scalar keys %{$$params{$k};}
Truth table25%1/4
A and B and not(C)
A= ref $params->{$k}B= ref $params->{$k} eq "HASH"C= not scalar keys %{$$params{$k};}
ABCresult
0XX0
10X0
1100
1111
514 ref $params->{$k} eq 'HASH' &&
515 0 !scalar keys %{$params->{$k}}) {
516 0 delete $params->{$k};
517 }
518 }
519 6 1;
520  
521 6 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $json = "JSON"->new->utf8; eval { do { $params = $json->decode($reqdata); 1 } } or do { $params = XMLin($reqdata) }; foreach my $k (keys %{$params;}) { if (ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}) { delete $params->{$k}; } ; } ; 1 } } or do { carp("Got error while parsing ${PDATA}: " . $ClearPress::view::EVAL_ERROR) } 6 0 6
522 #########
523 # Not an XML-formatted POST body. Ignore for now.
524 #
525 0 carp qq[Got error while parsing $PDATA: ].$EVAL_ERROR;
526 };
527 }
528  
529 #########
530 # parse old-style XML POST payload
531 #
532 14 my $xml = $cgi->param('XForms:Model');
533 14 ▶ if($xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml) 1 13 14
534 1 utf8::decode($xml);
535 1 $params = XMLin($xml);
536 1 for my $k (%{$params}) {
537 4 ▶ if(ref $params->{$k} &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}) 0 4 4
Condition33%2/6
ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}
!ll&&!rl&&r
400
ref $params->{$k} and ref $params->{$k} eq "HASH"
!ll&&!rl&&r
400
MC/DC0%0/3
ref $params->{$k} and ref $params->{$k} eq "HASH" and not scalar keys %{$$params{$k};}
ref $params->{$k} ref $params->{$k} eq "HASH" not scalar keys %{$$params{$k};}
Truth table25%1/4
A and B and not(C)
A= ref $params->{$k}B= ref $params->{$k} eq "HASH"C= not scalar keys %{$$params{$k};}
ABCresult
0XX0
10X0
1100
1111
538 ref $params->{$k} eq 'HASH' &&
539 0 !scalar keys %{$params->{$k}}) {
540 0 delete $params->{$k};
541 }
542 }
543 }
544  
545 14 for my $field (@fields) {
546 41 ▶ if(!exists $params->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{$field}) 28 13 41
547 28 next;
548 }
549 13 my $v = $params->{$field};
550  
551 #########
552 # $v here will always be defined
553 # but may be false, e.g. $v=q[] or $v=q[0]
554 #
555 13 ▶ if($self->autoescape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoescape) 13 0 13
556 13 $v = $cgi->escapeHTML($v);
557 }
558  
559 13 $model->$field($v);
560 }
561  
562 14 return 1;
563 }
564  
565 1 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
566 1 my $self = shift;
567 1 return $self->_populate_from_cgi;
568 }
569  
570 0 ▶sub edit {
Subroutine edit: not called CC=1 SCAR=6.9
Pod documented
571 0 my $self = shift;
572 0 return $self->_populate_from_cgi;
573 }
574  
575 0 ▶sub options {
Subroutine options: not called CC=1 SCAR=6.9
Pod documented
576 #########
577 # streamed response
578 #
579 0 return q[];
580 }
581  
582 5 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
583 5 return 1;
584 }
585  
586 3 ▶sub read { ## no critic (homonym)
Subroutine read: called CC=1 SCAR=0.0
Pod documented
587 3 return 1;
588 }
589  
590 2 ▶sub delete { ## no critic (homonym)
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
591 2 my $self = shift;
592 2 my $model = $self->model;
593  
594 2 ▶ $model->delete or croak qq[Failed to delete entity: $EVAL_ERROR];
Branch: 1 branch
BranchTrueFalseTotal
$model->delete or croak("Failed to delete entity: $ClearPress::view::EVAL_ERROR") 2 0 2
595  
596 2 return 1;
597 }
598  
599 7 ▶sub update {
Subroutine update: called CC=2 SCAR=7.0
Pod documented
600 7 my $self = shift;
601 7 my $model = $self->model;
602  
603 #########
604 # Populate model object with parameters posted into CGI
605 # by default (in controller.pm) model will only have util & its primary_key.
606 #
607 7 $self->_populate_from_cgi;
608  
609 7 ▶ $model->update or croak qq[Failed to update entity: $EVAL_ERROR];
Branch: 1 branch
BranchTrueFalseTotal
$model->update or croak("Failed to update entity: $ClearPress::view::EVAL_ERROR") 7 0 7
610 7 return 1;
611 }
612  
613 6 ▶sub create {
Subroutine create: called CC=2 SCAR=7.0
Pod documented
614 6 my $self = shift;
615 6 my $model = $self->model;
616  
617 #########
618 # Populate model object with parameters posted into CGI
619 # by default (in controller.pm) model will only have util & its primary_key.
620 #
621 6 $self->_populate_from_cgi;
622  
623 6 ▶ $model->create or croak qq[Failed to create entity: $EVAL_ERROR];
Branch: 1 branch
BranchTrueFalseTotal
$model->create or croak("Failed to create entity: $ClearPress::view::EVAL_ERROR") 6 0 6
624  
625 6 return 1;
626 }
627  
628 332 ▶sub add_tt_filter {
Subroutine add_tt_filter: called CC=3 SCAR=12.7
Pod documented
629 332 my ($self, $name, $code) = @_;
630  
631 332 ▶ if(!$name || !$code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name and $code) 0 332 332
Condition33%1/3
$name and $code
!ll&&!rl&&r
00332
MC/DC0%0/2
$name and $code
$name $code
Truth table33%1/3
A and B
A= $nameB= $code
ABresult
0X0
100
111
632 0 return;
633 }
634  
635 332 $self->tt_filters->{$name} = $code;
636  
637 332 return 1;
638 }
639  
640 346 ▶sub tt_filters {
Subroutine tt_filters: called CC=2 SCAR=6.9
Pod documented
641 346 my $self = shift;
642  
643 346 ▶ if(!$self->{tt_filters}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"tt_filters"}) 83 263 346
644 83 $self->{tt_filters} = {};
645 }
646  
647 346 return $self->{tt_filters};
648 }
649  
650 11 ▶sub tt_opts {
Subroutine tt_opts: called CC=1 SCAR=0.0
Pod documented
651 11 return {};
652 }
653  
654 40 ▶sub tt {
Subroutine tt: called CC=8 SCAR=21.7
Pod documented
655 40 my ($self, $tt) = @_;
656 40 my $util = $self->util;
657  
658 40 ▶ if($tt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt) 0 40 40
659 0 $util->{tt} = $tt;
660 }
661  
662 40 ▶ if(!$util->{tt}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($util->{"tt"}) 11 29 40
663 11 my $filters = Template::Filters->new({
664 FILTERS => $self->tt_filters,
665 });
666 11 my $opts = $self->tt_opts;
667 11 ▶ my $ns = $util->config->val('application', 'namespace') ||
Condition33%1/3
$util->config->val('application', 'namespace') || $util->config->val('application', 'name')
l!l&&r!l&&!r
1100
MC/DC0%0/2
$util->config->val('application', 'namespace') || $util->config->val('application', 'name')
$util->config->val('application', 'namespace') $util->config->val('application', 'name')
Truth table33%1/3
A || B
A= $util->config->val('application', 'namespace')B= $util->config->val('application', 'name')
ABresult
1X1
011
000
668 $util->config->val('application', 'name');
669 11 ▶ my $plugin_base = $ns ? q[ClearPress::Template::Plugin] : sprintf q[%s::plugin], $ns;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 11 0 11
670 11 my $defaults = {
671 PLUGIN_BASE => $plugin_base,
672 RECURSION => 1,
673 INCLUDE_PATH => (sprintf q[%s/templates], $util->data_path),
674 EVAL_PERL => 1,
675 ENCODING => 'utf8',
676 LOAD_FILTERS => [ $filters ],
677 };
678  
679 11 ▶ while (my ($k, $v) = each %{$defaults}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$defaults;}) 66 11 77
680 66 ▶ if(!exists $opts->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{$k}) 66 0 66
681 66 $opts->{$k} = $v;
682 }
683 }
684  
685 11 ▶ $util->{tt} = Template->new($opts) or croak $Template::ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$util->{"tt"} = "Template"->new($opts) or croak($Template::ERROR) 11 0 11
686 }
687 40 return $util->{tt};
688 }
689  
690 53 ▶sub decor {
Subroutine decor: called CC=5 SCAR=16.2
Pod documented
691 53 my $self = shift;
692 53 ▶ my $aspect = $self->aspect || q[];
Condition100%2/2
$self->aspect || ''
l!l
503
MC/DC100%1/1
$self->aspect || ''
$self->aspect
Truth table100%2/2
A
A= $self->aspect
Aresult
11
00
693  
694 53 ▶ if($self->action eq 'options') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "options") 0 53 53
695 0 return 0;
696 }
697  
698 53 for my $ending (qw(rss atom ajax xml
699 json js _png _jpg _svg _svgz
700 _txt _csv _xls)) {
701 453 ▶ if((substr $aspect, -length $ending, length $ending) eq $ending) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($aspect, -length($ending), length $ending) eq $ending) 26 427 453
702 26 return 0;
703 }
704 }
705 27 return 1;
706 }
707  
708 20 ▶sub output_flush {
Subroutine output_flush: called CC=4 SCAR=14.0
Pod documented
709 20 my ($self) = @_;
710 20 ▶ $DEBUG_OUTPUT and carp "output_flush: @{[scalar @{$self->{output_buffer}}]} blobs in queue";
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp(qq[output_flush: @{[scalar @{$self->{"output_buffer"};}];} blobs in queue]) 1 19 20
711  
712 eval {
713 20 ▶ print grep { $_ } @{$self->{output_buffer}} or croak $ERRNO;
Branch: 1 branch
BranchTrueFalseTotal
print grep({$_;} @{$$self{"output_buffer"};}) or croak($ClearPress::view::ERRNO) 20 0 20
714 20 1;
715  
716 20 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { print grep({$_;} @{$$self{"output_buffer"};}) or croak($ClearPress::view::ERRNO); 1 } } or do { carp("Error flushing output_buffer: $ClearPress::view::EVAL_ERROR") } 20 0 20
717 #########
718 # client stopped receiving (e.g. disconnect from lengthy streamed response)
719 #
720 0 carp qq[Error flushing output_buffer: $EVAL_ERROR];
721 };
722  
723 20 $self->output_reset;
724 20 return 1;
725 }
726  
727 16 ▶sub output_prepend {
Subroutine output_prepend: called CC=6 SCAR=20.5
Pod documented
728 16 my ($self, @args) = @_;
729 16 ▶ if(!$self->output_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->output_finished) 15 1 16
730 15 ▶ if(scalar @args == 2 && $args[1] eq "\n" && !$args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 2 and $args[1] eq "\n" and not $args[0]) 0 15 15
Condition33%2/6
scalar @args == 2 and $args[1] eq "\n" and not $args[0]
!ll&&!rl&&r
0150
scalar @args == 2 and $args[1] eq "\n"
!ll&&!rl&&r
0015
MC/DC0%0/3
scalar @args == 2 and $args[1] eq "\n" and not $args[0]
scalar @args == 2 $args[1] eq "\n" not $args[0]
Truth table25%1/4
A and B and not(C)
A= scalar @args == 2B= $args[1] eq "\n"C= not $args[0]
ABCresult
0XX0
10X0
1100
1111
731 0 return;
732 }
733 15 unshift @{$self->{output_buffer}}, grep { $_ } @args; # don't push undef or ""
734 0 ▶ $DEBUG_OUTPUT and carp "output_prepend prepended (@{[scalar @args]} blobs)";
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp("output_prepend prepended (@{[scalar @args];} blobs)") 0 15 15
735 }
736 16 return 1;
737 }
738  
739 35 ▶sub output_buffer {
Subroutine output_buffer: called CC=6 SCAR=18.3
Pod documented
740 35 my ($self, @args) = @_;
741 35 ▶ if(!$self->output_finished) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->output_finished) 33 2 35
742 33 ▶ if(scalar @args == 2 && $args[1] eq "\n" && !$args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 2 and $args[1] eq "\n" and not $args[0]) 0 33 33
Condition66%4/6
scalar @args == 2 and $args[1] eq "\n" and not $args[0]
!ll&&!rl&&r
3210
scalar @args == 2 and $args[1] eq "\n"
!ll&&!rl&&r
3201
MC/DC0%0/3
scalar @args == 2 and $args[1] eq "\n" and not $args[0]
scalar @args == 2 $args[1] eq "\n" not $args[0]
Truth table50%2/4
A and B and not(C)
A= scalar @args == 2B= $args[1] eq "\n"C= not $args[0]
ABCresult
0XX0
10X0
1100
1111
743 0 return;
744 }
745  
746 33 push @{$self->{output_buffer}}, grep { $_ } @args; # don't push undef or ""
747 33 ▶ $DEBUG_OUTPUT and carp "output_buffer added (@{[scalar @args]} blobs)";
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp("output_buffer added (@{[scalar @args];} blobs)") 2 31 33
748 }
749 35 return 1;
750 }
751  
752 70 ▶sub output_finished {
Subroutine output_finished: called CC=3 SCAR=11.0
Pod documented
753 70 my ($self, $val) = @_;
754 70 ▶ if(defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 19 51 70
755 19 $self->{output_finished} = $val;
756 19 ▶ $DEBUG_OUTPUT and carp "output_finished = $val";
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp("output_finished = $val") 1 18 19
757 }
758 70 return $self->{output_finished};
759 }
760  
761 19 ▶sub output_end {
Subroutine output_end: called CC=2 SCAR=6.9
Pod documented
762 19 my $self = shift;
763 19 ▶ $DEBUG_OUTPUT and carp "output_end: $self";
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp("output_end: $self") 1 18 19
764 19 $self->output_finished(1);
765 19 return $self->output_flush;
766 }
767  
768 26 ▶sub output_reset {
Subroutine output_reset: called CC=2 SCAR=6.9
Pod documented
769 26 my $self = shift;
770 26 $self->{output_buffer} = [];
771 26 ▶ $DEBUG_OUTPUT and carp 'output_reset';
Branch: 1 branch
BranchTrueFalseTotal
$ClearPress::view::DEBUG_OUTPUT and carp("output_reset") 2 24 26
772 26 return;
773 }
774  
775 11 ▶sub actions {
Subroutine actions: called CC=1 SCAR=0.0
Pod documented
776 11 my $self = shift;
777 11 my $content = q[];
778  
779 11 $self->process_template('actions.tt2', {}, \$content);
780 11 return $content;
781 }
782  
783 0 ▶sub redirect {
Subroutine redirect: not called CC=3 SCAR=24.8
Pod documented
784 0 my ($self, $url, $status) = @_;
785  
786 0 $self->headers->header('Status', HTTP_FOUND);
787 0 $self->headers->header('Location', $url);
788  
789 #########
790 # - reset all previously output but unflushed content
791 # - push headers down the pipe, and html redirects
792 # - finish up
793 #
794 0 $self->output_reset();
795  
796 0 ▶ if($TRAP_REDIR_OVERFLOW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ClearPress::view::TRAP_REDIR_OVERFLOW) 0 0 0
797 0 ▶ if(length $self->headers->as_string > $TRAP_REDIR_OVERFLOW) { # fudge for apparent buffer overflow with apache+mod_perl (ParseHeaders related?)
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->headers->as_string > $ClearPress::view::TRAP_REDIR_OVERFLOW) 0 0 0
798 0 carp q[warning: header block looks long];
799 0 $self->headers->remove_header('Location');
800 0 $self->headers->header('Status', HTTP_OK);
801 }
802 }
803  
804 0 $self->output_buffer($self->headers->as_string, "\n");
805 0 $self->decorator->meta_refresh(qq[0;URL='$url']);
806  
807 #########
808 # clean everything up and terminate
809 #
810 0 $self->output_flush();
811 0 $self->headers->clear();
812  
813 ########
814 # Warning: This ought to correspond to content-type, but doesn't!
815 #
816 return <<"EOT"
817 <p>This document has moved <a href="$url">here</a>.</p>
818 <script>document.location.href="$url";</script>
819 EOT
820 0 }
821  
822 #########
823 # automated method generation for core CRUD+ view methods
824 #
825 7 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
826 7 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
827 7 for my $ext (qw(xml ajax json csv)) {
828 28 for my $method (qw(create list read update delete options)) {
829 168 my $ns = sprintf q[%s_%s], $method, $ext;
830 168 ▶ *{$ns} = sub { my $self = shift; return $self->$method; };
Subroutine __ANON__: called CC=1 SCAR=0.0
831 }
832 }
833 }
834  
835 7 1;
836 __END__
« blib/lib/ClearPress/driver/mysql.pm ↑ Summary blib/lib/ClearPress/authdecor.pm »