blib/lib/File/Tabular/Web.pm

filter: Statementstmt 68.5% 257 / 375 Branchbran 38.6% 58 / 150 Conditioncond 31.2% 35 / 112 MC/DCmcdc 3.3% 2 / 59 Subroutinesub 69.4% 41 / 59 Podpod 93.5% 29 / 31 totaltotal 53.6% 422 / 786 CC 123 SCAR 30.8
CC
123
Cov
55%
update
110.0
before_update
56.0
sort_and_slice
41.4
CRAP 21.7
« blib/lib/File/Tabular/Web/Attachments.pm ↑ Summary blib/lib/File/Tabular/Web/Attachments/Indexed.pm »
1 package File::Tabular::Web; # documentation at bottom of file
2  
3 2 our $VERSION = "0.25";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw/min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::MoreUtils qw/uniq any all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use AppConfig qw/:argcount/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Tabular 0.71;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Search::QueryParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use parent 'Plack::Component';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 my %app_cache;
23 my %datafile_cache; # persistent data private to _cached_content
24  
25 #======================================================================
26 # MAIN ENTRY POINT
27 #======================================================================
28  
29 #----------------------------------------------------------------------
30 sub call { # Plack request dispatcher (see L<Plack::Component>)
31 #----------------------------------------------------------------------
32 5 ▶ my ($self, $env) = @_;
Subroutine call: called CC=1 SCAR=2.6
Pod documented
33  
34 try {
35 # regular response
36 5 ▶ $self->_new($env);
Subroutine __ANON__: called CC=1 SCAR=0.0
37 5 $self->_dispatch_request;
38 }
39 # in case of an exception
40 catch {
41 # try displaying through msg view..
42 0 ▶ $self->{msg} = "<b><font color='red'>ERROR</font></b> : $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 $self->{view} = 'msg';
44 0 ▶ try {$self->display}
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 catch {
46 # .. or else fallback with simple HTML page
47 0 ▶ my $res = Plack::Response->new(500);
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 $res->body("<html>$self->{msg}</html>");
49 0 $res->content_type('text/html');
50 0 return $res->finalize;
51 0 };
52 5 };
53 }
54  
55  
56  
57 #----------------------------------------------------------------------
58 sub handler : method { # for backwards compatibility : can be called
59 # as a modperl handler or from a CGI script.
60 # New apps should rather use the Plack interface.
61 #----------------------------------------------------------------------
62 0 ▶ my ($class, $request) = @_;
Subroutine handler: not called CC=4 SCAR=30.0
Pod documented
63  
64 0 my $self = $class->new;
65 0 my $app = $self->to_app;
66  
67 0 ▶ if ($request && ref($request) =~ /^Apache2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request and ref($request) =~ /^Apache2/l) { } 0 0 0
Condition0%0/3
$request and ref($request) =~ /^Apache2/l
!ll&&!rl&&r
000
MC/DC0%0/2
$request and ref($request) =~ /^Apache2/l
$request ref($request) =~ /^Apache2/l
Truth table0%0/3
A and B
A= $requestB= ref($request) =~ /^Apache2/l
ABresult
0X0
100
111
68 0 require Plack::Handler::Apache2;
69 0 Plack::Handler::Apache2->call_app($request, $app);
70 }
71 else {
72 0 require Plack::Handler::CGI;
73 0 ▶ $ENV{QUERY_STRING} = $request if $request;
Branch: 1 branch
BranchTrueFalseTotal
if $request 0 0 0
74 0 Plack::Handler::CGI->new->run($app);
75 }
76 }
77  
78  
79 #======================================================================
80 # METHODS FOR CREATING / INITIALIZING "APPLICATION" HASHREFS #
81 #======================================================================
82  
83 #----------------------------------------------------------------------
84 sub _app_new { # creates a new application hashref (not an object)
85 #----------------------------------------------------------------------
86 1 ▶ my ($self, $config_file) = @_;
Subroutine _app_new: called CC=7 SCAR=24.0
87 1 my $app = {};
88  
89 # application name and directory : defaults from the name of config file
90 1 @{$app}{qw(dir name)} = ($config_file =~ m[^(.+[/\\])(.+?)(?:\.[^.]*)$]);
91  
92 # read the config file
93 1 $app->{cfg} = $self->_app_read_config($config_file);
94  
95 1 my $tmp; # predeclare $tmp so that it can be used in "and" clauses
96  
97 # application directory
98 1 ▶ $tmp = $app->{cfg}->get('application_dir') and do {
Branch: 1 branch
BranchTrueFalseTotal
$tmp = $app->{"cfg"}->get("application_dir") and do { $tmp =~ s([^/\\]$)[/]l; $app->{"dir"} = $tmp } 0 1 1
99 0 $tmp =~ s{[^/\\]$}{/}; # add trailing "/" to dir if necessary
100 0 $app->{dir} = $tmp;
101 };
102  
103 # application name
104 1 ▶ $tmp = $app->{cfg}->get('application_name') and $app->{name} = $tmp;
Branch: 1 branch
BranchTrueFalseTotal
$tmp = $app->{"cfg"}->get("application_name") and $app->{"name"} = $tmp 0 1 1
105  
106 # data file
107 1 $tmp = $app->{cfg}->get('application_data');
108 1 ▶ $app->{data_file} = $app->{dir} . ($tmp || "$app->{name}.txt");
Condition50%1/2
$tmp || "$app->{'name'}.txt"
l!l
01
MC/DC0%0/1
$tmp || "$app->{'name'}.txt"
$tmp
Truth table50%1/2
A
A= $tmp
Aresult
11
00
109  
110 # application class
111 1 $app->{class} = ref $self; # initial value, may be overridden
112 1 ▶ $tmp = $app->{cfg}->get('application_class') and do {
Branch: 1 branch
BranchTrueFalseTotal
$tmp = $app->{"cfg"}->get("application_class") and do { eval "require $tmp" or die $@; $tmp->isa($app->{"class"}) or die qq[$tmp is not a $app->{"class"}]; $app->{"class"} = $tmp } 0 1 1
113 0 ▶ eval "require $tmp" or die $@; # dynamically load the requested code
Branch: 1 branch
BranchTrueFalseTotal
eval "require $tmp" or die $@ 0 0 0
114 0 ▶ $tmp->isa($app->{class}) or die "$tmp is not a $app->{class}";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->isa($app->{"class"}) or die qq[$tmp is not a $app->{"class"}] 0 0 0
115 0 $app->{class} = $tmp;
116 };
117  
118 1 return $app;
119 }
120  
121 #----------------------------------------------------------------------
122 sub _app_read_config { # read configuration file through Appconfig
123 #----------------------------------------------------------------------
124 1 ▶ my ($class, $config_file) = @_;
Subroutine _app_read_config: called CC=2 SCAR=6.9
125  
126 # error handler : die for all errors except "no such variable"
127 my $error_func = sub {
128 102 ▶ my $fmt = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
129 102 ▶ die sprintf("AppConfig : $fmt\n", @_)
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt =~ /no such variable/l 0 102 102
130 unless $fmt =~ /no such variable/;
131 1 };
132  
133 # create AppConfig object (options documented in L<AppConfig::State>)
134 1 my $cfg = AppConfig->new({
135 CASE => 1, # case-sensitive
136 CREATE => 1, # accept dynamic creation of variables
137 ERROR => $error_func, # specific error handler
138 GLOBAL => {ARGCOUNT => ARGCOUNT_ONE},# default option for undefined vars
139 });
140  
141 # define specific options for some variables
142 # NOTE: fields_upload is not used here, but by F::T::Attachments
143 1 foreach my $hash_var (qw/fields_default fields_time fields_upload/) {
144 3 $cfg->define($hash_var => {ARGCOUNT => ARGCOUNT_HASH});
145 }
146 1 $cfg->define(fieldSep => {DEFAULT => "|"});
147  
148 # read the configuration file
149 1 $cfg->file($config_file); # or croak "AppConfig: open $config_file: $^E";
150 # BUG : AppConfig does not return any error code if ->file(..) fails !!
151  
152 1 return $cfg;
153 }
154  
155  
156  
157 #----------------------------------------------------------------------
158 sub app_initialize {
159 #----------------------------------------------------------------------
160 # NOTE: this method is called after instance creation and therefore
161 # takes into account the subclass which may have been given in the
162 # config file.
163  
164 1 ▶ my ($self) = @_;
Subroutine app_initialize: called CC=3 SCAR=11.3
Pod documented
165 1 my $app = $self->{app};
166 1 my ($last_subdir) = ($app->{dir} =~ m[^.*[/\\](.+)[/\\]?$]);
167 my $default = $self->{template_root}
168 1 ▶ || $self->app_tmpl_default_dir;
Condition33%1/3
$self->{'template_root'} || $self->app_tmpl_default_dir
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{'template_root'} || $self->app_tmpl_default_dir
$self->{'template_root'} $self->app_tmpl_default_dir
Truth table33%1/3
A || B
A= $self->{'template_root'}B= $self->app_tmpl_default_dir
ABresult
1X1
011
000
169  
170 # directories to search for Templates
171 5 my @tmpl_dirs = grep {-d} ($app->{cfg}->get("template_dir"),
172 $app->{dir},
173 1 "$default/$last_subdir",
174 $default,
175 "$default/default",
176 );
177  
178 # initialize template toolkit object
179 1 ▶ $app->{tmpl} = Template->new({
Branch: 1 branch
BranchTrueFalseTotal
$app->{"tmpl"} = "Template"->new({"INCLUDE_PATH", \@tmpl_dirs, "FILTERS", $self->app_tmpl_filters, "EVAL_PERL", 1}) or die "Template"->error 0 1 1
180 INCLUDE_PATH => \@tmpl_dirs,
181 FILTERS => $self->app_tmpl_filters,
182 EVAL_PERL => 1,
183 })
184 or die Template->error;
185  
186 # special fields : time of last modif, author of last modif
187 1 $app->{time_fields} = $app->{cfg}->get('fields_time');
188 1 $app->{user_field} = $app->{cfg}->get('fields_user');
189 }
190  
191  
192 #----------------------------------------------------------------------
193 sub app_tmpl_default_dir { # default; override in subclasses
194 #----------------------------------------------------------------------
195 1 ▶ my ($self) = @_;
Subroutine app_tmpl_default_dir: called CC=1 SCAR=0.0
Pod documented
196  
197 1 return "$self->{app_root}/../lib/tmpl/ftw";
198 }
199  
200  
201 #----------------------------------------------------------------------
202 sub app_tmpl_filters { # default; override in subclasses
203 #----------------------------------------------------------------------
204 1 ▶ my ($self) = @_;
Subroutine app_tmpl_filters: called CC=4 SCAR=22.1
Pod documented
205 1 my $cfg = $self->{app}{cfg};
206 1 my $ini_marker = $cfg->get('preMatch');
207 1 my $end_marker = $cfg->get('postMatch');
208  
209 # no highlight filters without pre/postMatch
210 1 ▶ $ini_marker && $end_marker or return {};
Branch: 1 branch
BranchTrueFalseTotal
$ini_marker and $end_marker or return {} 1 0 1
Condition33%1/3
$ini_marker and $end_marker
!ll&&!rl&&r
100
MC/DC0%0/2
$ini_marker and $end_marker
$ini_marker $end_marker
Truth table33%1/3
A and B
A= $ini_markerB= $end_marker
ABresult
0X0
100
111
211  
212 0 ▶ my $HL_class = $cfg->get('highlightClass') || "HL";
Condition0%0/2
$cfg->get('highlightClass') || 'HL'
l!l
00
MC/DC0%0/1
$cfg->get('highlightClass') || 'HL'
$cfg->get('highlightClass')
Truth table0%0/2
A
A= $cfg->get('highlightClass')
Aresult
11
00
213 0 my $regex = qr/\Q$ini_marker\E(.*?)\Q$end_marker\E/s;
214  
215 my $filters = {
216 highlight => sub {
217 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 $text =~ s[$regex][<span class="$HL_class">$1</span>]g;
219 0 return $text;
220 },
221 unhighlight => sub {
222 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 $text =~ s[$regex][$1]g;
224 0 return $text;
225 }
226 0 };
227 0 return $filters;
228 }
229  
230  
231  
232  
233 #----------------------------------------------------------------------
234 sub app_phases_definitions {
235 #----------------------------------------------------------------------
236 5 ▶ my $class = shift;
Subroutine app_phases_definitions: called CC=1 SCAR=0.0
Pod undocumented
237  
238 # PHASES DEFINITIONS TABLE : each single letter is expanded into
239 # optional methods for data preparation, data operation, and view.
240 # It is also possible to differentiate between GET and POST requests.
241 return (
242  
243 5 A => # prepare a new record for adding
244 {GET => {pre => 'empty_record', view => 'modif'},
245 POST => {pre => 'empty_record', op => 'update' } },
246  
247 D => # delete record
248 {pre => 'search_key', op => 'delete' },
249  
250 H => # display home page
251 { view => 'home' },
252  
253 L => # display "long" view of one single record
254 {pre => 'search_key', view => 'long' },
255  
256 M => # modif: GET displays the form, POST performs the update
257 {GET => {pre => 'search_key', view => 'modif'},
258 POST => {pre => 'search_key', op => 'update' } },
259  
260 S => # search and display "short" view
261 {pre => 'search', op => 'sort_and_slice', view => 'short' },
262  
263 X => # display all records in "download view" (mnemonic: eXtract)
264 {pre => 'prepare_download', view => 'download'},
265  
266 );
267 }
268  
269  
270  
271 #======================================================================
272 # METHODS FOR INSTANCE CREATION / INITIALIZATION #
273 #======================================================================
274  
275  
276  
277 #----------------------------------------------------------------------
278 sub _new { # expands and re-blesses the File::Tabular::Web instance
279 #----------------------------------------------------------------------
280 5 ▶ my ($self, $env) = @_;
Subroutine _new: called CC=16 SCAR=31.9
281  
282 5 my $req = Plack::Request->new($env);
283 5 ▶ my $path_info = $req->path_info
Branch: 1 branch
BranchTrueFalseTotal
my $path_info = $req->path_info or die "File::Tabular::Web: no application (PATH_INFO is empty)" 0 5 5
284 or die __PACKAGE__ . ": no application (PATH_INFO is empty)";
285  
286 # add some fields within object
287 5 $self->{req} = $req;
288 5 ▶ $self->{user} = $req->user || "Anonymous";
Condition50%1/2
$req->user || 'Anonymous'
l!l
50
MC/DC0%0/1
$req->user || 'Anonymous'
$req->user
Truth table50%1/2
A
A= $req->user
Aresult
11
00
289 5 $self->{url} = $req->base . $path_info;
290 5 $self->{method} = $req->method;
291 5 $self->{msg} = "";
292  
293 # are we running under mod_perl ? if so, have a handle to the Rec object.
294 5 my $mod_perl = do {my $input = $self->{req}->env->{'psgi.input'};
295 5 ▶ $input->isa('Apache2::RequestRec') && $input};
Branch: 1 branch
BranchTrueFalseTotal
$input->isa("Apache2::RequestRec") and $input 0 5 5
296  
297 # find the app root, by default equal to server document root
298 $self->{app_root}
299 ||= $mod_perl && $mod_perl->document_root
300 || $env->{CONTEXT_DOCUMENT_ROOT} # new in Apache2.4
301 5 ▶ || $env->{DOCUMENT_ROOT}; # standard CGI protocol
Condition41%5/12
$self->{"app_root"} ||= $mod_perl && $mod_perl->document_root || $env->{"CONTEXT_DOCUMENT_ROOT"} || $env->{"DOCUMENT_ROOT"}
l!l&&r!l&&!r
410
$mod_perl && $mod_perl->document_root || $env->{'CONTEXT_DOCUMENT_ROOT'} || $env->{'DOCUMENT_ROOT'}
l!l&&r!l&&!r
010
$mod_perl and $mod_perl->document_root or $env->{"CONTEXT_DOCUMENT_ROOT"}
l!l&&r!l&&!r
001
$mod_perl and $mod_perl->document_root
!ll&&!rl&&r
100
MC/DC0%0/5
$self->{"app_root"} ||= $mod_perl && $mod_perl->document_root || $env->{"CONTEXT_DOCUMENT_ROOT"} || $env->{"DOCUMENT_ROOT"}
$self->{"app_root"} $mod_perl $mod_perl->document_root $env->{"CONTEXT_DOCUMENT_ROOT"} $env->{'DOCUMENT_ROOT'}
Truth table0%0/8
A ||= B and C or D || E
A= $self->{"app_root"}B= $mod_perlC= $mod_perl->document_rootD= $env->{"CONTEXT_DOCUMENT_ROOT"}E= $env->{'DOCUMENT_ROOT'}
ABCDEresult
1XXXX1
011XX1
00X1X1
0101X1
00X011
010011
00X000
010000
302  
303 # find application file
304 my $app_file = $mod_perl && $mod_perl->filename
305 || $env->{SCRIPT_FILENAME}
306 || $env->{PATH_TRANSLATED}
307 5 ▶ || $self->{app_root} . $req->script_name . $path_info;
Condition33%4/12
$mod_perl && $mod_perl->filename || $env->{'SCRIPT_FILENAME'} || $env->{'PATH_TRANSLATED'} || $self->{'app_root'} . $req->script_name . $path_info
l!l&&r!l&&!r
050
$mod_perl and $mod_perl->filename or $env->{"SCRIPT_FILENAME"} or $env->{"PATH_TRANSLATED"}
l!l&&r!l&&!r
005
$mod_perl and $mod_perl->filename or $env->{"SCRIPT_FILENAME"}
l!l&&r!l&&!r
005
$mod_perl and $mod_perl->filename
!ll&&!rl&&r
500
MC/DC0%0/5
$mod_perl && $mod_perl->filename || $env->{'SCRIPT_FILENAME'} || $env->{'PATH_TRANSLATED'} || $self->{'app_root'} . $req->script_name . $path_info
$mod_perl $mod_perl->filename $env->{"SCRIPT_FILENAME"} $env->{"PATH_TRANSLATED"} $self->{'app_root'} . $req->script_name . $path_info
Truth table11%1/9
A and B or C or D || E
A= $mod_perlB= $mod_perl->filenameC= $env->{"SCRIPT_FILENAME"}D= $env->{"PATH_TRANSLATED"}E= $self->{'app_root'} . $req->script_name . $path_info
ABCDEresult
11XXX1
0X1XX1
101XX1
0X01X1
1001X1
0X0011
100011
0X0000
100000
308  
309 # compare modification time with cache; load app if necessary
310 5 ▶ my $mtime = (stat $app_file)[9]
Branch: 1 branch
BranchTrueFalseTotal
my $mtime = (stat $app_file)[9] or die "couldn't stat app file for $path_info" 0 5 5
311 or die "couldn't stat app file for $path_info";
312 5 my $cache_entry = $app_cache{$app_file};
313 5 ▶ my $app_initialized = $cache_entry && $cache_entry->{mtime} == $mtime;
Condition66%2/3
$cache_entry && $cache_entry->{'mtime'} == $mtime
!ll&&!rl&&r
104
MC/DC50%1/2
$cache_entry && $cache_entry->{'mtime'} == $mtime
$cache_entry $cache_entry->{'mtime'} == $mtime
Truth table66%2/3
A && B
A= $cache_entryB= $cache_entry->{'mtime'} == $mtime
ABresult
0X0
100
111
314 5 ▶ if (not $app_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app_initialized) 1 4 5
315 1 $app_cache{$app_file} = {mtime => $mtime,
316 content => $self->_app_new($app_file)};
317 }
318 5 $self->{app} = $app_cache{$app_file}->{content};
319 5 $self->{cfg} = $self->{app}{cfg}; # shortcut
320  
321 # rebless the request obj into the application class, initialize and return
322 5 bless $self, $self->{app}{class};
323  
324 # now that we have the proper class, initialize the app if needed
325 5 ▶ $self->app_initialize unless $app_initialized;
Branch: 1 branch
BranchTrueFalseTotal
unless $app_initialized 1 4 5
326  
327 # initialize the request obj
328 5 $self->initialize;
329  
330 5 return $self;
331 }
332  
333  
334 #----------------------------------------------------------------------
335 sub initialize { # setup params from config and/or CGI params
336 #----------------------------------------------------------------------
337 5 ▶ my $self = shift;
Subroutine initialize: called CC=4 SCAR=15.2
Pod documented
338  
339 # default values
340 5 ▶ $self->{max} = $self->param('max') || 500;
Condition50%1/2
$self->param('max') || 500
l!l
50
MC/DC0%0/1
$self->param('max') || 500
$self->param('max')
Truth table50%1/2
A
A= $self->param('max')
Aresult
11
00
341 5 ▶ $self->{count} = $self->param('count') || 50;
Condition50%1/2
$self->param('count') || 50
l!l
50
MC/DC0%0/1
$self->param('count') || 50
$self->param('count')
Truth table50%1/2
A
A= $self->param('count')
Aresult
11
00
342 5 ▶ $self->{orderBy} = $self->param('orderBy')
Condition33%1/3
$self->param('orderBy') || $self->param('sortBy')
l!l&&r!l&&!r
005
MC/DC0%0/2
$self->param('orderBy') || $self->param('sortBy')
$self->param('orderBy') $self->param('sortBy')
Truth table33%1/3
A || B
A= $self->param('orderBy')B= $self->param('sortBy')
ABresult
1X1
011
000
343 || $self->param('sortBy'); # for backwards compatibility
344  
345 5 return $self;
346 }
347  
348  
349 #----------------------------------------------------------------------
350 sub _setup_phases { # decide about next phases
351 #----------------------------------------------------------------------
352 5 ▶ my $self = shift;
Subroutine _setup_phases: called CC=10 SCAR=28.3
353  
354 # get all phases definitions (expansions of single-letter param)
355 5 my %request_phases = $self->app_phases_definitions;
356  
357 # find out which single-letter was requested
358 5 my @letters = grep {defined $request_phases{$_}} uniq $self->param;
359  
360 # cannot ask for several operations at once
361 5 ▶ @letters <= 1 or die "conflict in request: " . join(" / ", @letters);
Branch: 1 branch
BranchTrueFalseTotal
@letters <= 1 or die 'conflict in request: ' . join(" / ", @letters) 0 5 5
362  
363 # by default : homepage
364 5 ▶ my $letter = $letters[0] || "H";
Condition100%2/2
$letters[0] || 'H'
l!l
41
MC/DC100%1/1
$letters[0] || 'H'
$letters[0]
Truth table100%2/2
A
A= $letters[0]
Aresult
11
00
365  
366 # argument passed to operation
367 5 my $letter_arg = $self->param($letters[0]);
368  
369 # special case : with POST requests, we want to also consider the ?A or ?M=..
370 # or ?D=.. from the query string
371 5 ▶ if (not @letters and $self->{method} eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @letters and $self->{"method"} eq "POST") 0 5 5
Condition66%2/3
not @letters and $self->{"method"} eq "POST"
!ll&&!rl&&r
410
MC/DC0%0/2
not @letters and $self->{"method"} eq "POST"
not @letters $self->{"method"} eq "POST"
Truth table66%2/3
not(A) and B
A= not @lettersB= $self->{"method"} eq "POST"
ABresult
0X0
100
111
372 LETTER:
373 0 for my $try_letter (qw/A M D/) {
374 0 $letter_arg = $self->{req}->query_parameters->get($try_letter);
375 0 ▶ $letter = $try_letter and last LETTER if defined($letter_arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $letter_arg 0 0 0
Condition0%0/5
defined $letter_arg && (($letter = $try_letter) && (last LETTER))
!ll&&!rl&&r
000
$letter = $try_letter and last LETTER
!ll
00
MC/DC0%0/2
defined $letter_arg && (($letter = $try_letter) && (last LETTER))
defined $letter_arg $letter = $try_letter
Truth table0%0/3
A && B
A= defined $letter_argB= $letter = $try_letter
ABresult
0X0
100
111
376 }
377 }
378  
379 # setup info in $self according to the chosen letter
380 5 my $entry = $request_phases{$letter};
381 5 ▶ my $phases = $entry->{$self->{method}} || $entry;
Condition66%2/3
$$entry{$self->{'method'}} || $entry
l!l&&r!l&&!r
140
MC/DC0%0/2
$$entry{$self->{'method'}} || $entry
$$entry{$self->{'method'}} $entry
Truth table66%2/3
A || B
A= $$entry{$self->{'method'}}B= $entry
ABresult
1X1
011
000
382 5 ▶ $self->{view} = $self->param('V') || $phases->{view};
Condition33%1/3
$self->param('V') || $phases->{'view'}
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->param('V') || $phases->{'view'}
$self->param('V') $phases->{'view'}
Truth table33%1/3
A || B
A= $self->param('V')B= $phases->{'view'}
ABresult
1X1
011
000
383 5 $self->{pre} = $phases->{pre};
384 5 $self->{op} = $phases->{op};
385  
386 5 return $letter_arg;
387 }
388  
389  
390 #----------------------------------------------------------------------
391 sub open_data { # open File::Tabular object on data file
392 #----------------------------------------------------------------------
393 5 ▶ my $self = shift;
Subroutine open_data: called CC=11 SCAR=34.3
Pod documented
394  
395 # parameters for opening the file
396 5 my $open_src = $self->{app}{data_file};
397 5 ▶ my $mtime = (stat $open_src)[9] or die "couldn't stat $open_src";
Branch: 1 branch
BranchTrueFalseTotal
my $mtime = (stat $open_src)[9] or die "couldn't stat $open_src" 0 5 5
398  
399 # text version of modified time for templates
400 5 ▶ if (my $fmt = $self->{cfg}->get('application_mtime')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fmt = $self->{"cfg"}->get("application_mtime")) 0 5 5
401 0 $self->{mtime} = strftime($fmt, localtime($mtime));
402 }
403  
404 5 ▶ my $open_mode = ($self->{op} =~ /delete|update/) ? "+<" : "<";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'op'} =~ /delete|update/l ? : 0 5 5
405  
406 # application option : use in-memory cache only for read operations
407 5 ▶ if ($self->{cfg}->get('application_useFileCache')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cfg"}->get("application_useFileCache") and $open_mode eq "<") 0 5 5
Condition33%1/3
$self->{"cfg"}->get("application_useFileCache") and $open_mode eq "<"
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"cfg"}->get("application_useFileCache") and $open_mode eq "<"
$self->{"cfg"}->get("application_useFileCache") $open_mode eq "<"
Truth table33%1/3
A and B
A= $self->{"cfg"}->get("application_useFileCache")B= $open_mode eq "<"
ABresult
0X0
100
111
408 && $open_mode eq '<') {
409 0 my $cache_entry = $datafile_cache{$open_src};
410 0 ▶ unless ($cache_entry && $cache_entry->{mtime} == $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_entry and $cache_entry->{"mtime"} == $mtime) 0 0 0
Condition0%0/3
$cache_entry and $cache_entry->{"mtime"} == $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
$cache_entry and $cache_entry->{"mtime"} == $mtime
$cache_entry $cache_entry->{"mtime"} == $mtime
Truth table0%0/3
A and B
A= $cache_entryB= $cache_entry->{"mtime"} == $mtime
ABresult
0X0
100
111
411 0 ▶ open my $fh, $open_src or die "open $open_src : $^E";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $open_src or die "open $open_src : $^E" 0 0 0
412 0 local $/ = undef;
413 0 my $content = <$fh>; # slurps the whole file into memory
414 0 close $fh;
415 0 $datafile_cache{$open_src} = {mtime => $mtime,
416 content => \$content };
417 }
418 0 $open_src = $cache_entry->{content}; # ref to in-memory content
419 }
420  
421 # set up options for creating File::Tabular object
422 5 my %options;
423 5 foreach (qw/preMatch postMatch avoidMatchKey fieldSep recordSep/) {
424 25 $options{$_} = $self->{cfg}->get($_);
425 }
426 5 $options{autoNumField} = $self->{cfg}->get('fields_autoNum');
427 5 my $jFile = $self->{cfg}->get('journal');
428 5 ▶ $options{journal} = "$self->{app}{dir}$jFile" if $jFile;
Branch: 1 branch
BranchTrueFalseTotal
if $jFile 0 5 5
429  
430 # create File::Tabular object
431 5 $self->{data} = new File::Tabular($open_mode, $open_src, \%options);
432 }
433  
434  
435 #======================================================================
436 # PUBLIC METHODS CALLABLE FROM TEMPLATES #
437 #======================================================================
438  
439  
440 #----------------------------------------------------------------------
441 sub param { # Encapsulates access to the lower layer param() method, and
442 # merge with config information.
443 #----------------------------------------------------------------------
444 37 ▶ my ($self, $param_name) = @_; # $param_name might be undef
Subroutine param: called CC=5 SCAR=16.1
Pod documented
445  
446 # Like old CGI->param(), we only return body parameters on POST
447 # requests (ignoring query parameters).
448 my $params = $self->{method} eq 'POST' ? $self->{req}->body_parameters
449 37 ▶ : $self->{req}->parameters;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'method'} eq 'POST' ? : 0 37 37
450  
451 # if no arg, just return the list of param names
452 37 ▶ return keys %$params if not defined $param_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param_name 6 31 37
453  
454 # otherwise, first check in "fixed" section in config
455 31 my $val = $self->{cfg}->get("fixed_$param_name");
456 31 ▶ return $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 5 26 31
457  
458 # then check in parameters to this request (flattened into a scalar)
459 26 my @vals = $params->get_all($param_name);
460 26 ▶ if (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 4 22 26
461 4 $val = join(' ', @vals); # join multiple values
462 4 $val =~ s/^\s+//; # remove initial spaces
463 4 $val =~ s/\s+$//; # remove final spaces
464 4 return $val;
465 }
466  
467 # finally check in "default" section in config
468 22 return $self->{cfg}->get("default_$param_name");
469 }
470  
471  
472 #----------------------------------------------------------------------
473 sub can_do { # can be called from templates; $record is optional
474 #----------------------------------------------------------------------
475 6 ▶ my ($self, $action, $record) = @_;
Subroutine can_do: called CC=10 SCAR=31.7
Pod documented
476  
477 6 my $allow = $self->{cfg}->get("permissions_$action");
478 6 my $deny = $self->{cfg}->get("permissions_no_$action");
479  
480 # some permissions are granted by default to everybody
481 6 ▶ $allow ||= "*" if $action =~ /^(read|search|download)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $action =~ /^(read|search|download)$/l 6 0 6
Condition50%1/2
$allow ||= "*"
l!l
06
MC/DC0%0/1
$allow ||= "*"
$allow
Truth table50%1/2
A
A= $allow
Aresult
11
00
482  
483 6 for ($allow, $deny) {
484 12 ▶ $_ or next; # no acl list => nothing to do
Branch: 1 branch
BranchTrueFalseTotal
$_ or next 6 6 12
485 $_ = $self->user_match($_) # if acl list matches user name
486 ||( /\$(\S+)\b/i # or if acl list contains a field name ...
487 && defined($record) # ... and got a specific record
488 && defined($record->{$1}) # ... and field is defined
489 6 ▶ && $self->user_match($record->{$1})); # ... and field content matches
Condition8%1/12
$self->user_match($_) || /\$(\S+)\b/il && defined $record && defined $record->{$1} && $self->user_match($record->{$1})
l!l&&r!l&&!r
600
/\$(\S+)\b/il and defined $record and defined $record->{$1} and $self->user_match($record->{$1})
!ll&&!rl&&r
000
/\$(\S+)\b/il and defined $record and defined $record->{$1}
!ll&&!rl&&r
000
/\$(\S+)\b/il and defined $record
!ll&&!rl&&r
000
MC/DC0%0/5
$self->user_match($_) || /\$(\S+)\b/il && defined $record && defined $record->{$1} && $self->user_match($record->{$1})
$self->user_match($_) /\$(\S+)\b/il defined $record defined $record->{$1} $self->user_match($record->{$1})
Truth table16%1/6
A || B and C and D and E
A= $self->user_match($_)B= /\$(\S+)\b/ilC= defined $recordD= defined $record->{$1}E= $self->user_match($record->{$1})
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
490 }
491  
492 6 ▶ return $allow && !$deny;
Condition33%1/3
$allow && !$deny
!ll&&!rl&&r
006
MC/DC0%0/2
$allow && !$deny
$allow !$deny
Truth table33%1/3
A && not(B)
A= $allowB= !$deny
ABresult
0X0
100
111
493 }
494  
495  
496  
497 #======================================================================
498 # REQUEST HANDLING : GENERAL METHODS #
499 #======================================================================
500  
501  
502 #----------------------------------------------------------------------
503 sub _dispatch_request { # go through phases and choose appropriate handling
504 #----------------------------------------------------------------------
505 5 ▶ my $self = shift;
Subroutine _dispatch_request: called CC=4 SCAR=13.9
506 5 my $method;
507  
508 # determine phases from single-letter param; keep arg value from that letter
509 5 my $letter_arg = $self->_setup_phases;
510  
511 # data access
512 5 $self->open_data;
513  
514 # data preparation : invoke method if any, passing the arg saved above
515 5 ▶ $method = $self->{pre} and $self->$method($letter_arg);
Branch: 1 branch
BranchTrueFalseTotal
$method = $self->{"pre"} and $self->$method($letter_arg) 4 1 5
516  
517 # data manipulation : invoke method if any
518 5 ▶ $method = $self->{op} and $self->$method;
Branch: 1 branch
BranchTrueFalseTotal
$method = $self->{"op"} and $self->$method 2 3 5
519  
520 # force message view if there is a message
521 5 ▶ $self->{view} = 'msg' if $self->{msg};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"msg"} 0 5 5
522  
523 # print the output
524 5 $self->display;
525 }
526  
527  
528 #----------------------------------------------------------------------
529 sub display { # display results in the requested view
530 #----------------------------------------------------------------------
531 5 ▶ my ($self) = @_;
Subroutine display: called CC=6 SCAR=18.9
Pod documented
532 5 ▶ my $view = $self->{view} or die "display : no view";
Branch: 1 branch
BranchTrueFalseTotal
my $view = $self->{"view"} or die "display : no view" 0 5 5
533  
534  
535 # name of the template for this view
536 5 ▶ my $default_tmpl = $view eq 'download' ? "download.tt"
Branch: 1 branch
BranchTrueFalseTotal
$view eq 'download' ? : 0 5 5
537 : "$self->{app}{name}_$view.tt";
538 5 ▶ my $tmpl_name = $self->{cfg}->get("template_$view") || $default_tmpl;
Condition33%1/3
$self->{'cfg'}->get("template_$view") || $default_tmpl
l!l&&r!l&&!r
500
MC/DC0%0/2
$self->{'cfg'}->get("template_$view") || $default_tmpl
$self->{'cfg'}->get("template_$view") $default_tmpl
Truth table33%1/3
A || B
A= $self->{'cfg'}->get("template_$view")B= $default_tmpl
ABresult
1X1
011
000
539  
540 # override template toolkit's failsafe counter for while loops
541 # in case of download action
542 5 ▶ local $Template::Directive::WHILE_MAX = 50000 if $view eq 'download';
Branch: 1 branch
BranchTrueFalseTotal
if $view eq "download" 0 5 5
543  
544 # call that template
545 5 my $body;
546 5 my $vars = {self => $self, found => $self->{results}};
547 $self->{app}{tmpl}->process($tmpl_name, $vars, \$body)
548 5 ▶ or die $self->{app}{tmpl}->error();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"app"}{"tmpl"}->process($tmpl_name, $vars, \$body) or die $self->{"app"}{"tmpl"}->error 0 5 5
549  
550 # generate Plack response
551 5 my $res = Plack::Response->new(200);
552 $res->headers({"Content-type" => "text/html",
553 "Content-length" => length($body),
554 "Last-modified" => $self->{data}->stat->{mtime},
555 5 "Expires" => 0});
556 5 $res->body($body);
557  
558 5 return $res->finalize;
559 }
560  
561  
562 #======================================================================
563 # REQUEST HANDLING : SEARCH METHODS #
564 #======================================================================
565  
566  
567 #----------------------------------------------------------------------
568 sub search_key { # search by record key
569 #----------------------------------------------------------------------
570 2 ▶ my ($self, $key) = @_;
Subroutine search_key: called CC=3 SCAR=11.1
Pod documented
571 2 ▶ $self->can_do("read") or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("read") or die qq[no 'read' permission for $self->{"user"}] 0 2 2
572 die "no 'read' permission for $self->{user}";
573 2 ▶ $key or die "search_key : no key!";
Branch: 1 branch
BranchTrueFalseTotal
$key or die "search_key : no key!" 0 2 2
574 2 $key =~ s/<.*?>//g; # remove any markup (maybe inserted by pre/postMatch)
575  
576 2 my $query = "K_E_Y:$key";
577  
578 2 my ($records, $lineNumbers) = $self->{data}->fetchall(where => $query);
579 2 my $count = @$records;
580 2 $self->{results}{count} = $count;
581 2 $self->{results}{records} = $records;
582 2 $self->{results}{lineNumbers} = $lineNumbers;
583 }
584  
585  
586  
587 #----------------------------------------------------------------------
588 sub search { # search records and display results
589 #----------------------------------------------------------------------
590 2 ▶ my ($self, $search_string) = @_;
Subroutine search: called CC=5 SCAR=16.2
Pod documented
591  
592 # check permissions
593 2 ▶ $self->can_do('search') or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("search") or die qq[no 'search' permission for $self->{"user"}] 0 2 2
594 die "no 'search' permission for $self->{user}";
595  
596 2 $self->{search_string_orig} = $search_string;
597 2 $self->before_search;
598 2 $self->log_search;
599  
600 2 $self->{results}{count} = 0;
601 2 $self->{results}{records} = [];
602 2 $self->{results}{lineNumbers} = [];
603  
604 2 ▶ return if $self->{search_string} =~ /^\s*$/; # no query, no results
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"search_string"} =~ /^\s*$/l 0 2 2
605  
606 2 my $qp = new Search::QueryParser;
607  
608 # compile query with an implicit '+' prefix in front of every item
609 2 ▶ my $parsedQ = $qp->parse($self->{search_string}, '+') or
Branch: 1 branch
BranchTrueFalseTotal
my $parsedQ = $qp->parse($self->{"search_string"}, "+") or die qq[error parsing query : $self->{"search_string"}] 0 2 2
610 die "error parsing query : $self->{search_string}";
611  
612 2 my $filter;
613  
614 2 ▶ eval {$filter = $self->{data}->compileFilter($parsedQ);} or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $filter = $self->{"data"}->compileFilter($parsedQ) } } or die "error in query : $@ ," . $qp->unparse($parsedQ) . qq[ ($self->{"search_string"})] 0 2 2
615 die("error in query : $@ ," . $qp->unparse($parsedQ)
616 . " ($self->{search_string})");
617  
618 # perform the search
619 2 @{$self->{results}}{qw(records lineNumbers)} =
620 2 $self->{data}->fetchall(where => $filter);
621 2 $self->{results}{count} = @{$self->{results}{records}};
622  
623 # VERY CHEAP way of generating regex for highlighting results
624 2 my @words_queried = uniq(grep {length($_)>2} $self->words_queried);
625 2 $self->{results}{wordsQueried} = join "|", @words_queried;
626 }
627  
628  
629 #----------------------------------------------------------------------
630 sub before_search {
631 #----------------------------------------------------------------------
632 2 ▶ my ($self) = @_;
Subroutine before_search: called CC=2 SCAR=7.1
Pod documented
633 2 ▶ $self->{search_string} = $self->{search_string_orig} || "";
Condition50%1/2
$self->{'search_string_orig'} || ''
l!l
20
MC/DC0%0/1
$self->{'search_string_orig'} || ''
$self->{'search_string_orig'}
Truth table50%1/2
A
A= $self->{'search_string_orig'}
Aresult
11
00
634 2 return $self;
635 }
636  
637  
638  
639 #----------------------------------------------------------------------
640 sub sort_and_slice { # sort results, then just keep the desired slice
641 #----------------------------------------------------------------------
642 2 ▶ my $self = shift;
Subroutine sort_and_slice: called CC=17 SCAR=37.2
Pod documented
643  
644 2 delete $self->{results}{lineNumbers}; # not going to use those
645  
646 # sort results
647 2 ▶ if ($self->{orderBy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"orderBy"}) 0 2 2
648 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $cmpfunc = $self->{"data"}->ht->cmp($self->{"orderBy"}); $self->{"results"}{"records"} = [(sort $cmpfunc @{$$self{"results"}{"records"};})] } } or die "orderBy : $@" 0 0 0
649 0 my $cmpfunc = $self->{data}->ht->cmp($self->{orderBy});
650 0 $self->{results}{records} = [sort $cmpfunc @{$self->{results}{records}}];
651 }
652 or die "orderBy : $@";
653 }
654  
655 # restrict to the desired slice
656 2 ▶ my $start_record = $self->param('start') || ($self->{results}{count} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'results'}{'count'} ? : 2 0 2
Condition33%1/3
$self->param('start') || ($self->{'results'}{'count'} ? 1 : 0)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->param('start') || ($self->{'results'}{'count'} ? 1 : 0)
$self->param('start') ($self->{'results'}{'count'} ? 1 : 0)
Truth table33%1/3
A || B
A= $self->param('start')B= ($self->{'results'}{'count'} ? 1 : 0)
ABresult
1X1
011
000
657 my $end_record = min($start_record + $self->{count} - 1,
658 2 $self->{results}{count});
659 2 ▶ die "illegal start value : $start_record" if $start_record > $end_record;
Branch: 1 branch
BranchTrueFalseTotal
if $start_record > $end_record 0 2 2
660 $self->{results}{records} = $self->{results}{count}
661 2 ▶ ? [ @{$self->{results}{records}}[$start_record-1 ... $end_record-1] ]
Branch: 1 branch
BranchTrueFalseTotal
$self->{'results'}{'count'} ? : 2 0 2
662 : [];
663  
664 # check read permission on records (looping over records only if necessary)
665 my $must_loop_on_records # true if permission depends on record fields
666 = (($self->{cfg}->get("permissions_read") || "") =~ /\$/)
667 2 ▶ || (($self->{cfg}->get("permissions_no_read") || "") =~ /\$/);
Condition42%3/7
($self->{'cfg'}->get('permissions_read') || '') =~ /\$/l || ($self->{'cfg'}->get('permissions_no_read') || '') =~ /\$/l
l!l&&r!l&&!r
002
$self->{'cfg'}->get('permissions_read') || ''
l!l
02
$self->{'cfg'}->get('permissions_no_read') || ''
l!l
02
MC/DC0%0/2
($self->{'cfg'}->get('permissions_read') || '') =~ /\$/l || ($self->{'cfg'}->get('permissions_no_read') || '') =~ /\$/l
$self->{'cfg'}->get('permissions_read') $self->{'cfg'}->get('permissions_no_read')
Truth table33%1/3
A || B
A= $self->{'cfg'}->get('permissions_read')B= $self->{'cfg'}->get('permissions_no_read')
ABresult
1X1
011
000
668 2 ▶ if ($must_loop_on_records) {
Branch: 1 branch
BranchTrueFalseTotal
if ($must_loop_on_records) { } 0 2 2
669 0 foreach my $record (@{$self->{results}{records}}) {
670 0 ▶ $self->can_do('read', $record)
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("read", $record) or die qq[no 'read' permission for $self->{"user"}] 0 0 0
671 or die "no 'read' permission for $self->{user}";
672 }
673 }
674 else { # no need for a loop
675 2 ▶ $self->can_do('read')
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("read") or die qq[no 'read' permission for $self->{"user"}] 0 2 2
676 or die "no 'read' permission for $self->{user}";
677 }
678  
679 # for user display : record numbers start with 1, not 0
680 2 $self->{results}{start} = $start_record;
681 2 $self->{results}{end} = $end_record;
682  
683  
684 # links to previous/next slice
685 2 my $prev_idx = $start_record - $self->{count};
686 2 ▶ $prev_idx = 1 if $prev_idx < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_idx < 1 2 0 2
687 2 ▶ $self->{results}{prev_link} = $self->_url_for_next_slice($prev_idx)
Branch: 1 branch
BranchTrueFalseTotal
if $start_record > 1 0 2 2
688 if $start_record > 1;
689 2 my $next_idx = $start_record + $self->{count};
690 $self->{results}{next_link} = $self->_url_for_next_slice($next_idx)
691 2 ▶ if $next_idx <= $self->{results}{count};
Branch: 1 branch
BranchTrueFalseTotal
if $next_idx <= $self->{"results"}{"count"} 1 1 2
692 }
693  
694  
695 #----------------------------------------------------------------------
696 sub _url_for_next_slice {
697 #----------------------------------------------------------------------
698 1 ▶ my ($self, $start) = @_;
Subroutine _url_for_next_slice: called CC=1 SCAR=0.1
699  
700 1 my $url = "?" . join "&", $self->params_for_next_slice($start);
701  
702 # uri encoding
703 1 $url =~ s/([^;\/?:@&=\$,A-Z0-9\-_.!~*'() ])/sprintf("%%%02X", ord($1))/ige;
704  
705 1 return $url;
706 }
707  
708  
709 #----------------------------------------------------------------------
710 sub params_for_next_slice {
711 #----------------------------------------------------------------------
712 1 ▶ my ($self, $start) = @_;
Subroutine params_for_next_slice: called CC=3 SCAR=11.2
Pod documented
713  
714 # need request object to invoke native param() method
715 1 my $req = $self->{req};
716  
717 1 my @params = ("S=$self->{search_string_orig}", "start=$start");
718 1 ▶ push @params, "orderBy=$self->{orderBy}" if $req->parameters->{orderBy};
Branch: 1 branch
BranchTrueFalseTotal
if $req->parameters->{"orderBy"} 0 1 1
719 1 ▶ push @params, "count=$self->{count}" if $req->parameters->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $req->parameters->{"count"} 0 1 1
720  
721 1 return @params;
722 }
723  
724  
725 #----------------------------------------------------------------------
726 sub words_queried {
727 #----------------------------------------------------------------------
728 2 ▶ my $self = shift;
Subroutine words_queried: called CC=1 SCAR=0.0
Pod documented
729 2 return ($self->{search_string_orig} =~ m([\w/]+)g);
730 }
731  
732  
733  
734 #----------------------------------------------------------------------
735 sub log_search {
736 #----------------------------------------------------------------------
737 2 ▶ my $self = shift;
Subroutine log_search: called CC=2 SCAR=9.2
Pod undocumented
738 2 ▶ return if not $self->{logger};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logger"} 2 0 2
739  
740 0 my $msg = "[$self->{search_string}] $self->{user}";
741 0 $self->{logger}->info($msg);
742 }
743  
744  
745 #======================================================================
746 # REQUEST HANDLING : UPDATE METHODS #
747 #======================================================================
748  
749  
750 #----------------------------------------------------------------------
751 sub empty_record { # to be displayed in "modif" view (when adding)
752 #----------------------------------------------------------------------
753 0 ▶ my ($self) = @_;
Subroutine empty_record: not called CC=5 SCAR=34.0
Pod documented
754  
755 0 ▶ $self->can_do("add") or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("add") or die qq[no 'add' permission for $self->{"user"}] 0 0 0
756 die "no 'add' permission for $self->{user}";
757  
758 # build a record and insert default values
759 0 my $record = $self->{data}->ht->new;
760 0 my $defaults = $self->{cfg}->get("fields_default");
761 0 ▶ if (my $auto_num = $self->{data}{autoNumField}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auto_num = $self->{"data"}{"autoNumField"}) 0 0 0
762 0 ▶ $defaults->{$auto_num} ||= $self->{data}{autoNumChar};
Condition0%0/3
$defaults->{$auto_num} ||= $self->{"data"}{"autoNumChar"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$defaults->{$auto_num} ||= $self->{"data"}{"autoNumChar"}
$defaults->{$auto_num} $self->{"data"}{"autoNumChar"}
Truth table0%0/3
A ||= B
A= $defaults->{$auto_num}B= $self->{"data"}{"autoNumChar"}
ABresult
1X1
011
000
763 }
764 0 $record->{$_} = $defaults->{$_} foreach $self->{data}->headers;
765  
766 0 $self->{results} = {count => 1, records => [$record], lineNumbers => [-1]};
767 }
768  
769  
770 #----------------------------------------------------------------------
771 sub update {
772 #----------------------------------------------------------------------
773 0 ▶ my ($self) = @_;
Subroutine update: not called CC=10 SCAR=47.0
Pod documented
774  
775 # check if there is one record to update
776 0 my $found = $self->{results};
777 0 ▶ $found->{count} == 1 or die "unexpected number of records to update";
Branch: 1 branch
BranchTrueFalseTotal
$found->{"count"} == 1 or die "unexpected number of records to update" 0 0 0
778  
779 # gather some info
780 0 my $record = $found->{records}[0];
781 0 my $line_nb = $found->{lineNumbers}[0];
782 0 my $is_adding = $line_nb == -1;
783 0 ▶ my $permission = $is_adding ? 'add' : 'modif';
Branch: 1 branch
BranchTrueFalseTotal
$is_adding ? : 0 0 0
784  
785 # check if user has permission
786 0 ▶ $self->can_do($permission, $record)
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do($permission, $record) or die qq[No permission '${permission}' for $self->{"user"}] 0 0 0
787 or die "No permission '$permission' for $self->{user}";
788  
789 # if adding, must make sure to read all rows so that autonum gets updated
790 0 ▶ if ($is_adding && $self->{cfg}->get('fields_autoNum')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_adding and $self->{"cfg"}->get("fields_autoNum")) 0 0 0
Condition0%0/3
$is_adding and $self->{"cfg"}->get("fields_autoNum")
!ll&&!rl&&r
000
MC/DC0%0/2
$is_adding and $self->{"cfg"}->get("fields_autoNum")
$is_adding $self->{"cfg"}->get("fields_autoNum")
Truth table0%0/3
A and B
A= $is_addingB= $self->{"cfg"}->get("fields_autoNum")
ABresult
0X0
100
111
791 0 ▶ while ($self->{data}->fetchrow) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}->fetchrow) 0 0 0
792 }
793  
794 # call hook before update
795 0 $self->before_update($record);
796  
797 # prepare message to user
798 0 my @headers = $self->{data}->headers;
799 0 my $data_line = join("|", @{$record}{@headers});
800 my ($msg, $id) = $is_adding ? ("Created", $self->{data}{autoNum})
801 0 ▶ : ("Updated", $self->key($record));
Branch: 1 branch
BranchTrueFalseTotal
$is_adding ? : 0 0 0
802 0 $self->{msg} .= "<br>$msg:<br>"
803 . "<a href='?S=K_E_Y:$id'>Record $id</a>: $data_line<br>";
804  
805 # do the update
806 0 ▶ my $to_delete = $is_adding ? 0 # no previous line to delete
Branch: 1 branch
BranchTrueFalseTotal
$is_adding ? : 0 0 0
807 : 1; # replace previous line
808 0 ▶ eval {$self->{data}->splices($line_nb, $to_delete, $record)} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"data"}->splices($line_nb, $to_delete, $record) } } or do { my $err = $@; $self->rollback_update($record); die $err } 0 0 0
809 0 my $err = $@;
810 0 $self->rollback_update($record);
811 0 die $err;
812 };
813  
814 # call hook after update
815 0 $self->after_update($record);
816 }
817  
818  
819 #----------------------------------------------------------------------
820 sub before_update { #
821 #----------------------------------------------------------------------
822 0 ▶ my ($self, $record) = @_;
Subroutine before_update: not called CC=7 SCAR=40.3
Pod documented
823  
824 # copy defined params into record ..
825 0 my $key_field = $self->param($self->key_field);
826 0 foreach my $field ($self->{data}->headers) {
827 0 my $val = $self->param($field);
828 0 ▶ next if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
829 0 ▶ if ($field eq $key_field and $val ne $self->key($record)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq $key_field and $val ne $self->key($record)) 0 0 0
Condition0%0/3
$field eq $key_field and $val ne $self->key($record)
!ll&&!rl&&r
000
MC/DC0%0/2
$field eq $key_field and $val ne $self->key($record)
$field eq $key_field $val ne $self->key($record)
Truth table0%0/3
A and B
A= $field eq $key_fieldB= $val ne $self->key($record)
ABresult
0X0
100
111
830 0 die "supplied key $val does not match record key";
831 }
832 0 $record->{$field} = $val;
833 }
834  
835 # force username into user field (if any)
836 0 my $user_field = $self->{app}{user_field};
837 0 ▶ $record->{$user_field} = $self->{user} if $user_field;
Branch: 1 branch
BranchTrueFalseTotal
if $user_field 0 0 0
838  
839 # force current time or date into time fields (if any)
840 0 ▶ while (my ($k, $fmt) = each %{$self->{app}{time_fields}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"app"}{"time_fields"};}) 0 0 0
841 0 $record->{$k} = strftime($fmt, localtime);
842 }
843 }
844  
845  
846 ▶sub after_update {} # override in subclasses
Subroutine after_update: not called CC=1 SCAR=0.0
Pod documented
847 ▶sub rollback_update {} # override in subclasses
Subroutine rollback_update: not called CC=1 SCAR=0.0
Pod documented
848  
849  
850 #======================================================================
851 # REQUEST HANDLING : DELETE METHODS #
852 #======================================================================
853  
854 #----------------------------------------------------------------------
855 sub delete {
856 #----------------------------------------------------------------------
857 0 ▶ my $self = shift;
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
858  
859 # check if there is one record to update
860 0 my $found = $self->{results};
861 0 ▶ $found->{count} == 1 or die "unexpected number of records to delete";
Branch: 1 branch
BranchTrueFalseTotal
$found->{"count"} == 1 or die "unexpected number of records to delete" 0 0 0
862  
863 # gather some info
864 0 my $record = $found->{records}[0];
865 0 my $line_nb = $found->{lineNumbers}[0];
866  
867 # check if user has permission
868 0 ▶ $self->can_do("delete", $record)
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("delete", $record) or die qq[No permission 'delete' for $self->{"user"}] 0 0 0
869 or die "No permission 'delete' for $self->{user}";
870  
871 # call hook before delete
872 0 $self->before_delete($record);
873  
874 # do the deletion
875 0 $self->{data}->splices($line_nb, 1, undef);
876  
877 # message to user
878 0 my @headers = $self->{data}->headers;
879 0 my @values = @{$record}{@headers};
880 0 $self->{msg} = "Deleted:<br>" . join("|", @values);
881  
882 # call hook after delete
883 0 $self->after_delete($record);
884 }
885  
886  
887 ▶sub before_delete {} # override in subclasses
Subroutine before_delete: not called CC=1 SCAR=0.0
Pod documented
888 ▶sub after_delete {} # override in subclasses
Subroutine after_delete: not called CC=1 SCAR=0.0
Pod documented
889  
890  
891 #======================================================================
892 # MISCELLANEOUS METHODS #
893 #======================================================================
894  
895  
896  
897 #----------------------------------------------------------------------
898 sub prepare_download {
899 #----------------------------------------------------------------------
900 0 ▶ my ($self, $which) = @_;
Subroutine prepare_download: not called CC=2 SCAR=17.9
Pod documented
901 0 ▶ $self->can_do('download')
Branch: 1 branch
BranchTrueFalseTotal
$self->can_do("download") or die qq[No permission 'download' for $self->{"user"}] 0 0 0
902 or die "No permission 'download' for $self->{user}";
903 }
904  
905  
906 #----------------------------------------------------------------------
907 sub print_help {
908 #----------------------------------------------------------------------
909 0 ▶ print "sorry, no help at the moment";
Subroutine print_help: not called CC=1 SCAR=6.9
Pod documented
910 }
911  
912  
913  
914 #----------------------------------------------------------------------
915 sub user_match {
916 #----------------------------------------------------------------------
917 6 ▶ my ($self, $access_control_list) = @_;
Subroutine user_match: called CC=1 SCAR=0.0
Pod documented
918  
919 # success if the list contains '*' or the current username
920 6 return ($access_control_list =~ /\*|\b\Q$self->{user}\E\b/i);
921 }
922  
923  
924 #----------------------------------------------------------------------
925 sub key_field {
926 #----------------------------------------------------------------------
927 0 ▶ my ($self) = @_;
Subroutine key_field: not called CC=1 SCAR=6.9
Pod documented
928 0 return ($self->{data}->headers)[0];
929 }
930  
931  
932 #----------------------------------------------------------------------
933 sub key { # returns the value in the first field of the record
934 #----------------------------------------------------------------------
935 0 ▶ my ($self, $record) = @_;
Subroutine key: not called CC=1 SCAR=6.9
Pod documented
936  
937 # optimized version, breaking encapsulation of File::Tabular
938 0 return (tied %$record)->[1];
939  
940 # going through official API would be : return $record->{$self->key_field};
941 }
942  
943 2 1;
944  
945  
946 __END__
« blib/lib/File/Tabular/Web/Attachments.pm ↑ Summary blib/lib/File/Tabular/Web/Attachments/Indexed.pm »